Esempio n. 1
0
 def UpdatePeer(self, peer):
     """
     Called when a peer has changed.
     """
     peer_id = peer.id_
     old_peer = self.peers[peer_id]
     self.peers[peer_id] = peer
     # Compare the peer's old services to its new ones...
     new_ids = set([_service.id_ for _service in peer.GetServices()])
     old_ids = set([_service.id_ for _service in old_peer.GetServices()])
     # Notify removed services
     for service_id in old_ids - new_ids:
         try:
             plugin = self.plugins[service_id]
         except KeyError:
             pass
         else:
             plugin.LostPeer(peer_id)
     # Notify added services
     for service_id in new_ids - old_ids:
         try:
             plugin = self.plugins[service_id]
         except KeyError:
             pass
         else:
             plugin.NewPeer(peer, peer.GetService(service_id))
     # Notify updated services
     for service_id in new_ids & old_ids:
         try:
             plugin = self.plugins[service_id]
         except KeyError:
             pass
         else:
             plugin.ChangedPeer(peer, peer.GetService(service_id))
    def Merge(self, other):
        """
        Merges the other history store into this one.
        The other history store takes precedence when there is a conflict.
        """
        this_peers = set(self.entries)
        other_peers = set(other.entries)
        for peer_id in other_peers - this_peers:
#             print "new", peer_id
            self.entries[peer_id] = other.entries[peer_id].Copy()
        for peer_id in other_peers & this_peers:
#             print "merge", peer_id
            self.entries[peer_id].Merge(other.entries[peer_id])
Esempio n. 3
0
 def StripMessage(self, message, version=None):
     """
     Strip unnecessary parameters from message.
     """
     version = version or message.version
     try:
         _req = REQUESTS[version][message.request]
     except KeyError:
         raise EventParsingError("Unknown request '%s' in version '%s'" % (message.request, str(version)))
     required_args = set([ATTRIBUTE_NAMES[arg_id] for arg_id in _req])
     args = message.args
     for k in set(args.__dict__) - required_args:
         delattr(args, k)
Esempio n. 4
0
    def __init__(self):
        self.oldseen = {}
        self.seen = {}
        self.unknown = {}
        self.seenids = {}

        self.dispatch = {
            types.ModuleType: self.rlen_module,
            dict: self.rlen_dict,
            list: self.rlen_seq,
            tuple: self.rlen_seq,
            set: self.rlen_seq,
            # frozenset and deque do not exist in Python 2.3
            #frozenset: self.rlen_seq,
            #deque: self.rlen_seq,
        }
        self.ignores = set()
        self.ignores.add(weakref.ProxyType)
        try:
            import zope.interface.interface as i
            import zope.interface.declarations as d
            import twisted.python.components as c
        except ImportError:
            pass
        else:
            for mod in (i, d, c):
                for k in dir(mod):
                    v = getattr(mod, k)
                    if isinstance(v, type):
                        self.ignores.add(v)
    def __init__(self, app, world, config_data, menu, *args, **kwds):
        self.app = app
        self.world = world
        self.config_data = config_data
        self.menu = menu

        # Item index => peer
        self.item_map = {}
        # Peer IDs
        self.selected_items = set()
        # Menu items
        self.menu_items = []

        # begin wxGlade: BookmarksDialog.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_1 = wx.Panel(self, -1)

        # Tool Bar
        self.toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.TB_TEXT|wx.TB_HORZ_LAYOUT|wx.TB_HORZ_TEXT)
        self.SetToolBar(self.toolbar)
        self.toolbar.AddLabelTool(TOOL_ADD_BOOKMARK, _("Add bookmark"), (TB(wx.ART_ADD_BOOKMARK)), wx.NullBitmap, wx.ITEM_NORMAL, _("Remember one of your current neighbours"), "")
        self.toolbar.AddLabelTool(TOOL_DEL_BOOKMARK, _("Remove"), (TB(wx.ART_DEL_BOOKMARK)), wx.NullBitmap, wx.ITEM_NORMAL, _("Remove selected bookmark"), "")
        self.toolbar.AddLabelTool(TOOL_PROPERTIES, _("Information"), (TB(wx.ART_FILE_OPEN)), wx.NullBitmap, wx.ITEM_NORMAL, _("Get bookmark information"), "")
        # Tool Bar end
        self.list_ctrl = wx.ListCtrl(self.panel_1, -1, style=wx.LC_REPORT|wx.LC_VRULES|wx.SUNKEN_BORDER)
        self.button_close = wx.Button(self.panel_1, wx.ID_CLOSE, "")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_TOOL, self.OnAddBookmark, id=TOOL_ADD_BOOKMARK)
        self.Bind(wx.EVT_TOOL, self.OnDelBookmark, id=TOOL_DEL_BOOKMARK)
        self.Bind(wx.EVT_TOOL, self.OnProperties, id=TOOL_PROPERTIES)
        self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnDeselectItem, self.list_ctrl)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectItem, self.list_ctrl)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnActivateItem, self.list_ctrl)
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=wx.ID_CLOSE)
        # end wxGlade
        self.Bind(wx.EVT_SHOW, self.OnShow)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

        # Calculate colour for URLs, by dimming
        # between default text and background colours
        # (note: doesn't work on GTK and Aqua backends)
        rgb_front = self.GetForegroundColour().Get()
        rgb_back = self.GetBackgroundColour().Get()
        c = [f * 0.7 + b * 0.3 for (f, b) in zip(rgb_front, rgb_back)]
        self.url_colour = wx.Colour(*c)

        # Fill UI with data
        self.config_data.AskNotify(self.ApplyConfig)
        self.ApplyConfig()
 def OnAddBookmark(self, event): # wxGlade: BookmarksDialog.<event_handler>
     all_peers = self.world.GetAllPeers()
     bookmarked_peers = set([peer.id_ for peer in self.bookmarks.GetAllPeers()])
     peers = [peer for peer in all_peers if peer.id_ not in bookmarked_peers]
     dialog = wx.SingleChoiceDialog(parent=self,
         message=_("Please choose the peer to add to your bookmarks list."),
         caption=_("Add bookmark"),
         choices=[peer.pseudo for peer in peers])
     if dialog.ShowModal() == wx.ID_OK:
         peer = peers[dialog.GetSelection()]
         self.bookmarks.AddPeer(peer)
         self.UpdateUI()
Esempio n. 7
0
    def __init__(self, viewport):
        """
        Constructor.
        """
        BaseWorld.__init__(self, viewport)
        UIProxyReceiver.__init__(self)
        self.charset = GetCharset()
        self.repository = images.ImageRepository()
        self.avatars = AvatarRepository()
        self.avatars.AskNotify(UIProxy(self).UpdateAvatars)

        self.sounds = sounds.SoundRepository()
        self.human_peers = set()
Esempio n. 8
0
    def Init(self, local_ip):
        self.avatars = AvatarRepository()
        self.reactor = self.service_api.GetReactor()
        self.host = local_ip
        self.port = 7780 + random.randrange(0, 100)

        # Network address container: { peer_id => (host, port) }
        self.hosts = {}
        self.node_avatar_hash = None
        self.node_id = None

        # Peers for which we have received an avatar hash but whose
        # address we don't know yet
        self.pending_peers = set()
Esempio n. 9
0
    def _NecessaryPeers(self, max_angle=None):
        """
        Returns id's of peers that are necessary for our global connectivity.
        """
        if len(self.peers) < 3:
            return self.peers.keys()
        if max_angle is None:
            max_angle = self.max_angle
        _angles = self.angle_peers
        pi2 = 2.0 * math.pi

        result = set()
        prev_angle, _ = _angles[-2]
        angle, id_ = _angles[-1]
        # For each peer, we check the angle between the predecessor and
        # the successor. If the angle is greater than the desired max angle,
        # then this peer is necessary.
        for next_angle, next_id in _angles:
            if (next_angle - prev_angle) % pi2 >= max_angle:
                result.add(id_)
            prev_angle = angle
            angle, id_ = next_angle, next_id

        return result
Esempio n. 10
0
 def PeersSet(self):
     """
     Returns a set of all peer ids.
     """
     return set(self.peers.iterkeys())
Esempio n. 11
0
    def ParseMessage(self, data, parse_only=False):
        """
        Parse and extract message from protocol data.
        """
        # Parse raw data to construct message (strip empty lines)
        lines = data.split(self.line_separator)
        # If message is empty, return false
        if not lines:
            return None
        # Parse request line
        m = self.request_syntax.match(lines[0])
        if m is None:
            raise EventParsingError("Invalid request syntax: " + repr(lines[0]))

        # Request is first word of the first line (e.g. NEAREST, or BEST ...)
        request = m.group(1).upper()
        # Extract protocol version
        version = float(m.group(2))

        # Version check
        if version > VERSION:
            raise EventParsingError("Unexpected protocol version: %s" % str(version))
        elif version < VERSION:
            self.logger.info("Received message from older protocol version: %s" % str(version))
        # Request check
        if not request in REQUESTS[version]:
            raise EventParsingError("Unknown request: " + request)

        # Get args for this request
        mandatory_args = REQUESTS[version][request]
        missing_args = set(mandatory_args)
        args = {}

        # Now let's parse each parameter line in turn
        for nb_line in xrange(1, len(lines)):
            line = lines[nb_line]
            if len(line) == 0:
                break
            # Get arg name and arg value
            t = line.split(':', 1)
            if len(t) != 2:
                raise EventParsingError("Invalid message syntax:\r\n" + data)
            name = t[0].strip()
            value = t[1].strip()

            # Each arg has its own syntax-checking regex
            try:
                arg_id = PROTOCOL_STRINGS.get_reverse(name)
                arg_syntax = ARGS_SYNTAX[arg_id]
            except KeyError:
                raise EventParsingError("Unknown arg '%s'" % (name))
            if not arg_syntax.match(value):
                raise EventParsingError("Invalid arg syntax for '%s': '%s'" % (name, value))

            # The syntax is correct => add this arg to the arg list
            if arg_id in args:
                raise EventParsingError("Duplicate value for arg '%s'" % name)

            # Build argument value from its registered constructor
            if not parse_only:
                args[arg_id] = ARGS_FROM_STRING[arg_id](value)

            # Log optional arguments
            try:
                missing_args.remove(arg_id)
            except KeyError:
                self.logger.debug("Unexpected argument '%s' in message '%s'" % (name, request))

        # Is there a payload ?
        if nb_line + 1 < len(lines):
            payload = self.line_separator.join(lines[nb_line+1:])
            if payload:
                if ARG_PAYLOAD in missing_args:
                    missing_args.remove(ARG_PAYLOAD)
                else:
                    self.logger.debug("Optional payload in message '%s'" % request)
                # Note: we don't try to convert the payload to unicode when
                # receiving, because it could really be a binary string.
                # This makes the message handling slightly asymetric.
                args[ARG_PAYLOAD] = payload

        # Check that all required fields have been encountered
        if missing_args:
            raise EventParsingError("Missing arguments (%s) in message '%s'"
                    % (",".join([PROTOCOL_STRINGS[arg] for arg in missing_args]), request))

        # Everything's ok
        if not parse_only:
            message = Message()
            message.request = request
            message.version = version
            for arg_id, value in args.iteritems():
                setattr(message.args, ATTRIBUTE_NAMES[arg_id], value)
            return message
        else:
            return True
Esempio n. 12
0
 def Reset(self):
     self.plugins = {}
     self.peers = {}
     self.enabled_services = set()