def run(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.connect((socket.gethostname(), 7357)) f = s.makefile() e = labcomm2014.Encoder(labcomm2014.StreamWriter(f)) d = labcomm2014.Decoder(labcomm2014.StreamReader(f)) e.add_decl(proto.subscribe.signature) e.add_decl(proto.publish.signature) e.add_decl(pos_vel.posRef.signature) pub = proto.publish() pub.topic = 'S__pt_posRef' e.encode(pub, pub.signature) sub = proto.subscribe() sub.topic = 'S__pt_velRef' e.encode(sub, sub.signature) dec_thread = threading.Thread(target=dec, args=(d, )) dec_thread.daemon = True dec_thread.start() i = 0 while running: print "send %d" % i pos = pos_vel.posRef() pos.x = 1.0 + i pos.y = 2.0 + i pos.z = 3.0 + i e.encode(pos, pos.signature) i += 1 sleep(1.0)
def __init__(self, port): # Setup socket connection to bridge. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.connect((socket.gethostname(), port)) # Setup LabComm. f = sock.makefile() self.e = labcomm2014.Encoder(labcomm2014.StreamWriter(f)) self.d = labcomm2014.Decoder(labcomm2014.StreamReader(f)) self.e.add_decl(lc_types.S__add_two_ints_PAR.signature) self.e.add_decl(lc_types.S__add_two_intsS__get_loggers_PAR.signature)
def decode(self, f): decoder = labcomm2014.Decoder(labcomm2014.StreamReader(f)) try: while True: value, decl = decoder.decode() if value != None: self.next.acquire() self.received_value = value self.received_decl = decl self.expected = None self.next.notify_all() self.next.release() pass pass except EOFError: pass pass
def run(self): while running: (csock, address) = self.sock.accept() f = csock.makefile() self.enc = labcomm2014.Encoder(labcomm2014.StreamWriter(f)) d = labcomm2014.Decoder(labcomm2014.StreamReader(f)) self.enc.add_decl(lc_types.S__ping.signature) self.counter = 0 self.send_ping() while running: value, decl = d.decode() if value: print decl.name, 'says:', value.s self.t.cancel()
def run(topic): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.connect((socket.gethostname(), 7357)) f = s.makefile() e = labcomm2014.Encoder(labcomm2014.StreamWriter(f)) d = labcomm2014.Decoder(labcomm2014.StreamReader(f)) e.add_decl(proto.subscribe.signature) sub = proto.subscribe() sub.topic = topic e.encode(sub, sub.signature) while True: val, decl = d.decode() if val: print(val) else: print("Br. reg. type: %s" % decl)
def __init__(self, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.connect((socket.gethostname(), port)) f = sock.makefile() e = labcomm2014.Encoder(labcomm2014.StreamWriter(f)) d = labcomm2014.Decoder(labcomm2014.StreamReader(f)) e.add_decl(proto.subscribe.signature) e.add_decl(proto.publish.signature) e.add_decl(lc_types.S__ping.signature) pub = proto.publish() pub.topic = 'S__ping' e.encode(pub, pub.signature) sub = proto.subscribe() sub.topic = 'S__pong' e.encode(sub, sub.signature) self.enc = e self.dec = d self.counter = 0
def main(port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('127.0.0.1', port)) sock.listen(5) while True: print 'Listening for new connection on port:', port (csock, addr) = sock.accept() print 'Got connection from:', addr f = csock.makefile() e = labcomm2014.Encoder(labcomm2014.StreamWriter(f)) d = labcomm2014.Decoder(labcomm2014.StreamReader(f)) e.add_decl(lc_types.S__ext_service_RET.signature) while True: value, decl = d.decode() if value: print 'Got call:', value.controller_id res = lc_types.S__ext_service_RET() res.status = value.controller_id time.sleep(5) e.encode(res, res.signature)
import os import sys import socket import rwsocket if not any('labcomm2014' in p for p in sys.path): sys.path.append('../../lib/python') import labcomm2014 if __name__ == "__main__": print "Trying to connect..." host = sys.argv[1] #'localhost' port = sys.argv[2] #'8081' addr = (host, int(port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(addr) print "Connected!" d = labcomm2014.Decoder(labcomm2014.StreamReader(sock)) while True: try: data, decl = d.decode() if data: print data except KeyError as e: print 'KeyError : "%s"' % str(e) except: print 'exception...', sys.exc_info()[0] break
def __init__(self, client_sock, client_addr, static_dict=None): super(ClientThread, self).__init__() self.subs = {} # topic -> subscriber self.pubs = {} # topic -> subscriber self.client_addr = client_addr self.client_socket = client_sock client_sock.settimeout(None) f = client_sock.makefile('w', 0) self.enc = labcomm2014.Encoder(labcomm2014.StreamWriter(f)) self.dec = labcomm2014.Decoder(labcomm2014.StreamReader(f)) self.enc_lock = threading.Lock() self.subscribed_conversions = {} # pt -> conv self.published_conversions = {} # pt -> conv self.ongoing_service_calls = {} # srv name -> dict import_srvs = [ srv for name, srv in conf.SERVICES.iteritems() if srv['direction'] == 'import' and name not in service_publishers ] for srv in import_srvs: n = msg2id(srv['name']) cb = ServiceCallback(n, self) self.ongoing_service_calls[n] = cb service_publishers[n] = rospy.Service(srv['name'], service_types_py[n], cb) if static_dict: for pubsub, topics in static_dict.iteritems(): for t in topics: if pubsub == 'publish' and t in conf.IMPORTS: rospy.loginfo( ('Setting up static publish for topic %s ' 'to client %s'), t, client_addr) self.pubs[t] = rospy.Publisher(t, topic_types_py[t]) elif pubsub == 'subscribe' and t in conf.EXPORTS: rospy.loginfo(('Setting up static subscribe for topic ' '%s to client %s'), t, client_addr) typ = ttmap[t] self.enc.add_decl(typ.signature) self.subs[t] = rospy.Subscriber(t, topic_types_py[t], self._convert_and_send, callback_args=t) if conf.AUTOPUBSUB: class Dummy(object): pass dummy = Dummy() for topic in conf.EXPORTS: dummy.topic = topic self._handle_subscribe(dummy) for topic, _ in pseudotopic_sources.iteritems(): dummy.topic = topic self._handle_subscribe(dummy) for topic in conf.IMPORTS: dummy.topic = topic self._handle_publish(dummy) for topic, _ in pseudotopic_sinks.iteritems(): dummy.topic = topic self._handle_publish(dummy)
def __init__(self): self.says = None if __name__ == '__main__': buf = StringIO.StringIO() encoder = labcomm2014.Encoder(labcomm2014.StreamWriter(buf)) encoder.add_decl(animal.cow.signature) encoder.add_decl(animal.dog.signature) encoder.add_decl(animal.duck.signature) theAnimal = Animal() theAnimal.says = 'Moo' encoder.encode(theAnimal, animal.cow.signature) theAnimal.says = 'Bow-Wow' encoder.encode(theAnimal, animal.dog.signature) theAnimal.says = 'Quack' encoder.encode(theAnimal, animal.duck.signature) buf.seek(0) decoder = labcomm2014.Decoder(labcomm2014.StreamReader(buf)) try: while True: value, decl = decoder.decode() if value: print decl.name, 'says', value pass pass pass except EOFError: pass pass
#!/usr/bin/python import labcomm2014 import sys if __name__ == "__main__": version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014" d = labcomm2014.Decoder(labcomm2014.StreamReader(open(sys.argv[1])), version) while True: try: data, decl = d.decode() if data: print data except Exception, e: print e break
def main(main_args): parser = argparse.ArgumentParser() parser.add_argument('elc', type=str, help="The log file.") parser.add_argument('-f', '--follow', action='store_true', help="find all registrations that already " "exist, then watch the file for changes. All " "future registrations are ignored (because " "the header has already been written).") parser.add_argument('-s', '--interval', action="store", type=float, default=0.040, help="time to sleep between failed reads. Requires -f.") parser.add_argument('-t', '--timeout', action="store", type=float, help="timeout to terminate when no changes are detected. " "Requires -f.") parser.add_argument('-w', '--no-default-columns', action="store_true", help="Do not fill columns for which there is no " "data with default values. Wait instead until at least " "one sample has arrived for each registration.") parser.add_argument('-a', '--trigger-all', action="store_true", help="Output one line for each sample instead of for " "each sample of the registration that has arrived with " "the highest frequency.") args = parser.parse_args(main_args) n_samples = {} # The number of received samples for each sample reg. current = {} # The most recent sample for each sample reg. type_ = {} # The type (declaration) of each sample reg. file_ = open(args.elc) if args.follow: reader = FollowingReader(file_, args.interval, args.timeout) else: reader = Reader(file_) d = labcomm2014.Decoder(reader) while True: try: o, t = d.decode() if o is None: n_samples[t.name] = 0 type_[t.name] = t else: n_samples[t.name] += 1 current[t.name] = o break except EOFError: break dump_labels(type_) if not args.no_default_columns: defaults(current, type_) n_rows = 0 while True: try: o, t = d.decode() if o is None: continue current[t.name] = o n_samples[t.name] += 1 if len(current) < len(type_): continue if args.trigger_all: dump(current, type_) else: if n_samples[t.name] > n_rows: n_rows = n_samples[t.name] dump(current, type_) except EOFError: break