From ba8bab8a1720770c6d84f99220a5b5ca0d273406 Mon Sep 17 00:00:00 2001 From: ccyj <2384899431@qq.com> Date: Mon, 23 Oct 2023 21:39:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=8E=A5=E6=94=B6=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E5=8C=85=E5=87=BD=E6=95=B0=EF=BC=9B=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=8F=98=E9=87=8F=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client.py | 4 ++-- src/server.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/client.py b/src/client.py index 303f19d..59a91cd 100644 --- a/src/client.py +++ b/src/client.py @@ -213,7 +213,7 @@ class IP_Message(BaseModel): dest_ip: str message_name: str source_ip: str - pk: int + pk: Tuple[int, int] class Request_Message(BaseModel): @@ -305,7 +305,7 @@ async def recieve_request(i_m: IP_Message): message = b"hello world" + random.randbytes(8) # send message to nodes - await send_messages(node_ips, message, dest_ip, pk_B, threshold) # type: ignore + await send_messages(tuple(node_ips), message, dest_ip, pk_B, threshold) response = {"threshold": threshold, "public_key": own_public_key} return response diff --git a/src/server.py b/src/server.py index c4b8fa3..18d6214 100644 --- a/src/server.py +++ b/src/server.py @@ -29,6 +29,7 @@ def init(): asyncio.create_task(receive_heartbeat_internal()) def clean_env(): + clear_database() # 关闭游标和连接 cursor.close() conn.close() @@ -86,14 +87,14 @@ async def delete_node(ip: str) -> None: print(f"Node with IP {ip} not found.") # 接收节点心跳包 -@app.post("/server/heartbeat") +@app.get("/server/heartbeat") async def receive_heartbeat(ip: str): cursor.execute("UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip)) return {"status": "received"} async def receive_heartbeat_internal(): while 1: - timeout = 70 + timeout = 7 # 删除超时的节点 # cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)) # conn.commit() @@ -120,12 +121,12 @@ async def send_nodes_list(count: int) -> list: return nodes_list -@app.get("/server/clear_database") -async def clear_database() -> None: +# @app.get("/server/clear_database") +def clear_database() -> None: cursor.execute("DELETE FROM nodes") conn.commit() if __name__ == "__main__": import uvicorn # pylint: disable=e0401 - uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True) \ No newline at end of file + uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True) From 5fd39d3c204db04dd8c4ac38f619174fb1b59ebe Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Mon, 23 Oct 2023 22:20:52 +0800 Subject: [PATCH 2/3] feat: update requirements --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9551179..8b65c2a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ gmssl-python fastapi -uvicorn \ No newline at end of file +uvicorn +requests \ No newline at end of file From 2277cd577d0d31757d1c0431df52baa2349a3156 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Mon, 23 Oct 2023 22:21:15 +0800 Subject: [PATCH 3/3] test: temp change --- src/client.ini | 2 +- src/client.py | 6 +++--- src/server.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/client.ini b/src/client.ini index 0725142..23e21d2 100644 --- a/src/client.ini +++ b/src/client.ini @@ -1,3 +1,3 @@ [settings] -server_address = 10.20.127.226:8000 +server_address = 10.20.14.232:8000 version = 1.0 diff --git a/src/client.py b/src/client.py index 303f19d..8ba515b 100644 --- a/src/client.py +++ b/src/client.py @@ -29,7 +29,7 @@ def init(): # load config from config file init_config() - # get_node_list(6, server_address) # type: ignore + get_node_list(2, server_address) # type: ignore def init_db(): @@ -286,7 +286,7 @@ async def recieve_request(i_m: IP_Message): if source_ip != i_m.dest_ip: return HTTPException(status_code=400, detail="Wrong ip") dest_ip = i_m.source_ip - threshold = random.randrange(1, 6) + threshold = random.randrange(1, 2) own_public_key = pk pk_B = i_m.pk @@ -352,4 +352,4 @@ local_ip = get_own_ip() if __name__ == "__main__": import uvicorn # pylint: disable=e0401 - uvicorn.run("client:app", host="0.0.0.0", port=8003, reload=True) + uvicorn.run("client:app", host="0.0.0.0", port=8002, reload=True) diff --git a/src/server.py b/src/server.py index c4b8fa3..f3e0c47 100644 --- a/src/server.py +++ b/src/server.py @@ -86,7 +86,7 @@ async def delete_node(ip: str) -> None: print(f"Node with IP {ip} not found.") # 接收节点心跳包 -@app.post("/server/heartbeat") +@app.get("/server/heartbeat") async def receive_heartbeat(ip: str): cursor.execute("UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip)) return {"status": "received"}