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 run(self): print >> sys.stderr, 'Testing', self.program p = subprocess.Popen(self.program, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=sys.stderr) self.expected = None self.failed = False self.next = threading.Condition() decoder = threading.Thread(target=self.decode, args=(p.stdout, )) decoder.start() encoder = labcomm2014.Encoder(labcomm2014.StreamWriter(p.stdin)) for signature in self.signatures: encoder.add_decl(signature) pass if self.uses_refs(self.signatures): for signature in self.signatures: encoder.add_ref(signature) for signature in self.signatures: print >> sys.stderr, "Checking", signature.name, for decl, value in self.generate(signature): sys.stderr.write('.') self.next.acquire() self.received_value = None self.received_decl = None encoder.encode(value, decl) self.next.wait(2) self.next.release() if p.poll() != None: print >> sys.stderr, "Failed with:", p.poll() self.failed = True elif value != self.received_value or decl != self.received_decl: print >> sys.stderr, "Coding error" print >> sys.stderr, value == self.received_value print >> sys.stderr, "Got: ", self.received_value print >> sys.stderr, " ", self.received_decl print >> sys.stderr, "Expected:", value print >> sys.stderr, " ", decl self.failed = True if self.failed: if p.poll() == None: p.terminate() exit(1) pass print >> sys.stderr pass p.stdin.close() if p.wait() != 0: exit(1) 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)
#!/usr/bin/python import labcomm2014 import sys import simple if __name__ == '__main__': version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014" encoder = labcomm2014.Encoder( labcomm2014.StreamWriter(open(sys.argv[1], 'w')), version) encoder.add_decl(simple.theTwoInts.signature) encoder.add_decl(simple.IntString.signature) foo = simple.theTwoInts() foo.a = 13 foo.b = 37 encoder.encode(foo, simple.theTwoInts.signature) bar = simple.IntString() bar.x = 1742 bar.s = "A string from Python" encoder.encode(bar, simple.IntString.signature)
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)
#!/usr/bin/python import labcomm2014 import animal import StringIO class Animal: 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: