Salome HOME
Improve build procedure
[tools/simanio.git] / src / SimanIO_Link.cxx
1 // Copyright (C) 2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "CreateConfigFile.h"
21 #include "CreateConfigFileResponse.h"
22 #include "GetFile.h"
23 #include "GetFileResponse.h"
24 #include "PutFile.h"
25 #include "PutFileResponse.h"
26 #include "SimanSalomeServiceStub.h"
27
28 #include "SimanIO_Link.hxx"
29
30 #include <Environment.h>
31 #include <ServiceClient.h>
32
33 #include <iostream>
34 #include <fstream>
35 #include <sstream>
36 #include <algorithm>
37
38 using namespace org_splat_ws_server_service_salome;
39 using namespace wso2wsf;
40
41 SimanIO_Link::SimanIO_Link(const char* theStudyID, 
42     const char* theScenarioID, const char* theUserID)
43 : myStudy(theStudyID), myScenario(theScenarioID), myUser(theUserID)
44 {
45 }
46
47 bool SimanIO_Link::IsConnected()
48 {
49   // TO DO
50   return true;
51 }
52
53 // unification of the error handling in the RetrieveConf and StoreConf  methods
54 #define CONFIGURSION_ERROR(message) {std::cerr<<message<<std::endl;}
55
56 SimanIO_Configuration SimanIO_Link::RetrieveConf()
57 {
58   SimanIO_Configuration aResult;
59   // for now use simple initial cfg file for testings purposes only
60   // in the future this file must be downloaded from the SIMAN server somehow
61   
62   char* aSimanHost = getenv("SIMAN_WS_HOST");
63   if (aSimanHost == NULL)
64   {
65     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
66     return aResult;
67   }
68   
69   std::string aSimanWSHost(aSimanHost);
70   std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
71   
72   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
73   char* aClientHomeEnv = getenv("WSFCPP_HOME");
74   
75   if (aClientHomeEnv == NULL)
76   {
77     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
78     return aResult;
79   }
80   
81   std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
82   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
83   ServiceClient *client = aStub->getServiceClient();
84   
85   //Set the ServiceClient options
86   client->engageModule(AXIS2_MODULE_ADDRESSING);
87   Options *options = client->getOptions();
88   axis2_options_t *axis2options = options->getAxis2Options();
89   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:createConfigFile");
90   
91   //MTOM options
92   options->setTimeout(1000000);
93   options->setSoapVersion(AXIOM_SOAP12);
94   
95   //CreateConfigFile operation
96   CreateConfigFile* aCreateConfigFileRequest = new CreateConfigFile();
97   
98   aCreateConfigFileRequest->setArgs0(atoll(myStudy.c_str()));
99   aCreateConfigFileRequest->setArgs1(atoll(myScenario.c_str()));
100   aCreateConfigFileRequest->setArgs2(atoll(myUser.c_str()));
101   
102   //std::cout<<"StudyId = " << atoll(myStudy.c_str()) << "\n";
103   //std::cout<<"ScenarioId = " << atoll(myScenario.c_str()) << "\n";
104   //std::cout<<"UserId = " << atoll(myUser.c_str()) << "\n";
105   
106   CreateConfigFileResponse* aCreateConfigFileResponse = new CreateConfigFileResponse();
107   aCreateConfigFileResponse = aStub->createConfigFile(aCreateConfigFileRequest);
108   std::string aResponseFilePath = aCreateConfigFileResponse->get_return();
109   
110   std::string aResponseFileName = aResponseFilePath.substr(aResponseFilePath.find_last_of("/\\")+1);
111   
112   //Get the created config file to client
113   
114   //Set the options
115   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:getFile");
116   
117   GetFile* aGetFileRequest = new GetFile();
118   aGetFileRequest->setArgs0(aResponseFilePath);
119   GetFileResponse* aGetFileResponse = new GetFileResponse();
120   aGetFileResponse = aStub->getFile(aGetFileRequest);
121   
122   //Parse the response and get the filename
123   std::string aTmpDir = "/tmp";
124   std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
125   //std::string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
126   std::string aClientFilePath = aClientFileDir + aResponseFileName;
127   
128   //Create the directories
129   system(("mkdir -p " + aClientFileDir).c_str());
130   system(("chmod -Rf g+w " + aTmpDir + "/SimanSalome/").c_str());
131   
132   //Download the siman-salome.conf file
133   std::ofstream outputStream;
134   outputStream.open(aClientFilePath.c_str(), std::ofstream::binary);
135   int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
136   int* aBufSize = new int[aBufferSize];
137   unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
138   outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
139   outputStream.flush();
140   outputStream.close();
141   delete[] aCopyBuffer;
142   
143   //Parse the downloaded siman-salome.conf file
144   aResult.Parse(aClientFilePath.c_str());
145   
146   //Set the actual file URLs and get these files onto client.
147   SimanIO_Configuration::ActivitiesIterator actIter(aResult);
148   for(; actIter.More(); actIter.Next()) {
149     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
150     for(; aDocIter.More(); aDocIter.Next()) {
151       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
152       SimanIO_Document::FilesIterator aFileIter(aDoc);
153       for(; aFileIter.More(); aFileIter.Next()) {
154         std::string aURL = aFileIter.URL();
155         std::string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
156         int startUnderlineIndex = aFileName.find_first_of("_") + 1;
157         std::string aSourceFileName = aFileName.substr(startUnderlineIndex);
158         
159         //std::cout << "aURL = " << aURL << "\n";
160         std::string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault/";
161         
162         //only for test only for WIN
163         //replace(aURL.begin(),aURL.end(),'/','\\');
164         //end only for test only for WIN
165         
166         //Get the files
167         //aGetFileRequest->setArgs0("http://" + aSimanWSHost + "/repository/" + aURL);
168         aGetFileRequest->setArgs0(aPathToVault + aURL);
169         aGetFileResponse = aStub->getFile(aGetFileRequest);
170         
171         outputStream.open((aClientFileDir + aSourceFileName).c_str(), std::ofstream::binary);
172         int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
173         int* aBufSize = new int[aBufferSize];
174         unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
175         outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
176         outputStream.flush();
177         outputStream.close();
178         delete[] aCopyBuffer;
179
180         //Set new URLs
181         aFileIter.SetURL((aClientFileDir + aSourceFileName).c_str());
182         //std::cout << "New location of the files " << aClientFileDir + aFileName << "\n";
183       }//aFileIter
184     }//aDocIter
185   }//actIter
186
187   //delete the siman-salome.conf file.
188 //  system(("rm " + aClientFilePath).c_str());
189   
190   //Delete objects
191   delete aCreateConfigFileRequest;
192   delete aCreateConfigFileResponse;
193   delete aGetFileResponse;
194   delete aGetFileRequest;
195   delete aStub;
196   
197   return aResult;
198 }
199
200 void SimanIO_Link::StoreConf(/*const*/ SimanIO_Configuration& theConf)
201 {
202   std::cout << "StoreConf method is started\n";
203   char* aSimanHost = getenv("SIMAN_WS_HOST");
204   if (aSimanHost == NULL)
205   {
206     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
207   }
208   
209   std::string aSimanWSHost(aSimanHost);
210   std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
211   
212   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
213   char* aClientHomeEnv = getenv("WSFCPP_HOME");
214   
215   if (aClientHomeEnv == NULL)
216   {
217     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
218   }
219   
220   std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
221   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
222   ServiceClient *client = aStub->getServiceClient();
223   
224   //Set the ServiceClient options
225   client->engageModule(AXIS2_MODULE_ADDRESSING);
226   Options *options = client->getOptions();
227   axis2_options_t *axis2options = options->getAxis2Options();
228   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:putFile");
229   
230   //MTOM options
231   options->setTimeout(1000000);
232   options->setSoapVersion(AXIOM_SOAP12);
233   
234   //Upload the file onto server
235   PutFile* aPutFileRequest = new PutFile();
236   PutFileResponse* aPutFileResponse = new PutFileResponse();
237   
238   //Prepare salome-siman.conf file
239   std::string aTmpDir = "/tmp";
240   std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
241   //std::string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
242   std::string aClientFilePath = aClientFileDir + "salome-siman.conf";
243   std::ofstream aSalomeSimanFile;
244   aSalomeSimanFile.open(aClientFilePath.c_str());
245   
246   //Get the actual file URLs and put these files onto server.
247   SimanIO_Configuration::ActivitiesIterator actIter(theConf);
248
249   for(; actIter.More(); actIter.Next()) {
250     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
251     for(; aDocIter.More(); aDocIter.Next()) {
252       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
253       SimanIO_Document::FilesIterator aFileIter(aDoc);
254       for(; aFileIter.More(); aFileIter.Next()) {
255         std::string aURL = aFileIter.URL();
256         std::string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
257         std::cout << "aURL = " << aURL << "\n";
258         std::cout << "aFileName= " << aFileName << "\n";
259         //get size of the input file
260         std::ifstream inputStream (aURL.c_str(), std::ifstream::binary);
261         inputStream.seekg(0,std::ifstream::end);
262         int aSize = inputStream.tellg();
263         inputStream.seekg(0);
264         
265         //Upload action
266         char* aCopyBuffer;
267         aCopyBuffer = new char [aSize];
268         inputStream.read(aCopyBuffer,aSize);
269         std::cout << "Upload a file with the size" << aSize << "\n";
270         axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
271         inputStream.close();
272         delete[] aCopyBuffer;
273
274         //Set arguments
275         aPutFileRequest->setArgs0(aRequestDH);
276         aPutFileRequest->setArgs1(aFileName);
277         aPutFileRequest->setArgs2(atoll(myUser.c_str()));
278         
279         aPutFileResponse = aStub->putFile(aPutFileRequest);
280         
281         std::cout << "End of upload. New file locaton is: " << aPutFileResponse->get_return() << "\n";
282         
283         //Write information into salome-siman.conf file
284         std::ostringstream activityId;
285         std::ostringstream documentId;
286         activityId << actIter.ActivityId();
287         documentId << aDocIter.DocId();
288         aSalomeSimanFile << myScenario +  "," +  myUser + "," + activityId.str() +  "," + documentId.str() + "," + aPutFileResponse->get_return() + "\n";
289         std::cout << "Line in the SalomeSimanFile " << myScenario +  "," +  myUser + "," + activityId.str() +  "," + documentId.str() + "," + aPutFileResponse->get_return() <<"\n";
290         std::cout <<"==========\n";
291
292       }//aFileIter
293     }//aDocIter
294   }//actIter
295
296   aSalomeSimanFile.close();
297   
298   //Put the salome-siman.conf file onto server
299   std::ifstream inputStream (aClientFilePath.c_str(), std::ifstream::binary);
300   inputStream.seekg(0,std::ifstream::end);
301   int aSize = inputStream.tellg();
302   inputStream.seekg(0);
303   
304   char* aCopyBuffer;
305   aCopyBuffer = new char [aSize];
306   inputStream.read(aCopyBuffer,aSize);
307   axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
308   inputStream.close();
309   delete[] aCopyBuffer;
310
311   //Set arguments
312   aPutFileRequest->setArgs0(aRequestDH);
313   aPutFileRequest->setArgs1("salome-siman.conf");
314   aPutFileRequest->setArgs2(atoll(myUser.c_str()));
315
316   aPutFileResponse = aStub->putFile(aPutFileRequest);
317
318   //Check in operation
319   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:checkIn");
320   
321   CheckIn* aCheckInRequest = new CheckIn();
322   CheckInResponse* aCheckInResponse = new CheckInResponse();
323   aCheckInRequest->setArgs0(aPutFileResponse->get_return());
324   aCheckInRequest->setArgs1(atoll(myScenario.c_str()));
325   aCheckInRequest->setArgs2(atoll(myUser.c_str()));
326   aCheckInResponse = aStub->checkIn(aCheckInRequest);
327   
328   //Delete the directories
329 //  system(("rm -rf " + aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/").c_str());
330   
331   //Delete objects
332   delete aPutFileRequest;
333   delete aPutFileResponse;
334   delete aCheckInRequest;
335   delete aCheckInResponse;
336   delete aStub;
337 }