[DEV] strat thinking micro service system in c++

This commit is contained in:
Edouard DUPIN 2016-05-12 22:45:40 +02:00
parent 3d76706e0f
commit e1cff69481
22 changed files with 612 additions and 1 deletions

View File

@ -1 +1,26 @@
# jus
jus
===
`jus` is a Json µ-service
Instructions
============
TODO ...
License (APACHE v2.0)
=====================
Copyright jus Edouard DUPIN
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

7
jus/Client.cpp Normal file
View File

@ -0,0 +1,7 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/

19
jus/Client.h Normal file
View File

@ -0,0 +1,19 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#pragma once
#include <jus/TcpString.h>
namespace jus {
class Client {
private:
jus::TcpString m_interfaceClient;
public:
Client() {}
virtual ~Client() {}
};
}

62
jus/GateWay.cpp Normal file
View File

@ -0,0 +1,62 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <jus/GateWay.h>
#include <jus/debug.h>
jus::GateWay::GateWay() :
propertyClientIp(this, "client-ip", "127.0.0.1", "Ip to listen client", &jus::GateWay::onPropertyChangeClientIp),
propertyClientPort(this, "client-port", 1983, "Port to listen client", &jus::GateWay::onPropertyChangeClientPort),
propertyClientMax(this, "client-max", 80, "Maximum of client at the same time", &jus::GateWay::onPropertyChangeClientMax),
propertyServiceIp(this, "service-ip", "127.0.0.1", "Ip to listen client", &jus::GateWay::onPropertyChangeServiceIp),
propertyServicePort(this, "service-port", 1982, "Port to listen client", &jus::GateWay::onPropertyChangeServicePort),
propertyServiceMax(this, "service-max", 80, "Maximum of client at the same time", &jus::GateWay::onPropertyChangeServiceMax) {
}
jus::GateWay::~GateWay() {
}
void jus::GateWay::start() {
m_clientWaiting = std::make_shared<jus::GateWayClient>();
m_clientConnected = m_clientWaiting->signalIsConnected.connect(this, &jus::GateWay::onClientConnect);
m_clientWaiting->start(*propertyClientIp, *propertyClientPort);
}
void jus::GateWay::stop() {
}
void jus::GateWay::onClientConnect(const bool& _value) {
JUS_TODO("lklklk: " << _value);
}
void jus::GateWay::onPropertyChangeClientIp() {
}
void jus::GateWay::onPropertyChangeClientPort() {
}
void jus::GateWay::onPropertyChangeClientMax() {
}
void jus::GateWay::onPropertyChangeServiceIp() {
}
void jus::GateWay::onPropertyChangeServicePort() {
}
void jus::GateWay::onPropertyChangeServiceMax() {
}

40
jus/GateWay.h Normal file
View 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 <jus/GateWayService.h>
#include <jus/GateWayClient.h>
namespace jus {
class GateWay : public eproperty::Interface {
private:
std::vector<ememory::SharedPtr<jus::GateWayService>> m_serviceList; //!< List of all service availlable with their specific connection interface
std::vector<ememory::SharedPtr<jus::GateWayClient>> m_clientList; //!< List of all Client interface with their own connection
//TODO: std::vector<jus::GateWayServer> m_ServerList; //!< List of all Server connected to this gateway
ememory::SharedPtr<jus::GateWayClient> m_clientWaiting;
esignal::Connection m_clientConnected;
public:
eproperty::Value<std::string> propertyClientIp;
eproperty::Value<uint16_t> propertyClientPort;
eproperty::Value<uint16_t> propertyClientMax;
eproperty::Value<std::string> propertyServiceIp;
eproperty::Value<uint16_t> propertyServicePort;
eproperty::Value<uint16_t> propertyServiceMax;
public:
GateWay();
virtual ~GateWay();
void start();
void stop();
private:
void onPropertyChangeClientIp();
void onPropertyChangeClientPort();
void onPropertyChangeClientMax();
void onPropertyChangeServiceIp();
void onPropertyChangeServicePort();
void onPropertyChangeServiceMax();
void onClientConnect(const bool& _value);
};
}

26
jus/GateWayClient.cpp Normal file
View File

@ -0,0 +1,26 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <jus/GateWayClient.h>
jus::GateWayClient::GateWayClient() {
}
jus::GateWayClient::~GateWayClient() {
}
void jus::GateWayClient::start(const std::string& _ip, uint16_t _port) {
m_interfaceClient.propertyIp.set(_ip);
m_interfaceClient.propertyPort.set(_port);
}
void jus::GateWayClient::stop() {
}

