def initialize(self): """Populates the list of existing peers and registers the current peer at each of the discovered peers. It only adds the peers with lower ids than this one or else deadlocks may occur. This method must be called after the owner object has been registered with the name service. """ self.lock.acquire() try: # # Your code here. # connected_peers = self.owner.name_service.require_all( self.owner.type) self.peers[self.owner.id] = orb.Stub(self.owner.address) for pid, peer_addr in connected_peers: if pid < self.owner.id: self.peers[pid] = orb.Stub(peer_addr) peer = self.peers[pid] try: peer.register_peer(self.owner.id, self.owner.address) except: continue finally: self.lock.release()
def initialize(self): """Populates the list of existing peers and registers the current peer at each of the discovered peers. It only adds the peers with lower ids than this one or else deadlocks may occur. This method must be called after the owner object has been registered with the name service. """ self.lock.acquire() try: for pid, paddr in self.owner.name_service.require_all(objectType.object_type): if pid == self.owner.id: self.peers[pid] = orb.Stub(paddr) if pid < self.owner.id: try: new_peer = orb.Stub(paddr) new_peer.register_peer(self.owner.id, self.owner.address) self.peers[pid] = new_peer print("Connected to peer " + str(pid)) except: print("Couldn't connect to peer " + str(pid)) finally: self.lock.release()
def initialize(self): """Populates the list of existing peers and registers the current peer at each of the discovered peers. It only adds the peers with lower ids than this one or else deadlocks may occur. This method must be called after the owner object has been registered with the name service. """ self.lock.acquire() try: peers_to_register_at = self.owner.name_service.require_all(self.owner.type) for peer_id, peer_address in peers_to_register_at: if peer_id >= self.owner.id: continue self.peers[peer_id] = orb.Stub(peer_address) for peer_id, peer_address in self.peers.items(): peer = self.peers[peer_id] try: peer.register_peer(self.owner.id, self.owner.address) except Exception as e: del self.peers[peer_id] finally: self.lock.release()
def register_peer(self, pid, paddr): """Register a new peer joining the network.""" # Synchronize access to the peer list as several peers might call # this method in parallel. self.lock.acquire() try: self.peers[pid] = orb.Stub(paddr) print("Peer {} has joined the system.".format(pid)) finally: self.lock.release()
def register_peer(self, pid, paddr, doubleChecking=True): """Register a new peer joining the network.""" if doubleChecking: self.check_all_alive() # Synchronize access to the peer list as several peers might call # this method in parallel. self.lock.acquire() try: self.peers[pid] = orb.Stub(paddr) finally: self.lock.release()
def initialize(self): """Populates the list of existing peers and registers the current peer at each of the discovered peers. It only adds the peers with lower ids than this one or else deadlocks may occur. This method must be called after the owner object has been registered with the name service. """ self.lock.acquire() try: # Ask for the peer list from the name service. print("requesting peer list") rep = self.owner.name_service.require_all(self.owner.type) # Create a stub object for each peer listed by the nameservice. for pid, paddr in rep: try: # Create the stub object. stub = orb.Stub(paddr) # Only create a stub object for this peer if it's not us. # And if its pid is lower, if it is higher it probably means # that we will receive a registration message and add it two times # This probably needs rethinking to avoid relying on incremental ids. if pid < self.owner.id: # Send the registration message. stub.register_peer(self.owner.id, self.owner.address) # Only add it to our list if we could be registered by this peer. # or if it's us self.peers[pid] = stub except: # One peer failing to be registered should not crash the whole program. # So we just do without him if we can't reach him. pass finally: self.lock.release()
parser.add_argument( "-p", "--peer", metavar="PEER_ID", dest="peer_id", type=int, help="The identifier of a particular server peer." ) opts = parser.parse_args() server_type = opts.type server_id = opts.peer_id assert server_type != "object", "Change the object type to something unique!" # ----------------------------------------------------------------------------- # The main program # ----------------------------------------------------------------------------- # Connect to the name service to obtain the address of the server. ns = orb.Stub(name_service_address) if server_id is None: server_address = tuple(ns.require_any(server_type)) else: server_address = tuple(ns.require_object(server_type, server_id)) print("Connecting to server: {}".format(server_address)) # Create the database object. db = orb.Stub(server_address) class TheRoof(Exception): pass if not opts.interactive: # Run in the normal mode.