def heartbeat_sender(self, sock, addr, socklock, parent): try: with closing(sock): while True: self.heartbeat_event.wait() pkt = makepacket(PacketType.PORT_AGENT_HEARTBEAT, ntp.now(), "") with socklock: sock.sendall(pkt) sleep() except socket.error, e: log.debug("heartbeat socket err: %s" % e)
def test_both(self): timestamp = 123.456 msgtype = 222 data = "hello, world." txpktbuf = makepacket(msgtype, timestamp, data) rxpktbuf = txpktbuf rxpktobj = ReceivedPacket(rxpktbuf[:HEADER_SIZE]) rxpktobj.validate(rxpktbuf[HEADER_SIZE:]) self.assertEquals(timestamp, rxpktobj.timestamp) self.assertEquals(msgtype, rxpktobj.msgtype) self.assertEquals(data, rxpktobj.data)
def work(self, sock, addr): socklock = Semaphore() heartbeat = spawn(self.heartbeat_sender, sock, addr, socklock, getcurrent()) heartbeat.link_exception(self.janitor) try: with self.subscription() as queue: while True: orbpkt, timestamp = queue.get() pkt = makepacket(PacketType.DATA_FROM_INSTRUMENT, timestamp, orbpkt) with socklock: sock.sendall(pkt) finally: try: heartbeat.kill() except: pass
def ping(self, val, sock): msg = "pong. version: " + __version__ sock.sendall(makepacket(PacketType.PORT_AGENT_STATUS, ntp.now(), msg))
def get_state(self, val, sock): statestr = self.states[self.state] sock.sendall(makepacket(PacketType.PORT_AGENT_STATUS, ntp.now(), statestr))
def test_makepacket(self): r = makepacket(0, 12345, []) self.assertEquals(len(r), 16) self.assertEquals([r[6], r[7]], [0x0, 0x40]) self.assertEquals(r, TESTPKT1)