commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Service.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_SERVICE_INCLUDED
6 #define CMR_SERVICE_INCLUDED
7 
8 #include "Commore.h"
9 #include "Interface.h"
10 #include "Listener.h"
11 
12 //
13 // TODO : session
14 //
15 
16 namespace commore
17 {
18  class Tuple;
19  class Service;
20  typedef AutoRef<Service> PService;
21  class Interface;
26 
27 
34  {
35  public:
36  struct Impl;
37  private:
38  Impl* impl_;
39  PServiceManager(Impl* impl);
40  public:
42  ~PServiceManager();
44  PServiceManager& operator = (const PServiceManager& p);
49  Impl* get_impl() const { return impl_; }
50  Impl* get() const { return impl_; }
54  long publish(const PService& service);
58  long unpublish(const PService& service);
62  bool add_listener(const AChar* spec);
66  bool add_listener(const PListener& listener);
71  bool remove_listener(const AChar* spec);
76  bool remove_listener(const PListener& listener);
77 
83  ListAString& get_addresses(ListAString& addresses) const;
84 
85 
89  PService getMyService(const char* className, const char* name);
90 
96  static long get_managers_of(const PService& service, List<PServiceManager>& managers);
97 
101  static PService getService(const char* className, const char* name);
102 
106  PServiceManager* operator ->() { return this; }
107  const PServiceManager* operator ->() const { return this; }
108  };
109 
122  class CMREXD Service : public Interface
123  {
124  friend class PServiceManager;
125  friend class Interface;
126 
127  public:
134  {
135  public:
140  CallMethod(const AChar* interface_name, const AChar* method_name);
141  virtual ~CallMethod();
142 
143  public:
147  virtual bool is_oneway() { return false; }
154  virtual long call(PInterface inter, const Tuple& in_arg, Tuple& out_arg) = 0;
155 
156  public:
161  static CallMethod* get(const AChar* interface_name, const AChar* method_name);
162 
163  private:
164  struct Impl;
165  Impl* impl_;
166 
167  };
168 
169 
170  public:
174  ListAString& get_addresses(ListAString& addresses) const;
175 
182  AString get_address() const;
183 
187  PService get_service() { return this; }
191  const AString& get_name() const;
195  void set_name(const AChar* name);
199  const AString& get_class_name() const;
203  virtual long connect(const AChar* user_name, const AChar* password);
204 
209  AString get_session_id() const;
213  void set_session_id(const AChar* session_id);
214 
219  Tuple* get_session_context_ptr(bool createIfNeeded = false);
225  virtual bool get_session_context(Tuple& t);
229  virtual void set_session_context(const Tuple& sessionContext);
230 
235  virtual bool get_self_session_context(Tuple& t) const;
236  virtual void set_self_session_context();
237 
241  long publish(PServiceManager& manager);
242 
246  long unpublish(PServiceManager& manager);
247 
251  long unpublish();
252 
258  long get_managers(List<PServiceManager>& managers) const;
259 
263  virtual long call(const Tuple& inArgs, Tuple& outArgs);
267  virtual long call(const AChar* interface_name, const Tuple& inArgs, Tuple& outArgs);
271  virtual long call(const AChar* interface_name, const Tuple& inArgs, Tuple& outArgs, const Tuple* param);
276  long call(Tuple& sessionContext, const AChar* interface_name, const Tuple& inArgs, Tuple& outArgs);
277 
285  long get_interface(const AChar* name, PInterface& inter);
286 
291  bool add_interface(const PInterface& inter);
292 
293  public:
298  static AutoRefBase create(const AChar*);
302  static AutoRefBase create(const AChar*, ServiceFactory& factory);
310  static AutoRefBase bind(const AChar* spec);
311 
312  protected:
316  Service(const AChar* class_name);
322  Service(const AChar* class_name, const AChar* name, const AChar* address);
327  Service(const AChar* class_name, const Tuple& init);
328  ~Service();
329  private:
331  Service(const Service& s) : Interface(s) {}
332  PInterface find_interface(const AChar* name);
333  };
334 
338  class CMREXD ProxyService : public Service
339  {
340  public:
341  ProxyService(const AChar* class_name, const AChar* name, const AChar* args);
342  ~ProxyService();
343  bool is_proxy() const { return true; }
344 
345  virtual long call(const Tuple& inArgs, Tuple& outArgs);
346  virtual long call(const AChar* interface_name, const Tuple& inArgs, Tuple& outArgs);
347  virtual long call(const AChar* interface_name, const Tuple& inArgs, Tuple& outArgs, const Tuple* param);
348 
352  AString get_address() const;
353 
357  bool check_connection();
358  };
360 
361 
366  {
367  public:
371  ServiceFactory(const AChar* class_name, bool noregister = false);
375  ~ServiceFactory();
376 
377  public:
378  const AString& get_class_name() const { return class_name_; }
385  virtual PService create(const AChar* class_name, const AChar* name, const AChar* args) = 0;
386 
387  public:
391  static int get(const AChar* class_name, List<ServiceFactory*>& factoryList);
392 
393  private:
394  AString class_name_;
395  bool noregister_;
396 
397  };
398 
405  template <class T> class DefaultServiceFactory : public ServiceFactory
406  {
407  public:
408  DefaultServiceFactory(bool noregister = false)
409  : ServiceFactory(T::get_factory_class_name(), noregister)
410  {
411  }
412  PService create(const AChar* class_name, const AChar* name, const AChar* args)
413  {
414  PService service = new T();
415  service->set_name(name);
416  return service;
417  }
418  };
419 };
420 
421 #endif
422 
423 
424 
425 
426 
427 
428 
429 
430 
431 
const AString & get_class_name() const
Definition: Service.h:378
Definition: Interface.h:117
Definition: Tuple.h:29
Definition: AString.h:39
AutoRef< Interface > PInterface
Definition: Interface.h:14
Definition: Service.h:133
A commore::Service is an object that mimics an object in an object oriented system : it has a class n...
Definition: Service.h:122
Definition: Interface.h:31
AutoRef< ProxyService > PProxyService
Definition: Service.h:359
bool is_proxy() const
Definition: Service.h:343
Definition: Service.h:405
virtual bool is_oneway()
Definition: Service.h:147
AutoRef< Service > PService
Definition: Interface.h:18
Definition: AutoRefBase.h:18
char AChar
Definition: Type.h:65
ServiceFactory * PServiceFactory
Definition: Service.h:22
#define CMREXD
Definition: Compiler.h:22
Impl * get_impl() const
Definition: Service.h:49
Definition: ServiceManager.h:26
Definition: Service.h:33
Definition: Service.h:338
InterfaceFactory * PInterfaceFactory
Definition: Service.h:24
PService create(const AChar *class_name, const AChar *name, const AChar *args)
Definition: Service.h:412
Definition: Service.h:365
AutoRef is a smart pointer on RefObject objects. AutoRef is templatized with the type of the pointed ...
Definition: AutoRef.h:86
DefaultServiceFactory(bool noregister=false)
Definition: Service.h:408
PService get_service()
Definition: Service.h:187
Definition: AutoRef.h:270
CMREXD int init()
Definition: Commore.cpp:17