Skip to content
Snippets Groups Projects
Commit 9eba4ee0 authored by Dixon Tirtayadi's avatar Dixon Tirtayadi
Browse files

Added more server stuff

parent 11feb868
No related branches found
No related tags found
No related merge requests found
......@@ -12,17 +12,18 @@ class Paxos(socketserver.TCPServer):
# Sets up other variables
self.is_leader = False
self.ballot = BallotNumber(1, self.address)
self.proposals = map()
self.accepted = map()
# ...
# Default leader during setup
if self.address == servers[0]:
self.is_leader = True
socketserver.TCPServer.__init__(self, address, PaxosRequestHandler)
class PaxosRequestHandler(socketserver.BaseRequestHandler):
class PaxosClientRequestHandler(socketserver.BaseRequestHandler):
"""
The request handler class for our server.
......@@ -30,26 +31,58 @@ class PaxosRequestHandler(socketserver.BaseRequestHandler):
override the handle() method to implement communication to the
client.
This will receive lock() / unlock() command
This will receive lock() / unlock() command from client
Handler for proposals and leader election will be a different class (I think, probably using different port?)
"""
def handle(self):
# Temporary random things
# Use self.server.arg to get servers fields, i.e. self.server.isLeader
pass
# # self.request is the TCP socket connected to the client
# self.data = self.request.recv(1024).strip()
# print("{} wrote:".format(self.client_address[0]))
# print(self.data)
# # just send back the same data, but upper-cased
# self.request.sendall(self.data.upper())
if not self.server.isLeader:
return; # Drop if not the leader
# Note that we guarantee communication client to server is exactly once,
# no need to worry about duplicate request and proposing two slot.
data = self.request.recv(1024).strip() # DEBUG LOG
print("{} wrote:".format(self.client_address[0])) # DEBUG LOG
print(data) # DEBUG LOG
# TODO: Change data into a format known for lock or unlock
# Propose
prop = Proposal(BallotNumber(self.ballot().seq_num, self.address()), data)
proposals[slotIn, prop]
accepted[slotIn, prop]
# TODO: PaxosServer.java line 236-256
# Create proposal Message
# Send proposal
# Accept our own proposal
# Wait untl hear majority
# Send back result
self.request.sendall(data.upper()) # TESTING CODE
@total_ordering
clas Proposal:
def __init__(self, ballot, value) -> None:
self.ballot = ballot
self.value = value
def __lt__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return self.ballot < other.ballot
def __eq__(self, other) -> bool:
if not self._is_valid_operand(other):
return NotImplemented
return self.ballot == other.ballot
@total_ordering
class BallotNumber:
def __init__(self, seq_num, addr) -> None:
self.seq_num = seq_num
self.addr = addr
self.seq_num = seq_num # Sequence number of the proposal
self.addr = addr # Address of the server proposing
def increaseBallot(self):
self.seq_num += 1
......
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