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