Exemplo n.º 1
0
    def gotChannelCastMessage(self, recv_msg, sender_permid, selversion):
        """ Receive and handle a ChannelCast message """
        # ChannelCast feature starts from eleventh version; hence, do not receive from lower version peers
        # Arno, 2010-02-05: v12 uses a different on-the-wire format, ignore those.
        # Andrea: 2010-04-08: v14 can still receive v13 channelcast messages
        # Niels: 2011-02-02: Channelcast is now using dispersy, but we can still receive old messages

        if selversion < OLPROTO_VER_THIRTEENTH:
            if DEBUG:
                print >>sys.stderr, "channelcast: Do not receive from lower version peer:", selversion
            return True

        if DEBUG:
            print >>sys.stderr, "channelcast: Received a msg from ", show_permid_short(sender_permid)
            print >>sys.stderr, "channelcast: my_permid=", show_permid_short(self.my_permid)

        if not sender_permid or sender_permid == self.my_permid:
            if DEBUG:
                print >>sys.stderr, "channelcast: warning - got channelcastMsg from a None/Self peer", show_permid_short(
                    sender_permid
                ), recv_msg
            return False

        channelcast_data = {}
        try:
            channelcast_data = bdecode(recv_msg)
        except:
            print >>sys.stderr, "channelcast: warning, invalid bencoded data"
            return False

        # check message-structure
        if not validChannelCastMsg(channelcast_data):
            print >>sys.stderr, "channelcast: invalid channelcast_message"
            return False

        # 19/02/10 Boudewijn: validChannelCastMsg passes when
        # PUBLISHER_NAME and TORRENTNAME are either string or
        # unicode-string.  However, all further code requires that
        # these are unicode!
        for ch in channelcast_data.values():
            if isinstance(ch["publisher_name"], str):
                ch["publisher_name"] = str2unicode(ch["publisher_name"])
            if isinstance(ch["torrentname"], str):
                ch["torrentname"] = str2unicode(ch["torrentname"])

        self.updateChannel(sender_permid, None, channelcast_data)

        # Log RECV_MSG of uncompressed message
        if self.log:
            dns = self.dnsindb(sender_permid)
            if dns:
                ip, port = dns
                MSG_ID = "CHANNELCAST"
                # 08/04/10 Andrea: representing the whole channelcast  + metadata message
                msg = repr(channelcast_data)
                self.overlay_log("RECV_MSG", ip, port, show_permid(sender_permid), selversion, MSG_ID, msg)

        if self.TESTASSERVER:
            self.createAndSendChannelCastMessage(sender_permid, selversion)
        return True
Exemplo n.º 2
0
def uniconvert(s, enc):
    """ Convert 's' to a string containing a Unicode sequence encoded using
    encoding "enc". If 's' is not a Unicode object, we first try to convert
    it to one, guessing the encoding if necessary. """
    if not isinstance(s, unicode):
        try:
            s = str2unicode(s)
        except UnicodeError:
            raise UnicodeError('bad filename: '+s)
    return s.encode(enc)
Exemplo n.º 3
0
def uniconvert(s, enc):
    """ Convert 's' to a string containing a Unicode sequence encoded using
    encoding "enc". If 's' is not a Unicode object, we first try to convert
    it to one, guessing the encoding if necessary. """
    if not isinstance(s, unicode):
        try:
            s = str2unicode(s)
        except UnicodeError:
            raise UnicodeError('bad filename: ' + s)
    return s.encode(enc)
