void MessageSender::sendMessage(MessageCommunication *messageCommunication, ResponseHandler *responseHandler) { // Sending a message is about preparing a handler for the response // that is expected to be received and sending the message information // via the socker to the destination node. // // Check we have a message and a socket to send data. if (messageCommunication == 0 || messageCommunication->message() == 0 || messageCommunication->socket() == 0) { return; } // Get the socket used for the communication rountrip. QTcpSocket *socket = messageCommunication->socket(); // Prepare the response waiter in case a response handler was // provided. if (responseHandler == 0) { responseHandler = new ResponseHandler(this); } // Prepare a new ResponseWaiter to use and throw away. ResponseWaiter * responseWaiter = new ResponseWaiter( this->m_communication_manager, messageCommunication, responseHandler, this); // Connect the receiving of a message in the socket with the // response handler of the response waiter. QObject::connect(socket, SIGNAL(readyRead()), responseWaiter, SLOT(messageReady())); // Prepare the message data to send via the socket. MessageModel *message = messageCommunication->message(); QString *jsonMessage = message->toCommJSON(); QByteArray dataMessage = jsonMessage->toUtf8(); // Send the message via the socket socket->write(dataMessage); // A response is expected to be received soon that will be handled // by the ResponseWaiter once it arrives. // cerr << "[MessageSender] Message sent : " << message->toJSON()->toStdString() << endl; }