def client():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    global g_send_count, g_client_num, g_done_client, server_addr, global_lock
    global g_round
    sock.connect(server_addr)
#        times = random.randint(1, 5000)
#        time.sleep(times/ 2000.0)
    for i in xrange(0, g_round):
        l = random.randint(1, MAX_LEN)
        data = random_string(l)
        send_all(sock, data)
#        print l, len(data)
        _data = recv_all(sock, l)
        if _data == data:
            #print "client", i
            global_lock.acquire()
            g_send_count += 1
            global_lock.release()
        else:
            global_lock.acquire()
            print "client recv invalid data", i, len(_data), len(data), l
            global_lock.release()
            os._exit(0)
#            time.sleep(0.01)
    print "client done", g_done_client
    sock.close()
    global_lock.acquire()
    g_done_client += 1
    global_lock.release()
def client_ssl(client_id):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    global g_send_count, g_client_num, g_done_client, server_addr, global_lock
    global data
    global round
    sock = ssl.wrap_socket(sock)
    sock.connect(server_addr)
#        times = random.randint(1, 5000)
#        time.sleep(times/ 2000.0)
    for i in xrange(0, round):
        send_all(sock, data)
        print "send", client_id, i, time.time()
        _data = recv_all(sock, len(data))
        if _data == data:
            global_lock.acquire()
            g_send_count += 1
            global_lock.release()
        else:
            print "client recv invalid data"
#            time.sleep(0.01)
    print "client done", client_id
    sock.close()
    global_lock.acquire()
    g_done_client += 1
    global_lock.release()
def client_pool(pool):
    global g_send_count, g_client_num, g_done_client, server_addr, global_lock
    global data
    global g_round
    conn = None
    try:
        for i in xrange(0, g_round):
            conn = pool.get_conn(server_addr)
            if conn == None:
                print "get_conn failed"
                return
            send_all(conn.sock, data)
            _data = recv_all(conn.sock, len(data))
            global_lock.acquire()
            g_send_count += 1
            global_lock.release()
            pool.put_back(conn)
        global_lock.acquire()
        g_done_client += 1
        global_lock.release()
        print "client done", g_done_client
    except Exception, e:
        getLogger("client").exception_ex("client: " + str(e))
        print "client: ", str(e)
        if conn:
            pool.put_back(conn, True)
            print "put_back conn , is_err=True"
        return
 def server_handler(conn):
     sock = conn.sock
     try:
         _data = recv_all(sock, len(data))
         send_all(sock, _data)
         server.watch_conn(conn)
     except Exception, e:
         print "server handler", str(e)
         getLogger("server").exception(str(e))
         server.close_conn(conn)
         return False