diff --git a/mp3/constants.py b/mp3/constants.py index 6d1bbff8e12d67bdf3b56e0f147fdbc432847415..bf0fce42c2f7e4dc22b1042e3f2f662a94aa5d6f 100644 --- a/mp3/constants.py +++ b/mp3/constants.py @@ -1,6 +1,7 @@ SERVER_IDENTIFIER = 0 SERVER_ADDRESS = 1 SERVER_PORT = 2 +ACCEPTING_PEER_PORT = 3 RECONNECT_TIME = 1 # 1s diff --git a/mp3/server.py b/mp3/server.py index 34c0882f1b43dd12e57bfb1aabdb1d5ff686f855..e0b665f6f9b38d1510e7a34593282b933eb5869e 100644 --- a/mp3/server.py +++ b/mp3/server.py @@ -4,7 +4,8 @@ import sys import time import traceback from utils import gprint, rprint, pprint -from constants import SERVER_IDENTIFIER, SERVER_ADDRESS, SERVER_PORT, RECONNECT_TIME, MSG_LENGTH, ENCODING_FORMAT +from constants import SERVER_IDENTIFIER, SERVER_ADDRESS, SERVER_PORT, RECONNECT_TIME, MSG_LENGTH, ENCODING_FORMAT, \ + ACCEPTING_PEER_PORT import socket import threading from classDef import WriteEntry, Account @@ -16,6 +17,7 @@ CURRENT_SERVER_ID = '' CURRENT_SERVER_IP = socket.gethostbyname(socket.gethostname()) SERVERS = {} CURRENT_SERVER_PORT = -1 +BASE_PORT_NUMBER = 12000 # !!! TW è¦ç”¨äºŽé¡ºå»¶å€™é€‰çš„æ—¶å€™è¦è¿›è¡ŒæŽ’åºï¼Œæ‰è¡Œï¼ï¼ï¼ 直接用sorted 对key 进行排åºå°±å¥½ # 关于write_commited_timestamp å˜æˆlist 的改å˜ï¼Œè™½ç„¶æˆ‘感觉ä¸ç”¨æ”¹æˆlist, å› ä¸ºta 的例åé”™äº†ï¼Œä½†æ˜¯å§æ”¹äº†ä¹‹åŽå…¨éƒ½åŠ äº†sort, 效果应该差ä¸å¤šæŠŠ accounts_map = dict() @@ -410,6 +412,7 @@ def abort(self_node: SelfNode, timestamp: str): # 链接部分 def parse_config(config_path: str) -> dict: servers = {} + designated_port_number = BASE_PORT_NUMBER with open(config_path, 'r') as file: for line in file: server_info = line.split(" ") @@ -417,8 +420,10 @@ def parse_config(config_path: str) -> dict: servers[server_info[SERVER_IDENTIFIER]] = { SERVER_IDENTIFIER: server_info[SERVER_IDENTIFIER], SERVER_ADDRESS: server_info[SERVER_ADDRESS], - SERVER_PORT: int(server_info[SERVER_PORT].rstrip()) + SERVER_PORT: int(server_info[SERVER_PORT].rstrip()), + ACCEPTING_PEER_PORT: designated_port_number } + designated_port_number += 100 return servers @@ -454,12 +459,13 @@ def connect_with_all_servers(servers: dict, current_server_id: str, return_value try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host_ip_address = SERVER_ID_TO_IP_MAPPING[server[SERVER_IDENTIFIER]] - server[SERVER_PORT] = 55555 # todo: to be deleted - address = (host_ip_address, server[SERVER_PORT]) + # server[SERVER_PORT] = SERVERS[CURRENT_SERVER_ID][SERVER_PORT]+1 # todo: to be deleted + address = (host_ip_address, server[server_id][ACCEPTING_PEER_PORT]) s.connect(address) connect_info[server_id] = s gprint(f"[Connect] - " - f"Connecting to {server[SERVER_IDENTIFIER]}: {host_ip_address}:{server[SERVER_PORT]} success!") + f"Connecting to {server[SERVER_IDENTIFIER]}: {host_ip_address}:" + f"{server[server_id][ACCEPTING_PEER_PORT]} success!") except Exception: rprint(f"[Connect] - Connection to " f"{server[SERVER_IDENTIFIER]} failed. " @@ -719,7 +725,7 @@ def accepting_clients(SELF_NODE): def accepting_peer_servers(SELF_NODE): try: server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - server.bind((CURRENT_SERVER_IP, 55555)) + server.bind((CURRENT_SERVER_IP, SERVERS[CURRENT_SERVER_ID][ACCEPTING_PEER_PORT])) server.listen() # print(f"[LISTENING] Server is listening on {ADDR}") # DEBUG while True: