RCFProto
 All Classes Functions Typedefs
ObjectFactoryService.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_OBJECTFACTORYSERVICE_HPP
20 #define INCLUDE_RCF_OBJECTFACTORYSERVICE_HPP
21 
22 #include <RCF/Config.hpp>
23 
24 #if RCF_FEATURE_LEGACY==0
25 #error This header is only supported in builds with RCF_FEATURE_LEGACY=1.
26 #endif
27 
28 #include <map>
29 #include <string>
30 #include <vector>
31 
32 #include <boost/cstdint.hpp>
33 #include <boost/noncopyable.hpp>
34 #include <boost/shared_ptr.hpp>
35 
36 #include <RCF/Export.hpp>
37 #include <RCF/GetInterfaceName.hpp>
38 #include <RCF/RcfServer.hpp>
39 #include <RCF/Service.hpp>
40 #include <RCF/StubFactory.hpp>
41 #include <RCF/ThreadLibrary.hpp>
42 #include <RCF/Token.hpp>
43 #include <RCF/TypeTraits.hpp>
44 
45 namespace RCF {
46 
47  class RcfServer;
48  class StubEntry;
49  class StubFactory;
50  class I_RcfClient;
51  class Token;
52  class TokenMapped;
53 
54  typedef boost::shared_ptr<StubEntry> StubEntryPtr;
55  typedef boost::shared_ptr<StubFactory> StubFactoryPtr;
56  typedef boost::shared_ptr<TokenMapped> TokenMappedPtr;
57 
58  class RCF_EXPORT StubFactoryRegistry
59  {
60  public:
61 
62  StubFactoryRegistry();
63 
64  // Binds an object factory to a name.
65 
66  template<typename I1, typename ImplementationT>
67  bool bind(const std::string &name_ = "")
68  {
69  return bind( (I1 *) NULL, (ImplementationT **) NULL, name_);
70  }
71 
72  template<typename I1, typename ImplementationT>
73  bool bind(I1 *, ImplementationT **, const std::string &name_ = "")
74  {
75  const std::string &name = (name_ == "") ?
76  getInterfaceName((I1 *) NULL) :
77  name_;
78 
79  StubFactoryPtr stubFactoryPtr(
80  new RCF::StubFactory_1<ImplementationT, I1>());
81 
82  std::string desc;
83  return insertStubFactory(name, desc, stubFactoryPtr);
84  }
85 
86  protected:
87 
88  bool insertStubFactory(
89  const std::string &objectName,
90  const std::string &desc,
91  StubFactoryPtr stubFactoryPtr);
92 
93  bool removeStubFactory(
94  const std::string &objectName);
95 
96  StubFactoryPtr getStubFactory(
97  const std::string &objectName);
98 
99  private:
100 
101  typedef std::map<
102  std::string,
103  StubFactoryPtr> StubFactoryMap;
104 
105  ReadWriteMutex mStubFactoryMapMutex;
106  StubFactoryMap mStubFactoryMap;
107 
108  };
109 
110  class RCF_EXPORT ObjectFactoryService :
111  public I_Service,
112  public StubFactoryRegistry,
113  boost::noncopyable
114  {
115  public:
116 
117  ObjectFactoryService();
118 
119  // Remotely accessible functions.
120  boost::int32_t CreateObject(const std::string &objectName, Token &token);
121  boost::int32_t DeleteObject(const Token &token);
122 
123  boost::int32_t addObject(TokenMappedPtr tokenMappedPtr, Token &token);
124 
125  boost::int32_t CreateSessionObject(const std::string &objectName);
126  boost::int32_t DeleteSessionObject();
127 
128  StubEntryPtr getStubEntryPtr(const Token &token);
129  TokenMappedPtr getTokenMappedPtr(const Token & token);
130 
131  private:
132  void onServiceAdded(RcfServer &server);
133  void onServiceRemoved(RcfServer &server);
134  void onServerStart(RcfServer &);
135  void onServerStop(RcfServer &);
136  void stopCleanup();
137  void cycleCleanup(int timeoutMs);
138  void cleanupStubMap(unsigned int timeoutS);
139 
140  typedef std::map<
141  Token,
142  std::pair<
143  MutexPtr,
144  TokenMappedPtr> > StubMap;
145 
146  // TokenFactory is internally synchronized
147  typedef boost::shared_ptr<TokenFactory> TokenFactoryPtr;
148  TokenFactoryPtr mTokenFactory;
149 
150  unsigned int mClientStubTimeoutS;
151  Mutex mCleanupThresholdMutex;
152  Condition mCleanupThresholdCondition;
153  unsigned int mCleanupIntervalS;
154  float mCleanupThreshold;
155 
156  ReadWriteMutex mStubMapMutex;
157  StubMap mStubMap;
158 
159  bool mLazyStarted;
160  };
161 
162  typedef boost::shared_ptr<ObjectFactoryService>
163  ObjectFactoryServicePtr;
164 
165 } // namespace RCF
166 
167 #endif // ! INCLUDE_RCF_OBJECTFACTORYSERVICE_HPP