24
jus/GateWayClient.h Normal file
View File

@ -0,0 +1,24 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#pragma once
#include <jus/TcpString.h>
#include <ememory/memory.h>
#include <esignal/Signal.h>
namespace jus {
class GateWayClient {
public:
jus::TcpString m_interfaceClient;
esignal::Signal<bool> signalIsConnected;
public:
GateWayClient();
virtual ~GateWayClient();
void start(const std::string& _ip, uint16_t _port);
void stop();
};
}

0
jus/GateWayService.cpp Normal file
View File

21
jus/GateWayService.h Normal file
View File

@ -0,0 +1,21 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#pragma once
#include <jus/TcpString.h>
#include <ememory/memory.h>
namespace jus {
class GateWayService {
public:
ememory::SharedPtr<jus::TcpString> m_interfaceService;
std::string m_name;
public:
GateWayService(const ememory::SharedPtr<jus::TcpString>& _interface);
virtual ~GateWayService();
};
}

7
jus/Service.cpp Normal file
View File

@ -0,0 +1,7 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/

21
jus/Service.h Normal file
View File

@ -0,0 +1,21 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#pragma once
namespace jus {
class Service {
public:
//eproperty::Value<std::string> propertyServiceName;
public:
Service();
virtual ~Service();
private:
// TODO: ...
void bind(const std::string& _functionName) {}
};
}

70
jus/TcpString.cpp Normal file
View File

@ -0,0 +1,70 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <jus/TcpString.h>
#include <jus/debug.h>
jus::TcpString::TcpString() :
propertyIp(this, "ip", "127.0.0.1", "ip to open or connect server", &jus::TcpString::onPropertyChangeIp),
propertyPort(this, "port", 1983, "Connection port of the server", &jus::TcpString::onPropertyChangePort),
propertyServer(this, "server", false, "is a server or not", &jus::TcpString::onPropertyChangeServer) {
m_connection.setHostNane(*propertyIp);
m_connection.setPort(*propertyPort);
m_connection.setServer(*propertyServer);
}
jus::TcpString::~TcpString() {
}
void jus::TcpString::connect(){
if (m_connection.link() == false) {
JUS_ERROR("can not connect to the socket...");
}
}
void jus::TcpString::disconnect(){
if (m_connection.unlink() == false) {
JUS_ERROR("can not disconnect to the socket...");
}
}
int32_t jus::TcpString::write(const std::string& _data) {
if (_data.size() == 0) {
return 0;
}
uint32_t size = _data.size();
m_connection.write(&size, 4);
return m_connection.write(_data.c_str(), _data.size());
}
std::string jus::TcpString::read() {
std::string out;
uint32_t size = 0;
int32_t len = m_connection.read(&size, 4);
if (len != 4) {
JUS_ERROR("Protocol error occured ...");
} else {
out.resize(size);
len = m_connection.read(&out[0], size);
if (len != 4) {
JUS_ERROR("Protocol error occured .2.");
}
}
return out;
}
void jus::TcpString::onPropertyChangeIp() {
m_connection.setHostNane(*propertyIp);
}
void jus::TcpString::onPropertyChangePort() {
m_connection.setPort(*propertyPort);
}
void jus::TcpString::onPropertyChangeServer() {
m_connection.setServer(*propertyServer);
}

34
jus/TcpString.h Normal file
View File

@ -0,0 +1,34 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#pragma once
#include <eproperty/Value.h>
#include <enet/Tcp.h>
#include <thread>
#include <memory>
namespace jus {
class TcpString : public eproperty::Interface {
private:
enet::Tcp m_connection;
std::unique_ptr<std::thread> m_receiveThread;
public:
eproperty::Value<std::string> propertyIp;
eproperty::Value<uint16_t> propertyPort;
eproperty::Value<bool> propertyServer;
public:
TcpString();
virtual ~TcpString();
void connect();
void disconnect();
int32_t write(const std::string& _data);
std::string read();
private:
void onPropertyChangeIp();
void onPropertyChangePort();
void onPropertyChangeServer();
};
}

12
jus/debug.cpp Normal file
View File

