const char * sendMessage(const char *message, int socketDesc) { char *serverReply = EMPTY_STRING; char tmp[BUFFSIZE] = EMPTY_STRING; // send message if (send(socketDesc, message, strlen(message), 0) < 0) { fprintf(stderr, "Send failed\n"); return EMPTY_STRING; } do { // empty buffer memset(tmp, 0, sizeof(tmp)); // check if there is data to receive if (isResponse(socketDesc) <= 0) { fprintf(stderr, "No data in socket to receive\n"); return EMPTY_STRING; } // receive reply if (recv(socketDesc, tmp, BUFFSIZE - 1, 0) < 0) { fprintf(stderr, "Recv failed\n"); return EMPTY_STRING; } strcat(serverReply, tmp); } while (serverReply != '\0'); return serverReply; }
bool F::receivedResponse (const std::string &requestId) { for (Json::Value message : recvMessages) { if (isResponse (message, requestId) ) { return true; } } return false; }
void CMessageBrokerController::sendJsonMessage(Json::Value& message) { DBG_MSG(("CMessageBrokerController::sendJsonMessage()\n")); sync_primitives::AutoLock auto_lock(queue_lock_); std::string mes = m_writer.write(message); if (!isNotification(message) && !isResponse(message)) {// not notification, not a response, store id and method name to recognize an answer mWaitResponseQueue.insert(std::map<std::string, std::string>::value_type(message["id"].asString(), message["method"].asString())); } int bytesSent = Send(mes); bytesSent = bytesSent; // to prevent compiler warnings in case DBG_MSG off DBG_MSG(("Length:%d, Sent: %d bytes\n", mes.length(), bytesSent)); }
Json::Value F::getResponse (const std::string &requestId) { for (auto it = recvMessages.begin(); it != recvMessages.end(); it++) { Json::Value message = *it; if (isResponse (message, requestId) ) { recvMessages.erase (it); return message; } } throw KurentoException (UNEXPECTED_ERROR, "Resonse not found"); }
void GameCycle::playCard(Player* player, PlayingCard* card, PlayingCard* targetCard) { m_contextDirty = 0; if (player != mp_requestedPlayer) throw BadPlayerException(mp_currentPlayer->id()); if (card->owner() != 0 && card->owner() != player) { throw BadCardException(); } if (isResponse()) throw BadGameStateException(); player->character()->playCard(card, targetCard); sendRequest(); }
void CMessageBrokerController::onMessageReceived(Json::Value message) { // Determine message type and process... Json::Value error; if (checkMessage(message, error)) { if (isNotification(message)) { DBG_MSG(("Message is notification!\n")); processNotification(message); } else if (isResponse(message)) { std::string id = message["id"].asString(); std::string method = findMethodById(id); DBG_MSG(("Message is response on: %s\n", method.c_str())); if ("" != method) { if ("MB.registerComponent" == method) { // initialize mControllersIdStart if (message.isMember("result") && message["result"].isInt()) { mControllersIdStart = message["result"].asInt(); } else { DBG_MSG_ERROR(("Not possible to initialize mControllersIdStart!\n")); } } else if ("MB.subscribeTo" == method || "MB.unregisterComponent" == method || "MB.unsubscribeFrom" == method) { //nothing to do for now } else { processResponse(method, message); } } else { DBG_MSG_ERROR(("Request with id %s has not been found!\n", id.c_str())); } } else { DBG_MSG(("Message is request!\n")); processRequest(message); } } else { DBG_MSG_ERROR(("Message contains wrong data!\n")); } }
Json::Value JsonRpcPrivate::handleObject(Json::Value const & jsonObject) { Json::Value response; if (!jsonObject.isObject()) { Json::Value const null; response = invalidRequest(null); } else { Json::Value const id(jsonObject["id"]); //TODO: handle special responses (e.g. parse errors) if (isRequest(jsonObject)) { response = handleRequest(jsonObject, id); } else if (isResponse(jsonObject)) { Json::Value const null; response = null; handleResponse(jsonObject, id); } else { response = invalidRequest(id); } } return response; }
void GameCycle::playCard(Player* player, PlayingCard* card) { m_contextDirty = 0; if (player != mp_requestedPlayer) throw BadPlayerException(mp_currentPlayer->id()); if (card->owner() != 0 && card->owner() != player) { throw BadCardException(); } if (isResponse()) { player->character()->respondCard(m_reactionHandlers.head(), card); } else { // if (card->owner() == 0) { // qDebug() << "Cannot play card owned by nobody."; // throw BadCardException(); // } player->character()->playCard(card); } sendRequest(); }
bool isRequest() { return !isResponse(); }
void GameCycle::assertResponse() const { if (!isResponse()) throw BadGamePlayStateException(m_state, GAMEPLAYSTATE_RESPONSE); }