def main(): core = AsyncCore() client_hid = core.new_connect('127.0.0.1', 8888, HEADER_WORDLSB) if client_hid < 0: print 'can not connect to localhost:8888' return -1 begin_send = False next_time = time.time() + 1 while True: core.wait(0.1) while True: event, hid, tag, data = core.read() if event == None: break if event == ASYNC_EVT_NEW: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh'%hid elif event == ASYNC_EVT_LEAVE: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh'%hid elif event == ASYNC_EVT_ESTAB: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'estab hid=%xh'%hid begin_send = True elif event == ASYNC_EVT_DATA: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh'%hid, 'data', data t = time.time() if t >= next_time: next_time = t + 1 if begin_send: data = ('sayHello', None) data = msgpack.packb(data) core.send(client_hid, data)
def main(): core = AsyncCore() client_hid = core.new_connect('127.0.0.1', 8888, HEADER_WORDLSB) if client_hid < 0: print 'can not connect to localhost:8888' return -1 while True: core.wait(0.1) while True: event, hid, tag, data = core.read() if event == None: break if event == ASYNC_EVT_NEW: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh' % hid elif event == ASYNC_EVT_LEAVE: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid elif event == ASYNC_EVT_ESTAB: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'estab hid=%xh' % hid core.send(hid, 'START') elif event == ASYNC_EVT_DATA: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data
class BaseClient(object): def __init__(self): self.core = AsyncCore() self.client_hid = None def new_connect(self, endpoint): if self.client_hid != None: return ip, port = endpoint self.client_hid = self.core.new_connect(ip, port, HEADER_WORDLSB) def dispatch_event(self, event, hid, data): if event == ASYNC_EVT_DATA: self.handle_data(data) elif event == ASYNC_EVT_ESTAB: self.handle_connected() elif event == ASYNC_EVT_LEAVE: self.handle_disconnected() self.client_hid = None def run(self, wait_time): self.core.wait(wait_time) while True: event, hid, tag, data = self.core.read() if event == None: break self.dispatch_event(event, hid, data) def hid(self): return self.client_hid def send(self, data): self.core.send(self.client_hid, data) def handle_connected(self): raise ValueError('please implement this method') def handle_disconnected(self): raise ValueError('please implement this method') def handle_data(self, data): raise ValueError('please implement this method')
def main(): core = AsyncCore() client_hid = core.new_connect('127.0.0.1', 8888, HEADER_WORDLSB) if client_hid < 0: print 'can not connect to localhost:8888' return -1 begin_send = False index = 1 next_time = time.time() + 1 while True: core.wait(0.1) while True: event, hid, tag, data = core.read() if event == None: break if event == ASYNC_EVT_NEW: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh' % hid elif event == ASYNC_EVT_LEAVE: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid elif event == ASYNC_EVT_ESTAB: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'estab hid=%xh' % hid begin_send = True elif event == ASYNC_EVT_DATA: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data t = time.time() if t >= next_time: next_time = t + 1 if begin_send: index += 1 if index % 2 == 0: data = ('hello', 'sayHello', None) data = msgpack.packb(data) core.send(client_hid, data) else: data = ('counter', 'incCount', (5, )) data = msgpack.packb(data) core.send(client_hid, data)
def main(): core = AsyncCore() client_hid = core.new_connect('127.0.0.1', 8888, HEADER_WORDLSB) if client_hid < 0: print 'can not connect to localhost:8888' return -1 while True: core.wait(0.1) while True: event, hid, tag, data = core.read() if event == None: break if event == ASYNC_EVT_NEW: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'new hid=%xh'%hid elif event == ASYNC_EVT_LEAVE: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh'%hid elif event == ASYNC_EVT_ESTAB: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'estab hid=%xh'%hid core.send(hid, 'START') elif event == ASYNC_EVT_DATA: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh'%hid, 'data', data