def main(): core = AsyncCore() hid_listen = core.new_listen('127.0.0.1', 8888, HEADER_LINESPLIT) if hid_listen < 0: print 'can not listen on port 8888' return -1 print 'listen on localhost:8888 hid=%xh' % hid_listen clients = set() 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 if core.get_mode(hid) == ASYNC_MODE_IN: clients.add(hid) _, port, ip = core.parse_remote(data) print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'accept hid=%xh'%hid, 'from %s:%d'%(ip,port) elif event == ASYNC_EVT_LEAVE: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh'%hid clients.remove(hid) elif event == ASYNC_EVT_DATA: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh'%hid, 'data', data core.send(hid, data) return 0
def main(): core = AsyncCore() hid_listen = core.new_listen('127.0.0.1', 8888, HEADER_LINESPLIT) if hid_listen < 0: print 'can not listen on port 8888' return -1 print 'listen on localhost:8888 hid=%xh' % hid_listen clients = set() 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 if core.get_mode(hid) == ASYNC_MODE_IN: clients.add(hid) _, port, ip = core.parse_remote(data) print time.strftime( '[%Y-%m-%d %H:%M:%S]' ), 'accept hid=%xh' % hid, 'from %s:%d' % (ip, port) elif event == ASYNC_EVT_LEAVE: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid clients.remove(hid) elif event == ASYNC_EVT_DATA: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data core.send(hid, data) return 0
class BaseServer(object): def __init__(self): self.core = AsyncCore() self.listen_hids = [] self.client_hids = [] def add_endpoint(self, endpoint): ip, port = endpoint hid = self.core.new_listen(ip, port, HEADER_WORDLSB) self.core.option(hid, 'REUSEADDR', 1) self.listen_hids.append(hid) def dispatch_event(self, event, hid, data): if event == ASYNC_EVT_NEW: if self.core.get_mode(hid) == ASYNC_MODE_IN: self.client_hids.append(hid) _, port, ip = self.core.parse_remote(data) self.handle_client_new(hid, (ip, port)) elif event == ASYNC_EVT_LEAVE: if hid in self.client_hids: self.client_hids.remove(hid) self.handle_client_leave(hid) elif event == ASYNC_EVT_DATA: if hid in self.client_hids: self.handle_client_data(hid, data) def run_internal(self): while True: self.core.wait(0.1) while True: event, hid, tag, data = self.core.read() if event == None: break self.dispatch_event(event, hid, data) def run(self): try: self.run_internal() except KeyboardInterrupt: pass def send(self, hid, data): self.core.send(hid, data) def handle_client_new(self, client_hid, client_addr): raise ValueError('please implement this method') def handle_client_data(self, child_hid, data): raise ValueError('please implement this method') def handle_client_leave(self, child_hid): raise ValueError('please implement this method')
def main(): core = AsyncCore() hid_listen = core.new_listen('127.0.0.1', 8888, HEADER_WORDLSB) if hid_listen < 0: print 'can not listen on port 8888' return -1 print 'listen on localhost:8888 hid=%xh' % hid_listen index = 0 clients = set() timeslap = time.time() 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 if core.get_mode(hid) == ASYNC_MODE_IN: clients.add(hid) print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'accept hid=%xh' % hid elif event == ASYNC_EVT_LEAVE: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'leave hid=%xh' % hid clients.remove(hid) elif event == ASYNC_EVT_DATA: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid, 'data', data if clients: current = time.time() if current > timeslap: timeslap = current + 5 for hid in clients: core.send(hid, 'ECHO\x00%d' % index) index += 1 return 0