class Client(orb.Peer):

    """Chat client class."""

    def __init__(self, local_address, ns_address, cient_type):
        """Initialize the client."""
        orb.Peer.__init__(self, local_address, ns_address, client_type)
        self.peer_list = PeerList(self)
        self.dispatched_calls = {
            "register_peer":     self.peer_list.register_peer,
            "unregister_peer":   self.peer_list.unregister_peer,
            "display_peers":     self.peer_list.display_peers
        }
        orb.Peer.start(self)
        self.peer_list.initialize()

    # Public methods

    def destroy(self):
        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 print_message(self, from_id, msg):
        print("Received a message from {}: {}".format(from_id, msg))

    def send_message(self, to_id, msg):
        try:
            self.peer_list.peer(to_id).print_message(self.id, msg)
        except Exception:
            print(("Cannot send messages to {}."
                   "Make sure it is in the list of peers.").format(to_id))
class Client(orb.Peer):

    """Chat client class."""

    def __init__(self, local_address, ns_address, cient_type):
        """Initialize the client."""
        orb.Peer.__init__(self, local_address, ns_address, client_type)
        self.peer_list = PeerList(self)
        self.dispatched_calls = {
            "register_peer":     self.peer_list.register_peer,
            "unregister_peer":   self.peer_list.unregister_peer,
            "display_peers":     self.peer_list.display_peers
        }
        orb.Peer.start(self)
        self.peer_list.initialize()

    # Public methods

    def destroy(self):
        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 print_message(self, from_id, msg):
        print("Received a message from {}: {}".format(from_id, msg))

    def send_message(self, to_id, msg):
        try:
            self.peer_list.peer(to_id).print_message(self.id, msg)
        except Exception:
            print(("Cannot send messages to {}."
                   "Make sure it is in the list of peers.").format(to_id))
예제 #3
0
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)
예제 #4
0
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,
            "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()

    # 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):
        print "read"
        self.drwlock.read_acquire()
        string = self.db.read()
        self.drwlock.read_release()
        return string

    def write(self, fortune):
        print "write"
        self.drwlock.write_acquire()
        self.db.write(fortune)

        pids = self.peer_list.peers.keys()
        pids.sort()
        for pid in pids:
            if pid != self.id:
                self.peer_list.peer(pid).write_no_lock(fortune)

        self.drwlock.write_release()

    def write_no_lock(self, fortune):
        print "write no lock"
        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."""

        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)
예제 #6
0
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

   

        #
        # Your code here.
        #


    # 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.
        #
        print("INTO SERVER READ")
        self.drwlock.read_acquire()
        self.distributed_lock.acquire()
        result = self.db.read()
        self.distributed_lock.release()
        self.drwlock.read_release()
        return result
        pass

    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.

       """
        
        self.drwlock.write_acquire()
       
        self.distributed_lock.acquire()
        to_write = '\n'+fortune+'\n%'
        self.db.write(to_write)
        temp = list(self.peer_list.peers.keys())
        for t_id in temp:
            if t_id != self.id:
                self.peer_list.peer(t_id).write_no_lock(to_write)
         
        
        self.distributed_lock.release()
        self.drwlock.write_release()
        pass

    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)

    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)