My Project
OSSolverAgent.cpp
Go to the documentation of this file.
1/* $Id$ */
16#include "OSSolverAgent.h"
17#include "OSWSUtil.h"
18#include "CoinTime.hpp"
19#include "OSParameters.h"
20#include "OSOutput.h"
21
22#include <cstdlib>
23
24#ifdef HAVE_CTIME
25# include <ctime>
26#else
27# ifdef HAVE_TIME_H
28# include <time.h>
29# else
30# error "don't have header file for time"
31# endif
32#endif
33
34#include <stdio.h>
35#include <sstream>
36
37using std::string;
38using std::ostringstream;
39using std::endl;
40
42
44{
45 string::size_type nstart = 0;
46 string::size_type posSlash;
47 // parse the solverURI
48 // get rid of http:// if it is there
49 if (solverURI.find("http://") != string::npos) solverURI = solverURI.substr(7);
50 // now find the first "/" and put in nstart
51 posSlash = solverURI.find("/", nstart);
52 if(posSlash != std::string::npos) nstart = posSlash;
53 postURI = solverURI.substr(nstart, solverURI.size() - 1);
54 // Do we have a port number
55 string::size_type colonlocation = solverURI.find(":");
56 if(colonlocation == string::npos)
57 {
58 solverAddress = solverURI.substr(0, nstart);
60 }
61 else
62 {
63 solverPortNumber = (unsigned short) atoi( &solverURI.substr(colonlocation + 1, nstart - colonlocation - 1)[0] ) ;
64 solverAddress = solverURI.substr(0, colonlocation);
65 }
66}
67
71
72string OSSolverAgent::solve(string osil, string osol)
73{
74 string sOSrL;
75 string theSOAP;
76 string solveResult;
77
78#ifndef NDEBUG
79 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Inside SolverAgent solve() method \n");
80
81 std::ostringstream outStr;
82 outStr.str("");
83 outStr.clear();
84
85 outStr << "Sending to the remote system:" << std::endl << std::endl;
86 outStr << "OSiL string:" << std::endl << std::endl;
87 outStr << osil << std::endl << std::endl;
88 outStr << "OSoL string:" << std::endl << std::endl;
89 outStr << osol << std::endl << std::endl;
90
92#endif
93
94 bool useCDATA = true;
95 // CreateSOAPMessage inputs
96 int numInputs = 2;
97 string smethod = "solve";
98 string msInputs[2];
99 // package up the inputs
100 // first run them through SAOPify, i.e. replace < with &lt; etc.
101
102#ifndef NDEBUG
103 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPify OSiL \n");
104#endif
105 msInputs[0] = WSUtil::SOAPify( osil, useCDATA) ;
106
107#ifndef NDEBUG
109 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPify OSoL \n");
110#endif
111 msInputs[1] = WSUtil::SOAPify( osol, useCDATA) ;
112#ifndef NDEBUG
114#endif
115 string msInputNames[2] = {"osil", "osol"};
116 string sSoapAction = "OSSolverService#solve";
117 // create the soap
118 double cpuTime;
119 double startTime = 0;
120 startTime = CoinCpuTime();
121 theSOAP = WSUtil::createSOAPMessage(numInputs, solverAddress, postURI,
122 smethod, msInputs, msInputNames, sSoapAction);
123 // send the soap to the HTTP server
124 cpuTime = CoinCpuTime() - startTime;
125 startTime = CoinWallclockTime();
126#ifndef NDEBUG
127 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SEND THE SOAP\n");
129#endif
131#ifndef NDEBUG
132 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Response received\n");
134#endif
135 cpuTime = CoinWallclockTime() - startTime;
136
137 // desoapify the result -- i.e. replace &lt; with < etc.
138
139 solveResult = WSUtil::getOSxL(solveResult, "solve");
140#ifndef NDEBUG
141 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Result received\n");
143 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "DeSOAPify result\n");
144#endif
145
146 useCDATA = false;
147 sOSrL = WSUtil::deSOAPify( solveResult, useCDATA);
148#ifndef NDEBUG
150 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "RETURN THE OSRL\n");
151#endif
152
153 return sOSrL;
154}//end solve
155
156string OSSolverAgent::fileUpload( string osilFileName, string theOSiLFile)
157{
158 string theHTTPPOST="";
159 string uploadResult="";
160 string boundaryName = "AaB03x";
162 osilFileName, theOSiLFile, boundaryName);
163
164 // send the soap to the HTTP server
165#ifndef NDEBUG
166 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Upload file in SolverAgent\n");
168#endif
169 uploadResult = WSUtil::sendSOAPMessage( theHTTPPOST, solverAddress, solverPortNumber);
170 return uploadResult;
171}//end solve
172
173
174bool OSSolverAgent::send(string osil, string osol)
175{
176 string theSOAP;
177 string sendResult;
178 bool useCDATA = true;
179
180#ifndef NDEBUG
181 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Inside SolverAgent send() method \n");
182
183 std::ostringstream outStr;
184 outStr.str("");
185 outStr.clear();
186
187 outStr << "Sending to the remote system:" << std::endl << std::endl;
188 outStr << "OSiL string:" << std::endl << std::endl;
189 outStr << osil << std::endl << std::endl;
190 outStr << "OSoL string:" << std::endl << std::endl;
191 outStr << osol << std::endl << std::endl;
192
194#endif
195
196 // CreateSOAPMessage inputs
197 int numInputs = 2;
198 string smethod = "send";
199 string msInputs[2];
200 // package up the inputs
201 // first run them through SAOPify, i.e. replace < with &lt; etc.
202#ifndef NDEBUG
203 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPIFY OSIL\n");
204#endif
205 msInputs[0] = WSUtil::SOAPify( osil, useCDATA);
206#ifndef NDEBUG
207
209 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPIFY OSOL\n");
210#endif
211 msInputs[1] = WSUtil::SOAPify( osol, useCDATA) ;
212#ifndef NDEBUG
214#endif
215
216 string msInputNames[2] = {"osil", "osol"};
217 string sSoapAction = "OSSolverService#send";
218 // create the soap
219 theSOAP = WSUtil::createSOAPMessage(numInputs, solverAddress, postURI,
220 smethod, msInputs, msInputNames, sSoapAction);
221#ifndef NDEBUG
222 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Send the SOAP\n");
224#endif
225
226 // send the soap to the HTTP server
228#ifndef NDEBUG
229 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Message returned\n");
231#endif
232
233 // strip out the OSxL that we want from the SOAP envelope
234#ifndef NDEBUG
235 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_detailed_trace, "Strip out OSxL string\n");
236#endif
237 sendResult = WSUtil::getOSxL(sendResult, "send");
238#ifndef NDEBUG
241#endif
242 // desoapify the result -- i.e. replace &lt; with < etc.
243 useCDATA = false;
244 sendResult = WSUtil::deSOAPify( sendResult, useCDATA);
245#ifndef NDEBUG
247#endif
248 if( sendResult.find("true") != string::npos ) return true;
249 else return false;
250}//end send
251
252string OSSolverAgent::getJobID(string osol)
253{
254 string sjobID = "";
255 string getJobIDResult;
256 string theSOAP;
257 bool useCDATA = true;
258
259#ifndef NDEBUG
260 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Inside SolverAgent getJobID() method \n");
261
262 std::ostringstream outStr;
263 outStr.str("");
264 outStr.clear();
265
266 outStr << "Sending to the remote system:" << std::endl << std::endl;
267 outStr << "OSoL string:" << std::endl << std::endl;
268 outStr << osol << std::endl << std::endl;
269
271#endif
272
273 // CreateSOAPMessage inputs
274 int numInputs = 1;
275 string smethod = "getJobID";
276 string msInputs[1];
277 // package up the inputs
278
279 // first run them through SAOPify, i.e. replace < with &lt; etc.
280#ifndef NDEBUG
281 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPIFY OSOL\n");
282#endif
283 msInputs[0] = WSUtil::SOAPify( osol, useCDATA) ;
284#ifndef NDEBUG
286#endif
287 string msInputNames[1] = {"osol"};
288 string sSoapAction = "OSSolverService#getJobID";
289 // create the soap
290 theSOAP = WSUtil::createSOAPMessage(numInputs, solverAddress, postURI,
291 smethod, msInputs, msInputNames, sSoapAction);
292#ifndef NDEBUG
293 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Send the SOAP\n");
295#endif
296 // send the soap to the HTTP server
297 getJobIDResult = WSUtil::sendSOAPMessage( theSOAP, solverAddress, solverPortNumber);
298#ifndef NDEBUG
299 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Message returned\n");
301#endif
302 // strip out the OSxL that we want from the SOAP envelope
303 getJobIDResult = WSUtil::getOSxL(getJobIDResult, "getJobID");
304#ifndef NDEBUG
307#endif
308 // desoapify the result -- i.e. replace &lt; with < etc.
309 useCDATA = false;
310 sjobID = WSUtil::deSOAPify( getJobIDResult, useCDATA);
311#ifndef NDEBUG
314#endif
315
316 return sjobID;
317}//end getJobID
318
319
320string OSSolverAgent::retrieve(string osol)
321{
322 string sOSrL;
323 string retrieveResult;
324 string theSOAP;
325 bool useCDATA = true;
326
327#ifndef NDEBUG
328 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Inside SolverAgent retrieve() method \n");
329
330 std::ostringstream outStr;
331 outStr.str("");
332 outStr.clear();
333
334 outStr << "Sending to the remote system:" << std::endl << std::endl;
335 outStr << "OSoL string:" << std::endl << std::endl;
336 outStr << osol << std::endl << std::endl;
337
339#endif
340
341 // CreateSOAPMessage inputs
342 int numInputs = 1;
343 string smethod = "retrieve";
344 string msInputs[1];
345 // package up the inputs
346 // first run them through SAOPify, i.e. replace < with &lt; etc.
347#ifndef NDEBUG
348 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPIFY OSoL\n");
349#endif
350 msInputs[0] = WSUtil::SOAPify( osol, useCDATA) ;
351#ifndef NDEBUG
353#endif
354 string msInputNames[1] = {"osol"};
355 string sSoapAction = "OSSolverService#retrieve";
356 // create the soap
357 theSOAP = WSUtil::createSOAPMessage(numInputs, solverAddress, postURI,
358 smethod, msInputs, msInputNames, sSoapAction);
359#ifndef NDEBUG
360 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Send the SOAP\n");
362#endif
363
364 // send the soap to the HTTP server
365 retrieveResult = WSUtil::sendSOAPMessage( theSOAP, solverAddress, solverPortNumber);
366#ifndef NDEBUG
367 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Message returned\n");
369#endif
370
371 // strip out the OSxL that we want from the SOAP envelope
372#ifndef NDEBUG
373 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_detailed_trace, "Strip out OSxL string\n");
374#endif
375 retrieveResult = WSUtil::getOSxL(retrieveResult, "retrieve");
376
377 // desoapify the result -- i.e. replace &lt; with < etc.
378#ifndef NDEBUG
381#endif
382 useCDATA = false;
383 sOSrL = WSUtil::deSOAPify( retrieveResult, useCDATA);
384#ifndef NDEBUG
386#endif
387
388 return sOSrL;
389}//end retrieve
390
391
392string OSSolverAgent::kill(string osol)
393{
394 string sOSpL;
395 string killResult;
396 string theSOAP;
397 bool useCDATA = true;
398
399#ifndef NDEBUG
400 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Inside SolverAgent kill() method \n");
401
402 std::ostringstream outStr;
403 outStr.str("");
404 outStr.clear();
405
406 outStr << "Sending to the remote system:" << std::endl << std::endl;
407 outStr << "OSoL string:" << std::endl << std::endl;
408 outStr << osol << std::endl << std::endl;
409
411#endif
412
413 // CreateSOAPMessage inputs
414 int numInputs = 1;
415 string smethod = "kill";
416 string msInputs[1];
417 // package up the inputs
418 // first run them through SAOPify, i.e. replace < with &lt; etc.
419#ifndef NDEBUG
420 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPify OSoL \n");
421#endif
422 msInputs[0] = WSUtil::SOAPify( osol, useCDATA) ;
423#ifndef NDEBUG
425#endif
426 string msInputNames[1] = {"osol"};
427 string sSoapAction = "OSSolverService#kill";
428 // create the soap
429 theSOAP = WSUtil::createSOAPMessage(numInputs, solverAddress, postURI,
430 smethod, msInputs, msInputNames, sSoapAction);
431 // send the soap to the HTTP server
432#ifndef NDEBUG
433 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SEND THE SOAP\n");
435#endif
437#ifndef NDEBUG
438 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Response received\n");
440#endif
441 // strip out the OSxL that we want from the SOAP envelope
442 killResult = WSUtil::getOSxL( killResult, "kill");
443
444 // desoapify the result -- i.e. replace &lt; with < etc.
445 useCDATA = false;
446#ifndef NDEBUG
447 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "DeSOAPify result\n");
448#endif
449 sOSpL = WSUtil::deSOAPify( killResult, useCDATA);
450#ifndef NDEBUG
452 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "RETURN THE OSpL\n");
453#endif
454
455 return sOSpL;
456}//end kill
457
458string OSSolverAgent::knock(string ospl, string osol)
459{
460 string sOSpL;
461 string theSOAP;
462 string knockResult;
463 bool useCDATA = true;
464#ifndef NDEBUG
465 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Inside SolverAgent knock() method \n");
466
467 std::ostringstream outStr;
468 outStr.str("");
469 outStr.clear();
470
471 outStr << "Sending to the remote system:" << std::endl << std::endl;
472 outStr << "OSpL string:" << std::endl << std::endl;
473 outStr << ospl << std::endl << std::endl;
474 outStr << "OSoL string:" << std::endl << std::endl;
475 outStr << osol << std::endl << std::endl;
476
478#endif
479
480 // CreateSOAPMessage inputs
481 int numInputs = 2;
482 string smethod = "knock";
483 string msInputs[2];
484 // package up the inputs
485 // first run them through SAOPify, i.e. replace < with &lt; etc.
486#ifndef NDEBUG
487 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPify OSpL \n");
488#endif
489 msInputs[0] = WSUtil::SOAPify( ospl, useCDATA) ;
490#ifndef NDEBUG
492 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SOAPify OSoL \n");
493#endif
494 msInputs[1] = WSUtil::SOAPify( osol, useCDATA) ;
495#ifndef NDEBUG
497#endif
498
499 string msInputNames[2] = {"ospl", "osol"};
500 string sSoapAction = "OSSolverService#knock";
501 // create the soap
502 theSOAP = WSUtil::createSOAPMessage(numInputs, solverAddress, postURI,
503 smethod, msInputs, msInputNames, sSoapAction);
504 // send the soap to the HTTP server
505#ifndef NDEBUG
506 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "SEND THE SOAP\n");
508#endif
510#ifndef NDEBUG
511 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "Response received\n");
513#endif
514
515 // strip out the OSxL that we want from the SOAP envelope
516 knockResult = WSUtil::getOSxL( knockResult, "knock");
517 // desoapify the result -- i.e. replace &lt; with < etc.
518 useCDATA = false;
519#ifndef NDEBUG
520 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "DeSOAPify result\n");
521#endif
522 sOSpL = WSUtil::deSOAPify( knockResult, useCDATA);
523#ifndef NDEBUG
525 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSAgent, ENUM_OUTPUT_LEVEL_trace, "RETURN THE OSpL\n");
526#endif
527
528 return sOSpL;
529}//end knock
const OSSmartPtr< OSOutput > osoutput
Definition OSOutput.cpp:39
const OSSmartPtr< OSOutput > osoutput
Definition OSOutput.cpp:39
Template class for Smart Pointers.
std::string solverAddress
solverAddress is the URI for the solver
OSSolverAgent(std::string solverURI)
Default constructor.
std::string retrieve(std::string osol)
implement the retrieve() method which is a virtual function in OShL
bool send(std::string osil, std::string osol)
implement the send() method which is a virtual function in OShL
std::string getJobID(std::string osol)
implement the getJobID() method which is a virtual function in OShL
std::string solve(std::string osil, std::string osol)
implement the solve() method which is a virtual function in OShL, this is synchronous
std::string knock(std::string ospl, std::string osol)
implement the knock() method which is a virtual function in OShL
std::string fileUpload(std::string osilFileName, std::string osil)
implement the fileUpload() method which is a virtual function in OShL
~OSSolverAgent()
Class destructor.
std::string kill(std::string osol)
implement the kill() method which is a virtual function in OShL
std::string postURI
postURI is the path to the solver that follows the first '/' in the solverAddress
unsigned short solverPortNumber
solverPortNumber is the port number for the solver
An interface that specified virtual methods to be implemented by agents.
Definition OShL.h:33
static std::string deSOAPify(std::string theXmlString, bool useCDATA)
take the XML from a SOAP envelop and replace < with < replace > with > replace &quot with ";
Definition OSWSUtil.cpp:292
static std::string createSOAPMessage(int numInputs, std::string solverAddress, std::string postURI, std::string smethod, std::string *msInputs, std::string *msInputNames, std::string sSoapAction)
create the SOAP message that is send to the solver Web Service
Definition OSWSUtil.cpp:155
static std::string createFormDataUpload(std::string solverAddress, std::string postURI, std::string fileName, std::string theFile, std::string boundaryName)
create the SOAP message that is sent to the solver Web Service
Definition OSWSUtil.cpp:200
static std::string sendSOAPMessage(std::string theSOAP, std::string serviceIP, unsigned int servicePortNumber)
open a socket and send a SOAP message to the solver Web Service
Definition OSWSUtil.cpp:65
static std::string getOSxL(std::string soapstring, std::string serviceMethod)
extract the appropriate OSxL protocol from the SOAP envelop
Definition OSWSUtil.cpp:374
static std::string SOAPify(std::string theXmlString, bool useCDATA)
prepare XML to be put into a SOAP envelop, replace < with < replace > with > replace " and ' with &quote;
Definition OSWSUtil.cpp:247
@ ENUM_OUTPUT_LEVEL_detailed_trace
@ ENUM_OUTPUT_LEVEL_debug
@ ENUM_OUTPUT_LEVEL_trace
@ ENUM_OUTPUT_AREA_OSAgent