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() 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 RpcService(object): def __init__(self, ip, port): self.core = AsyncCore() self.listenHid = self.core.new_listen(ip, port, HEADER_WORDLSB) print 'listen on localhost:8888 hid=%xh' % self.listenHid def run(self): core = self.core while True: core.wait(0.05) 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_DATA: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh'%hid fn, args = msgpack.unpackb(data, use_list=False) fn = getattr(self, fn, None) if fn: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'call method %s(%s)'%(fn,str(args)) if args: fn(*args) else: fn()
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 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() 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
class ServiceManager(object): def __init__(self, ip, port): self.serviceMap = {} self.core = AsyncCore() self.listenHid = self.core.new_listen(ip, port, HEADER_WORDLSB) print 'listen on localhost:8888 hid=%xh' % self.listenHid def addService(self, svc): self.serviceMap[svc.name] = svc def callService(self, name, fn, args): svc = self.serviceMap.get(name, None) if not svc: return fn = getattr(svc, fn, None) if not fn: return # print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'call method %s.%s(%s)'%(svc.__class__.__name__,fn,str(args)) if args: fn(*args) else: fn() def run(self): core = self.core while True: core.wait(0.05) 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_DATA: print time.strftime( '[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh' % hid name, fn, args = msgpack.unpackb(data, use_list=False) self.callService(name, fn, args)
class ServiceManager(object): def __init__(self, ip, port): self.serviceMap = {} self.core = AsyncCore() self.listenHid = self.core.new_listen(ip, port, HEADER_WORDLSB) print 'listen on localhost:8888 hid=%xh' % self.listenHid def addService(self, svc): self.serviceMap[svc.name] = svc def callService(self, name, fn, args): svc = self.serviceMap.get(name, None) if not svc: return fn = getattr(svc, fn, None) if not fn: return # print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'call method %s.%s(%s)'%(svc.__class__.__name__,fn,str(args)) if args: fn(*args) else: fn() def run(self): core = self.core while True: core.wait(0.05) 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_DATA: print time.strftime('[%Y-%m-%d %H:%M:%S]'), 'recv hid=%xh'%hid name, fn, args = msgpack.unpackb(data, use_list=False) self.callService(name, fn, args)
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 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
def __init__(self, ip, port): self.core = AsyncCore() self.listenHid = self.core.new_listen(ip, port, HEADER_WORDLSB) print 'listen on localhost:8888 hid=%xh' % self.listenHid
def __init__(self): self.core = AsyncCore() self.listen_hids = [] self.client_hids = []
def __init__(self): self.core = AsyncCore() self.client_hid = None