1 // Copyright (C) 2013-2015 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CreateConfigFile.h"
21 #include "CreateConfigFileResponse.h"
23 #include "GetFileResponse.h"
25 #include "PutFileResponse.h"
26 #include "SimanSalomeServiceStub.h"
28 #include "SimanIO_Link.hxx"
30 #include <Environment.h>
31 #include <ServiceClient.h>
42 using namespace org_splat_ws_server_service_salome;
43 using namespace wso2wsf;
45 SimanIO_Link::SimanIO_Link(const char* theStudyID,
46 const char* theScenarioID, const char* theUserID)
47 : myStudy(theStudyID), myScenario(theScenarioID), myUser(theUserID)
51 bool SimanIO_Link::IsConnected()
57 // unification of the error handling in the RetrieveConf and StoreConf methods
58 #define CONFIGURSION_ERROR(message) {std::cerr<<message<<std::endl;}
60 SimanIO_Configuration SimanIO_Link::RetrieveConf()
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
66 char* aSimanHost = getenv("SIMAN_WS_HOST");
67 if (aSimanHost == NULL)
69 CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
73 std::string aSimanWSHost(aSimanHost);
74 std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
76 Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
77 char* aClientHomeEnv = getenv("WSFCPP_HOME");
79 if (aClientHomeEnv == NULL)
81 CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
85 std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
86 SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
87 ServiceClient *client = aStub->getServiceClient();
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");
96 options->setTimeout(1000000);
97 options->setSoapVersion(AXIOM_SOAP12);
99 //CreateConfigFile operation
100 CreateConfigFile* aCreateConfigFileRequest = new CreateConfigFile();
103 aCreateConfigFileRequest->setArgs0(_atoi64(myStudy.c_str()));
104 aCreateConfigFileRequest->setArgs1(_atoi64(myScenario.c_str()));
105 aCreateConfigFileRequest->setArgs2(_atoi64(myUser.c_str()));
107 aCreateConfigFileRequest->setArgs0(std::atoll(myStudy.c_str()));
108 aCreateConfigFileRequest->setArgs1(std::atoll(myScenario.c_str()));
109 aCreateConfigFileRequest->setArgs2(std::atoll(myUser.c_str()));
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";
117 CreateConfigFileResponse* aCreateConfigFileResponse = new CreateConfigFileResponse();
118 aCreateConfigFileResponse = aStub->createConfigFile(aCreateConfigFileRequest);
119 std::string aResponseFilePath = aCreateConfigFileResponse->get_return();
121 std::string aResponseFileName = aResponseFilePath.substr(aResponseFilePath.find_last_of("/\\")+1);
123 //Get the created config file to client
126 axis2_options_set_action(axis2options, Environment::getEnv(), "urn:getFile");
128 GetFile* aGetFileRequest = new GetFile();
129 aGetFileRequest->setArgs0(aResponseFilePath);
130 GetFileResponse* aGetFileResponse = new GetFileResponse();
131 aGetFileResponse = aStub->getFile(aGetFileRequest);
133 //Parse the response and get the filename
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;
141 //Create the directories
142 system(("if not exist aClientFileDir).c_str() mkdir " + aClientFileDir).c_str());
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;
149 //Create the directories
150 system(("mkdir -p " + aClientFileDir).c_str());
151 system(("chmod -Rf g+w " + aTmpDir + "/SimanSalome/").c_str());
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;
165 //Parse the downloaded siman-salome.conf file
166 aResult.Parse(aClientFilePath.c_str());
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);
181 //std::cout << "aURL = " << aURL << "\n";
183 std::string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault\\";
185 std::string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault/";
188 //only for test only for WIN
189 //replace(aURL.begin(),aURL.end(),'/','\\');
190 //end only for test only for WIN
193 //aGetFileRequest->setArgs0("http://" + aSimanWSHost + "/repository/" + aURL);
194 aGetFileRequest->setArgs0(aPathToVault + aURL);
195 aGetFileResponse = aStub->getFile(aGetFileRequest);
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;
207 aFileIter.SetURL((aClientFileDir + aSourceFileName).c_str());
208 //std::cout << "New location of the files " << aClientFileDir + aFileName << "\n";
213 //delete the siman-salome.conf file.
214 // system(("rm " + aClientFilePath).c_str());
217 delete aCreateConfigFileRequest;
218 delete aCreateConfigFileResponse;
219 delete aGetFileResponse;
220 delete aGetFileRequest;
226 void SimanIO_Link::StoreConf(/*const*/ SimanIO_Configuration& theConf)
228 std::cout << "StoreConf method is started\n";
229 char* aSimanHost = getenv("SIMAN_WS_HOST");
230 if (aSimanHost == NULL)
232 CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
235 std::string aSimanWSHost(aSimanHost);
236 std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
238 Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
239 char* aClientHomeEnv = getenv("WSFCPP_HOME");
241 if (aClientHomeEnv == NULL)
243 CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
246 std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
247 SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
248 ServiceClient *client = aStub->getServiceClient();
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");
257 options->setTimeout(1000000);
258 options->setSoapVersion(AXIOM_SOAP12);
260 //Upload the file onto server
261 PutFile* aPutFileRequest = new PutFile();
262 PutFileResponse* aPutFileResponse = new PutFileResponse();
264 //Prepare salome-siman.conf file
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/";
272 std::string aTmpDir = "/tmp";
273 std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
274 //std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myUser + "/download/";
276 std::string aClientFilePath = aClientFileDir + "salome-siman.conf";
277 std::ofstream aSalomeSimanFile;
278 aSalomeSimanFile.open(aClientFilePath.c_str());
280 //Get the actual file URLs and put these files onto server.
281 SimanIO_Configuration::ActivitiesIterator actIter(theConf);
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);
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);
306 delete[] aCopyBuffer;
309 aPutFileRequest->setArgs0(aRequestDH);
310 aPutFileRequest->setArgs1(aFileName);
313 aPutFileRequest->setArgs2(_atoi64(myUser.c_str()));
315 aPutFileRequest->setArgs2(std::atoll(myUser.c_str()));
318 aPutFileResponse = aStub->putFile(aPutFileRequest);
320 std::cout << "End of upload. New file locaton is: " << aPutFileResponse->get_return() << "\n";
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";
335 aSalomeSimanFile.close();
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);
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);
348 delete[] aCopyBuffer;
351 aPutFileRequest->setArgs0(aRequestDH);
352 aPutFileRequest->setArgs1("salome-siman.conf");
355 aPutFileRequest->setArgs2(_atoi64(myUser.c_str()));
357 aPutFileRequest->setArgs2(std::atoll(myUser.c_str()));
360 aPutFileResponse = aStub->putFile(aPutFileRequest);
363 axis2_options_set_action(axis2options, Environment::getEnv(), "urn:checkIn");
365 CheckIn* aCheckInRequest = new CheckIn();
366 CheckInResponse* aCheckInResponse = new CheckInResponse();
367 aCheckInRequest->setArgs0(aPutFileResponse->get_return());
370 aCheckInRequest->setArgs1(_atoi64(myScenario.c_str()));
371 aCheckInRequest->setArgs2(_atoi64(myUser.c_str()));
373 aCheckInRequest->setArgs1(std::atoll(myScenario.c_str()));
374 aCheckInRequest->setArgs2(std::atoll(myUser.c_str()));
377 aCheckInResponse = aStub->checkIn(aCheckInRequest);
379 //Delete the directories
380 // system(("rm -rf " + aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/").c_str());
383 delete aPutFileRequest;
384 delete aPutFileResponse;
385 delete aCheckInRequest;
386 delete aCheckInResponse;