Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 if __name__ == "__main__":
00055
00056
00057 print ("ERROR: clearpath.horizon.protocol is a module and can NOT be run"\
00058 " as a script!\nFor a command-line interface demo of Horizon, run:"\
00059 "\n python -m clearpath.horizon.demo\n"\
00060 "For Horizon message forwarding, run:\n"\
00061 " python -m clearpath.horizon.forward")
00062
00063
00064 import sys
00065 sys.exit(1)
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 """Horizon Protocol Message Handlers
00153
00154 Copyright © 2010 Clearpath Robotics, Inc.
00155 All rights reserved
00156
00157 Created: 18/01/10
00158 Authors: Ryan Gariepy & Malcolm Robert
00159 Version: 1.0
00160 """
00161
00162
00163
00164 from .. import utils
00165 from . import codes
00166 from . import messages
00167 from . import transports
00168
00169
00170 import datetime
00171 import logging
00172 import sys
00173 import time
00174 import math
00175
00176
00177
00178 __version__ = "1.0"
00179 __revision__ = "$Revision: 916 $"
00180
00181
00182
00183 logger = logging.getLogger('clearpath.horizon.protocol')
00184 """Horizon Protocol Module Log"""
00185 logger.setLevel(logging.NOTSET)
00186 logger.addHandler(utils.NullLoggingHandler())
00187 logger.propagate = False
00188 logger.debug("Loading clearpath.horizon.protocol ...")
00189
00190
00191
00192
00193 class Client(object):
00194 """Horizon Transport Protocol Controller - Client Device"""
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 def __init__(self, transport, transport_args, retries,
00226 send_timeout, rec_timeout, store_timeout):
00227
00228
00229
00230 self._handlers_lock = transports.threading.Lock()
00231 self._handlers = { 0:[] }
00232
00233 self.start_time = time.time()
00234 self._transport = None
00235 self._transport_func = transport
00236 self._transport_args = transport_args
00237 self._retries = retries
00238 self._send_timeout = send_timeout
00239 self._rec_timeout = rec_timeout
00240
00241 self.acks = True;
00242
00243
00244 transport_args['receive_callback'] = self.do_handlers
00245 transport_args['store_timeout'] = store_timeout
00246
00247
00248 def __del__(self):
00249 """Destroy A Horizon Transport"""
00250
00251 self.close()
00252
00253
00254 def __str__(self):
00255 """Return the transport name."""
00256 return str(self._transport)
00257
00258
00259 def open(self):
00260 if not self._transport:
00261 self._transport = self._transport_func(**self._transport_args)
00262 if not isinstance(self._transport, transports.Transport):
00263 raise ValueError ("Invalid transport!")
00264
00265 if not self._transport.is_open():
00266 self._transport.open()
00267
00268
00269 def close(self):
00270 self.remove_handler()
00271 if self._transport != None:
00272 self._transport.close()
00273
00274
00275
00276 def timestamp(self):
00277 return math.floor((time.time() - self.start_time) * 1000)
00278
00279
00280 def emergency_stop(self):
00281 code = codes.codes['safety_status']
00282 self.send_message(code.set, code.payload(code.payload.EMERGENCY_STOP))
00283
00284
00285 def command(self, name, args):
00286 if 'self' in args: del args['self']
00287 self.send_message(messages.Message.command(name, args, self.timestamp(), no_ack=(not self.acks)))
00288
00289
00290 def request(self, name, args):
00291 try:
00292
00293 self._received = None
00294 self.add_handler(handler = self._receiver, request = name)
00295
00296
00297 if 'self' in args: del args['self']
00298 message = messages.Message.request(name, args, self.timestamp())
00299 self.send_message(message)
00300
00301
00302
00303 if 'subscription' in args and args['subscription'] == 0xFFFF:
00304 return;
00305
00306
00307 retries = self._retries
00308 start = self.timestamp()
00309 while True:
00310 if self.timestamp() - start > self._rec_timeout:
00311 if retries > 0:
00312 message = message.copy(timestamp=self.timestamp())
00313 self.send_message(message)
00314 retries -= 1
00315 start = self.timestamp()
00316 else:
00317 raise utils.TimeoutError (
00318 "Timeout Occurred waiting for response!")
00319
00320 if self._received != None:
00321 return self._received[1]
00322
00323 time.sleep(0.001)
00324 finally:
00325 self.remove_handler(handler = self._receiver, request = name)
00326
00327
00328
00329 def _receiver(self, name, payload, timestamp):
00330 self._received = (name, payload, timestamp)
00331
00332
00333
00334 def send_message(self, message):
00335 """Horizon Protocol Send Message"""
00336 if not self._transport.is_open():
00337 raise IOError ("Transport has not been opened!")
00338
00339
00340 self._transport.send_message(message)
00341
00342
00343
00344 if not message.no_ack:
00345 tries = self._retries
00346 while True:
00347 ack = self._transport.receiver.has_ack(message.timestamp)
00348 if ack:
00349 if ack.payload.bad_code:
00350 raise utils.UnsupportedCodeError("Acknowledgment says Bad Code.")
00351 elif ack.payload.bad_format:
00352 raise utils.FormatError("Acknowledgment says Bad Format.")
00353 elif ack.payload.bad_values:
00354 raise ValueError("Acknowledgment says Bad Values.")
00355 elif ack.payload.bad_frequency:
00356 raise utils.SubscriptionError("Acknowledgment says Bad Frequency.")
00357 elif ack.payload.bad_code_count:
00358 raise utils.SubscriptionError("Acknowledgment says Too Many Subscriptions.")
00359 elif ack.payload.bad_bandwidth:
00360 raise utils.SubscriptionError("Acknowledgment says Not Enough Bandwidth.")
00361 else:
00362
00363 return True
00364
00365 if self.timestamp() - message.timestamp > self._send_timeout:
00366 if tries > 0:
00367
00368 tries -= 1
00369 message = message.copy(timestamp = self.timestamp())
00370 self._transport.send_message(message)
00371 else:
00372 raise utils.TimeoutError("Message Timeout Occurred!")
00373
00374 time.sleep(0.001)
00375
00376
00377 def add_handler(self, handler, backtrack = False, request = None):
00378 """Horizon Protocol Add Data Message Handler"""
00379 code = 0
00380 if request != None:
00381 code = codes.codes[request].data()
00382
00383 with self._handlers_lock:
00384
00385 if code not in self._handlers:
00386 self._handlers[code] = []
00387 self._handlers[code].append(handler)
00388
00389
00390 if backtrack:
00391 for tup in self.get_waiting(request):
00392 handler(tup[0], tup[1], tup[2])
00393
00394
00395 def remove_handler(self, handler=None, request=None):
00396 """Horizon Protocol Remove Data Message Handler"""
00397 code = 0
00398 if request != None:
00399 code = codes.codes[request].data()
00400
00401 with self._handlers_lock:
00402
00403 if code in self._handlers:
00404 if handler != None and handler in self._handlers[code]:
00405 self._handlers[code].remove(handler)
00406 elif handler == None:
00407 self._handlers[code] = []
00408
00409
00410
00411
00412 def do_handlers(self, message):
00413 with self._handlers_lock:
00414 handlers = self._handlers[0][:]
00415 if message.code in self._handlers:
00416 handlers += self._handlers[message.code]
00417
00418 for handler in handlers:
00419 handler(codes.names[message.code], message.payload, message.timestamp)
00420
00421 return len(handlers) > 0
00422
00423
00424 def get_waiting(self, request = None):
00425 code = 0
00426 if request != None:
00427 code = codes.codes[request].data
00428
00429 waiting = []
00430 for message in self._transport.receiver.get_waiting(code):
00431 waiting.append((codes.names[message.code], message.payload, message.timestamp))
00432
00433 return waiting
00434
00435
00436 def is_open(self):
00437 return self._transport != None and self._transport.is_open()
00438
00439
00440 logger.debug("... clearpath.horizon.protocol loaded.")