Remote Call Framework 3.0
ServerObjectService.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2018, 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: 3.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_SERVEROBJECTSERVICE_HPP
20 #define INCLUDE_RCF_SERVEROBJECTSERVICE_HPP
21 
22 #include <RCF/Any.hpp>
23 #include <RCF/Export.hpp>
24 #include <RCF/PeriodicTimer.hpp>
25 #include <RCF/Service.hpp>
26 #include <RCF/Tools.hpp>
27 
28 
29 namespace RCF {
30 
31  class ServerObjectService;
32  typedef std::shared_ptr<ServerObjectService> ServerObjectServicePtr;
33 
34  class RCF_EXPORT ServerObjectHolder
35  {
36  public:
37 
38  ServerObjectHolder();
39 
40  ServerObjectHolder(const Any & serverObject, std::uint32_t timeoutMs);
41 
42  std::uint32_t mTimeoutMs;
43  std::uint32_t mLastTouchMs;
44  int mUseCount;
45  Any mServerObject;
46  };
47 
48  class RCF_EXPORT ServerObjectService : public I_Service, Noncopyable
49  {
50  public:
51  ServerObjectService();
52 
53  private:
54  void onServerStart(RcfServer & server);
55  void onServerStop(RcfServer & server);
56  void onTimer();
57  void customDeleter(const std::string & objectKey, void * pt);
58 
59  typedef std::map<std::string, ServerObjectHolder> ServerObjectMap;
60 
61  RcfServer * mpRcfServer;
62  PeriodicTimer mPeriodicTimer;
63 
64  std::uint32_t mHarvestingIntervalS;
65  std::uint32_t mLastHarvestMs;
66 
67  Mutex mMutex;
68  ServerObjectMap mServerObjectMap;
69 
70 
71 
72  template<typename T>
73  std::shared_ptr<T> getServerObjectImpl(
74  const std::string & objectKey,
75  std::uint32_t timeoutMs,
76  bool createIfDoesntExist)
77  {
78  typedef std::shared_ptr<T> TPtr;
79 
80  Lock lock(mMutex);
81 
82  ServerObjectMap::iterator iter = mServerObjectMap.find(objectKey);
83  if (iter != mServerObjectMap.end())
84  {
85  ServerObjectHolder & holder = iter->second;
86  Any & a = holder.mServerObject;
87  TPtr & tPtr = a.get<TPtr>();
88  T * pt = tPtr.get();
89  RCF_ASSERT(pt);
90 
91  // Return shared_ptr with custom deleter.
92  holder.mLastTouchMs = getCurrentTimeMs();
93  RCF_ASSERT(holder.mUseCount >= 0);
94  ++holder.mUseCount;
95  using namespace std::placeholders;
96  TPtr ptr(pt, std::bind(&ServerObjectService::customDeleter, this, objectKey, _1));
97  return ptr;
98  }
99  else if (createIfDoesntExist)
100  {
101  T * pt = new T();
102  TPtr tPtr(pt);
103  mServerObjectMap[objectKey] = ServerObjectHolder(Any(tPtr), timeoutMs);
104  ServerObjectHolder & holder = mServerObjectMap[objectKey];
105 
106  // Return shared_ptr with custom deleter.
107  holder.mLastTouchMs = getCurrentTimeMs();
108  RCF_ASSERT(holder.mUseCount >= 0);
109  ++holder.mUseCount;
110  using namespace std::placeholders;
111  TPtr ptr(pt, std::bind(&ServerObjectService::customDeleter, this, objectKey, _1));
112  return ptr;
113  }
114  else
115  {
116  return TPtr();
117  }
118  }
119 
120  public:
121 
122  template<typename T>
123  std::shared_ptr<T> queryServerObject(
124  const std::string & objectKey)
125  {
126  return getServerObjectImpl<T>(objectKey, 0, false);
127  }
128 
129  template<typename T>
130  std::shared_ptr<T> getServerObject(
131  const std::string & objectKey,
132  std::uint32_t timeoutMs)
133  {
134  return getServerObjectImpl<T>(objectKey, timeoutMs, true);
135  }
136 
137  void deleteServerObject(const std::string & objectKey);
138  };
139 
140 } // namespace RCF
141 
142 #endif // ! INCLUDE_RCF_SERVEROBJECTSERVICE_HPP
Definition: AmiIoHandler.hpp:24