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 
119  auto matchSignature = [sig = getSignature()](const auto& method) { return method.signature == sig; };
120 
121  while (!boost::this_thread::interruption_requested()) {
122  if (MIRA_FW.getRPCManager().getLocalServices().count(mServiceProvider)) {
123  const auto& methods = MIRA_FW.getRPCManager().getLocalService(mServiceProvider).methods;
124  if (std::any_of(methods.begin(), methods.end(), matchSignature)) {
125  return true;
126  }
127  }
128  // the service may unregister, it is not necessarily found in either local or remote services
129  else if (MIRA_FW.getRPCManager().getRemoteServices().count(mServiceProvider)) {
130  const auto& methods = MIRA_FW.getRPCManager().getRemoteService(mServiceProvider).methods;
131  if (std::any_of(methods.begin(), methods.end(), matchSignature)) {
132  return true;
133  }
134  }
135 
136  if (Time::now() > end) // handle timeout
137  break;
138 
139  MIRA_SLEEP(100)
140  }
141  return false;
142  }
143 
144  [[nodiscard]] RPCSignature getSignature() const
145  {
146  return mira::makeRPCSignature<Ret, ARGS...>(mMethod);
147  }
148 
149  template<typename Reflector>
150  void reflect(Reflector& r)
151  {
152  r.member("ServiceProvider", mServiceProvider, "Provider for the service method",
154  r.member("Method", mMethod, "Name of method to be called",
156  }
157 
158 private:
159  std::string mServiceProvider;
160  std::string mMethod;
161 };
162 
164 
165 } // namespace mira
166 
167 #endif // MIRA_FRAMEWORK_INCLUDE_FW_SERVICECALL_H_
void waitForServiceCall() const
Definition: ServiceCall.h:105
RPCSignature getSignature() const
Definition: ServiceCall.h:144
bool isValid() const
Checks if this duration is invalid.
Definition: Time.h:255
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:416
Use this class to represent time durations.
Definition: Time.h:104
void reflect(Reflector &r)
Definition: ServiceCall.h:150
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:240
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:262
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:479
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