Remote Call Framework 3.2
ServerObjectService.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2020, 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.2
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  TPtr ptr(pt, std::bind(&ServerObjectService::customDeleter, this, objectKey, std::placeholders::_1));
96  return ptr;
97  }
98  else if (createIfDoesntExist)
99  {
100  T * pt = new T();
101  TPtr tPtr(pt);
102  mServerObjectMap[objectKey] = ServerObjectHolder(Any(tPtr), timeoutMs);
103  ServerObjectHolder & holder = mServerObjectMap[objectKey];
104 
105  // Return shared_ptr with custom deleter.
106  holder.mLastTouchMs = getCurrentTimeMs();
107  RCF_ASSERT(holder.mUseCount >= 0);
108  ++holder.mUseCount;
109  TPtr ptr(pt, std::bind(&ServerObjectService::customDeleter, this, objectKey, std::placeholders::_1));
110  return ptr;
111  }
112  else
113  {
114  return TPtr();
115  }
116  }
117 
118  public:
119 
120  template<typename T>
121  std::shared_ptr<T> queryServerObject(
122  const std::string & objectKey)
123  {
124  return getServerObjectImpl<T>(objectKey, 0, false);
125  }
126 
127  template<typename T>
128  std::shared_ptr<T> getServerObject(
129  const std::string & objectKey,
130  std::uint32_t timeoutMs)
131  {
132  return getServerObjectImpl<T>(objectKey, timeoutMs, true);
133  }
134 
135  void deleteServerObject(const std::string & objectKey);
136  };
137 
138 } // namespace RCF
139 
140 #endif // ! INCLUDE_RCF_SERVEROBJECTSERVICE_HPP
Definition: AmiIoHandler.hpp:24