MIRA
ServiceCall.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef MIRA_FRAMEWORK_INCLUDE_FW_SERVICECALL_H_
48 #define MIRA_FRAMEWORK_INCLUDE_FW_SERVICECALL_H_
49 
50 #include <fw/Framework.h>
51 
52 namespace mira {
53 
55 
56 template<typename>
57 class ServiceCall;
58 
65 template<class Ret, class... ARGS>
66 class ServiceCall<Ret(ARGS...)>
67 {
68 public:
78  ServiceCall(std::string serviceProvider, std::string method)
79  : mServiceProvider(std::move(serviceProvider)), mMethod(std::move(method))
80  {}
81 
87  ServiceCall() = default;
88 
95  [[nodiscard]] Ret operator()(const ARGS&... args) const
96  {
97  return call(args...).get();
98  }
99 
100  [[nodiscard]] RPCFuture<Ret> call(const ARGS&... args) const
101  {
102  return MIRA_FW.getRPCManager().call<Ret>(mServiceProvider, mMethod, args...);
103  }
104 
105  void waitForServiceCall() const
106  {
107  const auto hasServiceCall = waitForServiceCall(Duration::infinity());
108  }
109 
110  [[nodiscard]] bool waitForServiceCall(Duration timeout) const
111  {
112  const Time end =
113  (!timeout.isValid() || timeout.isInfinity()) ? Time::eternity() : Time::now() + timeout;
114 
115  if (!MIRA_FW.getRPCManager().waitForService(mServiceProvider, timeout)) {
116  return false;
117  }
118  const auto signature = getSignature();
119 
120  while (!boost::this_thread::interruption_requested()) {
121  if (MIRA_FW.getRPCManager().getLocalServices().count(mServiceProvider)) {
122  const auto& methods = MIRA_FW.getRPCManager().getLocalService(mServiceProvider).methods;
123  auto it = std::find_if(methods.begin(), methods.end(),
124  [&](const auto& method) { return method.signature == signature; });
125  if (it != methods.end()) {
126  return true;
127  }
128  }
129  else {
130  const auto& methods = MIRA_FW.getRPCManager().getRemoteService(mServiceProvider).methods;
131  auto it = std::find_if(methods.begin(), methods.end(),
132  [&](const auto& method) { return method.signature == signature; });
133  if (it != methods.end()) {
134  return true;
135  }
136  }
137 
138  if (Time::now() > end) // handle timeout
139  break;
140 
141  MIRA_SLEEP(100)
142  }
143  return false;
144  }
145 
146  [[nodiscard]] RPCSignature getSignature() const
147  {
148  return mira::makeRPCSignature<Ret, ARGS...>(mMethod);
149  }
150 
151  template<typename Reflector>
152  void reflect(Reflector& r)
153  {
154  r.member("ServiceProvider", mServiceProvider, "Provider for the service method",
156  r.member("Method", mMethod, "Name of method to be called",
158  }
159 
160 private:
161  std::string mServiceProvider;
162  std::string mMethod;
163 };
164 
166 
167 } // namespace mira
168 
169 #endif // MIRA_FRAMEWORK_INCLUDE_FW_SERVICECALL_H_
void waitForServiceCall() const
Definition: ServiceCall.h:105
RPCSignature getSignature() const
Definition: ServiceCall.h:146
bool isValid() const
Checks if this duration is invalid.
Definition: Time.h:260
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
STL namespace.
RPCSignature makeRPCSignature(std::string name)
Definition: RPCSignature.h:145
#define MIRA_FW
Macro for accessing the framework instance.
Definition: Framework.h:73
An RPCFuture is a proxy for the result of an asynchronous RPC call.
Definition: RPCFuture.h:173
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:421
Use this class to represent time durations.
Definition: Time.h:104
void reflect(Reflector &r)
Definition: ServiceCall.h:152
Ret operator()(const ARGS &... args) const
Call operator to allow function like usage.
Definition: ServiceCall.h:95
static Duration infinity()
Returns a special duration time representing positive infinity.
Definition: Time.h:245
RPCFuture< Ret > call(const ARGS &... args) const
Definition: ServiceCall.h:100
ServiceCall(std::string serviceProvider, std::string method)
Construct a ServiceCall object referring to a specific service provider and method.
Definition: ServiceCall.h:78
bool isInfinity() const
Checks if this duration is infinity.
Definition: Time.h:267
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:484
bool waitForServiceCall(Duration timeout) const
Definition: ServiceCall.h:110
Stores the signature of an RPC method including the methods name and its parameter types...
Definition: RPCSignature.h:68
#define MIRA_SLEEP(ms)
Sleeps for ms milliseconds This is a thread interruption point - if interruption of the current threa...
Definition: Thread.h:95
The framework that holds all manager classes and provides startup and shutdown of all framework relat...
Definition: Authority.h:78
When this flag is used in calls to Reflector::member(), that member is also reflected as read-only pr...
Definition: ReflectControlFlags.h:103