[DEV] complete remove of json to better manage of websocket

This commit is contained in:
Edouard DUPIN 2016-06-16 21:15:53 +02:00
parent 5023d6842b
commit 557eb8736b
20 changed files with 62 additions and 699 deletions

View File

@ -9,6 +9,7 @@
#include <jus/ParamType.h>
#include <jus/File.h>
#include <jus/Buffer.h>
#include <ememory/memory.h>
namespace jus {
class TcpString;

View File

@ -70,10 +70,6 @@ void jus::Buffer::internalComposeWith(const uint8_t* _buffer, uint32_t _lenght)
void jus::Buffer::composeWith(const std::vector<uint8_t>& _buffer) {
internalComposeWith(&_buffer[0], _buffer.size());
}
void jus::Buffer::composeWith(const std::string& _buffer) {
fromJson(ejson::Object(_buffer));
}
void jus::Buffer::clear() {
JUS_VERBOSE("clear buffer");
@ -2253,318 +2249,3 @@ std::string jus::Buffer::getErrorHelp() {
return "";
}
ejson::Object jus::Buffer::toJson() const {
ejson::Object out;
out.add("id", ejson::Number(getTransactionId()));
out.add("client-id", ejson::Number(getClientId()));
uint16_t partId = getPartId();
bool partFinish = getPartFinish();
if ( partId != 0
|| partFinish == false) {
out.add("part", ejson::Number(partId));
if (partFinish == true) {
out.add("finish", ejson::Boolean(partFinish));
}
}
if (getType() == jus::Buffer::typeMessage::call) {
out.add("call", ejson::String(getCall()));
ejson::Array listParam;
out.add("param", listParam);
for (int32_t iii=0; iii<getNumberParameter(); ++iii) {
std::string type = getParameterType(iii);
const uint8_t* dataPointer = getParameterPointer(iii);
uint32_t dataLenght = getParameterSize(iii);
ejson::Value param;
if (createType<bool>() == type) {
param = convertBinaryToJson<bool>(dataPointer, dataLenght);
} else if (createType<int8_t>() == type) {
param = convertBinaryToJson<int8_t>(dataPointer, dataLenght);
} else if (createType<int16_t>() == type) {
param = convertBinaryToJson<int16_t>(dataPointer, dataLenght);
} else if (createType<int32_t>() == type) {
param = convertBinaryToJson<int32_t>(dataPointer, dataLenght);
} else if (createType<int64_t>() == type) {
param = convertBinaryToJson<int64_t>(dataPointer, dataLenght);
} else if (createType<uint8_t>() == type) {
param = convertBinaryToJson<uint8_t>(dataPointer, dataLenght);
} else if (createType<uint16_t>() == type) {
param = convertBinaryToJson<uint16_t>(dataPointer, dataLenght);
} else if (createType<uint32_t>() == type) {
param = convertBinaryToJson<uint32_t>(dataPointer, dataLenght);
} else if (createType<uint64_t>() == type) {
param = convertBinaryToJson<uint64_t>(dataPointer, dataLenght);
} else if (createType<float>() == type) {
param = convertBinaryToJson<float>(dataPointer, dataLenght);
} else if (createType<double>() == type) {
param = convertBinaryToJson<double>(dataPointer, dataLenght);
} else if (createType<std::string>() == type) {
param = convertBinaryToJson<std::string>(dataPointer, dataLenght);
} else if (createType<std::vector<bool>>() == type) {
//param = convertBinaryToJson<std::vector<bool>>(dataPointer, dataLenght);
} else if (createType<std::vector<int8_t>>() == type) {
//param = convertBinaryToJson<std::vector<int8_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<int16_t>>() == type) {
//param = convertBinaryToJson<std::vector<int16_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<int32_t>>() == type) {
//param = convertBinaryToJson<std::vector<int32_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<int64_t>>() == type) {
//param = convertBinaryToJson<std::vector<int64_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<uint8_t>>() == type) {
//param = convertBinaryToJson<std::vector<uint8_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<uint16_t>>() == type) {
//param = convertBinaryToJson<std::vector<uint16_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<uint32_t>>() == type) {
//param = convertBinaryToJson<std::vector<uint32_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<uint64_t>>() == type) {
//param = convertBinaryToJson<std::vector<uint64_t>>(dataPointer, dataLenght);
} else if (createType<std::vector<float>>() == type) {
//param = convertBinaryToJson<std::vector<float>>(dataPointer, dataLenght);
} else if (createType<std::vector<double>>() == type) {
//param = convertBinaryToJson<std::vector<double>>(dataPointer, dataLenght);
} else if (createType<std::vector<std::string>>() == type) {
param = convertBinaryToJson<std::vector<std::string>>(dataPointer, dataLenght);
} else {
JUS_ERROR("Unknow param ==> can not convert ...");
}
listParam.add(param);
}
} else if (getType() == jus::Buffer::typeMessage::answer) {
} else if (getType() == jus::Buffer::typeMessage::event) {
JUS_ERROR(" NOT managed ...");
} else {
JUS_ERROR("Unknow TYPE ...");
}
return out;
}
void jus::Buffer::fromJson(const ejson::Object& _data) {
clear();
uint32_t valueClientId = 0;
uint16_t partId = 0;
bool partFinish = true;
{
ejson::Value val = _data["id"];
if (val.exist() == false) {
// TODO...
return;
}
ejson::Number valNumber = val.toNumber();
if (valNumber.exist() == false) {
// TODO...
return;
}
uint64_t value = valNumber.getU64();
if (value > 0xFFFFFFFFLU) {
// TODO...
return;
}
if (value == 0) {
// TODO...
return;
}
setTransactionId(value);
}
{
// can be null (==> access at the gateway ...)
ejson::Value val = _data["client-id"];
if (val.exist() == true) {
ejson::Number valNumber = val.toNumber();
if (valNumber.exist() == false) {
// TODO...
return;
}
uint64_t value = valNumber.getU64();
if (value > 0xFFFFFFFFLU) {
// TODO...
return;
}
if (value == 0) {
// TODO...
return;
}
setClientId(value);
}
}
{
// can be null (==> access at the gateway ...)
ejson::Value val = _data["part"];
if (val.exist() == true) {
ejson::Number valNumber = val.toNumber();
if (valNumber.exist() == false) {
// TODO...
return;
}
uint64_t value = valNumber.getU64();
if (value > 0xFFFFLU) {
// TODO...
return;
}
if (value == 0) {
// TODO...
return;
}
setPartId(value);
}
}
{
// can be null (==> access at the gateway ...)
ejson::Value val = _data["finish"];
if (val.exist() == true) {
ejson::Boolean valBoolean = val.toBoolean();
if (valBoolean.exist() == false) {
// TODO...
return;
}
bool value = valBoolean.get();
setPartFinish(value);
}
}
// call mode
{
// can be null (==> access at the gateway ...)
ejson::Value val = _data["call"];
if (val.exist() == true) {
setType(jus::Buffer::typeMessage::call);
addParameter(val);
val = _data["param"];
if (val.exist() == true) {
ejson::Array list = val.toArray();
if (list.exist() == false) {
// TODO: Error
return;
}
for (auto it : list) {
addParameter(it);
}
}
return;
}
}
// event mode
{
// can be null (==> access at the gateway ...)
ejson::Value val = _data["event"];
if (val.exist() == true) {
setType(jus::Buffer::typeMessage::event);
addParameter(val);
return;
}
}
// answer mode
{
// can be null (==> access at the gateway ...)
ejson::Value val = _data["return"];
if (val.exist() == true) {
setType(jus::Buffer::typeMessage::answer);
addParameter(val);
}
// check error
val = _data["error"];
if (val.exist() == true) {
ejson::String valError = val.toString();
if (valError.exist() == false) {
// TODO...
return;
}
if (getType() != jus::Buffer::typeMessage::answer) {
setType(jus::Buffer::typeMessage::answer);
}
val = _data["error-help"];
if (val.exist() == true) {
ejson::String valErrorHelp = val.toString();
if (valErrorHelp.exist() == false) {
// TODO...
return;
}
addError(valError.get(), valErrorHelp.get());
} else {
addError(valError.get(), "");
}
}
return;
}
JUS_ERROR("Unknow message type");
// TODO ...
}
void jus::Buffer::addParameter(ejson::Value _value) {
if (_value.isBoolean() == true) {
addParameter(_value.toBoolean().get());
return;
}
if (_value.isNumber() == true) {
ejson::Number num = _value.toNumber();
if (num.getType() == ejson::internal::Number::type::tDouble) {
addParameter(num.get());
} else if (num.getType() == ejson::internal::Number::type::tInt) {
int64_t val = num.getI64();
if (val<-2147483648) {
addParameter(int64_t(val));
} else if (val>2147483647) {
addParameter(int64_t(val));
} else if (val<-65338) {
addParameter(int32_t(val));
} else if (val>65337) {
addParameter(int32_t(val));
} else if (val<-128) {
addParameter(int16_t(val));
} else if (val>127) {
addParameter(int16_t(val));
} else {
addParameter(int8_t(val));
}
} else {
uint64_t val = num.getU64();
if (val>4294967295) {
addParameter(int64_t(val));
} else if (val>65535) {
addParameter(int32_t(val));
} else if (val>256) {
addParameter(int16_t(val));
} else {
addParameter(int8_t(val));
}
}
return;
}
if (_value.isString() == true) {
addParameter(_value.toString().get());
return;
}
if (_value.isArray() == true) {
ejson::Array elementArray = _value.toArray();
if (elementArray.size() == 0) {
addParameterEmptyVector();
return;
}
if (elementArray[0].isBoolean() == true) {
std::vector<bool> tmp;
for (auto it : elementArray) {
tmp.push_back(jus::convertJsonTo<bool>(it));
}
addParameter(tmp);
} else if (elementArray[0].isNumber() == true) {
std::vector<int64_t> tmp;
for (auto it : elementArray) {
tmp.push_back(jus::convertJsonTo<int64_t>(it));
}
addParameter(tmp);
} else if (elementArray[0].isString() == true) {
std::vector<std::string> tmp;
for (auto it : elementArray) {
tmp.push_back(jus::convertJsonTo<std::string>(it));
}
addParameter(tmp);
} else {
JUS_ERROR("Array of object or array is not managed ...");
}
return;
}
if (_value.isObject() == true) {
JUS_ERROR("Not manage element Object");
return;
}
JUS_ERROR("Unknow TYPE ...");
}

View File

@ -5,7 +5,6 @@
*/
#pragma once
#include <etk/types.h>
#include <ejson/ejson.h>
namespace jus {
//U32 message lenght
@ -156,9 +155,6 @@ namespace jus {
const uint8_t* getParameterPointer(int32_t _id) const;
uint32_t getParameterSize(int32_t _id) const;
private:
// add parameter for transcription of json element
void addParameter(ejson::Value _value);
public:
template<class JUS_TYPE_DATA>
void addParameter(const JUS_TYPE_DATA& _value);
@ -194,12 +190,7 @@ namespace jus {
void addData(void* _data, uint32_t _size);
void prepare();
ejson::Object toJson() const;
void fromJson(const ejson::Object& _data);
};
std::ostream& operator <<(std::ostream& _os, enum jus::Buffer::typeMessage _value);
template<class JUS_TYPE>
ejson::Value convertBinaryToJson(const uint8_t* _data, uint32_t _size);
}

View File

@ -11,7 +11,6 @@
#include <chrono>
#include <unistd.h>
#include <jus/Future.h>
#include <jus/connectionMode.h>
#include <jus/ServiceRemote.h>
namespace jus {

View File

@ -7,6 +7,8 @@
#include <etk/types.h>
#include <jus/Buffer.h>
#include <functional>
#include <ememory/memory.h>
namespace jus {
@ -19,7 +21,6 @@ namespace jus {
bool m_isSynchronous;
bool m_isFinished;
jus::Buffer m_returnData;
//std::vector<ejson::Value> m_returnDataPart;
ObserverFinish m_callbackFinish;
std::chrono::steady_clock::time_point m_sendTime;
std::chrono::steady_clock::time_point m_receiveTime;

View File

@ -6,7 +6,6 @@
#include <jus/debug.h>
#include <jus/GateWayClient.h>
#include <ejson/ejson.h>
#include <jus/GateWay.h>
#include <unistd.h>
@ -47,23 +46,9 @@ void jus::GateWayClient::stop() {
continue;
}
it->m_interfaceClient.callClient(m_uid, "_delete");
/*
ejson::Object linkService;
linkService.add("call", ejson::String("_delete"));
linkService.add("id", ejson::Number(m_transactionLocalId++));
linkService.add("param", ejson::Array());
it->SendData(m_uid, linkService);
*/
}
if (m_userService != nullptr) {
m_userService->m_interfaceClient.callClient(m_uid2, "_delete");
/*
ejson::Object linkService;
linkService.add("call", ejson::String("_delete"));
linkService.add("id", ejson::Number(m_transactionLocalId++));
linkService.add("param", ejson::Array());
m_userService->SendData(m_uid2, linkService);
*/
m_userService = nullptr;
}
m_listConnectedService.clear();
@ -108,25 +93,7 @@ void jus::GateWayClient::onClientData(jus::Buffer& _value) {
answerProtocolError(transactionId, "Gateway internal error 2");
return;
}
if (callFunction == "setMode") {
std::string mode = _value.getParameter<std::string>(0);
if (mode == "JSON") {
JUS_WARNING("[" << m_uid << "] Change mode in: JSON");
m_interfaceClient.answerValue(transactionId, true);
m_interfaceClient.setMode(jus::connectionMode::modeJson);
} else if (mode == "BIN") {
JUS_WARNING("[" << m_uid << "] Change mode in: BINARY");
m_interfaceClient.answerValue(transactionId, true);
m_interfaceClient.setMode(jus::connectionMode::modeBinary);
} else if (mode == "XML") {
JUS_WARNING("[" << m_uid << "] Change mode in: XML");
//m_interfaceMode = jus::connectionMode::modeXml;
m_interfaceClient.answerValue(transactionId, false);
} else {
answerProtocolError(transactionId, std::string("Call setMode with unknow argument : '") /*+ etk::to_string(int32_t(mode))*/ + "' supported [JSON/XML/BIN]");
}
return;
} else if (callFunction == "connectToUser") {
if (callFunction == "connectToUser") {
m_userConnectionName = _value.getParameter<std::string>(0);
if (m_userConnectionName == "") {
answerProtocolError(transactionId, "Call connectToUser with no parameter 'user'");
@ -246,7 +213,7 @@ void jus::GateWayClient::onClientData(jus::Buffer& _value) {
}
if (callFunction == "getServiceList") {
m_interfaceClient.answerValue(transactionId, m_clientServices);
//listService.add(ejson::String("ServiceManager/v0.1.0"));
//"ServiceManager/v0.1.0"
return;
}
if (callFunction == "link") {
@ -343,16 +310,7 @@ void jus::GateWayClient::onClientData(jus::Buffer& _value) {
tmpp.setTransactionId(transactionId);
tmpp.setServiceId(serviceId+1);
JUS_DEBUG("transmit=" << tmpp.generateHumanString());
if (m_interfaceClient.getMode() == jus::connectionMode::modeJson) {
ejson::Object obj = tmpp.toJson();
m_interfaceClient.writeJson(obj);
} else if (m_interfaceClient.getMode() == jus::connectionMode::modeBinary) {
m_interfaceClient.writeBinary(tmpp);
} else if (m_interfaceClient.getMode() == jus::connectionMode::modeXml) {
JUS_ERROR("TODO ... ");
} else {
JUS_ERROR("wrong type of communication");
}
m_interfaceClient.writeBinary(tmpp);
// multiple send element ...
return tmpp.getPartFinish();
});

View File

@ -11,7 +11,6 @@
#include <jus/GateWayService.h>
#include <jus/Future.h>
#include <jus/AbstractFunction.h>
#include <jus/connectionMode.h>
@ -30,10 +29,6 @@ namespace jus {
private:
jus::GateWay* m_gatewayInterface;
jus::TcpString m_interfaceClient;
public:
enum jus::connectionMode getMode() {
return m_interfaceClient.getMode();
}
public:
esignal::Signal<bool> signalIsConnected;
ememory::SharedPtr<jus::GateWayService> m_userService;

View File

@ -7,7 +7,6 @@
#include <jus/debug.h>
#include <jus/GateWayService.h>
#include <jus/GateWay.h>
#include <ejson/ejson.h>
// todo : cHANGE THIS ...
static const std::string protocolError = "PROTOCOL-ERROR";
@ -43,28 +42,15 @@ void jus::GateWayService::stop() {
m_interfaceClient.disconnect();
}
void jus::GateWayService::SendData(uint64_t _userSessionId, ejson::Object _data) {
_data.add("client-id", ejson::Number(_userSessionId));
m_interfaceClient.writeJson(_data);
}
void jus::GateWayService::SendData(uint64_t _userSessionId, jus::Buffer& _data) {
_data.setClientId(_userSessionId);
_data.prepare();
if (m_interfaceClient.getMode() == jus::connectionMode::modeJson) {
ejson::Object obj = _data.toJson();
m_interfaceClient.writeJson(obj);
} else if (m_interfaceClient.getMode() == jus::connectionMode::modeXml) {
JUS_ERROR("NOT manage transcriptioon binary to XML ... ");
} else if (m_interfaceClient.getMode() == jus::connectionMode::modeBinary) {
m_interfaceClient.writeBinary(_data);
} else {
JUS_ERROR("NOT manage transcriptioon binary to ??? mode ... ");
}
m_interfaceClient.writeBinary(_data);
}
void jus::GateWayService::onServiceData(jus::Buffer& _value) {
JUS_DEBUG("On service data: " << _value.toJson().generateHumanString());
JUS_DEBUG("On service data: " << _value.generateHumanString());
uint32_t transactionId = _value.getTransactionId();
//data.add("from-service", ejson::String(m_name));
if (_value.getType() == jus::Buffer::typeMessage::event) {
@ -88,25 +74,7 @@ void jus::GateWayService::onServiceData(jus::Buffer& _value) {
}
if (_value.getType() == jus::Buffer::typeMessage::call) {
std::string callFunction = _value.getCall();
if (callFunction == "setMode") {
std::string mode = _value.getParameter<std::string>(0);
if (mode == "JSON") {
JUS_WARNING("Change mode in: JSON");
m_interfaceClient.answerValue(transactionId, true);
m_interfaceClient.setMode(jus::connectionMode::modeJson);
} else if (mode == "BIN") {
JUS_WARNING("Change mode in: BINARY");
m_interfaceClient.answerValue(transactionId, true);
m_interfaceClient.setMode(jus::connectionMode::modeBinary);
} else if (mode == "XML") {
JUS_WARNING("Change mode in: XML");
//m_interfaceMode = jus::connectionMode::modeXml;
m_interfaceClient.answerValue(transactionId, false);
} else {
answerProtocolError(transactionId, std::string("Call setMode with unknow argument : '") /*+ etk::to_string(int32_t(mode))*/ + "' supported [JSON/XML/BIN]");
}
return;
} else if (callFunction == "connect-service") {
if (callFunction == "connect-service") {
if (m_name != "") {
JUS_WARNING("Service interface ==> try change the servie name after init: '" << _value.getParameter<std::string>(0));
m_interfaceClient.answerValue(transactionId, false);

View File

@ -8,8 +8,6 @@
#include <jus/TcpString.h>
#include <ememory/memory.h>
#include <esignal/Signal.h>
#include <ejson/ejson.h>
#include <jus/connectionMode.h>
namespace jus {
class GateWay;
@ -29,7 +27,6 @@ namespace jus {
void stop();
void onServiceData(jus::Buffer& _value);
public:
void SendData(uint64_t _userSessionId, ejson::Object _data);
void SendData(uint64_t _userSessionId, jus::Buffer& _data);
const std::string& getName() {
return m_name;

View File

@ -7,7 +7,6 @@
#include <jus/TcpString.h>
#include <eproperty/Value.h>
#include <ejson/ejson.h>
#include <jus/AbstractFunctionTypeDirect.h>
#include <jus/AbstractFunctionTypeClass.h>
#include <jus/debug.h>

View File

@ -8,7 +8,6 @@
#include <jus/debug.h>
#include <etk/stdTools.h>
#include <enet/TcpClient.h>
#include <ejson/ejson.h>
#include <unistd.h>
@ -85,15 +84,7 @@ void jus::Service::connect(const std::string& _serviceName, uint32_t _numberRetr
m_interfaceClient->connect(this, &jus::Service::onClientData);
m_interfaceClient->setInterface(std::move(connection));
m_interfaceClient->connect();
jus::Future<bool> ret = m_interfaceClient->call("setMode", "BIN");
ret.wait();
if (ret.get() == false) {
JUS_ERROR("Can not communicate with the gateway in Binary mode ... STAY in JSON");
} else {
JUS_INFO("Change mode in Binary");
m_interfaceClient->setMode(jus::connectionMode::modeBinary);
}
ret = m_interfaceClient->call("connect-service", _serviceName);
jus::Future<bool> ret = m_interfaceClient->call("connect-service", _serviceName);
ret.wait();
if (ret.get() == false) {
JUS_ERROR("Can not configure the interface for the service with the current name ...");

View File

@ -7,7 +7,6 @@
#include <jus/TcpString.h>
#include <eproperty/Value.h>
#include <ejson/ejson.h>
#include <jus/AbstractFunctionTypeDirect.h>
#include <jus/AbstractFunctionTypeClass.h>
#include <jus/debug.h>

View File

@ -7,12 +7,10 @@
#include <jus/TcpString.h>
#include <eproperty/Value.h>
#include <ejson/ejson.h>
#include <jus/debug.h>
#include <jus/AbstractFunction.h>
#include <jus/ServiceRemote.h>
#include <jus/Future.h>
#include <jus/connectionMode.h>
#include <jus/TcpString.h>
namespace jus {

View File

@ -15,7 +15,6 @@ jus::TcpString::TcpString(enet::Tcp _connection) :
m_threadAsync(nullptr) {
m_threadRunning = false;
m_threadAsyncRunning = false;
m_interfaceMode = jus::connectionMode::modeJson;
m_transmissionId = 1;
}
@ -26,7 +25,6 @@ jus::TcpString::TcpString() :
m_threadAsync(nullptr) {
m_threadRunning = false;
m_threadAsyncRunning = false;
m_interfaceMode = jus::connectionMode::modeJson;
m_transmissionId = 1;
}
@ -114,23 +112,9 @@ void jus::TcpString::disconnect(bool _inThreadStop){
JUS_DEBUG("disconnect [STOP]");
}
int32_t jus::TcpString::writeJson(ejson::Object& _data) {
JUS_DEBUG("Send JSON '" << _data.generateHumanString() << "'");
if (m_threadRunning == false) {
return -2;
}
m_lastSend = std::chrono::steady_clock::now();
m_connection.write("J", 1);
std::string tmpData = _data.generateMachineString();
uint32_t dataSize = tmpData.size();
m_connection.write(&dataSize, sizeof(uint32_t));
m_connection.write(&tmpData[0], tmpData.size());
return 1;
}
int32_t jus::TcpString::writeBinary(jus::Buffer& _data) {
_data.prepare();
JUS_DEBUG("Send BINARY '" << _data.toJson().generateHumanString() << "'");
JUS_DEBUG("Send BINARY '" << _data.generateHumanString() << "'");
if (m_threadRunning == false) {
return -2;
}
@ -174,24 +158,36 @@ void jus::TcpString::read() {
}
out += char(type);
JUS_INFO(" ** " << out);
if ( out[out.size()-1] == '\n'
if ( out.size() > 4
&& out[out.size()-1] == '\n'
&& out[out.size()-2] == '\r'
&& out[out.size()-3] == '\n'
&& out[out.size()-4] == '\r') {
break;
}
if ( out.size() > 2
&& out[out.size()-1] == '\n'
&& out[out.size()-2] == '\n') {
break;
}
/*
if (char(type) == '\n') {
if (out.find("User-Agent") != std::string::npos) {
break;
}
}
*/
}
JUS_INFO("Find WebSocket ...");
JUS_INFO("data='" << out << "'");
if ( out[0] != 'G'
&& out[1] != 'E'
&& out[2] != 'T') {
std::string ret = "HTTP/1.0 400 Bad Request\n";
ret += "Content-Type : application/octet-stream\n";
ret += "\n";
if ( if out.size() < 3
|| ( out[0] != 'G'
&& out[1] != 'E'
&& out[2] != 'T'
)
) {
std::string ret = "HTTP/1.0 400 Bad Request\r\n";
ret += "\r\n";
m_connection.write(&ret[0], ret.size());
disconnect(true);
} else {
@ -229,49 +225,12 @@ void jus::TcpString::read() {
}
}
JUS_VERBOSE("ReadRaw [STOP]");
} else if ( type == 'X' // XML
|| type == 'J' // JSON
) {
JUS_VERBOSE("Read sized String [START]");
// XML/JSON mode ... start with the lenght of the stream
std::string out;
uint32_t size = 0;
len = m_connection.read(&size, 4);
if (len != 4) {
JUS_ERROR("Protocol error occured ...");
} else {
if (size == -1) {
JUS_WARNING("Remote close connection");
m_threadRunning = false;
//m_connection.unlink();
} else {
int64_t offset = 0;
out.resize(size);
while (offset != size) {
len = m_connection.read(&out[offset], size-offset);
offset += len;
if (len == 0) {
JUS_WARNING("Read No data");
}
}
jus::Buffer dataRaw;
dataRaw.composeWith(out);
newBuffer(dataRaw);
}
}
JUS_VERBOSE("Read sized String [STOP]");
} else if (type == '{') {
// JSON Raw mode ... Finish with a \0
// TODO: m_dataBuffer
} else if (type == '<') {
// XML Raw mode ... Finish with a \0
// TODO : m_dataBuffer
}
}
}
void jus::TcpString::newBuffer(jus::Buffer& _buffer) {
JUS_VERBOSE("Receive Binary :" << _buffer.toJson().generateHumanString());
JUS_VERBOSE("Receive Binary :" << _buffer.generateHumanString());
jus::FutureBase future;
uint64_t tid = _buffer.getTransactionId();
if (tid == 0) {
@ -363,77 +322,6 @@ void jus::TcpString::threadAsyncCallback() {
class SendAsyncJson {
private:
std::vector<jus::ActionAsyncClient> m_async;
uint64_t m_transactionId;
uint32_t m_serviceId;
uint32_t m_partId;
public:
SendAsyncJson(uint64_t _transactionId, const uint32_t& _serviceId, const std::vector<jus::ActionAsyncClient>& _async) :
m_async(_async),
m_transactionId(_transactionId),
m_serviceId(_serviceId),
m_partId(1) {
}
bool operator() (jus::TcpString* _interface){
auto it = m_async.begin();
while (it != m_async.end()) {
bool ret = (*it)(_interface, m_serviceId, m_transactionId, m_partId);
if (ret == true) {
// Remove it ...
it = m_async.erase(it);
} else {
++it;
}
m_partId++;
}
if (m_async.size() == 0) {
ejson::Object obj;
if (m_serviceId != 0) {
obj.add("service", ejson::Number(m_serviceId));
}
obj.add("id", ejson::Number(m_transactionId));
obj.add("part", ejson::Number(m_partId));
obj.add("finish", ejson::Boolean(true));
_interface->writeJson(obj);
return true;
}
return false;
}
};
jus::FutureBase jus::TcpString::callJson(uint64_t _transactionId,
ejson::Object _obj,
const std::vector<ActionAsyncClient>& _async,
jus::FutureData::ObserverFinish _callback,
const uint32_t& _serviceId) {
JUS_VERBOSE("Send JSON [START] ");
if (isActive() == false) {
jus::Buffer obj;
obj.setType(jus::Buffer::typeMessage::answer);
obj.addError("NOT-CONNECTED", "Client interface not connected (no TCP)");
return jus::FutureBase(_transactionId, true, obj, _callback);
}
jus::FutureBase tmpFuture(_transactionId, _callback);
{
std::unique_lock<std::mutex> lock(m_pendingCallMutex);
m_pendingCall.push_back(std::make_pair(uint64_t(0), tmpFuture));
}
if (_async.size() != 0) {
_obj.add("part", ejson::Number(0));
}
writeJson(_obj);
if (_async.size() != 0) {
addAsync(SendAsyncJson(_transactionId, _serviceId, _async));
}
JUS_VERBOSE("Send JSON [STOP]");
return tmpFuture;
}
class SendAsyncBinary {
private:
std::vector<jus::ActionAsyncClient> m_async;
@ -527,16 +415,7 @@ jus::FutureBase jus::TcpString::callForward(uint32_t _clientId,
std::unique_lock<std::mutex> lock(m_pendingCallMutex);
m_pendingCall.push_back(std::make_pair(_singleReferenceId, tmpFuture));
}
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object obj = _buffer.toJson();
writeJson(obj);
} else if (m_interfaceMode == jus::connectionMode::modeBinary) {
writeBinary(_buffer);
} else if (m_interfaceMode == jus::connectionMode::modeXml) {
JUS_ERROR("TODO ... ");
} else {
JUS_ERROR("wrong type of communication");
}
writeBinary(_buffer);
JUS_VERBOSE("Send Forward [STOP]");
return tmpFuture;
}
@ -552,16 +431,7 @@ void jus::TcpString::callForwardMultiple(uint32_t _clientId,
// Find element ==> transit it ...
_buffer.setTransactionId(itCall.second.getTransactionId());
_buffer.setClientId(_clientId);
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object obj = _buffer.toJson();
writeJson(obj);
} else if (m_interfaceMode == jus::connectionMode::modeBinary) {
writeBinary(_buffer);
} else if (m_interfaceMode == jus::connectionMode::modeXml) {
JUS_ERROR("TODO ... ");
} else {
JUS_ERROR("wrong type of communication");
}
writeBinary(_buffer);
return;
}
}
@ -569,49 +439,20 @@ void jus::TcpString::callForwardMultiple(uint32_t _clientId,
}
void jus::TcpString::answerError(uint64_t _clientTransactionId, const std::string& _errorValue, const std::string& _errorHelp, uint32_t _clientId) {
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object answer;
answer.add("error", ejson::String(_errorValue));
answer.add("id", ejson::Number(_clientTransactionId));
if (_clientId != 0) {
answer.add("client-id", ejson::Number(_clientId));
}
answer.add("error-help", ejson::String(_errorHelp));
writeJson(answer);
} else if (m_interfaceMode == jus::connectionMode::modeBinary) {
jus::Buffer answer;
answer.setType(jus::Buffer::typeMessage::answer);
answer.setTransactionId(_clientTransactionId);
answer.setClientId(_clientId);
answer.addError(_errorValue, _errorHelp);
writeBinary(answer);
} else if (m_interfaceMode == jus::connectionMode::modeXml) {
JUS_ERROR("TODO ... ");
} else {
JUS_ERROR("wrong type of communication");
}
jus::Buffer answer;
answer.setType(jus::Buffer::typeMessage::answer);
answer.setTransactionId(_clientTransactionId);
answer.setClientId(_clientId);
answer.addError(_errorValue, _errorHelp);
writeBinary(answer);
}
void jus::TcpString::answerVoid(uint64_t _clientTransactionId, uint32_t _clientId) {
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object answer;
answer.add("id", ejson::Number(_clientTransactionId));
if (_clientId != 0) {
answer.add("client-id", ejson::Number(_clientId));
}
answer.add("return", ejson::Null());
writeJson(answer);
} else if (m_interfaceMode == jus::connectionMode::modeBinary) {
jus::Buffer answer;
answer.setType(jus::Buffer::typeMessage::answer);
answer.setTransactionId(_clientTransactionId);
answer.setClientId(_clientId);
answer.addParameter();
writeBinary(answer);
} else if (m_interfaceMode == jus::connectionMode::modeXml) {
JUS_ERROR("TODO ... ");
} else {
JUS_ERROR("wrong type of communication");
}
jus::Buffer answer;
answer.setType(jus::Buffer::typeMessage::answer);
answer.setTransactionId(_clientTransactionId);
answer.setClientId(_clientId);
answer.addParameter();
writeBinary(answer);
}

View File

@ -10,7 +10,6 @@
#include <enet/Tcp.h>
#include <thread>
#include <memory>
#include <jus/connectionMode.h>
#include <jus/AbstractFunction.h>
#include <jus/FutureBase.h>
@ -26,15 +25,6 @@ namespace jus {
}
std::mutex m_pendingCallMutex;
std::vector<std::pair<uint64_t, jus::FutureBase>> m_pendingCall;
protected:
enum jus::connectionMode m_interfaceMode;
public:
enum jus::connectionMode getMode() {
return m_interfaceMode;
}
void setMode(enum jus::connectionMode _mode) {
m_interfaceMode = _mode;
}
std::vector<uint8_t> m_buffer;
std::vector<uint8_t> m_temporaryBuffer;
std::chrono::steady_clock::time_point m_lastReceive;
@ -63,7 +53,6 @@ namespace jus {
void disconnect(bool _inThreadStop = false);
bool isActive() const;
void setInterfaceName(const std::string& _name);
int32_t writeJson(ejson::Object& _data);
int32_t writeBinary(jus::Buffer& _data);
std::string asyncRead();
private:
@ -93,11 +82,6 @@ namespace jus {
m_threadAsyncList.push_back(_elem);
}
private:
jus::FutureBase callJson(uint64_t _transactionId,
ejson::Object _obj,
const std::vector<ActionAsyncClient>& _async,
jus::FutureData::ObserverFinish _callback=nullptr,
const uint32_t& _service=0);
jus::FutureBase callBinary(uint64_t _transactionId,
jus::Buffer& _obj,
const std::vector<ActionAsyncClient>& _async,
@ -108,25 +92,15 @@ namespace jus {
jus::FutureBase call(const std::string& _functionName, _ARGS&&... _args) {
uint16_t id = getId();
std::vector<jus::ActionAsyncClient> asyncAction;
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object callElem = jus::createCall(asyncAction, id, _functionName, std::forward<_ARGS>(_args)...);
return callJson(id, callElem, asyncAction);
} else {
jus::Buffer callElem = jus::createBinaryCall(asyncAction, id, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncAction);
}
jus::Buffer callElem = jus::createBinaryCall(asyncAction, id, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncAction);
}
template<class... _ARGS>
jus::FutureBase callAction(const std::string& _functionName, _ARGS&&... _args, jus::FutureData::ObserverFinish _callback) {
uint16_t id = getId();
std::vector<jus::ActionAsyncClient> asyncAction;
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object callElem = jus::createCall(asyncAction, id, _functionName, std::forward<_ARGS>(_args)...);
return callJson(id, callElem, asyncAction, _callback);
} else {
jus::Buffer callElem = jus::createBinaryCall(asyncAction, id, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncAction, _callback);
}
jus::Buffer callElem = jus::createBinaryCall(asyncAction, id, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncAction, _callback);
}
public: // section call with service ID / Client ID
@ -134,25 +108,15 @@ namespace jus {
jus::FutureBase callService(uint32_t _serviceId, const std::string& _functionName, _ARGS&&... _args) {
uint16_t id = getId();
std::vector<jus::ActionAsyncClient> asyncActionToDo;
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object callElem = jus::createCallService(asyncActionToDo, id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
return callJson(id, callElem, asyncActionToDo);
} else {
jus::Buffer callElem = jus::createBinaryCallService(asyncActionToDo, id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncActionToDo);
}
jus::Buffer callElem = jus::createBinaryCallService(asyncActionToDo, id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncActionToDo);
}
template<class... _ARGS>
jus::FutureBase callServiceAction(uint32_t _serviceId, const std::string& _functionName, _ARGS&&... _args, jus::FutureData::ObserverFinish _callback) {
uint16_t id = getId();
std::vector<jus::ActionAsyncClient> asyncActionToDo;
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object callElem = jus::createCallService(asyncActionToDo, id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
return callJson(id, callElem, asyncActionToDo, _callback);
} else {
jus::Buffer callElem = jus::createBinaryCallService(asyncActionToDo, id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncActionToDo, _callback);
}
jus::Buffer callElem = jus::createBinaryCallService(asyncActionToDo, id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
return callBinary(id, callElem, asyncActionToDo, _callback);
}
template<class... _ARGS>
jus::FutureBase callClient(uint32_t _clientId,
@ -179,30 +143,12 @@ namespace jus {
void answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp);
template<class JUS_ARG>
void answerValue(uint64_t _clientTransactionId, JUS_ARG _value, uint32_t _clientId=0) {
if (m_interfaceMode == jus::connectionMode::modeJson) {
ejson::Object answer;
answer.add("id", ejson::Number(_clientTransactionId));
if (_clientId != 0) {
answer.add("client-id", ejson::Number(_clientId));
}
std::vector<jus::ActionAsyncClient> asyncAction;
answer.add("return", jus::convertToJson(asyncAction, -1, _value));
if (asyncAction.size() != 0) {
JUS_ERROR("ASYNC datas ... TODO ///");
}
writeJson(answer);
} else if (m_interfaceMode == jus::connectionMode::modeBinary) {
jus::Buffer answer;
answer.setType(jus::Buffer::typeMessage::answer);
answer.setTransactionId(_clientTransactionId);
answer.setClientId(_clientId);
answer.addAnswer(_value);
writeBinary(answer);
} else if (m_interfaceMode == jus::connectionMode::modeXml) {
JUS_ERROR("TODO ... ");
} else {
JUS_ERROR("wrong type of communication");
}
jus::Buffer answer;
answer.setType(jus::Buffer::typeMessage::answer);
answer.setTransactionId(_clientTransactionId);
answer.setClientId(_clientId);
answer.addAnswer(_value);
writeBinary(answer);
}
void answerVoid(uint64_t _clientTransactionId, uint32_t _clientId=0);
void answerError(uint64_t _clientTransactionId, const std::string& _errorValue, const std::string& _errorComment="", uint32_t _clientId=0);

View File

View File

@ -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', 'ejson'])
my_module.add_module_depend(['etk', 'enet', 'ememory', 'eproperty', 'esignal'])
my_module.add_src_file([
'jus/debug.cpp'
])
@ -39,7 +39,6 @@ def create(target, module_name):
'jus/Future.cpp',
'jus/File.cpp',
'jus/Buffer.cpp',
'jus/BufferConvertBinaryToJson.cpp',
'jus/ParamType.cpp',
'jus/Client.cpp',
'jus/GateWay.cpp',
@ -60,7 +59,6 @@ def create(target, module_name):
'jus/Future.h',
'jus/File.h',
'jus/Buffer.h',
'jus/connectionMode.h',
'jus/ParamType.h',
'jus/debug.h',
'jus/Client.h',

View File

@ -27,7 +27,7 @@ def get_maintainer():
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_module_depend(['jus', 'ejson'])
my_module.add_src_file([
'appl/debug.cpp',
'appl/main.cpp'

View File

@ -27,7 +27,7 @@ def get_maintainer():
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_module_depend(['jus', 'ejson'])
my_module.add_src_file([
'appl/debug.cpp',
'appl/main.cpp'