Salome HOME
Porting SimanIO library on WIN32 platform.
[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 #ifdef WIN32
39   #include <stdlib.h>
40 #endif
41
42 using namespace org_splat_ws_server_service_salome;
43 using namespace wso2wsf;
44
45 SimanIO_Link::SimanIO_Link(const char* theStudyID, 
46     const char* theScenarioID, const char* theUserID)
47 : myStudy(theStudyID), myScenario(theScenarioID), myUser(theUserID)
48 {
49 }
50
51 bool SimanIO_Link::IsConnected()
52 {
53   // TO DO
54   return true;
55 }
56
57 // unification of the error handling in the RetrieveConf and StoreConf  methods
58 #define CONFIGURSION_ERROR(message) {std::cerr<<message<<std::endl;}
59
60 SimanIO_Configuration SimanIO_Link::RetrieveConf()
61 {
62   SimanIO_Configuration aResult;
63   // for now use simple initial cfg file for testings purposes only
64   // in the future this file must be downloaded from the SIMAN server somehow
65   
66   char* aSimanHost = getenv("SIMAN_WS_HOST");
67   if (aSimanHost == NULL)
68   {
69     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
70     return aResult;
71   }
72   
73   std::string aSimanWSHost(aSimanHost);
74   std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
75   
76   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
77   char* aClientHomeEnv = getenv("WSFCPP_HOME");
78   
79   if (aClientHomeEnv == NULL)
80   {
81     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
82     return aResult;
83   }
84   
85   std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
86   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
87   ServiceClient *client = aStub->getServiceClient();
88   
89   //Set the ServiceClient options
90   client->engageModule(AXIS2_MODULE_ADDRESSING);
91   Options *options = client->getOptions();
92   axis2_options_t *axis2options = options->getAxis2Options();
93   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:createConfigFile");
94   
95   //MTOM options
96   options->setTimeout(1000000);
97   options->setSoapVersion(AXIOM_SOAP12);
98   
99   //CreateConfigFile operation
100   CreateConfigFile* aCreateConfigFileRequest = new CreateConfigFile();
101   
102 #ifdef WIN32
103   aCreateConfigFileRequest->setArgs0(_atoi64(myStudy.c_str()));
104   aCreateConfigFileRequest->setArgs1(_atoi64(myScenario.c_str()));
105   aCreateConfigFileRequest->setArgs2(_atoi64(myUser.c_str()));
106 #else
107   aCreateConfigFileRequest->setArgs0(std::atoll(myStudy.c_str()));
108   aCreateConfigFileRequest->setArgs1(std::atoll(myScenario.c_str()));
109   aCreateConfigFileRequest->setArgs2(std::atoll(myUser.c_str()));
110
111   //std::cout<<"StudyId = " << std::atoll(myStudy.c_str()) << "\n";
112   //std::cout<<"ScenarioId = " << std::atoll(myScenario.c_str()) << "\n";
113   //std::cout<<"UserId = " << std::atoll(myUser.c_str()) << "\n";
114 #endif
115   
116   
117   CreateConfigFileResponse* aCreateConfigFileResponse = new CreateConfigFileResponse();
118   aCreateConfigFileResponse = aStub->createConfigFile(aCreateConfigFileRequest);
119   std::string aResponseFilePath = aCreateConfigFileResponse->get_return();
120   
121   std::string aResponseFileName = aResponseFilePath.substr(aResponseFilePath.find_last_of("/\\")+1);
122   
123   //Get the created config file to client
124   
125   //Set the options
126   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:getFile");
127   
128   GetFile* aGetFileRequest = new GetFile();
129   aGetFileRequest->setArgs0(aResponseFilePath);
130   GetFileResponse* aGetFileResponse = new GetFileResponse();
131   aGetFileResponse = aStub->getFile(aGetFileRequest);
132   
133   //Parse the response and get the filename
134 #ifdef WIN32
135   char* aHome = getenv("HOME");
136   std::string aTmpDir(aHome);
137   std::string aClientFileDir = aTmpDir + "\\SimanSalome\\" + myStudy + "\\" + myScenario + "\\" + myUser + "\\";
138   //std::string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
139   std::string aClientFilePath = aClientFileDir + aResponseFileName;
140   
141   //Create the directories
142   system(("if not exist aClientFileDir).c_str() mkdir " + aClientFileDir).c_str());
143 #else
144   std::string aTmpDir = "/tmp";
145   std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
146   //std::string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
147   std::string aClientFilePath = aClientFileDir + aResponseFileName;
148   
149   //Create the directories
150   system(("mkdir -p " + aClientFileDir).c_str());
151   system(("chmod -Rf g+w " + aTmpDir + "/SimanSalome/").c_str());
152 #endif
153   
154   //Download the siman-salome.conf file
155   std::ofstream outputStream;
156   outputStream.open(aClientFilePath.c_str(), std::ofstream::binary);
157   int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
158   int* aBufSize = new int[aBufferSize];
159   unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
160   outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
161   outputStream.flush();
162   outputStream.close();
163   delete[] aCopyBuffer;
164   
165   //Parse the downloaded siman-salome.conf file
166   aResult.Parse(aClientFilePath.c_str());
167   
168   //Set the actual file URLs and get these files onto client.
169   SimanIO_Configuration::ActivitiesIterator actIter(aResult);
170   for(; actIter.More(); actIter.Next()) {
171     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
172     for(; aDocIter.More(); aDocIter.Next()) {
173       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
174       SimanIO_Document::FilesIterator aFileIter(aDoc);
175       for(; aFileIter.More(); aFileIter.Next()) {
176         std::string aURL = aFileIter.URL();
177         std::string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
178         int startUnderlineIndex = aFileName.find_first_of("_") + 1;
179         std::string aSourceFileName = aFileName.substr(startUnderlineIndex);
180         
181         //std::cout << "aURL = " << aURL << "\n";
182         #ifdef WIN32
183         std::string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault\\";
184         #else
185         std::string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault/";
186         #endif
187         
188         //only for test only for WIN
189         //replace(aURL.begin(),aURL.end(),'/','\\');
190         //end only for test only for WIN
191         
192         //Get the files
193         //aGetFileRequest->setArgs0("http://" + aSimanWSHost + "/repository/" + aURL);
194         aGetFileRequest->setArgs0(aPathToVault + aURL);
195         aGetFileResponse = aStub->getFile(aGetFileRequest);
196         
197         outputStream.open((aClientFileDir + aSourceFileName).c_str(), std::ofstream::binary);
198         int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
199         int* aBufSize = new int[aBufferSize];
200         unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
201         outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
202         outputStream.flush();
203         outputStream.close();
204         delete[] aCopyBuffer;
205
206         //Set new URLs
207         aFileIter.SetURL((aClientFileDir + aSourceFileName).c_str());
208         //std::cout << "New location of the files " << aClientFileDir + aFileName << "\n";
209       }//aFileIter
210     }//aDocIter
211   }//actIter
212
213   //delete the siman-salome.conf file.
214 //  system(("rm " + aClientFilePath).c_str());
215   
216   //Delete objects
217   delete aCreateConfigFileRequest;
218   delete aCreateConfigFileResponse;
219   delete aGetFileResponse;
220   delete aGetFileRequest;
221   delete aStub;
222   
223   return aResult;
224 }
225
226 void SimanIO_Link::StoreConf(/*const*/ SimanIO_Configuration& theConf)
227 {
228   std::cout << "StoreConf method is started\n";
229   char* aSimanHost = getenv("SIMAN_WS_HOST");
230   if (aSimanHost == NULL)
231   {
232     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
233   }
234   
235   std::string aSimanWSHost(aSimanHost);
236   std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
237   
238   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
239   char* aClientHomeEnv = getenv("WSFCPP_HOME");
240   
241   if (aClientHomeEnv == NULL)
242   {
243     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
244   }
245   
246   std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
247   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
248   ServiceClient *client = aStub->getServiceClient();
249   
250   //Set the ServiceClient options
251   client->engageModule(AXIS2_MODULE_ADDRESSING);
252   Options *options = client->getOptions();
253   axis2_options_t *axis2options = options->getAxis2Options();
254   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:putFile");
255   
256   //MTOM options
257   options->setTimeout(1000000);
258   options->setSoapVersion(AXIOM_SOAP12);
259   
260   //Upload the file onto server
261   PutFile* aPutFileRequest = new PutFile();
262   PutFileResponse* aPutFileResponse = new PutFileResponse();
263   
264   //Prepare salome-siman.conf file
265 #ifdef WIN32
266   char* aHome = getenv("HOME");
267   std::string aTmpDir(aHome);
268   std::string aClientFileDir = aTmpDir + "\\SimanSalome\\" + myStudy + "\\" + myScenario + "\\" + myUser + "\\";
269   //std::string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
270   
271 #else
272   std::string aTmpDir = "/tmp";
273   std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
274   //std::string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
275 #endif
276   std::string aClientFilePath = aClientFileDir + "salome-siman.conf";
277   std::ofstream aSalomeSimanFile;
278   aSalomeSimanFile.open(aClientFilePath.c_str());
279   
280   //Get the actual file URLs and put these files onto server.
281   SimanIO_Configuration::ActivitiesIterator actIter(theConf);
282
283   for(; actIter.More(); actIter.Next()) {
284     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
285     for(; aDocIter.More(); aDocIter.Next()) {
286       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
287       SimanIO_Document::FilesIterator aFileIter(aDoc);
288       for(; aFileIter.More(); aFileIter.Next()) {
289         std::string aURL = aFileIter.URL();
290         std::string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
291         std::cout << "aURL = " << aURL << "\n";
292         std::cout << "aFileName= " << aFileName << "\n";
293         //get size of the input file
294         std::ifstream inputStream (aURL.c_str(), std::ifstream::binary);
295         inputStream.seekg(0,std::ifstream::end);
296         int aSize = inputStream.tellg();
297         inputStream.seekg(0);
298         
299         //Upload action
300         char* aCopyBuffer;
301         aCopyBuffer = new char [aSize];
302         inputStream.read(aCopyBuffer,aSize);
303         std::cout << "Upload a file with the size" << aSize << "\n";
304         axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
305         inputStream.close();
306         delete[] aCopyBuffer;
307
308         //Set arguments
309         aPutFileRequest->setArgs0(aRequestDH);
310         aPutFileRequest->setArgs1(aFileName);
311
312 #ifdef WIN32
313         aPutFileRequest->setArgs2(_atoi64(myUser.c_str()));
314 #else
315         aPutFileRequest->setArgs2(std::atoll(myUser.c_str()));
316 #endif
317         
318         aPutFileResponse = aStub->putFile(aPutFileRequest);
319         
320         std::cout << "End of upload. New file locaton is: " << aPutFileResponse->get_return() << "\n";
321         
322         //Write information into salome-siman.conf file
323         std::ostringstream activityId;
324         std::ostringstream documentId;
325         activityId << actIter.ActivityId();
326         documentId << aDocIter.DocId();
327         aSalomeSimanFile << myScenario +  "," +  myUser + "," + activityId.str() +  "," + documentId.str() + "," + aPutFileResponse->get_return() + "\n";
328         std::cout << "Line in the SalomeSimanFile " << myScenario +  "," +  myUser + "," + activityId.str() +  "," + documentId.str() + "," + aPutFileResponse->get_return() <<"\n";
329         std::cout <<"==========\n";
330
331       }//aFileIter
332     }//aDocIter
333   }//actIter
334
335   aSalomeSimanFile.close();
336   
337   //Put the salome-siman.conf file onto server
338   std::ifstream inputStream (aClientFilePath.c_str(), std::ifstream::binary);
339   inputStream.seekg(0,std::ifstream::end);
340   int aSize = inputStream.tellg();
341   inputStream.seekg(0);
342   
343   char* aCopyBuffer;
344   aCopyBuffer = new char [aSize];
345   inputStream.read(aCopyBuffer,aSize);
346   axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
347   inputStream.close();
348   delete[] aCopyBuffer;
349
350   //Set arguments
351   aPutFileRequest->setArgs0(aRequestDH);
352   aPutFileRequest->setArgs1("salome-siman.conf");
353
354 #ifdef WIN32
355   aPutFileRequest->setArgs2(_atoi64(myUser.c_str()));
356 #else
357   aPutFileRequest->setArgs2(std::atoll(myUser.c_str()));
358 #endif
359
360   aPutFileResponse = aStub->putFile(aPutFileRequest);
361
362   //Check in operation
363   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:checkIn");
364   
365   CheckIn* aCheckInRequest = new CheckIn();
366   CheckInResponse* aCheckInResponse = new CheckInResponse();
367   aCheckInRequest->setArgs0(aPutFileResponse->get_return());
368
369 #ifdef WIN32
370   aCheckInRequest->setArgs1(_atoi64(myScenario.c_str()));
371   aCheckInRequest->setArgs2(_atoi64(myUser.c_str()));
372 #else
373   aCheckInRequest->setArgs1(std::atoll(myScenario.c_str()));
374   aCheckInRequest->setArgs2(std::atoll(myUser.c_str()));
375 #endif
376
377   aCheckInResponse = aStub->checkIn(aCheckInRequest);
378   
379   //Delete the directories
380 //  system(("rm -rf " + aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/").c_str());
381   
382   //Delete objects
383   delete aPutFileRequest;
384   delete aPutFileResponse;
385   delete aCheckInRequest;
386   delete aCheckInResponse;
387   delete aStub;
388 }