Json::Value JsonRpcPrivate::handleRequest( const Json::Value &request, const Json::Value &id) { const std::string methodName(request["method"].asString()); const auto iter(m_methods.find(methodName)); if (iter == m_methods.end()) { return methodNotFound(id); } else { auto wp_method(iter->second); if (auto method = wp_method.lock()) { // Params member may be omitted according to the JSON-RPC 2.0 spec Json::Value params( request.isMember("params") ? request["params"] : Json::objectValue); if (isNotification(request)) { // Notifications are requests that do not get a response try { method->invoke(params, nullptr); } catch (const std::exception &e) { // NOLINT // No logging here, dump directly to stdout fprintf( stderr, "exception from notification handler: %s\n", e.what()); } return Json::nullValue; } else { const auto response( std::make_shared<JsonRpcMethod::Response>(this, id)); m_methodResponses.emplace_back(response); try { method->invoke(params, response); return Json::nullValue; } catch (const std::exception &e) { // NOLINT // Failed to invoke the method; send a default error return errorResponse( id, -32603, "exception in method", e.what()); } } } else { // Method was deleted, nothing useful to do (could delete the // weak_ptr, but not bothering for now) return Json::nullValue; } } }
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)); }
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")); } }
bool AbstractClient::isSpecialWindow() const { // TODO return isDesktop() || isDock() || isSplash() || isToolbar() || isNotification() || isOnScreenDisplay(); }