-
Notifications
You must be signed in to change notification settings - Fork 19
/
pyMOOS.cpp
406 lines (300 loc) · 14.5 KB
/
pyMOOS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <exception>
#include "MOOS/libMOOS/Comms/MOOSMsg.h"
#include "MOOS/libMOOS/Comms/MOOSCommClient.h"
#include "MOOS/libMOOS/Comms/MOOSAsyncCommClient.h"
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
typedef std::vector<CMOOSMsg> MsgVector;
typedef std::vector<MOOS::ClientCommsStatus> CommsStatusVector;
namespace bp = boost::python;
struct pyMOOSException : std::exception {
pyMOOSException() {
}
;
virtual ~pyMOOSException() throw () {
}
;
pyMOOSException(const std::string & s) :
s_(s) {
}
char const* what() const throw () {
return s_.c_str();
}
std::string s_;
};
void MOOSExceptionTranslator(const pyMOOSException & e) {
// Use the Python 'C' API to set up an exception object
PyErr_SetString(PyExc_RuntimeError, e.what());
}
namespace MOOS {
/** this is a class which wraps MOOS::MOOSAsyncCommClient to provide
* and interface more suitable for python wrapping
*/
class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
private:
typedef MOOSAsyncCommClient BASE;
public:
~AsyncCommsWrapper(){
Close(true);
}
bool Run(const std::string & sServer, int Port, const std::string & sMyName) {
return BASE::Run(sServer, Port, sMyName, 0);//Comms Tick not used in Async version
}
//we can support vectors of objects by not lists so
//here we have a funtion which copies a list to vector
MsgVector FetchMailAsVector() {
MsgVector v;
MOOSMSG_LIST M;
if (Fetch(M)) {
std::copy(M.begin(), M.end(), std::back_inserter(v));
}
return v;
}
/* python strings can be binary lets make this specific*/
bool NotifyBinary(const std::string& sKey, const std::string & sData,
double dfTime) {
CMOOSMsg M(MOOS_NOTIFY, sKey, sData.size(), (void *) sData.data(),
dfTime);
return BASE::Post(M);
}
static bool on_connect_delegate(void * pParam) {
MOOS::AsyncCommsWrapper * pMe =
static_cast<MOOS::AsyncCommsWrapper*> (pParam);
return pMe->on_connect();
}
bool SetOnConnectCallback(bp::object func) {
BASE: SetOnConnectCallBack(on_connect_delegate, this);
on_connect_object_ = func;
return true;
}
bool Close(bool nice){
bool bResult = false;
Py_BEGIN_ALLOW_THREADS
//PyGILState_STATE gstate = PyGILState_Ensure();
closing_ = true;
bResult = BASE::Close(true);
//PyGILState_Release(gstate);
Py_END_ALLOW_THREADS
return bResult;
}
bool on_connect() {
bool bResult = false;
PyGILState_STATE gstate = PyGILState_Ensure();
try {
bp::object result = on_connect_object_();
bResult = bp::extract<bool>(result);
} catch (const bp::error_already_set& e) {
PyGILState_Release(gstate);
throw pyMOOSException(
"OnConnect:: caught an exception thrown in python callback");
}
PyGILState_Release(gstate);
return bResult;
}
bool SetOnMailCallback(bp::object func) {
BASE: SetOnMailCallBack(on_mail_delegate, this);
on_mail_object_ = func;
return true;
}
static bool on_mail_delegate(void * pParam) {
MOOS::AsyncCommsWrapper * pMe =
static_cast<MOOS::AsyncCommsWrapper*> (pParam);
return pMe->on_mail();
}
bool on_mail() {
bool bResult = false;
PyGILState_STATE gstate = PyGILState_Ensure();
try {
if(!closing_){
bp::object result = on_mail_object_();
bResult = bp::extract<bool>(result);
}
} catch (const bp::error_already_set& e) {
PyGILState_Release(gstate);
throw pyMOOSException(
"OnMail:: caught an exception thrown in python callback");
}
PyGILState_Release(gstate);
return bResult;
}
static bool active_queue_delegate(CMOOSMsg & M, void* pParam) {
MeAndQueue * maq = static_cast<MeAndQueue*> (pParam);
return maq->me_->OnQueue(M, maq->queue_name_);
}
bool OnQueue(CMOOSMsg & M, const std::string & sQueueName) {
std::map<std::string, MeAndQueue*>::iterator q;
{
MOOS::ScopedLock L(queue_api_lock_);
q = active_queue_details_.find(sQueueName);
if (q == active_queue_details_.end())
return false;
}
bool bResult = false;
PyGILState_STATE gstate = PyGILState_Ensure();
try {
bp::object result = q->second->func_(M);
bResult = bp::extract<bool>(result);
} catch (const bp::error_already_set& e) {
PyGILState_Release(gstate);
throw pyMOOSException(
"ActiveQueue:: caught an exception thrown in python callback");
}
PyGILState_Release(gstate);
return bResult;
}
virtual bool AddActiveQueue(const std::string & sQueueName, bp::object func) {
MOOS::ScopedLock L(queue_api_lock_);
MeAndQueue* maq = new MeAndQueue;
maq->me_ = this;
maq->queue_name_ = sQueueName;
maq->func_ = func;
std::cerr << "adding active queue OK\n";
active_queue_details_[sQueueName] = maq;
return BASE::AddActiveQueue(sQueueName, active_queue_delegate, maq);
}
private:
/** this is a structure which is used to dispatch
* active queue callbacks
*/
struct MeAndQueue {
AsyncCommsWrapper* me_;
std::string queue_name_;
bp::object func_;
};
std::map<std::string, MeAndQueue*> active_queue_details_;
CMOOSLock queue_api_lock_;
/** callback functions (stored) */
bp::object on_connect_object_;
bp::object on_mail_object_;
/** close connection flag */
bool closing_;
};
}
;//namesapce
////////////////////////////////////////////////////////////////////////////////////////////
/** here comes the boost python stuff */
////////////////////////////////////////////////////////////////////////////////////////////
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(f_overloads, f, 1, 2)
;
BOOST_PYTHON_FUNCTION_OVERLOADS(time_overloads, MOOSTime, 0, 1)
;
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(notify_overloads_2_3, Notify, 2,3)
;
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(notify_overloads_3_4, Notify, 3,4)
;
BOOST_PYTHON_MODULE(pymoos)
{
bp::docstring_options local_docstring_options(true, true, false);
PyEval_InitThreads();
/*********************************************************************
MOOSMsg e class
*********************************************************************/
bp::class_<CMOOSMsg>("moos_msg","Bar class")
.def("time", &CMOOSMsg::GetTime)
.def("trace",&CMOOSMsg::Trace)
.def("name", &CMOOSMsg::GetKey)
.def("key", &CMOOSMsg::GetKey)
.def("is_name", &CMOOSMsg::IsName)
.def("source", &CMOOSMsg::GetSource)
.def("is_double", &CMOOSMsg::IsDouble)
.def("double", &CMOOSMsg::GetDouble)
.def("double_aux",&CMOOSMsg::GetDoubleAux)
.def("is_string", &CMOOSMsg::IsString)
.def("string", &CMOOSMsg::GetString)
.def("is_binary", &CMOOSMsg::IsBinary)
.def("binary_data",&CMOOSMsg::GetString)
.def("binary_data_size",&CMOOSMsg::GetBinaryDataSize)
.def("mark_as_binary",&CMOOSMsg::MarkAsBinary);
/*********************************************************************
vector of messages
*********************************************************************/
bp::class_<MsgVector>("moos_msg_list")
.def(bp::vector_indexing_suite<MsgVector>());
/*********************************************************************
communications status class
*********************************************************************/
bp::class_<MOOS::ClientCommsStatus>("moos_comms_status")
.def("appraise",&MOOS::ClientCommsStatus::Appraise)
.def("print",&MOOS::ClientCommsStatus::Write);
/*********************************************************************
vector of communications status classes
*********************************************************************/
bp::class_<CommsStatusVector>("moos_comms_status_list")
.def(bp::vector_indexing_suite<CommsStatusVector>());
/*********************************************************************
comms base class
*********************************************************************/
bp::class_<CMOOSCommObject>("base_comms_object", bp::no_init);
/*********************************************************************
synchronous comms base class
*********************************************************************/
bp::class_<CMOOSCommClient, bp::bases<CMOOSCommObject>, boost::noncopyable>("base_sync_comms",bp::no_init)
.def("register",static_cast<bool(CMOOSCommClient::*)(const std::string&, double)> (&CMOOSCommClient::Register))
.def("register",static_cast<bool(CMOOSCommClient::*)(const std::string&,const std::string&,double)> (&CMOOSCommClient::Register))
.def("is_registered_for",&CMOOSCommClient::IsRegisteredFor)
.def("notify",static_cast<bool(CMOOSCommClient::*)(const std::string&,const std::string&, double)> (&CMOOSCommClient::Notify),notify_overloads_2_3())
.def("notify",static_cast<bool(CMOOSCommClient::*)(const std::string&,const std::string&,const std::string&,double)> (&CMOOSCommClient::Notify),notify_overloads_2_3())
.def("notify",static_cast<bool(CMOOSCommClient::*)(const std::string&,const char *,double)> (&CMOOSCommClient::Notify))
.def("notify",static_cast<bool(CMOOSCommClient::*)(const std::string&,const char *,const std::string&,double)> (&CMOOSCommClient::Notify))
.def("notify",static_cast<bool(CMOOSCommClient::*)(const std::string&,double,double)> (&CMOOSCommClient::Notify))
.def("notify",static_cast<bool(CMOOSCommClient::*)(const std::string&,double,const std::string&,double)> (&CMOOSCommClient::Notify))
.def("get_community_name", &CMOOSCommClient::GetCommunityName)
.def("get_moos_name",&CMOOSCommClient::GetMOOSName)
.def("close", &CMOOSCommClient::Close)
.def("get_published", &CMOOSCommClient::GetPublished)
.def("get_registered",&CMOOSCommClient::GetRegistered)
.def("get_description",&CMOOSCommClient::GetDescription)
.def("is_running", &CMOOSCommClient::IsRunning)
.def("is_asynchronous",&CMOOSCommClient::IsAsynchronous)
.def("is_connected",&CMOOSCommClient::IsConnected)
.def("wait_until_connected",&CMOOSCommClient::WaitUntilConnected)
.def("get_number_of_unread_messages",&CMOOSCommClient::GetNumberOfUnreadMessages)
.def("get_number_of_unsent_messages",&CMOOSCommClient::GetNumberOfUnsentMessages)
.def("get_number_bytes_sent",&CMOOSCommClient::GetNumBytesSent)
.def("get_number_bytes_read",&CMOOSCommClient::GetNumBytesReceived)
.def("get_number_messages_sent",&CMOOSCommClient::GetNumMsgsSent)
.def("get_number_message_read",&CMOOSCommClient::GetNumMsgsReceived)
.def("set_comms_control_timewarp_scale_factor",&CMOOSCommClient::SetCommsControlTimeWarpScaleFactor)
.def("get_comms_control_timewarp_scale_factor", &CMOOSCommClient::GetCommsControlTimeWarpScaleFactor)
.def("do_local_time_correction",&CMOOSCommClient::DoLocalTimeCorrection)
.def("set_verbose_debug", &CMOOSCommClient::SetVerboseDebug)
.def("set_comms_tick",&CMOOSCommClient::SetCommsTick)
.def("set_quiet",&CMOOSCommClient::SetQuiet)
.def("enable_comms_status_monitoring",&CMOOSCommClient::EnableCommsStatusMonitoring)
.def("get_client_comms_status",&CMOOSCommClient::GetClientCommsStatus)
.def("get_client_comms_statuses",&CMOOSCommClient::GetClientCommsStatuses)
;
/*********************************************************************
Asynchronous comms base class
*********************************************************************/
bp::class_<MOOS::MOOSAsyncCommClient, bp::bases<CMOOSCommClient>,boost::noncopyable>("base_async_comms", bp::no_init)
.def("run",&MOOS::MOOSAsyncCommClient::Run);
/*********************************************************************/
/** FINALLY HERE IS THE CLASS WE EXPECT TO BE INSTANTIATED IN PYTHON */
/*********************************************************************/
//this is the one to use
bp::class_<MOOS::AsyncCommsWrapper, bp::bases<MOOS::MOOSAsyncCommClient>,boost::noncopyable>("comms")
.def("run", &MOOS::AsyncCommsWrapper::Run)
.def("close", &MOOS::AsyncCommsWrapper::Close)
.def("fetch",&MOOS::AsyncCommsWrapper::FetchMailAsVector)
.def("set_on_connect_callback",&MOOS::AsyncCommsWrapper::SetOnConnectCallback)
.def("set_on_mail_callback",&MOOS::AsyncCommsWrapper::SetOnMailCallback)
.def("notify_binary",&MOOS::AsyncCommsWrapper::NotifyBinary)
.def("add_active_queue",&MOOS::AsyncCommsWrapper::AddActiveQueue)
.def("remove_message_route_to_active_queue",&MOOS::AsyncCommsWrapper::RemoveMessageRouteToActiveQueue)
.def("remove_active_queue",&MOOS::AsyncCommsWrapper::RemoveActiveQueue)
.def("has_active_queue",&MOOS::AsyncCommsWrapper::HasActiveQueue)
.def("print_message_to_active_queue_routing",&MOOS::AsyncCommsWrapper::PrintMessageToActiveQueueRouting)
.def("add_message_route_to_active_queue",static_cast<bool(CMOOSCommClient::*)(const std::string &,const std::string &)> (&MOOS::AsyncCommsWrapper::AddMessageRouteToActiveQueue))
;
/*********************************************************************
some MOOS Utilities
*********************************************************************/
/** here are some global help functions */
bp::def("time", &MOOSTime, time_overloads());
bp::def("local_time", &MOOSLocalTime, time_overloads());
bp::def("is_little_end_in", &IsLittleEndian);
bp::def("set_moos_timewarp", &SetMOOSTimeWarp);
bp::def("get_moos_timewarp", &GetMOOSTimeWarp);
bp::register_exception_translator<pyMOOSException>(&MOOSExceptionTranslator);
}