Commit 06ecffd9 authored by legoc's avatar legoc
Browse files

Added convenient function in Com

parent 43c9b960
......@@ -135,6 +135,8 @@ public:
std::unique_ptr<RequestSocketImpl> createRequestSocket(const std::string& endpoint) const;
std::unique_ptr<RequestSocketImpl> createRequestSocket(const std::string& endpoint, int timeout) const;
std::string createJSONResponse(int value, const std::string& message) const;
void removePort(const std::string& name) const;
private:
......@@ -201,8 +203,6 @@ private:
void initApplication(const std::string& name, const std::string& endpoint);
void initApplication();
//bool inited() const;
static State parseState(const std::string& value);
State getState(int id) const;
......@@ -254,6 +254,8 @@ public:
std::string getKeyValue(const std::string& key) const;
json::Object request(const std::string& request, int overrideTimeout = -1) const;
std::string createJSONResponse(int value, const std::string& message) const;
private:
Com(Server* server);
......
......@@ -100,6 +100,19 @@ std::unique_ptr<RequestSocketImpl> This::Com::createRequestSocket(const std::str
return m_server->createRequestSocket(endpoint, timeout);
}
std::string This::Com::createJSONResponse(int value, const std::string& message) const {
json::StringObject request;
request.pushKey(message::RequestResponse::VALUE);
request.pushInt64(value);
request.pushKey(message::RequestResponse::MESSAGE);
request.pushString(message);
return request.toString();
}
void This::Com::removePort(const std::string& name) const {
json::Object response = m_server->requestJSON(createRemovePortV0Request(m_applicationId, name));
......@@ -445,6 +458,19 @@ json::Object Instance::Com::request(const std::string& request, int overrideTime
return m_server->requestJSON(request, overrideTimeout);
}
std::string Instance::Com::createJSONResponse(int value, const std::string& message) const {
json::StringObject request;
request.pushKey(message::RequestResponse::VALUE);
request.pushInt64(value);
request.pushKey(message::RequestResponse::MESSAGE);
request.pushString(message);
return request.toString();
}
Instance::Instance(Server * server) :
m_server(server),
m_id(-1),
......
......@@ -138,7 +138,23 @@ public class Application {
throw new UnexpectedException("Cannot parse message");
}
}
/**
* Method provided by convenience to simplify the use of JSON messages.
* @param value
* @param message
* @return
*/
public JSONObject createJSONResponse(int value, String message) {
JSONObject response = new JSONObject();
response.put(Messages.RequestResponse.VALUE, value);
response.put(Messages.RequestResponse.MESSAGE, "OK");
return response;
}
//TODO Remove when use of key values is done.
public void removePort(String name) {
JSONObject response = server.requestJSON(Messages.createRemovePortV0Request(applicationId, name));
......@@ -441,6 +457,35 @@ public class Application {
public JSONObject request(JSONObject request) {
return server.requestJSON(request);
}
/**
* Method provided by convenience to simplify the parsing of JSON messages.
* @param message
* @return
*/
public JSONObject parse(byte[] message) {
try {
return server.parse(message);
}
catch (ParseException e) {
throw new UnexpectedException("Cannot parse message");
}
}
/**
* Method provided by convenience to simplify the use of JSON messages.
* @param value
* @param message
* @return
*/
public JSONObject createJSONResponse(int value, String message) {
JSONObject response = new JSONObject();
response.put(Messages.RequestResponse.VALUE, value);
response.put(Messages.RequestResponse.MESSAGE, "OK");
return response;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment