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._del_peer(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()

    def on_service_data(self, peer_id, message):
        """demand to establish connection from peer that failed to
        connect through TCP"""
        if self.peers.assert_id(peer_id):
            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 __init__(self):
     self.client = ProfileClientFactory(self)
     self.server = ProfileServerFactory(self)
     self.peers = PeerManager(self.client.connect)