def start_echo_server():
    global server_addr
    poll = io_poll.get_poll()
    print str(poll)
    server = CoroSocketEngine(poll, is_blocking=False)
    server.set_logger(getLogger("server"))

    def _on_readable(conn):
        buf, eof = conn.read_avail(4096)
        if buf:
            #print "write", len(buf)
            yield conn.write(buf)
        if eof:
            conn.close()
        else:
            conn.watch()
        return
    server.listen_addr(server_addr, readable_cb=_on_readable)

    def _run(_server):
        while True:
            try:
                _server.poll()
            except Exception, e:
                traceback.print_exc()
                os._exit(1)
        return
class CoroSocketEngineTest(object):
    
    th = None
    running = False
    engine = None

    server_addr = None
    server_sock = None

    def __init__(self, logger):
        self.engine = CoroSocketEngine(iopoll.EPoll(), is_blocking=False, debug=True)
        self.engine.set_logger(logger)
        self.engine.set_timeout(5, 5)

    # NOTE: you can only be client/server at one time
    def start_client(self):
        assert not self.running
        self.running = True
        def _run(self):
            print "client started"
            while self.running:
                try:
                    l = self.engine.poll(1)
                except Exception, e:
                    traceback.print_exc()
                    os._exit(1)
            return
        self.th = threading.Thread(target=_run, args=(self, ))
        self.th.setDaemon(1)
        self.th.start()
def start_coro_server(poll=None):
    global server_addr
    poll = iopoll.get_poll()
    server = CoroSocketEngine(poll, is_blocking=False, debug=False)
    server.set_logger(getLogger("server"))
#    server.get_time = tc.time
    print "starting unblock server with", str(poll)
    def _handler(conn):
        try:
            buf = yield conn.read(len(data))
            yield conn.write(buf)
            server.watch_conn(conn)
        except PeerCloseError:
            pass
        except Exception, e:
            getLogger("server").exception(e)
            print e
        return
            conn.close()
            global_lock.acquire()
            g_done_client += 1
            print "client done", g_done_client
            if g_done_client == g_client_num:
                print "time:", time.time() - start_time
                global_lock.release()
                os._exit(0)
            global_lock.release()
        except socket.error, e:
            engine.logger.exception(e)
            print e
            os._exit(1)
        return

    engine = CoroSocketEngine(io_poll.get_poll())
    engine.set_logger(getLogger("client"))
    for i in xrange(g_client_num):
        engine.run_coro(client(engine, i, server_addr, g_round))
    while True:
        engine.poll()


def main():
    Log("client", config=conf)
    Log("server", config=conf)
    server = start_echo_server()
    time.sleep(1)
    test_client()

if __name__ == '__main__':
 def __init__(self, logger):
     self.engine = CoroSocketEngine(iopoll.EPoll(), is_blocking=False, debug=True)
     self.engine.set_logger(logger)
     self.engine.set_timeout(5, 5)