Skip to content
Snippets Groups Projects
Commit 6502f180 authored by Hang's avatar Hang
Browse files

fast push

parent adf4c8d4
No related branches found
No related tags found
1 merge request!10MP3 Log Version Ready
SERVER_IDENTIFIER = 0 SERVER_IDENTIFIER = 0
SERVER_ADDRESS = 1 SERVER_ADDRESS = 1
SERVER_PORT = 2 SERVER_PORT = 2
ACCEPTING_PEER_PORT = 3
RECONNECT_TIME = 1 # 1s RECONNECT_TIME = 1 # 1s
......
...@@ -4,7 +4,8 @@ import sys ...@@ -4,7 +4,8 @@ import sys
import time import time
import traceback import traceback
from utils import gprint, rprint, pprint 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 socket
import threading import threading
from classDef import WriteEntry, Account from classDef import WriteEntry, Account
...@@ -16,6 +17,7 @@ CURRENT_SERVER_ID = '' ...@@ -16,6 +17,7 @@ CURRENT_SERVER_ID = ''
CURRENT_SERVER_IP = socket.gethostbyname(socket.gethostname()) CURRENT_SERVER_IP = socket.gethostbyname(socket.gethostname())
SERVERS = {} SERVERS = {}
CURRENT_SERVER_PORT = -1 CURRENT_SERVER_PORT = -1
BASE_PORT_NUMBER = 12000
# !!! TW 要用于顺延候选的时候要进行排序,才行!!! 直接用sorted 对key 进行排序就好 # !!! TW 要用于顺延候选的时候要进行排序,才行!!! 直接用sorted 对key 进行排序就好
# 关于write_commited_timestamp 变成list 的改变,虽然我感觉不用改成list, 因为ta 的例子错了,但是吧改了之后全都加了sort, 效果应该差不多把 # 关于write_commited_timestamp 变成list 的改变,虽然我感觉不用改成list, 因为ta 的例子错了,但是吧改了之后全都加了sort, 效果应该差不多把
accounts_map = dict() accounts_map = dict()
...@@ -410,6 +412,7 @@ def abort(self_node: SelfNode, timestamp: str): ...@@ -410,6 +412,7 @@ def abort(self_node: SelfNode, timestamp: str):
# 链接部分 # 链接部分
def parse_config(config_path: str) -> dict: def parse_config(config_path: str) -> dict:
servers = {} servers = {}
designated_port_number = BASE_PORT_NUMBER
with open(config_path, 'r') as file: with open(config_path, 'r') as file:
for line in file: for line in file:
server_info = line.split(" ") server_info = line.split(" ")
...@@ -417,8 +420,10 @@ def parse_config(config_path: str) -> dict: ...@@ -417,8 +420,10 @@ def parse_config(config_path: str) -> dict:
servers[server_info[SERVER_IDENTIFIER]] = { servers[server_info[SERVER_IDENTIFIER]] = {
SERVER_IDENTIFIER: server_info[SERVER_IDENTIFIER], SERVER_IDENTIFIER: server_info[SERVER_IDENTIFIER],
SERVER_ADDRESS: server_info[SERVER_ADDRESS], 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 return servers
...@@ -454,12 +459,12 @@ def connect_with_all_servers(servers: dict, current_server_id: str, return_value ...@@ -454,12 +459,12 @@ def connect_with_all_servers(servers: dict, current_server_id: str, return_value
try: try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_ip_address = SERVER_ID_TO_IP_MAPPING[server[SERVER_IDENTIFIER]] host_ip_address = SERVER_ID_TO_IP_MAPPING[server[SERVER_IDENTIFIER]]
server[SERVER_PORT] = SERVERS[CURRENT_SERVER_ID][SERVER_PORT]+1 # todo: to be deleted # server[SERVER_PORT] = SERVERS[CURRENT_SERVER_ID][SERVER_PORT]+1 # todo: to be deleted
address = (host_ip_address, server[SERVER_PORT]) address = (host_ip_address, server[server_id][ACCEPTING_PEER_PORT])
s.connect(address) s.connect(address)
connect_info[server_id] = s connect_info[server_id] = s
gprint(f"[Connect] - " 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}:{server[server_id][ACCEPTING_PEER_PORT]} success!")
except Exception: except Exception:
rprint(f"[Connect] - Connection to " rprint(f"[Connect] - Connection to "
f"{server[SERVER_IDENTIFIER]} failed. " f"{server[SERVER_IDENTIFIER]} failed. "
...@@ -719,7 +724,7 @@ def accepting_clients(SELF_NODE): ...@@ -719,7 +724,7 @@ def accepting_clients(SELF_NODE):
def accepting_peer_servers(SELF_NODE): def accepting_peer_servers(SELF_NODE):
try: try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((CURRENT_SERVER_IP, SERVERS[CURRENT_SERVER_ID][SERVER_PORT]+1)) server.bind((CURRENT_SERVER_IP, SERVERS[CURRENT_SERVER_ID][ACCEPTING_PEER_PORT]))
server.listen() server.listen()
# print(f"[LISTENING] Server is listening on {ADDR}") # DEBUG # print(f"[LISTENING] Server is listening on {ADDR}") # DEBUG
while True: while True:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment