[DEV] First client call on RPC but not auto bind Module function
This commit is contained in:
parent
11170b362e
commit
af9c2fa9e7
29
.gitignore
vendored
29
.gitignore
vendored
@ -1,28 +1 @@
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.pyc
|
||||
|
@ -7,18 +7,46 @@
|
||||
#include <jus/Client.h>
|
||||
#include <jus/debug.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
jus::Client::Client() :
|
||||
propertyIp(this, "ip", "127.0.0.1", "Ip to connect server", &jus::Client::onPropertyChangeIp),
|
||||
propertyPort(this, "port", 1983, "Port to connect server", &jus::Client::onPropertyChangePort) {
|
||||
propertyPort(this, "port", 1983, "Port to connect server", &jus::Client::onPropertyChangePort),
|
||||
m_id(0) {
|
||||
m_interfaceClient.propertyIp.set(*propertyIp);
|
||||
m_interfaceClient.propertyPort.set(*propertyPort);
|
||||
m_interfaceClient.propertyServer.set(false);
|
||||
m_dataCallback = m_interfaceClient.signalData.connect(this, &jus::Client::onClientData);
|
||||
}
|
||||
|
||||
jus::Client::~Client() {
|
||||
|
||||
}
|
||||
|
||||
void jus::Client::onClientData(const std::string& _value) {
|
||||
m_newData.push_back(_value);
|
||||
}
|
||||
|
||||
std::string jus::Client::asyncRead() {
|
||||
int32_t iii = 5000;
|
||||
while (iii>0) {
|
||||
usleep(10000);
|
||||
if (m_newData.size() != 0) {
|
||||
break;
|
||||
}
|
||||
--iii;
|
||||
}
|
||||
if (iii == 0) {
|
||||
// Time-out ...
|
||||
return "";
|
||||
}
|
||||
std::string out;
|
||||
out = std::move(m_newData[0]);
|
||||
m_newData.erase(m_newData.begin());
|
||||
JUS_DEBUG("get async data: " << out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void jus::Client::onPropertyChangeIp() {
|
||||
m_interfaceClient.propertyIp.set(*propertyIp);
|
||||
}
|
||||
@ -38,4 +66,22 @@ void jus::Client::disconnect(){
|
||||
JUS_DEBUG("disconnect [START]");
|
||||
m_interfaceClient.disconnect();
|
||||
JUS_DEBUG("disconnect [STOP]");
|
||||
}
|
||||
}
|
||||
|
||||
ejson::Object jus::Client::createBaseCall(const std::string& _functionName) {
|
||||
ejson::Object obj;
|
||||
obj.add("call", ejson::String(_functionName));
|
||||
obj.add("transaction-id", ejson::Number(m_id++));
|
||||
obj.add("param", ejson::Array());
|
||||
return obj;
|
||||
}
|
||||
|
||||
ejson::Object jus::Client::callJson(const ejson::Object& _obj) {
|
||||
JUS_VERBOSE("Call JSON [START] ");
|
||||
std::string tmpVal = _obj.generate();
|
||||
JUS_DEBUG("Call JSON '" << tmpVal << "'");
|
||||
m_interfaceClient.write(_obj.generate());
|
||||
std::string ret = asyncRead();
|
||||
JUS_VERBOSE("Call JSON [STOP]");
|
||||
return ejson::Object(ret);
|
||||
}
|
||||
|
328
jus/Client.h
328
jus/Client.h
@ -7,6 +7,8 @@
|
||||
|
||||
#include <jus/TcpString.h>
|
||||
#include <eproperty/Value.h>
|
||||
#include <ejson/ejson.h>
|
||||
#include <jus/debug.h>
|
||||
|
||||
namespace jus {
|
||||
class Client : public eproperty::Interface {
|
||||
@ -15,15 +17,339 @@ namespace jus {
|
||||
eproperty::Value<uint16_t> propertyPort;
|
||||
private:
|
||||
jus::TcpString m_interfaceClient;
|
||||
uint32_t m_id;
|
||||
esignal::Connection m_dataCallback;
|
||||
std::vector<std::string> m_newData;
|
||||
public:
|
||||
Client();
|
||||
virtual ~Client();
|
||||
void connect();
|
||||
void disconnect();
|
||||
|
||||
private:
|
||||
void onClientData(const std::string& _value);
|
||||
std::string asyncRead();
|
||||
ejson::Object callJson(const ejson::Object& _obj);
|
||||
ejson::Object createBaseCall(const std::string& _functionName);
|
||||
void createParam(ejson::Object& _obj) {
|
||||
// Finish recursive parse ...
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const char* _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
if (_param == nullptr) {
|
||||
array.add(ejson::String());
|
||||
} else {
|
||||
array.add(ejson::String(_param));
|
||||
}
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const std::string& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
array.add(ejson::String(_param));
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const bool& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
array.add(ejson::Boolean(_param));
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const int32_t& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
array.add(ejson::Number(_param));
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const double& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
array.add(ejson::Number(_param));
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const float& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
array.add(ejson::Number(_param));
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const std::vector<std::string>& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
ejson::Array array2;
|
||||
for (auto& it : _param) {
|
||||
array2.add(ejson::String(it));
|
||||
}
|
||||
array.add(array2);
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const std::vector<bool>& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
ejson::Array array2;
|
||||
for (auto& it : _param) {
|
||||
array2.add(ejson::Boolean(it));
|
||||
}
|
||||
array.add(array2);
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const std::vector<int32_t>& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
ejson::Array array2;
|
||||
for (auto& it : _param) {
|
||||
array2.add(ejson::Number(it));
|
||||
}
|
||||
array.add(array2);
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const std::vector<double>& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
ejson::Array array2;
|
||||
for (auto& it : _param) {
|
||||
array2.add(ejson::Number(it));
|
||||
}
|
||||
array.add(array2);
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createParam(ejson::Object& _obj, const std::vector<float>& _param, _ARGS&&... _args) {
|
||||
ejson::Array array = _obj["param"].toArray();
|
||||
ejson::Array array2;
|
||||
for (auto& it : _param) {
|
||||
array2.add(ejson::Number(it));
|
||||
}
|
||||
array.add(array2);
|
||||
createParam(_obj, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
public:
|
||||
template<class... _ARGS>
|
||||
void call(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
}
|
||||
template<class... _ARGS>
|
||||
int32_t call_i(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return 0;
|
||||
}
|
||||
if (val.isNumber() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'Number'");
|
||||
return 0;
|
||||
}
|
||||
return int32_t(val.toNumber().get());
|
||||
}
|
||||
template<class... _ARGS>
|
||||
double call_d(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return 0.0;
|
||||
}
|
||||
if (val.isNumber() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'Number'");
|
||||
return 0.0;
|
||||
}
|
||||
return val.toNumber().get();
|
||||
}
|
||||
template<class... _ARGS>
|
||||
std::string call_s(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return "";
|
||||
}
|
||||
if (val.isString() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'String'");
|
||||
return "";
|
||||
}
|
||||
return val.toString().get();
|
||||
}
|
||||
template<class... _ARGS>
|
||||
bool call_b(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return false;
|
||||
}
|
||||
if (val.isBoolean() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'Boolean'");
|
||||
return false;
|
||||
}
|
||||
return val.toBoolean().get();
|
||||
}
|
||||
template<class... _ARGS>
|
||||
std::vector<int32_t> call_vi(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
if (obj.valueExist("return") == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<int32_t>();
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<int32_t>();
|
||||
}
|
||||
if (val.isArray() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'Array'");
|
||||
return std::vector<int32_t>();
|
||||
}
|
||||
std::vector<int32_t> out;
|
||||
for (auto it : val.toArray()) {
|
||||
if (val.isNumber() == false) {
|
||||
JUS_WARNING("Wrong return Type (part of array) get '" << it.getType() << " instead of 'Number'");
|
||||
continue;
|
||||
}
|
||||
out.push_back(int32_t(val.toNumber().get()));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
template<class... _ARGS>
|
||||
std::vector<double> call_vd(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
if (obj.valueExist("return") == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<double>();
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<double>();
|
||||
}
|
||||
if (val.isArray() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'Array'");
|
||||
return std::vector<double>();
|
||||
}
|
||||
std::vector<double> out;
|
||||
for (auto it : val.toArray()) {
|
||||
if (it.isNumber() == false) {
|
||||
JUS_WARNING("Wrong return Type (part of array) get '" << it.getType() << " instead of 'Number'");
|
||||
continue;
|
||||
}
|
||||
out.push_back(it.toNumber().get());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
template<class... _ARGS>
|
||||
std::vector<std::string> call_vs(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
if (obj.valueExist("return") == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
if (val.isArray() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'Array'");
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
std::vector<std::string> out;
|
||||
for (auto it : val.toArray()) {
|
||||
if (it.isString() == false) {
|
||||
JUS_WARNING("Wrong return Type (part of array) get '" << it.getType() << " instead of 'String'");
|
||||
continue;
|
||||
}
|
||||
out.push_back(it.toString().get());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
template<class... _ARGS>
|
||||
std::vector<bool> call_vb(const std::string& _functionName, _ARGS&&... _args) {
|
||||
ejson::Object callElem = createBaseCall(_functionName);
|
||||
createParam(callElem, std::forward<_ARGS>(_args)...);
|
||||
ejson::Object obj = callJson(callElem);
|
||||
if (obj.valueExist("error") == true) {
|
||||
JUS_WARNING("call error: " << obj["error"]);
|
||||
}
|
||||
if (obj.valueExist("return") == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<bool>();
|
||||
}
|
||||
ejson::Value val = obj["return"];
|
||||
if (val.exist() == false) {
|
||||
JUS_WARNING("No Return value ...");
|
||||
return std::vector<bool>();
|
||||
}
|
||||
if (val.isArray() == false) {
|
||||
JUS_WARNING("Wrong return Type get '" << val.getType() << " instead of 'Array'");
|
||||
return std::vector<bool>();
|
||||
}
|
||||
std::vector<bool> out;
|
||||
for (auto it : val.toArray()) {
|
||||
if (it.isBoolean() == false) {
|
||||
JUS_WARNING("Wrong return Type (part of array) get '" << it.getType() << " instead of 'Boolean'");
|
||||
continue;
|
||||
}
|
||||
out.push_back(it.toBoolean().get());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
private:
|
||||
void onPropertyChangeIp();
|
||||
void onPropertyChangePort();
|
||||
};
|
||||
/*
|
||||
template<class RETURN_TYPE>
|
||||
RETURN_TYPE Client::call(const std::string& _functionName) {
|
||||
ejson::Object callElem = ejson::Object(std::string("{ 'call':'") + _functionName + "'}");
|
||||
ejson::Object obj = callJson(callElem);
|
||||
return 256;
|
||||
}
|
||||
template<class RETURN_TYPE>
|
||||
RETURN_TYPE Client::call(const std::string& _functionName) {
|
||||
ejson::Object callElem = ejson::Object(std::string("{ 'call':'") + _functionName + "'}");
|
||||
ejson::Object obj = callJson(callElem);
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,9 @@
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <jus/debug.h>
|
||||
#include <jus/GateWayClient.h>
|
||||
#include <ejson/ejson.h>
|
||||
|
||||
jus::GateWayClient::GateWayClient() {
|
||||
|
||||
@ -19,9 +21,50 @@ void jus::GateWayClient::start(const std::string& _ip, uint16_t _port) {
|
||||
m_interfaceClient.propertyPort.set(_port);
|
||||
m_interfaceClient.propertyServer.set(true);
|
||||
m_interfaceClient.connect();
|
||||
m_dataCallback = m_interfaceClient.signalData.connect(this, &jus::GateWayClient::onClientData);
|
||||
}
|
||||
|
||||
void jus::GateWayClient::stop() {
|
||||
m_interfaceClient.disconnect();
|
||||
}
|
||||
|
||||
void jus::GateWayClient::onClientData(const std::string& _value) {
|
||||
JUS_DEBUG("On data: " << _value);
|
||||
ejson::Object data(_value);
|
||||
if (data.valueExist("service") == false) {
|
||||
// add default service
|
||||
data.add("service", ejson::String("ServiceManager"));
|
||||
JUS_WARNING("missing service name ==> set it by default at ServiceManager");
|
||||
}
|
||||
std::string service = data["service"].toString().get();
|
||||
// Thsi is 2 default service for the cient interface that manage the authorisation of view:
|
||||
if (service == "ServiceManager") {
|
||||
std::string call = data["call"].toString().get();
|
||||
ejson::Object answer;
|
||||
answer.add("from-service", ejson::String("ServiceManager"));
|
||||
answer.add("transaction-id", data["transaction-id"]);
|
||||
if (call == "getServiceCount") {
|
||||
// TODO : Do it better:
|
||||
answer.add("return", ejson::Number(2));
|
||||
} else if (call == "getServiceList") {
|
||||
ejson::Array listService;
|
||||
listService.add(ejson::String("ServiceManager/v0.1.0"));
|
||||
listService.add(ejson::String("getServiceInformation/v0.1.0"));
|
||||
answer.add("return", listService);
|
||||
} else if (call == "getServiceInformation") {
|
||||
|
||||
} else {
|
||||
JUS_ERROR("Function does not exist ... '" << call << "'");
|
||||
answer.add("error", ejson::String("CALL-UNEXISTING"));
|
||||
}
|
||||
std::string valueReturn = answer.generate();
|
||||
JUS_DEBUG("answer: " << valueReturn);
|
||||
m_interfaceClient.write(valueReturn);
|
||||
} else if (service == "Authentification") {
|
||||
std::string call = data["call"].toString().get();
|
||||
|
||||
} else {
|
||||
JUS_TODO("mmmmmmmmmmm: " << _value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,11 +14,13 @@ namespace jus {
|
||||
public:
|
||||
jus::TcpString m_interfaceClient;
|
||||
esignal::Signal<bool> signalIsConnected;
|
||||
esignal::Connection m_dataCallback;
|
||||
public:
|
||||
GateWayClient();
|
||||
virtual ~GateWayClient();
|
||||
void start(const std::string& _ip, uint16_t _port);
|
||||
void stop();
|
||||
void onClientData(const std::string& _value);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -4,4 +4,60 @@
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <jus/Service.h>
|
||||
#include <jus/debug.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
jus::Service::Service() :
|
||||
propertyIp(this, "ip", "127.0.0.1", "Ip to connect server", &jus::Service::onPropertyChangeIp),
|
||||
propertyPort(this, "port", 1984, "Port to connect server", &jus::Service::onPropertyChangePort),
|
||||
m_id(0) {
|
||||
m_interfaceClient.propertyIp.set(*propertyIp);
|
||||
m_interfaceClient.propertyPort.set(*propertyPort);
|
||||
m_interfaceClient.propertyServer.set(false);
|
||||
m_dataCallback = m_interfaceClient.signalData.connect(this, &jus::Service::onClientData);
|
||||
}
|
||||
|
||||
jus::Service::~Service() {
|
||||
|
||||
}
|
||||
|
||||
void jus::Service::onClientData(const std::string& _value) {
|
||||
ejson::Object request(_value);
|
||||
JUS_INFO("Request : " << _value);
|
||||
m_interfaceClient.write("{ \"error\": \"NOT-IMPLEMENTED\"}");
|
||||
}
|
||||
|
||||
void jus::Service::onPropertyChangeIp() {
|
||||
m_interfaceClient.propertyIp.set(*propertyIp);
|
||||
}
|
||||
|
||||
void jus::Service::onPropertyChangePort(){
|
||||
m_interfaceClient.propertyPort.set(*propertyPort);
|
||||
}
|
||||
|
||||
|
||||
void jus::Service::connect(){
|
||||
JUS_DEBUG("connect [START]");
|
||||
m_interfaceClient.connect();
|
||||
JUS_DEBUG("connect [STOP]");
|
||||
}
|
||||
|
||||
void jus::Service::disconnect(){
|
||||
JUS_DEBUG("disconnect [START]");
|
||||
m_interfaceClient.disconnect();
|
||||
JUS_DEBUG("disconnect [STOP]");
|
||||
}
|
||||
|
||||
ejson::Object jus::Service::callJson(const ejson::Object& _obj) {
|
||||
/*
|
||||
JUS_VERBOSE("Call JSON [START] ");
|
||||
std::string tmpVal = _obj.generate();
|
||||
JUS_DEBUG("Call JSON '" << tmpVal << "'");
|
||||
m_interfaceClient.write(_obj.generate());
|
||||
std::string ret = asyncRead();
|
||||
JUS_VERBOSE("Call JSON [STOP]");
|
||||
*/
|
||||
return ejson::Object();
|
||||
}
|
||||
|
@ -5,17 +5,80 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <jus/TcpString.h>
|
||||
#include <eproperty/Value.h>
|
||||
#include <ejson/ejson.h>
|
||||
#include <jus/debug.h>
|
||||
|
||||
namespace jus {
|
||||
class Service {
|
||||
class Service : public eproperty::Interface {
|
||||
public:
|
||||
//eproperty::Value<std::string> propertyServiceName;
|
||||
eproperty::Value<std::string> propertyIp;
|
||||
eproperty::Value<uint16_t> propertyPort;
|
||||
private:
|
||||
jus::TcpString m_interfaceClient;
|
||||
uint32_t m_id;
|
||||
esignal::Connection m_dataCallback;
|
||||
std::vector<std::string> m_newData;
|
||||
public:
|
||||
Service();
|
||||
virtual ~Service();
|
||||
// Genenric function call:
|
||||
ejson::Object callJson(const ejson::Object& _obj);
|
||||
void connect();
|
||||
void disconnect();
|
||||
private:
|
||||
// TODO: ...
|
||||
void bind(const std::string& _functionName) {}
|
||||
void onClientData(const std::string& _value);
|
||||
std::string asyncRead();
|
||||
private:
|
||||
void onPropertyChangeIp();
|
||||
void onPropertyChangePort();
|
||||
protected:
|
||||
|
||||
void createSignatureInternal(std::vector<std::string>& _signature) {
|
||||
// Finish recursive parse ...
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createSignatureInternal(std::vector<std::string>& _signature, const std::string& _param, _ARGS&&... _args) {
|
||||
_signature.push_back("string");
|
||||
createSignatureInternal(_signature, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createSignatureInternal(std::vector<std::string>& _signature, const bool& _param, _ARGS&&... _args) {
|
||||
_signature.push_back("bool");
|
||||
createSignatureInternal(_signature, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createSignatureInternal(std::vector<std::string>& _signature, const double& _param, _ARGS&&... _args) {
|
||||
_signature.push_back("double");
|
||||
createSignatureInternal(_signature, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
void createSignatureInternal(std::vector<std::string>& _signature, const int32_t& _param, _ARGS&&... _args) {
|
||||
_signature.push_back("int32");
|
||||
createSignatureInternal(_signature, std::forward<_ARGS>(_args)...);
|
||||
}
|
||||
template<class... _ARGS>
|
||||
std::vector<std::string> createSignature(_ARGS&&... _args) {
|
||||
std::vector<std::string> signature;
|
||||
createSignatureInternal(signature, std::forward<_ARGS>(_args)...);
|
||||
return signature;
|
||||
}
|
||||
|
||||
template<class JUS_RETURN_VALUE,
|
||||
class JUS_CLASS_TYPE,
|
||||
class... JUS_FUNC_ARGS_TYPE>
|
||||
void advertise(const std::string& _name,
|
||||
JUS_RETURN_VALUE (JUS_CLASS_TYPE::*_func)(const JUS_FUNC_ARGS_TYPE&... _args),
|
||||
const std::string& _desc) {
|
||||
/*
|
||||
std::vector<std::string> plop = createSignature(_args):
|
||||
JUS_ERROR("signature:");
|
||||
for (auto& it : plop) {
|
||||
JUS_ERROR(" - " << it);
|
||||
}
|
||||
*/
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <jus/TcpString.h>
|
||||
#include <jus/debug.h>
|
||||
#include <ethread/tools.h>
|
||||
#include <unistd.h>
|
||||
|
||||
jus::TcpString::TcpString() :
|
||||
m_thread(nullptr),
|
||||
@ -34,10 +35,11 @@ void jus::TcpString::threadCallback() {
|
||||
}
|
||||
signalIsConnected.emit(true);
|
||||
// get datas:
|
||||
while (m_threadRunning == true) {
|
||||
while ( m_threadRunning == true
|
||||
&& m_connection.getConnectionStatus() == enet::Tcp::status::link) {
|
||||
// READ section data:
|
||||
std::string data = std::move(read());
|
||||
JUS_WARNING("Receive data: '" << data << "'");
|
||||
JUS_VERBOSE("Receive data: '" << data << "'");
|
||||
if (data.size() != 0) {
|
||||
signalData.emit(data);
|
||||
}
|
||||
@ -59,17 +61,27 @@ void jus::TcpString::connect(){
|
||||
JUS_ERROR("creating callback thread!");
|
||||
return;
|
||||
}
|
||||
while ( m_threadRunning == true
|
||||
&& m_connection.getConnectionStatus() != enet::Tcp::status::link) {
|
||||
usleep(50000);
|
||||
}
|
||||
//ethread::setPriority(*m_receiveThread, -6);
|
||||
JUS_DEBUG("connect [STOP]");
|
||||
}
|
||||
|
||||
void jus::TcpString::disconnect(){
|
||||
JUS_DEBUG("disconnect [START]");
|
||||
uint32_t size = 0xFFFFFFFF;
|
||||
m_connection.write(&size, 4);
|
||||
if (m_connection.getConnectionStatus() == enet::Tcp::status::link) {
|
||||
uint32_t size = 0xFFFFFFFF;
|
||||
m_connection.write(&size, 4);
|
||||
}
|
||||
if (m_thread != nullptr) {
|
||||
m_threadRunning = false;
|
||||
}
|
||||
if (m_connection.getConnectionStatus() != enet::Tcp::status::unlink) {
|
||||
m_connection.unlink();
|
||||
}
|
||||
if (m_thread != nullptr) {
|
||||
m_thread->join();
|
||||
delete m_thread;
|
||||
m_thread = nullptr;
|
||||
@ -88,7 +100,7 @@ int32_t jus::TcpString::write(const std::string& _data) {
|
||||
|
||||
std::string jus::TcpString::read() {
|
||||
// TODO : Do it better with a correct way to check data size ...
|
||||
JUS_WARNING("Read [START]");
|
||||
JUS_VERBOSE("Read [START]");
|
||||
std::string out;
|
||||
uint32_t size = 0;
|
||||
int32_t len = m_connection.read(&size, 4);
|
||||
@ -110,7 +122,7 @@ std::string jus::TcpString::read() {
|
||||
}
|
||||
}
|
||||
}
|
||||
JUS_WARNING("Read [STOP]");
|
||||
JUS_VERBOSE("Read [STOP]");
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ namespace jus {
|
||||
void connect();
|
||||
void disconnect();
|
||||
int32_t write(const std::string& _data);
|
||||
std::string asyncRead();
|
||||
private:
|
||||
std::string read();
|
||||
private:
|
||||
|
@ -1,2 +1,3 @@
|
||||
tools/gateway
|
||||
test/client
|
||||
test/client
|
||||
test/service1
|
@ -26,7 +26,7 @@ def get_version():
|
||||
|
||||
def create(target, module_name):
|
||||
my_module = module.Module(__file__, module_name, get_type())
|
||||
my_module.add_module_depend(['etk', 'enet', 'ememory', 'eproperty', 'esignal'])
|
||||
my_module.add_module_depend(['etk', 'enet', 'ememory', 'eproperty', 'esignal', 'ejson'])
|
||||
my_module.add_src_file([
|
||||
'jus/debug.cpp'
|
||||
])
|
||||
@ -48,6 +48,8 @@ def create(target, module_name):
|
||||
'jus/Service.h',
|
||||
'jus/TcpString.h',
|
||||
])
|
||||
# build in C++ mode
|
||||
my_module.compile_version("c++", 2011)
|
||||
return my_module
|
||||
|
||||
|
||||
|
@ -33,10 +33,27 @@ int main(int _argc, const char *_argv[]) {
|
||||
APPL_INFO("== JUS test client start ==");
|
||||
APPL_INFO("==================================");
|
||||
client1.connect();
|
||||
APPL_INFO(" ----------------------------------");
|
||||
APPL_INFO(" -- Get service count --");
|
||||
APPL_INFO(" ----------------------------------");
|
||||
|
||||
std::vector<double> tmp;
|
||||
tmp.push_back(1);
|
||||
tmp.push_back(22);
|
||||
tmp.push_back(333);
|
||||
tmp.push_back(4444);
|
||||
int32_t val = client1.call_i("getServiceCount", tmp, "soucou", false);
|
||||
APPL_INFO("Nb services = " << val);
|
||||
std::vector<std::string> val2 = client1.call_vs("getServiceList");
|
||||
APPL_INFO("List services:");
|
||||
for (auto &it: val2) {
|
||||
APPL_INFO(" - " << it);
|
||||
}
|
||||
|
||||
int32_t iii=0;
|
||||
while (iii < 5) {
|
||||
while (iii < 3) {
|
||||
usleep(500000);
|
||||
APPL_INFO("Appl in waiting ... " << iii << "/5");
|
||||
APPL_INFO("Appl in waiting ... " << iii << "/3");
|
||||
iii++;
|
||||
}
|
||||
client1.disconnect();
|
||||
|
12
test/service1/appl/debug.cpp
Normal file
12
test/service1/appl/debug.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <appl/debug.h>
|
||||
|
||||
int32_t appl::getLogId() {
|
||||
static int32_t g_val = elog::registerInstance("jus-test-service1");
|
||||
return g_val;
|
||||
}
|
40
test/service1/appl/debug.h
Normal file
40
test/service1/appl/debug.h
Normal file
@ -0,0 +1,40 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <elog/log.h>
|
||||
|
||||
namespace appl {
|
||||
int32_t getLogId();
|
||||
};
|
||||
|
||||
#define APPL_BASE(info,data) ELOG_BASE(appl::getLogId(),info,data)
|
||||
|
||||
#define APPL_PRINT(data) APPL_BASE(-1, data)
|
||||
#define APPL_CRITICAL(data) APPL_BASE(1, data)
|
||||
#define APPL_ERROR(data) APPL_BASE(2, data)
|
||||
#define APPL_WARNING(data) APPL_BASE(3, data)
|
||||
#ifdef DEBUG
|
||||
#define APPL_INFO(data) APPL_BASE(4, data)
|
||||
#define APPL_DEBUG(data) APPL_BASE(5, data)
|
||||
#define APPL_VERBOSE(data) APPL_BASE(6, data)
|
||||
#define APPL_TODO(data) APPL_BASE(4, "TODO : " << data)
|
||||
#else
|
||||
#define APPL_INFO(data) do { } while(false)
|
||||
#define APPL_DEBUG(data) do { } while(false)
|
||||
#define APPL_VERBOSE(data) do { } while(false)
|
||||
#define APPL_TODO(data) do { } while(false)
|
||||
#endif
|
||||
|
||||
#define APPL_ASSERT(cond,data) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
APPL_CRITICAL(data); \
|
||||
assert(!#cond); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
62
test/service1/appl/main.cpp
Normal file
62
test/service1/appl/main.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <appl/debug.h>
|
||||
#include <jus/Service.h>
|
||||
#include <etk/etk.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <etk/stdTools.h>
|
||||
namespace appl {
|
||||
class Service1 : public jus::Service {
|
||||
private:
|
||||
double mul(const double& _val1, const double& _val2) {
|
||||
return _val1*_val2;
|
||||
}
|
||||
public:
|
||||
Service1() {
|
||||
advertise("mul", &appl::Service1::mul, "simple multiplication to test double IO");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
int main(int _argc, const char *_argv[]) {
|
||||
etk::init(_argc, _argv);
|
||||
appl::Service1 service1;
|
||||
for (int32_t iii=0; iii<_argc ; ++iii) {
|
||||
std::string data = _argv[iii];
|
||||
if (etk::start_with(data, "--ip=") == true) {
|
||||
service1.propertyIp.set(std::string(&data[5]));
|
||||
} else if (etk::start_with(data, "--port=") == true) {
|
||||
service1.propertyPort.set(etk::string_to_uint16_t(std::string(&data[7])));
|
||||
} else if ( data == "-h"
|
||||
|| data == "--help") {
|
||||
APPL_PRINT(etk::getApplicationName() << " - help : ");
|
||||
APPL_PRINT(" " << _argv[0] << " [options]");
|
||||
APPL_PRINT(" --ip=XXX Server connection IP (default: 1.7.0.0.1)");
|
||||
APPL_PRINT(" --port=XXX Server connection PORT (default: 1983)");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
APPL_INFO("==================================");
|
||||
APPL_INFO("== JUS test service1 start ==");
|
||||
APPL_INFO("==================================");
|
||||
/*
|
||||
service1.connect();
|
||||
int32_t iii=0;
|
||||
while (true) {
|
||||
usleep(500000);
|
||||
APPL_INFO("Appl in waiting ... " << iii << "/inf");
|
||||
iii++;
|
||||
}
|
||||
service1.disconnect();
|
||||
*/
|
||||
APPL_INFO("==================================");
|
||||
APPL_INFO("== JUS test service1 stop ==");
|
||||
APPL_INFO("==================================");
|
||||
return 0;
|
||||
}
|
38
test/service1/lutin_jus-test-service1.py
Normal file
38
test/service1/lutin_jus-test-service1.py
Normal file
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/python
|
||||
import lutin.module as module
|
||||
import lutin.tools as tools
|
||||
|
||||
|
||||
def get_type():
|
||||
return "BINARY"
|
||||
|
||||
def get_sub_type():
|
||||
return "TOOLS"
|
||||
|
||||
def get_desc():
|
||||
return "JUS test service"
|
||||
|
||||
def get_licence():
|
||||
return "APACHE-2"
|
||||
|
||||
def get_compagny_type():
|
||||
return "com"
|
||||
|
||||
def get_compagny_name():
|
||||
return "atria-soft"
|
||||
|
||||
def get_maintainer():
|
||||
return ["Mr DUPIN Edouard <yui.heero@gmail.com>"]
|
||||
|
||||
def create(target, module_name):
|
||||
my_module = module.Module(__file__, module_name, get_type())
|
||||
my_module.add_export_path(tools.get_current_path(__file__))
|
||||
my_module.add_module_depend(['jus'])
|
||||
my_module.add_src_file([
|
||||
'appl/debug.cpp',
|
||||
'appl/main.cpp'
|
||||
])
|
||||
return my_module
|
||||
|
||||
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user