class NetworkManager:
    """high level class managing clients and servers for each peer"""

    def __init__(self):
        self.client = ProfileClientFactory(self)
        self.server = ProfileServerFactory(self)
        self.peers = PeerManager(self.client.connect)

    def start(self):
        self.peers.start()
        return self.server.start_listening()

    def stop(self):
        self.server.stop_listening()
        self.peers.stop()

    # calls from plugin ##############################################
    def on_new_peer(self, peer):
        """tries to connect to new peer"""
        self.peers.add_peer(peer.id_)
        self.server.send_udp_message(peer.id_, MESSAGE_HELLO)

    def on_change_peer(self, peer, service):
        """tries to connect to new peer"""
        if self.peers.remote_ids.has_key(peer.id_):
            self.peers.remote_ids[peer.id_].lose()
        else:
            log("on_change_peer. Unknow", peer.id_)
        self.on_new_peer(peer)

    def on_lost_peer(self, peer_id):
        """tries to connect to new peer"""
        if self.peers.remote_ids.has_key(peer_id):
            self.peers.remote_ids[peer_id].lose()
        else:
            log("on_lost_peer. Unknow", peer_id)

    def on_service_data(self, peer_id, message):
        """demand to establish connection from peer that failed to
        connect through TCP"""
        if not self.peers.remote_ids.has_key(peer_id):
            log("on_service_data. Unknow", peer_id, message)
            self.peers.add_peer(peer_id)
        else:
            log("on_service_data", peer_id, message)
            try:
                message = Message.create_message(message)
                if message.command != MESSAGE_HELLO:
                    SecurityAlert(peer_id,
                                  "unvalid message '%s' from %s"\
                                  % (str(message), peer_id))
                else:
                    self.peers.set_peer(peer_id, message)
            except ValueError, err:
                SecurityAlert(peer_id, str(err))
 def setUp(self):
     # create manager with small timeout (not to slow down test)
     self.manager = PeerManager(None)
     self.manager.CHECKING_FREQUENCY = 0.2
     self.manager.start()
     # create default message
     self.message =  Message.create_message(
         "HELLO 127.0.0.1:23501 data youpi")
 def __init__(self):
     self.client = ProfileClientFactory(self)
     self.server = ProfileServerFactory(self)
     self.peers = PeerManager(self.client.connect)
class PeerManagerTest(BaseTest):

    def setUp(self):
        # create manager with small timeout (not to slow down test)
        self.manager = PeerManager(None)
        self.manager.CHECKING_FREQUENCY = 0.2
        self.manager.start()
        # create default message
        self.message =  Message.create_message(
            "HELLO 127.0.0.1:23501 data youpi")

    def tearDown(self):
        # stop manager
        self.manager.stop()

    def test_add_manager(self):
        self.assert_no_peer()
        self.manager.add_peer("toto")
        self.manager.set_peer("toto", self.message)
        self.assert_peer()
        self.assertEquals(self.manager.remote_ids["toto"],
                          self.manager.remote_ips["127.0.0.1"])

    def test_del_manager(self):
        self.manager.add_peer("toto")
        self.manager.set_peer("toto", self.message)
        time.sleep(0.1)
        self.manager._del_peer("toto")
        self.assert_no_peer()

    def test_lose_manager(self):
        # add peer
        self.manager.add_peer("toto")
        self.manager.set_peer("toto", self.message)
        # reduce timeout in order not to slow down test
        self.manager.remote_ids["toto"].PEER_TIMEOUT = 0.5
        # loose peer. it should be deleted after PEER_TIMEOUT
        self.manager.remote_ids["toto"].lose()
        self.assert_peer_lost()
        time.sleep(1)
        self.assert_no_peer()