-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
40 lines (30 loc) · 1.21 KB
/
server.py
File metadata and controls
40 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# python3 server.py localhost 3000
# python3 server.py localhost 3001 localhost 3000
import sys
from typing import Optional
from xmlrpc.server import SimpleXMLRPCServer
from lib.app import MessageQueue
from lib.raft import RaftNode
from lib.struct.address import Address
def start_serving(addr: Address, contact_node_addr: Optional[Address]):
print(f"Starting Raft Server at {addr.ip}:{addr.port}")
with SimpleXMLRPCServer((addr.ip, addr.port)) as server:
server.register_introspection_functions()
server.register_instance(RaftNode(MessageQueue(), addr, contact_node_addr))
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nKeyboard interrupt received, exiting.")
server.shutdown()
sys.exit(0)
if __name__ == "__main__":
if len(sys.argv) < 3:
# contact node is leader node
print(sys.argv)
print("server.py <ip> <port> <opt: contact ip> <opt: contact port>")
exit()
contact_addr = None
if len(sys.argv) == 5:
contact_addr = Address(sys.argv[3], int(sys.argv[4]))
server_addr = Address(sys.argv[1], int(sys.argv[2]))
start_serving(server_addr, contact_addr)