@ -0,0 +1,12 @@
/** @file
* @author Edouard DUPIN
* @copyright 2014, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <jus/debug.h>
int32_t jus::getLogId() {
static int32_t g_val = elog::registerInstance("jus");
return g_val;
}

39
jus/debug.h Normal file
View File

@ -0,0 +1,39 @@
/** @file
* @author Edouard DUPIN
* @copyright 2014, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#pragma once
#include <elog/log.h>
namespace jus {
int32_t getLogId();
};
#define JUS_BASE(info,data) ELOG_BASE(jus::getLogId(),info,data)
#define JUS_CRITICAL(data) JUS_BASE(1, data)
#define JUS_ERROR(data) JUS_BASE(2, data)
#define JUS_WARNING(data) JUS_BASE(3, data)
#ifdef DEBUG
#define JUS_INFO(data) JUS_BASE(4, data)
#define JUS_DEBUG(data) JUS_BASE(5, data)
#define JUS_VERBOSE(data) JUS_BASE(6, data)
#define JUS_TODO(data) JUS_BASE(4, "TODO : " << data)
#else
#define JUS_INFO(data) do { } while(false)
#define JUS_DEBUG(data) do { } while(false)
#define JUS_VERBOSE(data) do { } while(false)
#define JUS_TODO(data) do { } while(false)
#endif
#define JUS_ASSERT(cond,data) \
do { \
if (!(cond)) { \
JUS_CRITICAL(data); \
assert(!#cond); \
} \
} while (0)

1
lutinParseSubFolders.txt Normal file
View File

@ -0,0 +1 @@
tools/gateway

58
lutin_jus.py Normal file
View File

@ -0,0 +1,58 @@
#!/usr/bin/python
import lutin.module as module
import lutin.tools as tools
def get_type():
return "LIBRARY"
def get_desc():
return "Json micro-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 get_version():
return [0,1,"dev"]
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_src_file([
'jus/debug.cpp'
])
my_module.add_path(tools.get_current_path(__file__))
my_module.add_src_file([
'jus/Client.cpp',
'jus/GateWay.cpp',
'jus/GateWayService.cpp',
'jus/GateWayClient.cpp',
'jus/Service.cpp',
'jus/TcpString.cpp',
])
my_module.add_header_file([
'jus/debug.h',
'jus/Client.h',
'jus/GateWay.h',
'jus/GateWayService.h',
'jus/GateWayClient.h',
'jus/Service.h',
'jus/TcpString.h',
])
return my_module

View 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-gateway");
return g_val;
}

View 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)

View File

@ -0,0 +1,55 @@
/** @file
* @author Edouard DUPIN
* @copyright 2014, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <appl/debug.h>
#include <jus/GateWay.h>
#include <etk/etk.h>
#include <unistd.h>
#include <etk/stdTools.h>
int main(int _argc, const char *_argv[]) {
etk::init(_argc, _argv);
jus::GateWay basicGateway;
for (int32_t iii=0; iii<_argc ; ++iii) {
std::string data = _argv[iii];
if (etk::start_with(data, "--client-ip=") == true) {
basicGateway.propertyClientIp.set(std::string(&data[12]));
} else if (etk::start_with(data, "--client-port=") == true) {
basicGateway.propertyClientPort.set(etk::string_to_uint16_t(std::string(&data[14])));
} else if (etk::start_with(data, "--client-max=") == true) {
basicGateway.propertyClientMax.set(etk::string_to_uint16_t(std::string(&data[13])));
} else if (etk::start_with(data, "--service-ip=") == true) {
basicGateway.propertyServiceIp.set(std::string(&data[13]));
} else if (etk::start_with(data, "--service-port=") == true) {
basicGateway.propertyServicePort.set(etk::string_to_uint16_t(std::string(&data[15])));
} else if (etk::start_with(data, "--service-max=") == true) {
basicGateway.propertyServiceMax.set(etk::string_to_uint16_t(std::string(&data[14])));
} else if ( data == "-h"
|| data == "--help") {
APPL_PRINT(etk::getApplicationName() << " - help : ");
APPL_PRINT(" " << _argv[0] << " [options]");
APPL_PRINT(" --client-ip=XXX Client connection IP (default: 1.7.0.0.1)");
APPL_PRINT(" --client-port=XXX Client connection PORT (default: 1983)");
APPL_PRINT(" --client-max=XXX Clainet Maximum parallele connection (default: 80)");
APPL_PRINT(" --service-ip=XXX Service connection IP (default: 1.7.0.0.1)");
APPL_PRINT(" --service-port=XXX Service connection PORT (default: 1984)");
APPL_PRINT(" --service-max=XXX Service Maximum IO (default: 15)");
return -1;
}
}
APPL_INFO("==================================");
APPL_INFO("== JUS gateway start ==");
APPL_INFO("==================================");
basicGateway.start();
while (true) {
usleep(200000);
}
APPL_INFO("==================================");
APPL_INFO("== JUS gateway stop ==");
APPL_INFO("==================================");
return 0;
}

View 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 generic gateway"
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