ctkNetworkConnectorQtSoap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * ctkNetworkConnectorQtSoap.cpp
  3. * ctkEventBus
  4. *
  5. * Created by Daniele Giunchi on 14/07/10.
  6. * Copyright 2009 B3C. All rights reserved.
  7. *
  8. * See Licence at: http://tiny.cc/QXJ4D
  9. *
  10. */
  11. #include "ctkNetworkConnectorQtSoap.h"
  12. #include "ctkEventBusManager.h"
  13. #include <service/event/ctkEvent.h>
  14. using namespace ctkEventBus;
  15. ctkNetworkConnectorQtSoap::ctkNetworkConnectorQtSoap() : ctkNetworkConnector(), m_Http(NULL), m_WSDLUrl(""),m_Response(NULL) {
  16. m_Protocol = "SOAP";
  17. }
  18. ctkNetworkConnectorQtSoap::~ctkNetworkConnectorQtSoap() {
  19. if(m_Http) {
  20. delete m_Http;
  21. }
  22. }
  23. ctkNetworkConnector *ctkNetworkConnectorQtSoap::clone() {
  24. ctkNetworkConnectorQtSoap *copy = new ctkNetworkConnectorQtSoap();
  25. return copy;
  26. }
  27. void ctkNetworkConnectorQtSoap::initializeForEventBus() {
  28. ctkRegisterRemoteSignal("ctk/remote/eventBus/comunication/soap", this, "remoteCommunication(const QString, ctkEventArgumentsList *)");
  29. ctkRegisterRemoteCallback("ctk/remote/eventBus/comunication/soap", this, "send(const QString, ctkEventArgumentsList *)");
  30. }
  31. void ctkNetworkConnectorQtSoap::registerServerMethod(QString methodName, QList<QVariant::Type> types) {
  32. m_RegisterMethodsMap.insert(methodName, types);
  33. }
  34. void ctkNetworkConnectorQtSoap::createClient(const QString hostName, const unsigned int port) {
  35. if(m_Http == NULL) {
  36. m_Http = new QtSoapHttpTransport();
  37. QObject::connect(m_Http, SIGNAL(responseReady()), this, SLOT(retrieveRemoteResponse()));
  38. }
  39. //maf3 service registration
  40. QList<QVariant::Type> parametersForRegisterteredFunction;
  41. parametersForRegisterteredFunction.append(QVariant::String); //return argument
  42. parametersForRegisterteredFunction.append(QVariant::List); //parameters to send, event control parameters
  43. parametersForRegisterteredFunction.append(QVariant::List); //parameters to send, data parameters
  44. //registration of the method REMOTE_COMMUNICATION_SOAP at Soap level
  45. // this method need to reflect the name of the action of the service while QVariant::List are list of
  46. // strings, in which each string represent the correct name of the parameter in the sevice function.
  47. registerServerMethod("testArray", parametersForRegisterteredFunction);
  48. //
  49. // Construct a method request message.
  50. //ARRAY TEST
  51. //QtSoapArray arr(QtSoapQName("arr", ""), QtSoapType::String, 2);
  52. //arr.insert(0, new QtSoapSimpleType(QtSoapQName("name"), "First"));
  53. //arr.insert(1, new QtSoapSimpleType(QtSoapQName("surName"), "Second"));
  54. //exp
  55. /*QtSoapArray arrEvent(QtSoapQName("arrEvent", ""), QtSoapType::String, 2);
  56. arrEvent.insert(0, new QtSoapSimpleType(QtSoapQName("n1"), "eventA1"));
  57. arrEvent.insert(1, new QtSoapSimpleType(QtSoapQName("n2"), "eventA2"));
  58. QtSoapArray arrData(QtSoapQName("arrData", ""), QtSoapType::String, 2);
  59. arrData.insert(0, new QtSoapSimpleType(QtSoapQName("d1"), "dataA1"));
  60. arrData.insert(1, new QtSoapSimpleType(QtSoapQName("d2"), "dataA2"));*/
  61. //end exp
  62. m_Http->setHost(hostName, false, port);
  63. //ARRAY TEST
  64. // Set the method and add one argument.
  65. /*request.setMethod("algorithmSIBA");
  66. request.addMethodArgument("input", "", "input.aim");
  67. request.addMethodArgument("output", "", "output.aim");
  68. request.addMethodArgument("gaussian", "", "0.42");
  69. request.addMethodArgument("load", "", "8");
  70. request.addMethodArgument("iteration", "", "40");
  71. qDebug() << request.toXmlString();
  72. // Submit the request the the web service.
  73. m_Http->setHost("localhost", false, 7889);
  74. m_Http->setAction("algorithmSIBA");*/
  75. //m_Http->submitRequest(m_Request, "http://localhost:7889/HelloWordService?wsdl");
  76. // Set the method and add one argument.
  77. /*request.setMethod("getPopulation", "http://www.abundanttech.com/WebServices/Population");
  78. request.addMethodArgument("strCountry", "", "Italy");
  79. // Submit the request the the web service.
  80. m_Http->setHost("www.abundanttech.com");
  81. m_Http->setAction("http://www.abundanttech.com/WebServices/Population/getPopulation");
  82. m_Http->submitRequest(request, "/WebServices/Population/population.asmx");*/
  83. qDebug("retrieve value...");
  84. }
  85. void ctkNetworkConnectorQtSoap::createServer(const unsigned int port) {
  86. Q_UNUSED(port);
  87. qDebug() << tr("QtSoap doesn't support server side implementation.").toLatin1();
  88. }
  89. void ctkNetworkConnectorQtSoap::stopServer() {
  90. qDebug() << tr("QtSoap doesn't support server side implementation.").toLatin1();
  91. }
  92. void ctkNetworkConnectorQtSoap::startListen() {
  93. qDebug() << tr("QtSoap doesn't support server side implementation.").toLatin1();
  94. }
  95. QtSoapType *ctkNetworkConnectorQtSoap::marshall(const QString name, const QVariant &parameter) {
  96. QtSoapType *returnValue = NULL;
  97. switch( parameter.type() ){
  98. case QVariant::Int:
  99. returnValue = new QtSoapSimpleType(QtSoapQName(name), QString::number(parameter.toInt()));
  100. break;
  101. case QVariant::UInt:
  102. returnValue = new QtSoapSimpleType(QtSoapQName(name), QString::number(parameter.toUInt()));
  103. break;
  104. case QVariant::LongLong:
  105. returnValue = new QtSoapSimpleType(QtSoapQName(name), QString::number(parameter.toLongLong()));
  106. break;
  107. case QVariant::ULongLong:
  108. returnValue = new QtSoapSimpleType(QtSoapQName(name), QString::number(parameter.toULongLong()));
  109. break;
  110. case QVariant::Double:
  111. returnValue = new QtSoapSimpleType(QtSoapQName(name), QString::number(parameter.toDouble()));
  112. break;
  113. case QVariant::Bool:
  114. returnValue = new QtSoapSimpleType(QtSoapQName(name), parameter.toBool()?"True":"False");
  115. break;
  116. case QVariant::Date:
  117. returnValue = new QtSoapSimpleType(QtSoapQName(name), parameter.toDate().toString());
  118. break;
  119. case QVariant::DateTime:
  120. returnValue = new QtSoapSimpleType(QtSoapQName(name), parameter.toDateTime().toString());
  121. break;
  122. case QVariant::Time:
  123. returnValue = new QtSoapSimpleType(QtSoapQName(name), parameter.toTime().toString());
  124. break;
  125. case QVariant::StringList:
  126. case QVariant::List: {
  127. QtSoapArray *arr = new QtSoapArray(QtSoapQName(name, ""), QtSoapType::String, parameter.toList().size());
  128. int index = 0;
  129. foreach( QVariant item, parameter.toList() ) {
  130. arr->insert(index, marshall(QString("Elem_").append(QString::number(index)), item ));
  131. index++;
  132. }
  133. returnValue = arr;
  134. break;
  135. }
  136. case QVariant::Map: {
  137. QMap<QString, QVariant> map = parameter.toMap();
  138. QMap<QString, QVariant>::ConstIterator iter = map.begin();
  139. QtSoapArray *arr = new QtSoapArray(QtSoapQName(name, ""), QtSoapType::String, parameter.toMap().size());
  140. int index = 0;
  141. while( iter != map.end() ) {
  142. arr->insert(index, marshall(iter.key(), *iter));
  143. ++iter;
  144. index++;
  145. }
  146. returnValue = arr;
  147. break;
  148. }
  149. case QVariant::Hash: {
  150. QHash<QString, QVariant> hash = parameter.toHash();
  151. QHash<QString, QVariant>::ConstIterator iter = hash.begin();
  152. QtSoapArray *arr = new QtSoapArray(QtSoapQName(name, ""), QtSoapType::String, parameter.toHash().size());
  153. int index = 0;
  154. while( iter != hash.end() ) {
  155. arr->insert(index, marshall(iter.key(), *iter));
  156. ++iter;
  157. index++;
  158. }
  159. returnValue = arr;
  160. break;
  161. }
  162. case QVariant::ByteArray: {
  163. returnValue = new QtSoapSimpleType(QtSoapQName(name), parameter.toByteArray().data());
  164. break;
  165. }
  166. default: {
  167. if( parameter.canConvert(QVariant::String) ) {
  168. returnValue = new QtSoapSimpleType(QtSoapQName(name), parameter.toString());
  169. }
  170. else {
  171. //self representation?
  172. }
  173. break;
  174. }
  175. }
  176. //ENSURE(returnValue != NULL);
  177. return returnValue;
  178. }
  179. void ctkNetworkConnectorQtSoap::send(const QString methodName, ctkEventArgumentsList *argList) {
  180. //REQUIRE(!params->at(0).isNull());
  181. //REQUIRE(params->at(0).canConvert(QVariant::Hash) == true);
  182. QString type = argList->at(0).name();
  183. if(argList == NULL || type != "ctkEventHash") {
  184. qDebug() << "NULL or invalid argument, nothing to send!";
  185. return;
  186. }
  187. m_Request.clear();
  188. m_Request.setMethod(methodName);
  189. ctkEventHash *values;
  190. values = reinterpret_cast<ctkEventHash *> (argList->at(0).data());
  191. int i = 0, size = values->size();
  192. for(;i<size;i++) {
  193. m_Request.addMethodArgument(marshall(values->keys().at(i), values->values().at(i)));
  194. }
  195. qDebug() << m_Request.toXmlString();
  196. // Submit the request the the web service.
  197. m_Http->setAction(m_Action);
  198. m_Http->submitRequest(m_Request, m_Path);
  199. }
  200. void ctkNetworkConnectorQtSoap::retrieveRemoteResponse()
  201. {
  202. // Get a reference to the response message.
  203. const QtSoapMessage &message = m_Http->getResponse();
  204. qDebug() << message.toXmlString();
  205. // Check if the response is a SOAP Fault message
  206. if (message.isFault()) {
  207. qDebug("Error: %s", message.faultString().value().toString().toLatin1().constData());
  208. m_Response = NULL;
  209. }
  210. else {
  211. // Get the return value, and print the result.
  212. m_Response = const_cast<QtSoapType *>( &(message.returnValue()));
  213. }
  214. }
  215. /*
  216. void ctkNetworkConnectorQtSoap::processReturnValue( int requestId, QVariant value ) {
  217. Q_UNUSED( requestId );
  218. Q_ASSERT( value.canConvert( QVariant::String ) );
  219. qDebug("%s", value.toString().toLatin1().data());
  220. ctkEventBusManager::instance()->notifyEvent("ctk/local/eventBus/remoteCommunicationDone", ctkEventTypeLocal);
  221. }
  222. void ctkNetworkConnectorQtSoap::processFault( int requestId, int errorCode, QString errorString ) {
  223. // Log the error.
  224. qDebug("%s", tr("Process Fault for requestID %1 with error %2 - %3").arg(QString::number(requestId), QString::number(errorCode), errorString).toLatin1().data());
  225. ctkEventBusManager::instance()->notifyEvent("ctk/local/eventBus/remoteCommunicationFailed", ctkEventTypeLocal);
  226. }
  227. void ctkNetworkConnectorQtSoap::processRequest( int requestId, QString methodName, QList<xmlrpc::Variant> parameters ) {
  228. Q_UNUSED( methodName );
  229. REQUIRE(parameters.count() >= 2);
  230. //first parameter is ctkEventBus message
  231. enum {
  232. EVENT_PARAMETERS,
  233. DATA_PARAMETERS,
  234. };
  235. enum {
  236. EVENT_ID,
  237. EVENT_ITEM_TYPE,
  238. EVENT_SIGNATURE_TYPE,
  239. EVENT_METHOD_SIGNATURE,
  240. };
  241. if(parameters.at(EVENT_PARAMETERS).toList().count() == 0) {
  242. m_Server->sendReturnValue( requestId, QString("No Command to Execute, command list is empty") );
  243. }
  244. //here eventually can be used a filter for events
  245. //first argument regards local signal to be called.
  246. QString id_name = parameters.at(EVENT_PARAMETERS).toList().at(EVENT_ID).toString();
  247. int size = parameters.count();
  248. ctkEventArgumentsList *argList = NULL;
  249. mafList<QVariant> *p = & (parameters.at(1).value< mafList<QVariant> >());
  250. if(size > 1 && p->count() != 0) {
  251. argList = new ctkEventArgumentsList();
  252. argList->push_back(Q_ARG(mafList<QVariant>, *p));
  253. }
  254. if ( ctkEventBusManager::instance()->isLocalSignalPresent(id_name) ) {
  255. ctkBusEvent dictionary;
  256. mafCore::mafId id = mafCore::mafIdProvider::instance()->idValue(id_name);
  257. dictionary.setEventId(id);
  258. dictionary.setEventType(ctkEventTypeLocal);
  259. ctkEventBusManager::instance()->notifyEvent(dictionary, argList);
  260. m_Server->sendReturnValue( requestId, QString("OK") );
  261. } else {
  262. m_Server->sendReturnValue( requestId, QString("FAIL") );
  263. }
  264. mafDEL(argList);
  265. }*/