diff --git a/server/controlServer.cpp b/server/controlServer.cpp index 14b0795b..e8e8392e 100644 --- a/server/controlServer.cpp +++ b/server/controlServer.cpp @@ -22,10 +22,14 @@ #include "message/request.h" #include "message/command.h" #include "common/log.h" +#include "common/snapException.h" +#include "json.hpp" #include using namespace std; +using json = nlohmann::json; + ControlServer::ControlServer(size_t port) : port_(port) { @@ -63,8 +67,8 @@ void ControlServer::send(const std::string& message) void ControlServer::onMessageReceived(ControlSession* connection, const std::string& message) { - logO << "received: " << message << "\n"; - if (message == "quit") + logO << "received: \"" << message << "\"\n"; + if ((message == "quit") || (message == "exit") || (message == "bye")) { for (auto it = sessions_.begin(); it != sessions_.end(); ++it) { @@ -76,7 +80,92 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str } } else - connection->send("echo " + message); + { + int id = -1; + try + { + // http://www.jsonrpc.org/specification + // code message meaning + // -32700 Parse error Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text. + // -32600 Invalid Request The JSON sent is not a valid Request object. + // -32601 Method not found The method does not exist / is not available. + // -32602 Invalid params Invalid method parameter(s). + // -32603 Internal error Internal JSON-RPC error. + // -32000 to -32099 Server error Reserved for implementation-defined server-errors. + json request; + try + { + try + { + request = json::parse(message); + } + catch (const exception& e) + { + throw SnapException(e.what(), -32700); + } + + id = request["id"].get(); + string jsonrpc = request["jsonrpc"].get(); + if (jsonrpc != "2.0") + throw SnapException("invalid jsonrpc value: " + jsonrpc, -32600); + string method = request["method"].get(); + if (method.empty()) + throw SnapException("method must not be empty", -32600); + if (id < 0) + throw SnapException("id must be a positive integer", -32600); + + json response = { + {"jsonrpc", "2.0"}, + {"id", id} + }; + + if (method == "get") + { + response["result"] = "???";//nullptr; + } + else if (method == "set") + { + response["result"] = "234";//nullptr; + } + else + throw SnapException("method not found: \"" + method + "\"", -32601); + + connection->send(response.dump()); + } + catch (const SnapException& e) + { + throw; + } + catch (const exception& e) + { + throw SnapException(e.what(), -32603); + } + } + catch (const SnapException& e) + { + int errorCode = e.errorCode(); + if (errorCode == 0) + errorCode = -32603; + + json response = { + {"jsonrpc", "2.0"}, + {"error", { + {"code", errorCode}, + {"message", e.what()} + }}, + }; + if (id == -1) + response["id"] = nullptr; + else + response["id"] = id; + + connection->send(response.dump()); + } + } +//get status +//get status server +//get status client[:MAC] +//set volume client[:MAC] {VOLUME} } diff --git a/server/controlSession.cpp b/server/controlSession.cpp index 45a2aa40..3b952867 100644 --- a/server/controlSession.cpp +++ b/server/controlSession.cpp @@ -115,11 +115,11 @@ void ControlSession::reader() { boost::asio::streambuf response; boost::asio::read_until(*socket_, response, "\r\n"); - std::istream response_stream(&response); - std::string strResponse; - response_stream >> strResponse; + std::string s((istreambuf_iterator(&response)), istreambuf_iterator()); + s.resize(s.length() - 2); + if (messageReceiver_ != NULL) - messageReceiver_->onMessageReceived(this, strResponse); + messageReceiver_->onMessageReceived(this, s); } } catch (const std::exception& e)