Exemplo n.º 4
0
def save_ssocnet_peer(self,permid,record,persinfo_ignore,hrwidinfo_ignore,ipinfo_ignore):
    """ This function is used by both BootstrapMsgHandler and 
        OverlapMsgHandler, and uses their database pointers. Hence the self 
        parameter. persinfo_ignore and ipinfo_ignore are booleans that
        indicate whether to ignore the personal info, resp. ip info in
        this record, because they were unsigned in the message and
        we already received signed versions before.
    """
    if permid == self.mypermid:
        return
    
    # 1. Save persinfo
    if not persinfo_ignore:
        persinfo = record['persinfo']
        
        if DEBUG:
            print >>sys.stderr,"socnet: Got persinfo",persinfo.keys()
            if len(persinfo.keys()) > 1:
                print >>sys.stderr,"socnet: Got persinfo THUMB THUMB THUMB THUMB"
        
        # Arno, 2008-08-22: to avoid UnicodeDecode errors when commiting 
        # on sqlite
        name = str2unicode(persinfo['name'])

        if DEBUG:
            print >> sys.stderr,"socnet: SOCIAL_OVERLAP",show_permid_short(permid),`name`
        
        if self.peer_db.hasPeer(permid):
            self.peer_db.updatePeer(permid, name=name)
        else:
            self.peer_db.addPeer(permid,{'name':name})
    
        # b. Save icon
        if 'icontype' in persinfo and 'icondata' in persinfo: 
            if DEBUG:
                print >> sys.stderr,"socnet: saving icon for",show_permid_short(permid),`name`
            self.peer_db.updatePeerIcon(permid, persinfo['icontype'],persinfo['icondata'])    
Exemplo n.º 5
0
    def gotChannelCastMessage(self, recv_msg, sender_permid, selversion):
        """ Receive and handle a ChannelCast message """
        # ChannelCast feature starts from eleventh version; hence, do not receive from lower version peers
        # Arno, 2010-02-05: v12 uses a different on-the-wire format, ignore those.
        # Andrea: 2010-04-08: v14 can still receive v13 channelcast messages
        # Niels: 2011-02-02: Channelcast is now using dispersy, but we can still receive old messages

        if selversion < OLPROTO_VER_THIRTEENTH:
            if DEBUG:
                print >> sys.stderr, "channelcast: Do not receive from lower version peer:", selversion
            return True

        if DEBUG:
            print >> sys.stderr, 'channelcast: Received a msg from ', show_permid_short(
                sender_permid)
            print >> sys.stderr, "channelcast: my_permid=", show_permid_short(
                self.my_permid)

        if not sender_permid or sender_permid == self.my_permid:
            if DEBUG:
                print >> sys.stderr, "channelcast: warning - got channelcastMsg from a None/Self peer", \
                        show_permid_short(sender_permid), recv_msg
            return False

        channelcast_data = {}
        try:
            channelcast_data = bdecode(recv_msg)
        except:
            print >> sys.stderr, "channelcast: warning, invalid bencoded data"
            return False

        # check message-structure
        if not validChannelCastMsg(channelcast_data):
            print >> sys.stderr, "channelcast: invalid channelcast_message"
            return False

        # 19/02/10 Boudewijn: validChannelCastMsg passes when
        # PUBLISHER_NAME and TORRENTNAME are either string or
        # unicode-string.  However, all further code requires that
        # these are unicode!
        for ch in channelcast_data.values():
            if isinstance(ch["publisher_name"], str):
                ch["publisher_name"] = str2unicode(ch["publisher_name"])
            if isinstance(ch["torrentname"], str):
                ch["torrentname"] = str2unicode(ch["torrentname"])

        self.updateChannel(sender_permid, None, channelcast_data)

        #Log RECV_MSG of uncompressed message
        if self.log:
            dns = self.dnsindb(sender_permid)
            if dns:
                ip, port = dns
                MSG_ID = "CHANNELCAST"
                # 08/04/10 Andrea: representing the whole channelcast  + metadata message
                msg = repr(channelcast_data)
                self.overlay_log('RECV_MSG', ip, port,
                                 show_permid(sender_permid), selversion,
                                 MSG_ID, msg)

        if self.TESTASSERVER:
            self.createAndSendChannelCastMessage(sender_permid, selversion)
        return True
Exemplo n.º 6
0
 def test_unicode_string_2(self):
     self.assertIsInstance(str2unicode('hi\xa0there'), unicode)
Exemplo n.º 7
0
 def test_unicode_string_1(self):
     self.assertIsInstance(str2unicode("test"), unicode)