Commit 22a0c63b authored by legoc's avatar legoc
Browse files

(split) Added storage functions to the C++ API

parent 06cce2de
......@@ -5,6 +5,7 @@
* Added Server::getVersion() which returns the server version.
* Added Output::isEndOfLine() which returns true if the message terminates with an end of line.
* Removed Configuration::getRetries().
* Added storage functions: This::storeKeyValue(), This::getKeyValue(), This::removeKey(), Instance::getKeyValue().
0.3.3
-----
......
......@@ -28,6 +28,8 @@
#include "PublisherCreationException.h"
#include "RequesterCreationException.h"
#include "ResponderCreationException.h"
#include "UndefinedApplicationException.h"
#include "UndefinedKeyException.h"
#include "Response.h"
#include "Serializer.h"
#include "Services.h"
......@@ -143,6 +145,10 @@ public:
*/
static std::unique_ptr<Instance> connectToStarter();
static void storeKeyValue(const std::string& key, const std::string& value);
static std::string getKeyValue(const std::string& key);
static void removeKey(const std::string& key);
private:
void initApplication(int argc, char *argv[]);
......@@ -228,6 +234,8 @@ public:
std::shared_ptr<OutputStreamSocket> getOutputStreamSocket();
std::string getKeyValue(const std::string& key);
private:
Instance(Server * server);
......
......@@ -60,6 +60,7 @@ typedef rapidjson::Document Object;
typedef rapidjson::Value Value;
void parse(Object & object, zmq::message_t * message);
void parse(Object & object, const std::string& string);
}
}
......
......@@ -122,6 +122,9 @@ private:
std::unique_ptr<application::Subscriber> createSubscriber(int id, const std::string& publisherName, const std::string& instanceName);
int getAvailableTimeout() const;
int getStreamPort(const std::string& name);
void storeKeyValue(int id, const std::string& key, const std::string& value);
std::string getKeyValue(int id, const std::string& key);
void removeKey(int id, const std::string& key);
std::mutex m_eventListenersMutex;
std::vector<EventListener *> m_eventListeners;
......
......@@ -14,54 +14,19 @@
* limitations under the Licence.
*/
#ifndef CAMEO_JSON_H_
#define CAMEO_JSON_H_
#ifndef CAMEO_UNDEFINEDAPPLICATIONEXCEPTION_H_
#define CAMEO_UNDEFINEDAPPLICATIONEXCEPTION_H_
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <rapidjson/document.h>
#include "zmq.hpp"
#include <string>
#include <stdint.h>
#include "RemoteException.h"
namespace cameo {
namespace json {
/**
* Helper class wrapping the rapidjson writer.
*/
class StringObject {
class UndefinedApplicationException : public RemoteException {
public:
StringObject();
void pushKey(const char* key);
void pushInt(int value);
void pushInt64(int64_t value);
void pushBool(bool value);
void pushDouble(double value);
void pushString(const std::string& value);
void startObject();
void endObject();
void startArray();
void endArray();
std::string toString();
private:
rapidjson::StringBuffer m_buffer;
rapidjson::Writer<rapidjson::StringBuffer> m_writer;
UndefinedApplicationException(const std::string& message);
};
typedef rapidjson::Document Object;
typedef rapidjson::Value Value;
void parse(Object & object, zmq::message_t * message);
}
}
#endif
/*
* Copyright 2015 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#ifndef CAMEO_UNDEFINEDKEYEXCEPTION_H_
#define CAMEO_UNDEFINEDKEYEXCEPTION_H_
#include "RemoteException.h"
namespace cameo {
class UndefinedKeyException : public RemoteException {
public:
UndefinedKeyException(const std::string& message);
};
}
#endif
......@@ -21,6 +21,8 @@
#include <iostream>
#include <stdexcept>
#include <vector>
#include "JSON.h"
#include "EventStreamSocket.h"
#include "impl/ServicesImpl.h"
#include "impl/PublisherImpl.h"
......@@ -33,7 +35,6 @@
#include "impl/HandlerImpl.h"
#include "impl/StreamSocketImpl.h"
#include "impl/RequestSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include "Server.h"
#include "StarterServerException.h"
......@@ -403,6 +404,18 @@ std::unique_ptr<Instance> This::connectToStarter() {
return unique_ptr<Instance>(nullptr);
}
void This::storeKeyValue(const std::string& key, const std::string& value) {
m_instance.m_server->storeKeyValue(m_instance.m_id, key, value);
}
std::string This::getKeyValue(const std::string& key) {
return m_instance.m_server->getKeyValue(m_instance.m_id, key);
}
void This::removeKey(const std::string& key) {
m_instance.m_server->removeKey(m_instance.m_id, key);
}
void This::stoppingFunction(StopFunctionType stop) {
application::State state = waitForStop();
......@@ -665,6 +678,11 @@ std::shared_ptr<OutputStreamSocket> Instance::getOutputStreamSocket() {
return m_outputStreamSocket;
}
std::string Instance::getKeyValue(const std::string& key) {
// TODO catch exceptions and rethrow an exception: TerminatedException?
return m_server->getKeyValue(m_id, key);
}
///////////////////////////////////////////////////////////////////////////
// InstanceArray
......
......@@ -16,13 +16,13 @@
#include "EventStreamSocket.h"
#include "JSON.h"
#include "impl/SocketWaitingImpl.h"
#include "PortEvent.h"
#include "PublisherEvent.h"
#include "ResultEvent.h"
#include "StatusEvent.h"
#include "impl/StreamSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
using namespace std;
......
......@@ -19,9 +19,9 @@
#include "impl/SocketWaitingImpl.h"
#include "impl/ServicesImpl.h"
#include "impl/StreamSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include <iostream>
#include "JSON.h"
using namespace std;
......
......@@ -21,10 +21,12 @@
#include "EventThread.h"
#include "impl/StreamSocketImpl.h"
#include "impl/RequestSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include "UndefinedApplicationException.h"
#include "UndefinedKeyException.h"
#include <iostream>
#include <sstream>
#include "JSON.h"
using namespace std;
......@@ -422,6 +424,54 @@ std::unique_ptr<ConnectionChecker> Server::createConnectionChecker(ConnectionChe
return connectionChecker;
}
void Server::storeKeyValue(int id, const std::string& key, const std::string& value) {
unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createStoreKeyValueRequest(id, key, value));
// Get the JSON response.
json::Object response;
json::parse(response, reply.get());
}
std::string Server::getKeyValue(int id, const std::string& key) {
unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createGetKeyValueRequest(id, key));
// Get the JSON response.
json::Object response;
json::parse(response, reply.get());
int value = response[message::RequestResponse::VALUE].GetInt();
if (value == 0) {
return response[message::RequestResponse::MESSAGE].GetString();
}
else if (value == -1) {
throw UndefinedApplicationException(response[message::RequestResponse::MESSAGE].GetString());
}
else if (value == -2) {
throw UndefinedKeyException(response[message::RequestResponse::MESSAGE].GetString());
}
return "";
}
void Server::removeKey(int id, const std::string& key) {
unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createRemoveKeyRequest(id, key));
// Get the JSON response.
json::Object response;
json::parse(response, reply.get());
int value = response[message::RequestResponse::VALUE].GetInt();
if (value == -1) {
throw UndefinedApplicationException(response[message::RequestResponse::MESSAGE].GetString());
}
else if (value == -2) {
throw UndefinedKeyException(response[message::RequestResponse::MESSAGE].GetString());
}
}
std::vector<EventListener *> Server::getEventListeners() {
std::unique_lock<std::mutex> lock(m_eventListenersMutex);
return m_eventListeners;
......
......@@ -19,11 +19,11 @@
#include "impl/ServicesImpl.h"
#include "impl/StreamSocketImpl.h"
#include "impl/RequestSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include <iostream>
#include <sstream>
#include <stdexcept>
#include "JSON.h"
using namespace std;
......
/*
* Copyright 2015 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#include "UndefinedApplicationException.h"
namespace cameo {
UndefinedApplicationException::UndefinedApplicationException(const std::string& message) :
RemoteException(message) {
}
}
/*
* Copyright 2015 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#include "UndefinedKeyException.h"
namespace cameo {
UndefinedKeyException::UndefinedKeyException(const std::string& message) :
RemoteException(message) {
}
}
......@@ -19,9 +19,9 @@
#include "Serializer.h"
#include "ServicesImpl.h"
#include "RequestSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include <sstream>
#include "JSON.h"
using namespace std;
......
......@@ -20,9 +20,9 @@
#include "Serializer.h"
#include "ServicesImpl.h"
#include "RequestSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include <sstream>
#include "JSON.h"
using namespace std;
......
......@@ -19,9 +19,9 @@
#include "Serializer.h"
#include "ServicesImpl.h"
#include "RequestSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include <sstream>
#include "JSON.h"
using namespace std;
......
......@@ -20,9 +20,9 @@
#include "ServicesImpl.h"
#include "RequestImpl.h"
#include "RequestSocketImpl.h"
#include "message/JSON.h"
#include "message/Message.h"
#include <sstream>
#include "JSON.h"
using namespace std;
......
......@@ -17,11 +17,11 @@
#include "ServicesImpl.h"
#include "SocketException.h"
#include "ConnectionTimeout.h"
#include "message/JSON.h"
#include "message/Message.h"
#include "RequestSocketImpl.h"
#include <iostream>
#include <sstream>
#include "JSON.h"
// Using Visual Studio preprocessor.
// It must be improved in case of other compilers.
......@@ -393,6 +393,55 @@ std::string ServicesImpl::createRequestResponse(int64_t value, const std::string
return request.toString();
}
std::string ServicesImpl::createStoreKeyValueRequest(int id, const std::string& key, const std::string& value) {
json::StringObject request;
request.pushKey(message::TYPE);
request.pushInt(message::STORE_KEY_VALUE);
request.pushKey(message::StoreKeyValueRequest::ID);
request.pushInt(id);
request.pushKey(message::StoreKeyValueRequest::KEY);
request.pushString(key);
request.pushKey(message::StoreKeyValueRequest::VALUE);
request.pushString(value);
return request.toString();
}
std::string ServicesImpl::createGetKeyValueRequest(int id, const std::string& key) {
json::StringObject request;
request.pushKey(message::TYPE);
request.pushInt(message::GET_KEY_VALUE);
request.pushKey(message::GetKeyValueRequest::ID);
request.pushInt(id);
request.pushKey(message::GetKeyValueRequest::KEY);
request.pushString(key);
return request.toString();
}
std::string ServicesImpl::createRemoveKeyRequest(int id, const std::string& key) {
json::StringObject request;
request.pushKey(message::TYPE);
request.pushInt(message::REMOVE_KEY);
request.pushKey(message::RemoveKeyRequest::ID);
request.pushInt(id);
request.pushKey(message::RemoveKeyRequest::KEY);
request.pushString(key);
return request.toString();
}
zmq::socket_t * ServicesImpl::createEventSubscriber(const std::string& endpoint, const std::string& cancelEndpoint) {
zmq::socket_t * subscriber = new zmq::socket_t(m_context, ZMQ_SUB);
......
......@@ -59,6 +59,9 @@ public:
std::string createOutputRequest(const std::string& name) const;
std::string createRequestResponse(int64_t value) const;
std::string createRequestResponse(int64_t value, const std::string& message) const;
std::string createStoreKeyValueRequest(int id, const std::string& key, const std::string& value);
std::string createGetKeyValueRequest(int id, const std::string& key);
std::string createRemoveKeyRequest(int id, const std::string& key);
zmq::socket_t * createEventSubscriber(const std::string& endpoint, const std::string& cancelEndpoint);
zmq::socket_t * createOutputStreamSubscriber(const std::string& endpoint, const std::string& cancelEndpoint);
......
......@@ -20,9 +20,9 @@
#include "ServicesImpl.h"
#include "RequestSocketImpl.h"
#include "Server.h"
#include "message/JSON.h"
#include "message/Message.h"
#include <sstream>
#include "JSON.h"
using namespace std;
......
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