Skip to content
Snippets Groups Projects
Commit 142589c9 authored by WinJ's avatar WinJ
Browse files

add config files

parent e6913fd4
No related branches found
No related tags found
No related merge requests found
......@@ -27,8 +27,16 @@ Write-up
Communication is reliable, no atmost-once or atleast-once issues
- How to compile, deploy and run your implementation
Spawn the paxos group (replace n with number of nodes): python3 main.py n
Start the client and execute commands: python3 client.py localhost 9000
Config file to indicate the addresses of the paxos group: config.json
Spawn the paxos group based on the config file:
python3 main.py
Start the client and send commands to a random paxos server based on the config file:
python3 client.py
lock 1
lock 2
unlock 1
...
exit
- Any issues or scenarios explicitly not considered
If a client send a message and it gets dropped, we assume it was lost and won't be executed as clients won't resend messages
......
import json
import pickle
import random
import socket
import sys
......@@ -6,27 +8,32 @@ from message import * # Can change this after testing done
if __name__ == "__main__":
# create a UDP socket
HOST, PORT = "localhost", 8000
# TODO: Uncomment below for multiple nodes paxos
with open("config.json", encoding='utf-8') as f:
json_dict = json.loads(f.read())
server_addresses = json_dict["servers"]
server_addresses = [(server_addresses[i], int(server_addresses[i + 1])) for i in range(0, len(server_addresses), 2)]
serv_addresses = ((HOST, 8000), (HOST, 8001))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('localhost', 0))
address = sock.getsockname()
print("Starting client {0}".format(address), file=sys.stderr)
input_str = input("Enter a command (e.g. lock 2 / unlock 2 / exit): ") # Input would be "lock 2" / "unlock 1", etc or "exit"
sock.bind((sys.argv[1], int(sys.argv[2])))
client_addr = sock.getsockname()
print(f"Starting client {client_addr}", file=sys.stderr)
prompt_str = "Enter a command (e.g. lock 2 / unlock 2 / exit): "
input_str = input(prompt_str) # Input would be "lock 2" / "unlock 1", etc or "exit"
try:
while input_str != "exit":
lock, val = input_str.split(" ", 2)
paxos_req = PaxosRequest(address, LockCommand(lock, val))
paxos_req = PaxosRequest(client_addr, LockCommand(lock, val))
data = pickle.dumps(paxos_req)
print(f"Sending lock command {paxos_req}", file=sys.stderr)
# send request to server
for serv_addr in serv_addresses:
sock.sendto(data, serv_addr)
random.seed(4) # TODO: comment to do randomness
server_address = random.choice(server_addresses)
print(f"Sending command {paxos_req} to {server_address}", file=sys.stderr)
# send request to random server
sock.sendto(data, server_address)
# receive data back from the server
res = pickle.loads(sock.recv(1024))
......@@ -38,7 +45,7 @@ if __name__ == "__main__":
else:
print("Command failed to run", file=sys.stderr)
input_str = input("\nEnter a command: ")
input_str = input(prompt_str)
finally:
# Close socket
sock.close()
......@@ -9,18 +9,19 @@ if __name__ == "__main__":
with open("config.json", encoding='utf-8') as f:
json_dict = json.loads(f.read())
addresses = json_dict["servers"]
try:
for i in range(0, len(addresses), 2):
paxos_host = addresses[i]
paxos_port = addresses[i + 1]
proc = subprocess.Popen(["python3", "paxos.py", paxos_host, paxos_port] + addresses)
processes.append(proc)
# Needed to sleep forever here until ctrl-c so we go into "finally" and kill processes
while True:
time.sleep(10)
finally:
for proc in processes:
# proc.send_signal(signal.SIGINT)
proc.kill()
\ No newline at end of file
addresses = json_dict["servers"]
try:
for i in range(0, len(addresses), 2):
paxos_host = addresses[i]
paxos_port = addresses[i + 1]
proc = subprocess.Popen(["python3", "paxos.py", paxos_host, paxos_port] + addresses)
processes.append(proc)
# Needed to sleep forever here until ctrl-c so we go into "finally" and kill processes
while True:
time.sleep(10)
finally:
for proc in processes:
# proc.send_signal(signal.SIGINT)
proc.kill()
\ No newline at end of file
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