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 <map>
23 #include <string>
24 #include <vector>
25 
26 #include <boost/cstdint.hpp>
27 #include <boost/noncopyable.hpp>
28 #include <boost/shared_ptr.hpp>
29 
30 #include <RCF/Export.hpp>
31 #include <RCF/GetInterfaceName.hpp>
32 #include <RCF/RcfServer.hpp>
33 #include <RCF/Service.hpp>
34 #include <RCF/StubFactory.hpp>
35 #include <RCF/ThreadLibrary.hpp>
36 #include <RCF/Token.hpp>
37 #include <RCF/TypeTraits.hpp>
38 
39 namespace RCF {
40 
41  class RcfServer;
42  class StubEntry;
43  class StubFactory;
44  class I_RcfClient;
45  class Token;
46  class TokenMapped;
47 
48  typedef boost::shared_ptr<StubEntry> StubEntryPtr;
49  typedef boost::shared_ptr<StubFactory> StubFactoryPtr;
50  typedef boost::shared_ptr<TokenMapped> TokenMappedPtr;
51 
52  class RCF_EXPORT StubFactoryRegistry
53  {
54  public:
55 
56  StubFactoryRegistry();
57 
58  // Binds an object factory to a name.
59 
60  template<typename I1, typename ImplementationT>
61  bool bind(const std::string &name_ = "")
62  {
63  return bind( (I1 *) NULL, (ImplementationT **) NULL, name_);
64  }
65 
66  template<typename I1, typename I2, typename ImplementationT>
67  bool bind(const std::string &name_ = "")
68  {
69  return bind( (I1 *) NULL, (I2 *) NULL, (ImplementationT **) NULL, name_);
70  }
71 
72  template<typename I1, typename I2, typename I3, typename ImplementationT>
73  bool bind(const std::string &name_ = "")
74  {
75  return bind( (I1 *) NULL, (I2 *) NULL, (I3 *) NULL, (ImplementationT **) NULL, name_);
76  }
77 
78  template<typename I1, typename I2, typename I3, typename I4, typename ImplementationT>
79  bool bind(const std::string &name_ = "")
80  {
81  return bind( (I1 *) NULL, (I2 *) NULL, (I3 *) NULL, (I4 *) NULL, (ImplementationT **) NULL, name_);
82  }
83 
84  template<typename I1, typename ImplementationT>
85  bool bind(I1 *, ImplementationT **, const std::string &name_ = "")
86  {
87  const std::string &name = (name_ == "") ?
88  getInterfaceName((I1 *) NULL) :
89  name_;
90 
91  StubFactoryPtr stubFactoryPtr(
92  new RCF::StubFactory_1<ImplementationT, I1>());
93 
94  std::string desc;
95  return insertStubFactory(name, desc, stubFactoryPtr);
96  }
97 
98  template<typename I1, typename I2, typename ImplementationT>
99  bool bind(I1 *, I2 *, ImplementationT **, const std::string &name_ = "")
100  {
101  const std::string &name = (name_ == "") ?
102  getInterfaceName((I1 *) NULL) :
103  name_;
104 
105  StubFactoryPtr stubFactoryPtr(
106  new RCF::StubFactory_2<ImplementationT, I1, I2>());
107 
108  std::string desc;
109  return insertStubFactory(name, desc, stubFactoryPtr);
110  }
111 
112  template<typename I1, typename I2, typename I3, typename ImplementationT>
113  bool bind(I1 *, I2 *, I3 *, ImplementationT **, const std::string &name_ = "")
114  {
115  const std::string &name = (name_ == "") ?
116  getInterfaceName((I1 *) NULL) :
117  name_;
118 
119  StubFactoryPtr stubFactoryPtr(
120  new RCF::StubFactory_3<ImplementationT, I1, I2, I3>());
121 
122  std::string desc;
123  return insertStubFactory(name, desc, stubFactoryPtr);
124  }
125 
126  template<typename I1, typename I2, typename I3, typename I4, typename ImplementationT>
127  bool bind(I1 *, I2 *, I3 *, I4 *, ImplementationT **, const std::string &name_ = "")
128  {
129  const std::string &name = (name_ == "") ?
130  getInterfaceName((I1 *) NULL) :
131  name_;
132 
133  StubFactoryPtr stubFactoryPtr(
134  new RCF::StubFactory_4<ImplementationT, I1, I2, I3, I4>());
135 
136  std::string desc;
137  return insertStubFactory(name, desc, stubFactoryPtr);
138  }
139 
140  protected:
141 
142  bool insertStubFactory(
143  const std::string &objectName,
144  const std::string &desc,
145  StubFactoryPtr stubFactoryPtr);
146 
147  bool removeStubFactory(
148  const std::string &objectName);
149 
150  StubFactoryPtr getStubFactory(
151  const std::string &objectName);
152 
153  private:
154 
155  typedef std::map<
156  std::string,
157  StubFactoryPtr> StubFactoryMap;
158 
159  ReadWriteMutex mStubFactoryMapMutex;
160  StubFactoryMap mStubFactoryMap;
161 
162  };
163 
164  class RCF_EXPORT ObjectFactoryService :
165  public I_Service,
166  public StubFactoryRegistry,
167  boost::noncopyable
168  {
169  public:
170 
171  ObjectFactoryService();
172 
173  // Remotely accessible functions.
174  boost::int32_t CreateObject(const std::string &objectName, Token &token);
175  boost::int32_t DeleteObject(const Token &token);
176 
177  boost::int32_t addObject(TokenMappedPtr tokenMappedPtr, Token &token);
178 
179  boost::int32_t CreateSessionObject(const std::string &objectName);
180  boost::int32_t DeleteSessionObject();
181 
182  StubEntryPtr getStubEntryPtr(const Token &token);
183  TokenMappedPtr getTokenMappedPtr(const Token & token);
184 
185  private:
186  void onServiceAdded(RcfServer &server);
187  void onServiceRemoved(RcfServer &server);
188  void onServerStart(RcfServer &);
189  void onServerStop(RcfServer &);
190  void stopCleanup();
191  void cycleCleanup(int timeoutMs);
192  void cleanupStubMap(unsigned int timeoutS);
193 
194  typedef std::map<
195  Token,
196  std::pair<
197  MutexPtr,
198  TokenMappedPtr> > StubMap;
199 
200  // TokenFactory is internally synchronized
201  typedef boost::shared_ptr<TokenFactory> TokenFactoryPtr;
202  TokenFactoryPtr mTokenFactory;
203 
204  unsigned int mClientStubTimeoutS;
205  Mutex mCleanupThresholdMutex;
206  Condition mCleanupThresholdCondition;
207  unsigned int mCleanupIntervalS;
208  float mCleanupThreshold;
209 
210  ReadWriteMutex mStubMapMutex;
211  StubMap mStubMap;
212 
213  bool mLazyStarted;
214  };
215 
216  typedef boost::shared_ptr<ObjectFactoryService>
217  ObjectFactoryServicePtr;
218 
219 } // namespace RCF
220 
221 #endif // ! INCLUDE_RCF_OBJECTFACTORYSERVICE_HPP