feat: get_node_ip测试完毕

This commit is contained in:
dqy 2023-10-23 16:41:48 +08:00
parent 9efc8e2c7b
commit 5ea41956fd
3 changed files with 20 additions and 18 deletions

View File

@ -1,3 +1,3 @@
[settings] [settings]
server_address = "127.0.0.1:8000" server_address = 10.20.127.226:8000
version = 1.0 version = 1.0

View File

@ -25,7 +25,7 @@ def init():
init_db() init_db()
pk, sk = GenerateKeyPair() pk, sk = GenerateKeyPair()
init_config() init_config()
# get_node_list(6, server_address) # type: ignore get_node_list(6, server_address) # type: ignore
def init_db(): def init_db():
@ -277,22 +277,25 @@ def get_own_ip() -> str:
# get node list from central server # get node list from central server
def get_node_list(count: int, server_addr: str): def get_node_list(count: int, server_addr: str):
url = "http://" + server_addr + "/server/send_nodes_list" url = "http://" + server_addr + "/server/send_nodes_list?count=" + str(count)
payload = {"count": count} # payload = {"count": count}
response = requests.post(url, json=payload) # response = requests.post(url, json=payload)
response = requests.get(url)
# Checking the response # Checking the response
if response.status_code == 200: if response.status_code == 200:
print("Success get node list") print("Success get node list")
node_ip = response.text node_ip = response.text
node_ip = eval(node_ip)
print(node_ip)
# insert node ip to database # insert node ip to database
with sqlite3.connect("client.db") as db: with sqlite3.connect("client.db") as db:
db.executemany( db.executemany(
""" """
INSERT INTO node INSERT INTO node
nodeip (nodeip)
VALUE (?) VALUES (?)
""", """,
node_ip, [(ip,) for ip in node_ip],
) )
db.commit() db.commit()
print("Success add node ip") print("Success add node ip")
@ -311,4 +314,4 @@ local_ip = get_own_ip()
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn # pylint: disable=e0401 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=8003, reload=True)

View File

@ -95,20 +95,20 @@ async def receive_heartbeat_internal():
while 1: while 1:
timeout = 70 timeout = 70
# 删除超时的节点 # 删除超时的节点
cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)) # cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,))
conn.commit() # conn.commit()
await asyncio.sleep(timeout) await asyncio.sleep(timeout)
@app.get("/server/send_nodes_list") @app.get("/server/send_nodes_list")
async def send_nodes_list(count: int) -> JSONResponse: async def send_nodes_list(count: int) -> list:
''' '''
中心服务器与客户端交互, 客户端发送所需节点个数, 中心服务器从数据库中顺序取出节点封装成json格式返回给客户端 中心服务器与客户端交互, 客户端发送所需节点个数, 中心服务器从数据库中顺序取出节点封装成list格式返回给客户端
params: params:
count: 所需节点个数 count: 所需节点个数
return: return:
JSONResponse: {id: ip,...} nodes_list: list
''' '''
nodes_list = {} nodes_list = []
# 查询数据库中的节点数据 # 查询数据库中的节点数据
cursor.execute("SELECT * FROM nodes LIMIT ?", (count,)) cursor.execute("SELECT * FROM nodes LIMIT ?", (count,))
@ -116,10 +116,9 @@ async def send_nodes_list(count: int) -> JSONResponse:
for row in rows: for row in rows:
id, ip, last_heartbeat = row id, ip, last_heartbeat = row
nodes_list[id] = ip nodes_list.append(ip)
json_result = jsonable_encoder(nodes_list) return nodes_list
return JSONResponse(content=json_result)
@app.get("/server/clear_database") @app.get("/server/clear_database")
async def clear_database() -> None: async def clear_database() -> None: