RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 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 "Factory.h"
28 
29 #include <stdexcept>
30 
31 #include <boost/thread.hpp>
32 
35 #include "inprocess/OutConnector.h"
36 
37 #ifdef RSB_WITH_SPREAD_TRANSPORT
38 #include "spread/InPushConnector.h"
39 #include "spread/InPullConnector.h"
40 #include "spread/OutConnector.h"
41 #endif
42 
43 #ifdef RSB_WITH_SOCKET_TRANSPORT
44 #include "socket/InPushConnector.h"
45 #include "socket/InPullConnector.h"
46 #include "socket/OutConnector.h"
47 #endif
48 
49 #include "transports.h"
50 
51 using namespace std;
52 
53 namespace rsb {
54 namespace transport {
55 
56 static bool registered = false;
57 static boost::mutex registrationMutex;
58 
60  boost::mutex::scoped_lock lock(registrationMutex);
61 
62  if (registered) {
63  return;
64  }
65  registered = true;
66 
67 #ifdef RSB_WITH_SPREAD_TRANSPORT
68 
69  // Verify that the version of the library that we linked against
70  // is compatible with the version of the headers we compiled
71  // against.
72  GOOGLE_PROTOBUF_VERIFY_VERSION;
73 
74 #endif
75 
76  // In-direction, push-style connectors
77  {
78  InPushFactory& factory = getInPushFactory();
79  factory.registerConnector("inprocess",
80  &inprocess::InPushConnector::create,
81  "inprocess",
82  false);
83 
84 #ifdef RSB_WITH_SPREAD_TRANSPORT
85  {
86  set<string> options;
87  options.insert("host");
88  options.insert("port");
89 
90  factory.registerConnector("spread",
91  &spread::InPushConnector::create,
92  "spread",
93  true,
94  options);
95  }
96 #endif
97 
98 #ifdef RSB_WITH_SOCKET_TRANSPORT
99  {
100  set<string> options;
101  options.insert("host");
102  options.insert("port");
103  options.insert("server");
104  options.insert("tcpnodelay");
105 
106  factory.registerConnector("socket",
107  &socket::InPushConnector::create,
108  "socket",
109  true,
110  options);
111  }
112 #endif
113 
114  }
115 
116  // In-direction, pull-style connectors
117  {
118  InPullFactory& factory = getInPullFactory();
119 
120  factory.registerConnector("inprocess",
121  &inprocess::InPullConnector::create,
122  "inprocess",
123  false);
124 
125 #ifdef RSB_WITH_SPREAD_TRANSPORT
126  {
127  set<string> options;
128  options.insert("host");
129  options.insert("port");
130 
131  factory.registerConnector("spread",
132  &spread::InPullConnector::create,
133  "spread",
134  true,
135  options);
136  }
137 #endif
138 
139 #ifdef RSB_WITH_SOCKET_TRANSPORT
140  {
141  set<string> options;
142  options.insert("host");
143  options.insert("port");
144  options.insert("server");
145  options.insert("tcpnodelay");
146 
147  factory.registerConnector("socket",
148  &socket::InPullConnector::create,
149  "socket",
150  true,
151  options);
152  }
153 #endif
154 
155  }
156 
157  // Out-direction connectors
158  {
159  OutFactory& factory = getOutFactory();
160  factory.registerConnector("inprocess",
161  &inprocess::OutConnector::create,
162  "inprocess",
163  false);
164 
165 #ifdef RSB_WITH_SPREAD_TRANSPORT
166  {
167  set<string> options;
168  options.insert("host");
169  options.insert("port");
170  options.insert("maxfragmentsize");
171 
172  factory.registerConnector("spread",
173  &spread::OutConnector::create,
174  "spread",
175  true,
176  options);
177  }
178 #endif
179 
180 #ifdef RSB_WITH_SOCKET_TRANSPORT
181  {
182  set<string> options;
183  options.insert("host");
184  options.insert("port");
185  options.insert("server");
186  options.insert("tcpnodelay");
187 
188  factory.registerConnector("socket",
189  &socket::OutConnector::create,
190  "socket",
191  true,
192  options);
193  }
194 #endif
195 
196  }
197 
198 }
199 
200 bool isRemote(const string& transportName) {
201  bool remote = false;
202  bool validResult = false;
203  try {
204  InPullFactory& factory = getInPullFactory();
206  = factory.getConnectorInfo(transportName);
207  remote = info.isRemote();
208  validResult = true;
209  } catch (const rsc::runtime::NoSuchObject&) {
210  }
211  try {
212  InPushFactory& factory = getInPushFactory();
214  = factory.getConnectorInfo(transportName);
215  if (validResult && (remote != info.isRemote())) {
216  throw std::logic_error("connectors of one transport disagree about remoteness.");
217  }
218  remote = info.isRemote();
219  validResult = true;
220  } catch (const rsc::runtime::NoSuchObject&) {
221  }
222  try {
223  OutFactory& factory = getOutFactory();
225  = factory.getConnectorInfo(transportName);
226  if (validResult && (remote != info.isRemote())) {
227  throw std::logic_error("connectors of one transport disagree about remoteness.");
228  }
229  remote = info.isRemote();
230  validResult = true;
231  } catch (const rsc::runtime::NoSuchObject&) {
232  }
233  if (!validResult) {
234  throw rsc::runtime::NoSuchObject(transportName);
235  }
236  return remote;
237 }
238 
239 }
240 }