RSB  0.17.0
transports.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project
4  *
5  * Copyright (C) 2011, 2012, 2013 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "transports.h"
28 
29 #include "Factory.h"
30 
31 #include <stdexcept>
32 
33 #include <boost/thread.hpp>
34 
37 #include "inprocess/OutConnector.h"
38 
39 #ifdef RSB_WITH_SOCKET_TRANSPORT
40 #include "socket/InPushConnector.h"
41 #include "socket/InPullConnector.h"
42 #include "socket/OutConnector.h"
43 #endif
44 
45 using namespace std;
46 
47 namespace rsb {
48 namespace transport {
49 
50 static bool registered = false;
51 static boost::mutex registrationMutex;
52 
54  boost::mutex::scoped_lock lock(registrationMutex);
55 
56  if (registered) {
57  return;
58  }
59  registered = true;
60 
61  // In-direction, push-style connectors
62  {
63  InPushFactory& factory = getInPushFactory();
64  factory.registerConnector("inprocess",
65  &inprocess::InPushConnector::create,
66  "inprocess",
67  false);
68 
69 #ifdef RSB_WITH_SOCKET_TRANSPORT
70  {
71  set<string> options;
72  options.insert("host");
73  options.insert("port");
74  options.insert("server");
75  options.insert("tcpnodelay");
76 
77  factory.registerConnector("socket",
78  &socket::InPushConnector::create,
79  "socket",
80  true,
81  options);
82  }
83 #endif
84 
85  }
86 
87  // In-direction, pull-style connectors
88  {
89  InPullFactory& factory = getInPullFactory();
90 
91  factory.registerConnector("inprocess",
92  &inprocess::InPullConnector::create,
93  "inprocess",
94  false);
95 
96 #ifdef RSB_WITH_SOCKET_TRANSPORT
97  {
98  set<string> options;
99  options.insert("host");
100  options.insert("port");
101  options.insert("server");
102  options.insert("tcpnodelay");
103 
104  factory.registerConnector("socket",
105  &socket::InPullConnector::create,
106  "socket",
107  true,
108  options);
109  }
110 #endif
111 
112  }
113 
114  // Out-direction connectors
115  {
116  OutFactory& factory = getOutFactory();
117  factory.registerConnector("inprocess",
118  &inprocess::OutConnector::create,
119  "inprocess",
120  false);
121 
122 #ifdef RSB_WITH_SOCKET_TRANSPORT
123  {
124  set<string> options;
125  options.insert("host");
126  options.insert("port");
127  options.insert("server");
128  options.insert("tcpnodelay");
129 
130  factory.registerConnector("socket",
131  &socket::OutConnector::create,
132  "socket",
133  true,
134  options);
135  }
136 #endif
137 
138  }
139 
140 }
141 
142 set<string> getAvailableTransports(unsigned int requiredDirections) {
143  struct intersection {
144  set<string> operator()(const set<string>& left,
145  const set<string>& right) const {
146  set<string> result;
147  set_intersection(left.begin(), left.end(), right.begin(), right.end(),
148  inserter(result, result.begin()));
149  return result;
150  }
151  };
152 
153  if (requiredDirections == 0) {
154  throw invalid_argument("At least one required direction has to be specified.");
155  }
156 
157  set<string> result;
158  set<string> inPullTransports;
159  {
160  set<InPullFactory::ConnectorInfo> infos = getInPullFactory().getConnectorInfos();
161  for (set<InPullFactory::ConnectorInfo>::const_iterator it
162  = infos.begin(); it != infos.end(); ++it) {
163  inPullTransports.insert(it->getName());
164  result.insert(it->getName());
165  }
166  }
167  set<string> inPushTransports;
168  {
169  set<InPushFactory::ConnectorInfo> infos = getInPushFactory().getConnectorInfos();
170  for (set<InPushFactory::ConnectorInfo>::const_iterator it
171  = infos.begin(); it != infos.end(); ++it) {
172  inPushTransports.insert(it->getName());
173  result.insert(it->getName());
174  }
175  }
176  set<string> outTransports;
177  {
178  set<OutFactory::ConnectorInfo> infos = getOutFactory().getConnectorInfos();
179  for (set<OutFactory::ConnectorInfo>::const_iterator it
180  = infos.begin(); it != infos.end(); ++it) {
181  outTransports.insert(it->getName());
182  result.insert(it->getName());
183  }
184  }
185 
186  if (requiredDirections & DIRECTION_IN_PULL) {
187  result = intersection()(result, inPullTransports);
188  }
189  if (requiredDirections & DIRECTION_IN_PUSH) {
190  result = intersection()(result, inPushTransports);
191  }
192  if (requiredDirections & DIRECTION_OUT) {
193  result = intersection()(result, outTransports);
194  }
195  return result;
196 }
197 
198 
199 bool isAvailable(const string& transportName,
200  unsigned int requiredDirections) {
201  if (requiredDirections == 0) {
202  throw invalid_argument("At least one required direction has to be specified.");
203  }
204 
205  if (requiredDirections & DIRECTION_IN_PULL) {
206  try {
207  getInPullFactory().getConnectorInfo(transportName);
208  } catch (const rsc::runtime::NoSuchObject&) {
209  return false;
210  }
211  }
212  if (requiredDirections & DIRECTION_IN_PUSH) {
213  try {
214  getInPushFactory().getConnectorInfo(transportName);
215  } catch (const rsc::runtime::NoSuchObject&) {
216  return false;
217  }
218  }
219  if (requiredDirections & DIRECTION_OUT) {
220  try {
221  getOutFactory().getConnectorInfo(transportName);
222  } catch (const rsc::runtime::NoSuchObject&) {
223  return false;
224  }
225  }
226  return true;
227 }
228 
229 bool isRemote(const string& transportName) {
230  bool remote = false;
231  bool validResult = false;
232  try {
233  InPullFactory& factory = getInPullFactory();
235  = factory.getConnectorInfo(transportName);
236  remote = info.isRemote();
237  validResult = true;
238  } catch (const rsc::runtime::NoSuchObject&) {
239  }
240  try {
241  InPushFactory& factory = getInPushFactory();
243  = factory.getConnectorInfo(transportName);
244  if (validResult && (remote != info.isRemote())) {
245  throw std::logic_error("connectors of one transport disagree about remoteness.");
246  }
247  remote = info.isRemote();
248  validResult = true;
249  } catch (const rsc::runtime::NoSuchObject&) {
250  }
251  try {
252  OutFactory& factory = getOutFactory();
254  = factory.getConnectorInfo(transportName);
255  if (validResult && (remote != info.isRemote())) {
256  throw std::logic_error("connectors of one transport disagree about remoteness.");
257  }
258  remote = info.isRemote();
259  validResult = true;
260  } catch (const rsc::runtime::NoSuchObject&) {
261  }
262  if (!validResult) {
263  throw rsc::runtime::NoSuchObject(transportName);
264  }
265  return remote;
266 }
267 
268 }
269 }
Instances of this class describe capabilities and properties of connector implementations.
Definition: Factory.h:82
bool isRemote(const string &transportName)
Returns true if transportName names a remote transport.
Definition: transports.cpp:229
void registerConnector(const std::string &name, const CreateFunction &constructor, const std::set< std::string > &schemas=std::set< std::string >(), bool remote=true, const std::set< std::string > &options=std::set< std::string >())
For the connector implementation named name, register the construct function constructor, supported schemas schemas and recognized configuration options options.
Definition: Factory.h:219
STL namespace.
ConnectorInfo getConnectorInfo(const std::string &name) const
Return information regarding the connector implementation named name.
Definition: Factory.h:189
std::set< ConnectorInfo > getConnectorInfos() const
Definition: Factory.h:197
static boost::mutex registrationMutex
Definition: transports.cpp:51
set< string > getAvailableTransports(unsigned int requiredDirections)
Returns the names of all available transports which support requiredDirections.
Definition: transports.cpp:142
void registerDefaultTransports()
Definition: transports.cpp:53
bool isAvailable(const string &transportName, unsigned int requiredDirections)
Returns true if transportName names a transport which is available and supports requiredDirections ...
Definition: transports.cpp:199
InPullFactory & getInPullFactory()
Definition: Factory.cpp:32
bool isRemote() const
Return "remoteness" of the implementation.
Definition: Factory.h:130
InPushFactory & getInPushFactory()
Definition: Factory.cpp:37
OutFactory & getOutFactory()
Definition: Factory.cpp:42
Objects of this class are specialized factories that construct Connector objects and provide introspe...
Definition: Factory.h:52
static bool registered
Definition: transports.cpp:50