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 if self._transport != None:
00271 self._transport.close()
00272
00273
00274
00275 def timestamp(self):
00276 return math.floor((time.time() - self.start_time) * 1000)
00277
00278
00279 def emergency_stop(self):
00280 code = codes.codes['safety_status']
00281 self.send_message(code.set, code.payload(code.payload.EMERGENCY_STOP))
00282
00283
00284 def command(self, name, args):
00285 if 'self' in args: del args['self']
00286 self.send_message(messages.Message.command(name, args, self.timestamp(), no_ack=(not self.acks)))
00287
00288
00289 def request(self, name, args):
00290
00291 self._received = None
00292 self.add_handler(handler = self._receiver, request = name)
00293
00294
00295 if 'self' in args: del args['self']
00296 message = messages.Message.request(name, args, self.timestamp())
00297 self.send_message(message)
00298
00299
00300
00301 if 'subscription' in args and args['subscription'] == 0xFFFF:
00302 return;
00303
00304
00305 retries = self._retries
00306 start = self.timestamp()
00307 while True:
00308 if self.timestamp() - start > self._rec_timeout:
00309 if retries > 0:
00310 message = message.copy(timestamp=self.timestamp())
00311 self.send_message(message)
00312 retries -= 1
00313 start = self.timestamp()
00314 else:
00315 raise utils.TimeoutError (
00316 "Timeout Occurred waiting for response!")
00317
00318 if self._received != None:
00319 self.remove_handler(handler = self._receiver, request = name)
00320 return self._received[1]
00321
00322 time.sleep(0.001)
00323
00324
00325 def _receiver(self, name, payload, timestamp):
00326 self._received = (name, payload, timestamp)
00327
00328
00329
00330 def send_message(self, message):
00331 """Horizon Protocol Send Message"""
00332 if not self._transport.is_open():
00333 raise IOError ("Transport has not been opened!")
00334
00335
00336 self._transport.send_message(message)
00337
00338
00339
00340 if not message.no_ack:
00341 tries = self._retries
00342 while True:
00343 ack = self._transport.receiver.has_ack(message.timestamp)
00344 if ack:
00345 if ack.payload.bad_code:
00346 raise utils.UnsupportedCodeError("Acknowledgment says Bad Code.")
00347 elif ack.payload.bad_format:
00348 raise utils.FormatError("Acknowledgment says Bad Format.")
00349 elif ack.payload.bad_values:
00350 raise ValueError("Acknowledgment says Bad Values.")
00351 elif ack.payload.bad_frequency:
00352 raise utils.SubscriptionError("Acknowledgment says Bad Frequency.")
00353 elif ack.payload.bad_code_count:
00354 raise utils.SubscriptionError("Acknowledgment says Too Many Subscriptions.")
00355 elif ack.payload.bad_bandwidth:
00356 raise utils.SubscriptionError("Acknowledgment says Not Enough Bandwidth.")
00357 else:
00358
00359 return True
00360
00361 if self.timestamp() - message.timestamp > self._send_timeout:
00362 if tries > 0:
00363
00364 tries -= 1
00365 message = message.copy(timestamp = self.timestamp())
00366 self._transport.send_message(message)
00367 else:
00368 raise utils.TimeoutError("Message Timeout Occurred!")
00369
00370 time.sleep(0.001)
00371
00372
00373 def add_handler(self, handler, backtrack = False, request = None):
00374 """Horizon Protocol Add Data Message Handler"""
00375 code = 0
00376 if request != None:
00377 code = codes.codes[request].data()
00378
00379 with self._handlers_lock:
00380
00381 if code not in self._handlers:
00382 self._handlers[code] = []
00383 self._handlers[code].append(handler)
00384
00385
00386 if backtrack:
00387 for tup in self.get_waiting(request):
00388 handler(tup[0], tup[1], tup[2])
00389
00390
00391 def remove_handler(self, handler, request = None):
00392 """Horizon Protocol Remove Data Message Handler"""
00393 code = 0
00394 if request != None:
00395 code = codes.codes[request].data
00396
00397 with self._handlers_lock:
00398
00399 if code in self._handlers:
00400 if handler != None and handler in self._handlers[code]:
00401 self._handlers[code].remove(handler)
00402 elif handler == None:
00403 self._handlers[code] = []
00404
00405
00406
00407
00408 def do_handlers(self, message):
00409 with self._handlers_lock:
00410 handlers = self._handlers[0][:]
00411 if message.code in self._handlers:
00412 handlers += self._handlers[message.code]
00413
00414 for handler in handlers:
00415 handler(codes.names[message.code], message.payload, message.timestamp)
00416
00417 return len(handlers) > 0
00418
00419
00420 def get_waiting(self, request = None):
00421 code = 0
00422 if request != None:
00423 code = codes.codes[request].data
00424
00425 waiting = []
00426 for message in self._transport.receiver.get_waiting(code):
00427 waiting.append((codes.names[message.code], message.payload, message.timestamp))
00428
00429 return waiting
00430
00431
00432 def is_open(self):
00433 return self._transport != None and self._transport.is_open()
00434
00435
00436 logger.debug("... clearpath.horizon.protocol loaded.")