Service.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <zeus/WebServer.hpp>
9 #include <eproperty/Value.hpp>
12 #include <zeus/debug.hpp>
14 #include <zeus/Future.hpp>
15 
19 namespace zeus {
26  public:
32  ClientProperty(const std::string& _clientName="", const std::vector<std::string>& _groups = std::vector<std::string>()) :
33  m_name(_clientName),
34  m_groups(_groups) {
35 
36  }
37  private:
38  std::string m_name;
39  public:
45  void setName(const std::string& _name) {
46  m_name = _name;
47  }
53  const std::string& getName() {
54  return m_name;
55  }
56  private:
57  std::vector<std::string> m_groups;
58  public:
64  void setGroups(std::vector<std::string> _groups) {
65  m_groups = _groups;
66  }
72  const std::vector<std::string>& getGroups() {
73  return m_groups;
74  }
75  private:
76  std::vector<std::string> m_listAthorizedFunction;
77  public:
83  void addAuthorized(const std::string& _funcName) {
84  m_listAthorizedFunction.push_back(_funcName);
85  }
91  bool isFunctionAuthorized(const std::string& _funcName) {
92  return std::find(m_listAthorizedFunction.begin(), m_listAthorizedFunction.end(), _funcName) != m_listAthorizedFunction.end();
93  }
94  };
95 }
96 namespace zeus {
103  protected:
104  std::mutex m_mutex;
105  public:
109  protected:
110  ememory::SharedPtr<zeus::WebServer> m_interfaceClient;
111  uint32_t m_id;
112  std::vector<std::string> m_newData;
113  std::vector<zeus::FutureBase> m_callMultiData;
114  public:
120  Service();
126  virtual ~Service();
132  void connect(uint32_t _numberRetry = 1);
138  void disconnect();
139  private:
145  void onClientData(ememory::SharedPtr<zeus::Buffer> _value);
146  public:
152  void pingIsAlive();
158  bool GateWayAlive();
159  private:
165  void onPropertyChangeServiceName();
171  void onPropertyChangeIp();
177  void onPropertyChangePort();
184  virtual void clientConnect(uint64_t _clientId, const std::string& _userName, const std::string& _clientName, const std::vector<std::string>& _groups) = 0;
190  virtual void clientDisconnect(uint64_t _clientId) = 0;
191  // Genenric function call:
197  void callBinary(ememory::SharedPtr<zeus::Buffer> _obj);
203  virtual void callBinary2(const std::string& _call, ememory::SharedPtr<zeus::BufferCall> _obj) = 0;
209  std::vector<std::string> getExtention();
210  public:
216  // Add Local fuction (depend on this class)
217  template<class ZEUS_RETURN_VALUE,
218  class ZEUS_CLASS_TYPE,
219  class... ZEUS_FUNC_ARGS_TYPE>
220  zeus::AbstractFunction* advertise(std::string _name,
221  ZEUS_RETURN_VALUE (ZEUS_CLASS_TYPE::*_func)(ZEUS_FUNC_ARGS_TYPE... _args)) {
222  _name = "srv." + _name;
223  for (auto &it : m_listFunction) {
224  if (it == nullptr) {
225  continue;
226  }
227  if (it->getName() == _name) {
228  ZEUS_ERROR("Advertise function already bind .. ==> can not be done...: '" << _name << "'");
229  return nullptr;
230  }
231  }
232  AbstractFunction* tmp = createAbstractFunctionClass(_name, _func);
233  if (tmp == nullptr) {
234  ZEUS_ERROR("can not create abstract function ... '" << _name << "'");
235  return nullptr;
236  }
238  ZEUS_INFO("Add function '" << _name << "' in local mode");
239  m_listFunction.push_back(tmp);
240  return tmp;
241  }
242  };
243  template<class ZEUS_TYPE_SERVICE, class ZEUS_USER_ACCESS>
244  class ServiceType : public zeus::Service {
245  private:
246  ememory::SharedPtr<ZEUS_USER_ACCESS> m_getUserInterface;
247  // no need of shared_ptr or unique_ptr (if service die all is lost and is client die, the gateway notify us...)
248  std::map<uint64_t, std::pair<ememory::SharedPtr<ClientProperty>, ememory::SharedPtr<ZEUS_TYPE_SERVICE>>> m_interface;
249  public:
255  template<class ZEUS_RETURN_VALUE,
256  class ZEUS_CLASS_TYPE,
257  class... ZEUS_FUNC_ARGS_TYPE>
258  zeus::AbstractFunction* advertise(const std::string& _name,
259  ZEUS_RETURN_VALUE (ZEUS_CLASS_TYPE::*_func)(ZEUS_FUNC_ARGS_TYPE... _args)) {
260  if (etk::start_with(_name, "srv.") == true) {
261  ZEUS_ERROR("Advertise function start with 'srv.' is not permited ==> only allow for internal service: '" << _name << "'");
262  return nullptr;
263  }
264  for (auto &it : m_listFunction) {
265  if (it == nullptr) {
266  continue;
267  }
268  if (it->getName() == _name) {
269  ZEUS_ERROR("Advertise function already bind .. ==> can not be done...: '" << _name << "'");
270  return nullptr;
271  }
272  }
274  if (tmp == nullptr) {
275  ZEUS_ERROR("can not create abstract function ... '" << _name << "'");
276  return nullptr;
277  }
279  ZEUS_INFO("Add function '" << _name << "' in object mode");
280  m_listFunction.push_back(tmp);
281  return tmp;
282  }
289  m_getUserInterface(_interface) {
290 
291  }
297  bool isFunctionAuthorized(uint64_t _clientId, const std::string& _funcName) {
298  auto it = m_interface.find(_clientId);
299  if (it == m_interface.end()) {
300  return false;
301  }
302  return it->second.first->isFunctionAuthorized(_funcName);
303  }
309  void clientConnect(uint64_t _clientId, const std::string& _userName, const std::string& _clientName, const std::vector<std::string>& _groups) {
310  std::unique_lock<std::mutex> lock(m_mutex);
311  ZEUS_DEBUG("connect: " << _clientId << " to '" << _userName << "'");
312  ZEUS_DEBUG(" client name='" << _clientName << "'");
313  ZEUS_DEBUG(" groups=" << etk::to_string(_groups));
314  ememory::SharedPtr<ClientProperty> tmpProperty = ememory::makeShared<ClientProperty>(_clientName, _groups);
316  if (m_getUserInterface == nullptr) {
317  tmpSrv = ememory::makeShared<ZEUS_TYPE_SERVICE>(nullptr, tmpProperty);
318  } else {
319  tmpSrv = ememory::makeShared<ZEUS_TYPE_SERVICE>(m_getUserInterface->getUser(_userName), tmpProperty);
320  }
321  m_interface.insert(std::make_pair(_clientId, std::make_pair(tmpProperty, tmpSrv)));
322  // enable list of function availlable:
323  for (auto &it : m_listFunction) {
324  if (it == nullptr) {
325  continue;
326  }
327  tmpProperty->addAuthorized(it->getName());
328  }
329  }
335  void clientDisconnect(uint64_t _clientId) {
336  std::unique_lock<std::mutex> lock(m_mutex);
337  ZEUS_DEBUG("disconnect: " << _clientId);
338  auto it = m_interface.find(_clientId);
339  if (it == m_interface.end()) {
340  ZEUS_WARNING("disconnect ==> Not find Client ID " << _clientId);
341  // noting to do ==> user never conected.
342  return;
343  }
344  m_interface.erase(it);
345  }
351  void clientSetName(uint64_t _clientId, const std::string& _clientName) {
352  std::unique_lock<std::mutex> lock(m_mutex);
353  auto it = m_interface.find(_clientId);
354  if (it == m_interface.end()) {
355  ZEUS_ERROR("Change the client property but client was not created ...");
356  return;
357  }
358  it->second.first->setName(_clientName);
359  }
365  void clientSetGroup(uint64_t _clientId, const std::vector<std::string>& _clientGroups) {
366  std::unique_lock<std::mutex> lock(m_mutex);
367  auto it = m_interface.find(_clientId);
368  if (it == m_interface.end()) {
369  ZEUS_ERROR("Change the client property but client was not created ...");
370  return;
371  }
372  it->second.first->setGroups(_clientGroups);
373  }
379  void callBinary2(const std::string& _call, ememory::SharedPtr<zeus::BufferCall> _obj) {
380  auto it = m_interface.find(_obj->getClientId());
381  if (it == m_interface.end()) {
382  m_interfaceClient->answerError(_obj->getTransactionId(), "CLIENT-UNKNOW", "", _obj->getClientId());
383  return;
384  }
385  for (auto &it2 : m_listFunction) {
386  if (it2 == nullptr) {
387  continue;
388  }
389  if (it2->getName() != _call) {
390  continue;
391  }
392  switch (it2->getType()) {
394  ZEUS_TYPE_SERVICE* elem = it->second.second.get();
395  it2->execute(m_interfaceClient, _obj, (void*)elem);
396  return;
397  }
399  it2->execute(m_interfaceClient, _obj, (void*)((RemoteProcessCall*)this));
400  return;
401  }
403  it2->execute(m_interfaceClient, _obj, (void*)this);
404  return;
405  }
407  it2->execute(m_interfaceClient, _obj, nullptr);
408  return;
409  }
411  ZEUS_ERROR("Can not call unknow type ...");
412  break;
413  }
414  }
415  m_interfaceClient->answerError(_obj->getTransactionId(), "FUNCTION-UNKNOW", "", _obj->getClientId());
416  return;
417  }
418  };
419 }
420 
421 
this is for service instance call
void setType(enum type _type)
Set the type of the call that must be done for this function.
ClientProperty(const std::string &_clientName="", const std::vector< std::string > &_groups=std::vector< std::string >())
Definition: Service.hpp:32
This call a service function (global function like "srv.xxx")
This is a global function.
Definition: Service.hpp:25
const std::string & getName()
Definition: Service.hpp:53
eproperty::Value< uint16_t > propertyPort
Port of the WebSocket connection.
Definition: Service.hpp:107
Interface to store a function and call it after with a zeus::Buffer.
Definition: AbstractFunction.hpp:23
void clientSetGroup(uint64_t _clientId, const std::vector< std::string > &_clientGroups)
Definition: Service.hpp:365
uint32_t getClientId() const
Get the Client identifier of the packet.
eproperty::Value< std::string > propertyNameService
Service name.
Definition: Service.hpp:108
void answerError(uint64_t _clientTransactionId, const std::string &_errorValue, const std::string &_errorComment="", uint32_t _clientId=0)
uint32_t getTransactionId() const
Get the transaction identifier of the packet.
bool isFunctionAuthorized(uint64_t _clientId, const std::string &_funcName)
Definition: Service.hpp:297
zeus::AbstractFunction * advertise(std::string _name, ZEUS_RETURN_VALUE(ZEUS_CLASS_TYPE::*_func)(ZEUS_FUNC_ARGS_TYPE... _args))
Definition: Service.hpp:220
eproperty::Value< std::string > propertyIp
Ip of WebSocket TCP connection.
Definition: Service.hpp:106
void setName(const std::string &_name)
Definition: Service.hpp:45
void setGroups(std::vector< std::string > _groups)
Definition: Service.hpp:64
Definition: Service.hpp:244
void clientConnect(uint64_t _clientId, const std::string &_userName, const std::string &_clientName, const std::vector< std::string > &_groups)
Definition: Service.hpp:309
zeus::AbstractFunction * advertise(const std::string &_name, ZEUS_RETURN_VALUE(ZEUS_CLASS_TYPE::*_func)(ZEUS_FUNC_ARGS_TYPE... _args))
Definition: Service.hpp:258
Main zeus library namespace.
Definition: AbstractFunction.hpp:15
void addAuthorized(const std::string &_funcName)
Definition: Service.hpp:83
AbstractFunction * createAbstractFunctionClass(const std::string &_name, ZEUS_RETURN(ZEUS_CLASS_TYPE::*_fffp)(ZEUS_TYPES...))
Create a function information with the function type.
Definition: AbstractFunctionTypeClass.hpp:157
const std::vector< std::string > & getGroups()
Definition: Service.hpp:72
void clientSetName(uint64_t _clientId, const std::string &_clientName)
Definition: Service.hpp:351
void clientDisconnect(uint64_t _clientId)
Definition: Service.hpp:335
Definition: Service.hpp:102
ServiceType(ememory::SharedPtr< ZEUS_USER_ACCESS > _interface)
Definition: Service.hpp:288
std::string to_string(const TYPE &_variable)
This is a local fucntion.
Does not know the type of the call.
void callBinary2(const std::string &_call, ememory::SharedPtr< zeus::BufferCall > _obj)
Definition: Service.hpp:379
bool isFunctionAuthorized(const std::string &_funcName)
Definition: Service.hpp:91
std::string getExtention(const std::string &_mineType)
Retrive the extention of a file with his mine type.
Local declaration of call local data.
Definition: RemoteProcessCall.hpp:17