def __init__(self, local_address, ns_address, client_type): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, client_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.dispatched_calls = { "display_peers": self.peer_list.display_peers, "acquire": self.distributed_lock.acquire, "release": self.distributed_lock.release, "request_token": self.distributed_lock.request_token, "obtain_token": self.distributed_lock.obtain_token, "display_status": self.distributed_lock.display_status } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize()
def __init__(self, local_address, ns_address, server_type, db_file): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, server_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.drwlock = DistributedReadWriteLock(self.distributed_lock) self.db = database.Database(db_file) self.dispatched_calls = { "display_peers": self.peer_list.display_peers, "acquire": self.distributed_lock.acquire, "release": self.distributed_lock.release, "request_token": self.distributed_lock.request_token, "obtain_token": self.distributed_lock.obtain_token, "display_status": self.distributed_lock.display_status, "write_acquire_local": self.drwlock.write_acquire_local, "write_release_local": self.drwlock.write_release_local } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize()
class Client(orb.Peer): """Distributed mutual exclusion client class.""" def __init__(self, local_address, ns_address, client_type): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, client_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.dispatched_calls = { "display_peers": self.peer_list.display_peers, "acquire": self.distributed_lock.acquire, "release": self.distributed_lock.release, "request_token": self.distributed_lock.request_token, "obtain_token": self.distributed_lock.obtain_token, "display_status": self.distributed_lock.display_status } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize() # Public methods def destroy(self): # Destroy the lock first to allow the token to be passed if we have it self.distributed_lock.destroy() orb.Peer.destroy(self) self.peer_list.destroy() def __getattr__(self, attr): """Forward calls are dispatched here.""" if attr in self.dispatched_calls: return self.dispatched_calls[attr] else: raise AttributeError( "Client instance has no attribute '{}'".format(attr)) def register_peer(self, pid, paddr): self.peer_list.register_peer(pid, paddr) self.distributed_lock.register_peer(pid) def unregister_peer(self, pid): self.peer_list.unregister_peer(pid) self.distributed_lock.unregister_peer(pid)
class Client(orb.Peer): """Distributed mutual exclusion client class.""" def __init__(self, local_address, ns_address, client_type): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, client_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.dispatched_calls = { "display_peers": self.peer_list.display_peers, "acquire": self.distributed_lock.acquire, "release": self.distributed_lock.release, "request_token": self.distributed_lock.request_token, "obtain_token": self.distributed_lock.obtain_token, "display_status": self.distributed_lock.display_status } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize() # Public methods def destroy(self): orb.Peer.destroy(self) self.distributed_lock.destroy() self.peer_list.destroy() def __getattr__(self, attr): """Forward calls are dispatched here.""" if attr in self.dispatched_calls: return self.dispatched_calls[attr] else: raise AttributeError( "Client instance has no attribute '{}'".format(attr)) def register_peer(self, pid, paddr): self.peer_list.register_peer(pid, paddr) self.distributed_lock.register_peer(pid) def unregister_peer(self, pid): self.peer_list.unregister_peer(pid) self.distributed_lock.unregister_peer(pid)
def __init__(self, local_address, ns_address, server_type, db_file): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, server_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.drwlock = DistributedReadWriteLock(self.distributed_lock) self.db = database.Database(db_file) self.dispatched_calls = { "display_peers": self.peer_list.display_peers, "acquire": self.distributed_lock.acquire, "release": self.distributed_lock.release, "request_token": self.distributed_lock.request_token, "obtain_token": self.distributed_lock.obtain_token, "obtain_token_no_lock": self.distributed_lock.obtain_token_no_lock, "display_status": self.distributed_lock.display_status, } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize()
class Server(orb.Peer): """Distributed mutual exclusion client class.""" def __init__(self, local_address, ns_address, server_type, db_file): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, server_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.drwlock = DistributedReadWriteLock(self.distributed_lock) self.db = database.Database(db_file) self.dispatched_calls = { "display_peers": self.peer_list.display_peers, "acquire": self.distributed_lock.acquire, "release": self.distributed_lock.release, "request_token": self.distributed_lock.request_token, "obtain_token": self.distributed_lock.obtain_token, "display_status": self.distributed_lock.display_status } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize() # Public methods def destroy(self): orb.Peer.destroy(self) self.distributed_lock.destroy() self.peer_list.destroy() def __getattr__(self, attr): """Forward calls are dispatched here.""" if attr in self.dispatched_calls: return self.dispatched_calls[attr] else: raise AttributeError( "Client instance has no attribute '{0}'".format(attr)) # Public methods def read(self): """Read a fortune from the database.""" self.drwlock.read_acquire() try: return self.db.read() finally: self.drwlock.read_release() def write(self, fortune): """Write a fortune to the database. Obtain the distributed lock and call all other servers to write the fortune as well. Call their 'write_local' as they cannot atempt to obtain the distributed lock when writting their copies. """ self.drwlock.write_acquire() self.db.write(fortune) # write the fortune locally # distribute the update to all peers = self.peer_list.get_peers().keys() for peer in peers: self.peer_list.peer(peer).write_local(fortune) self.drwlock.write_release() def write_local(self, fortune): """Write a fortune to the database. This method is called only by other servers once they've obtained the distributed lock. """ self.drwlock.write_acquire_local() try: self.db.write(fortune) finally: self.drwlock.write_release_local() def register_peer(self, pid, paddr): """Register a server peer in this server's peer list.""" self.peer_list.register_peer(pid, paddr) self.distributed_lock.register_peer(pid) def unregister_peer(self, pid): """Remove a server peer from this server's peer list.""" self.peer_list.unregister_peer(pid) self.distributed_lock.unregister_peer(pid)
class Server(orb.Peer): """Distributed mutual exclusion client class.""" def __init__(self, local_address, ns_address, server_type, db_file): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, server_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.drwlock = DistributedReadWriteLock(self.distributed_lock) self.db = database.Database(db_file) self.dispatched_calls = { "display_peers" : self.peer_list.display_peers, "acquire" : self.distributed_lock.acquire, "release" : self.distributed_lock.release, "request_token" : self.distributed_lock.request_token, "obtain_token" : self.distributed_lock.obtain_token, "display_status" : self.distributed_lock.display_status } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize() # Public methods def destroy(self): orb.Peer.destroy(self) self.distributed_lock.destroy() self.peer_list.destroy() def __getattr__(self, attr): """Forward calls are dispatched here.""" if attr in self.dispatched_calls: return self.dispatched_calls[attr] else: raise AttributeError( "Client instance has no attribute '{0}'".format(attr)) # Public methods def read(self): self.drwlock.read_acquire() fortune = self.db.read() self.drwlock.read_release() return fortune def write(self, fortune): self.drwlock.write_acquire() for peer in self.peer_list.peers.items(): peer.write(fortune) self.drwlock.write_release() def write_no_lock(self, fortune): self.db.write(fortune) def register_peer(self, pid, paddr): self.peer_list.register_peer(pid, paddr) self.distributed_lock.register_peer(pid) def unregister_peer(self, pid): self.peer_list.unregister_peer(pid) self.distributed_lock.unregister_peer(pid)
class Server(orb.Peer): """Distributed mutual exclusion client class.""" def __init__(self, local_address, ns_address, server_type, db_file): """Initialize the client.""" orb.Peer.__init__(self, local_address, ns_address, server_type) self.peer_list = PeerList(self) self.distributed_lock = DistributedLock(self, self.peer_list) self.drwlock = DistributedReadWriteLock(self.distributed_lock) self.db = database.Database(db_file) self.dispatched_calls = { "display_peers": self.peer_list.display_peers, "acquire": self.distributed_lock.acquire, "release": self.distributed_lock.release, "request_token": self.distributed_lock.request_token, "obtain_token": self.distributed_lock.obtain_token, "display_status": self.distributed_lock.display_status } orb.Peer.start(self) self.peer_list.initialize() self.distributed_lock.initialize() # Public methods def destroy(self): orb.Peer.destroy(self) self.distributed_lock.destroy() self.peer_list.destroy() def __getattr__(self, attr): """Forward calls are dispatched here.""" if attr in self.dispatched_calls: return self.dispatched_calls[attr] else: raise AttributeError( "Client instance has no attribute '{0}'".format(attr)) # Public methods def read(self): """Read a fortune from the database.""" # # Your code here. # self.drwlock.read_acquire() try: read = self.db.read() finally: self.drwlock.read_release() return read def write(self, fortune): """Write a fortune to the database. Obtain the distributed lock and call all other servers to write the fortune as well. Call their 'write_no_lock' as they cannot atempt to obtain the distributed lock when writting their copies. """ # # Your code here. # peers = self.peer_list.get_peers() self.drwlock.write_acquire() try: # self.peer_list.lock.acquire() print("Wrote:\n" + fortune) self.db.write(fortune) for peer in peers.values(): # if (not (pid == self.id)): peer.write_no_lock(fortune) finally: # self.peer_list.lock.acquire() self.drwlock.write_release() def write_no_lock(self, fortune): """Write a fortune to the database. This method is called only by other servers onces they've obtained the distributed lock. """ self.db.write(fortune) print("Wrote with no lock:\n" + fortune) def register_peer(self, pid, paddr): """Register a server peer in this server's peer list.""" self.peer_list.register_peer(pid, paddr) self.distributed_lock.register_peer(pid) def unregister_peer(self, pid): """Remove a server peer from this server's peer list.""" self.peer_list.unregister_peer(pid) self.distributed_lock.unregister_peer(pid)