Пример #1
0
    def __init__(self,
                 torrent_hash,
                 num_pieces,
                 coordinator_permid,
                 coordinator=None):
        self.overlay_bridge = OverlayThreadingBridge.getInstance()
        self.torrent_hash = torrent_hash
        if coordinator_permid is not None and coordinator_permid == '':
            self.coordinator_permid = None
        else:
            self.coordinator_permid = coordinator_permid
        self.coordinator_ip = None  # see is_coordinator()
        self.coordinator_port = -1

        if self.coordinator_permid is not None:
            peerdb = PeerDBHandler.getInstance()
            peer = peerdb.getPeer(coordinator_permid)
            if peer is not None:
                self.coordinator_ip = peer['ip']
                self.coordinator_port = peer['port']

        self.reserved_pieces = [False] * num_pieces
        self.ignored_pieces = [False] * num_pieces
        self.coordinator = coordinator
        self.counter = 0
        self.completed = False
        self.distr_reserved_pieces = [False] * num_pieces
        self.marker = [True] * num_pieces
        self.round = 0
        self.encoder = None
        self.continuations = []
        self.outstanding = None
        self.last_req_time = 0
Пример #2
0
    def __init__(self, torrent_hash, num_pieces, coordinator_permid, coordinator = None):
        self.overlay_bridge = OverlayThreadingBridge.getInstance()
        self.torrent_hash = torrent_hash
        if coordinator_permid is not None and coordinator_permid == '':
            self.coordinator_permid = None
        else:
            self.coordinator_permid = coordinator_permid
        self.coordinator_ip = None  # see is_coordinator()
        self.coordinator_port = -1

        if self.coordinator_permid is not None:
            peerdb = PeerDBHandler.getInstance()
            peer = peerdb.getPeer(coordinator_permid)
            if peer is not None:
                self.coordinator_ip = peer['ip']
                self.coordinator_port = peer['port']

        self.reserved_pieces = [False] * num_pieces
        self.ignored_pieces = [False] * num_pieces
        self.coordinator = coordinator
        self.counter = 0
        self.completed = False
        self.distr_reserved_pieces = [False] * num_pieces
        self.marker = [True] * num_pieces
        self.round = 0
        self.encoder = None
        self.continuations = []
        self.outstanding = None
        self.last_req_time = 0
Пример #3
0
def olthread_bartercast_conn_lost(ip, port, down_kb, up_kb):
    """ Called by OverlayThread to store information about the peer to
    whom the connection was just closed in the (slow) databases. """

    peerdb = PeerDBHandler.getInstance()
    bartercastdb = BarterCastDBHandler.getInstance()

    if bartercastdb:

        permid = peerdb.getPermIDByIP(ip)
        my_permid = bartercastdb.my_permid

        if DEBUG:
            print >> sys.stderr, "BARTERCAST (Connecter): Up %d down %d peer %s:%s (PermID = %s)" % (
                up_kb, down_kb, ip, port, permid)

        # Save exchanged KBs in BarterCastDB
        changed = False
        if permid is not None:
            name = bartercastdb.getName(permid)

            if down_kb > 0:
                new_value = bartercastdb.incrementItem((my_permid, permid),
                                                       'downloaded',
                                                       down_kb,
                                                       commit=False)
                changed = True

            if up_kb > 0:
                new_value = bartercastdb.incrementItem((my_permid, permid),
                                                       'uploaded',
                                                       up_kb,
                                                       commit=False)
                changed = True

        # For the record: save KBs exchanged with non-tribler peers
        else:
            if down_kb > 0:
                new_value = bartercastdb.incrementItem(
                    (my_permid, 'non-tribler'),
                    'downloaded',
                    down_kb,
                    commit=False)
                changed = True

            if up_kb > 0:
                new_value = bartercastdb.incrementItem(
                    (my_permid, 'non-tribler'),
                    'uploaded',
                    up_kb,
                    commit=False)
                changed = True

        if changed:
            bartercastdb.commit()

    else:
        if DEBUG:
            print >> sys.stderr, "BARTERCAST: No bartercastdb instance"
Пример #4
0
 def singtest_send_opened(self):
     print >> sys.stderr,"test: test_send_opened"
     self.wanted = True
     self.wanted2 = True
     peer_db = PeerDBHandler.getInstance()
     hispermid = self.peer2.my_permid
     peer_db.addPeer(hispermid,{'ip':"127.0.0.1", 'port':5678})
     msg = GET_METADATA+'12345678901234567890'
     self.peer1.secure_overlay.connect(hispermid,lambda e,d,p,s: self.send_opened_connect_callback(e,d,p,s,msg))
Пример #5
0
 def singtest_send_unopenedB(self):
     print >> sys.stderr,"test: test_send_unopenedB"
     self.wanted = True
     peer_db = PeerDBHandler.getInstance()
     hispermid = self.peer2.my_permid
     peer_db.addPeer(hispermid,{'ip':"127.0.0.1", 'port':5678})
     self.peer1.secure_overlay.send(hispermid,'msg=bla',self.send_unopenedB_send_callback)
     sleep(2) # let rawserver thread close connection, which should succeed
     self.assert_(len(self.peer1.secure_overlay.iplport2oc) == 0)
Пример #6
0
 def singtest_send_unopenedB(self):
     print >> sys.stderr, "test: test_send_unopenedB"
     self.wanted = True
     peer_db = PeerDBHandler.getInstance()
     hispermid = self.peer2.my_permid
     peer_db.addPeer(hispermid, {'ip': "127.0.0.1", 'port': 5678})
     self.peer1.secure_overlay.send(hispermid, 'msg=bla',
                                    self.send_unopenedB_send_callback)
     sleep(2)  # let rawserver thread close connection, which should succeed
     self.assert_(len(self.peer1.secure_overlay.iplport2oc) == 0)
Пример #7
0
    def singtest_connect_to_dead_peerB(self):
        print >> sys.stderr,"test: test_connect_to_dead_peerB"
        self.wanted = True

        peer_db = PeerDBHandler.getInstance()
        hispermid = self.peer2.my_permid
        peer_db.addPeer(hispermid, {'ip':"127.0.0.1", 'port':22220})

        self.peer1.secure_overlay.connect(hispermid,self.connect_to_dead_peerB_callback)
        sleep(2) # let rawserver thread establish connection, which should fail
        self.assert_(len(self.peer1.secure_overlay.iplport2oc) == 0)
Пример #8
0
 def __init__(self):
     if FriendshipMsgHandler.__singleton:
         raise RuntimeError, "FriendshipMsgHandler is singleton"
     self.overlay_bridge = None
     self.currmsgs = {}
     self.online_fsext_peers = Set() # online peers that speak FRIENDSHIP ext
     self.peerdb = PeerDBHandler.getInstance()
     self.frienddb = FriendDBHandler.getInstance()
     self.friendshipStatistics_db = FriendshipStatisticsDBHandler.getInstance()
     self.list_no_of_conn_attempts_per_target= {}
     self.usercallback = None
Пример #9
0
 def singtest_send_opened(self):
     print >> sys.stderr, "test: test_send_opened"
     self.wanted = True
     self.wanted2 = True
     peer_db = PeerDBHandler.getInstance()
     hispermid = self.peer2.my_permid
     peer_db.addPeer(hispermid, {'ip': "127.0.0.1", 'port': 5678})
     msg = GET_METADATA + '12345678901234567890'
     self.peer1.secure_overlay.connect(
         hispermid, lambda e, d, p, s: self.send_opened_connect_callback(
             e, d, p, s, msg))
Пример #10
0
 def __init__(self):
     if FriendshipMsgHandler.__singleton:
         raise RuntimeError, "FriendshipMsgHandler is singleton"
     self.overlay_bridge = None
     self.currmsgs = {}
     self.online_fsext_peers = Set() # online peers that speak FRIENDSHIP ext
     self.peerdb = PeerDBHandler.getInstance()
     self.frienddb = FriendDBHandler.getInstance()
     self.friendshipStatistics_db = FriendshipStatisticsDBHandler.getInstance()
     self.list_no_of_conn_attempts_per_target= {}
     self.usercallback = None
Пример #11
0
    def singtest_connect_to_live_peer(self):
        print >> sys.stderr,"test: test_connect_to_live_peer"
        self.wanted = True

        peer_db = PeerDBHandler.getInstance()
        hispermid = self.peer2.my_permid
        peer_db.addPeer(hispermid,{'ip':"127.0.0.1", 'port':5678})

        self.peer1.secure_overlay.connect(hispermid,self.connect_to_live_peer_callback)
        sleep(2) # let rawserver thread establish connection, which should succeed
        self.assert_(len(self.peer1.secure_overlay.iplport2oc) == 1)
        self.assert_(self.peer1.secure_overlay.iplport2oc.has_key('127.0.0.1:5678'))
Пример #12
0
    def singtest_got_conn_outgoing(self):
        print >> sys.stderr,"test: test_got_conn_outgoing"
        self.wanted = True
        self.wanted2 = True
        # register handler for connections
        self.peer1.secure_overlay.register_conns_callback(self.got_conn_outgoing_conns_callback)

        peer_db = PeerDBHandler.getInstance()
        hispermid = self.peer2.my_permid
        peer_db.addPeer(hispermid,{'ip':"127.0.0.1", 'port':5678})
        msg = GET_METADATA+'12345678901234567890'
        self.peer1.secure_overlay.connect(hispermid,lambda e,d,p,s:self.got_conn_outgoing_connect_callback(e,d,p,s,msg))
Пример #13
0
    def singtest_connect_to_dead_peerB(self):
        print >> sys.stderr, "test: test_connect_to_dead_peerB"
        self.wanted = True

        peer_db = PeerDBHandler.getInstance()
        hispermid = self.peer2.my_permid
        peer_db.addPeer(hispermid, {'ip': "127.0.0.1", 'port': 22220})

        self.peer1.secure_overlay.connect(hispermid,
                                          self.connect_to_dead_peerB_callback)
        sleep(
            2)  # let rawserver thread establish connection, which should fail
        self.assert_(len(self.peer1.secure_overlay.iplport2oc) == 0)
Пример #14
0
    def singtest_receive(self):
        print >> sys.stderr, "test: test_receive"
        self.wanted = True
        self.wanted2 = True
        # register handler for messages
        self.peer2.secure_overlay.register_recv_callback(
            self.receive_msg_callback)

        peer_db = PeerDBHandler.getInstance()
        hispermid = self.peer2.my_permid
        peer_db.addPeer(hispermid, {'ip': "127.0.0.1", 'port': 5678})
        msg = GET_METADATA + '12345678901234567890'
        self.peer1.secure_overlay.connect(
            hispermid,
            lambda e, d, p, s: self.receive_connect_callback(e, d, p, s, msg))
Пример #15
0
    def singtest_connect_to_live_peer(self):
        print >> sys.stderr, "test: test_connect_to_live_peer"
        self.wanted = True

        peer_db = PeerDBHandler.getInstance()
        hispermid = self.peer2.my_permid
        peer_db.addPeer(hispermid, {'ip': "127.0.0.1", 'port': 5678})

        self.peer1.secure_overlay.connect(hispermid,
                                          self.connect_to_live_peer_callback)
        sleep(
            2
        )  # let rawserver thread establish connection, which should succeed
        self.assert_(len(self.peer1.secure_overlay.iplport2oc) == 1)
        self.assert_(
            self.peer1.secure_overlay.iplport2oc.has_key('127.0.0.1:5678'))
Пример #16
0
def olthread_bartercast_conn_lost(ip,port,down_kb,up_kb):
    """ Called by OverlayThread to store information about the peer to
    whom the connection was just closed in the (slow) databases. """
    
    peerdb = PeerDBHandler.getInstance()
    bartercastdb = BarterCastDBHandler.getInstance()
    
    if bartercastdb:
    
        permid = peerdb.getPermIDByIP(ip)
        my_permid = bartercastdb.my_permid
    
        if DEBUG:
            print >> sys.stderr, "BARTERCAST (Connecter): Up %d down %d peer %s:%s (PermID = %s)" % (up_kb, down_kb, ip, port, permid)
    
        # Save exchanged KBs in BarterCastDB
        changed = False
        if permid is not None:
            name = bartercastdb.getName(permid)
            
            if down_kb > 0:
                new_value = bartercastdb.incrementItem((my_permid, permid), 'downloaded', down_kb, commit=False)
                changed = True
     
            if up_kb > 0:
                new_value = bartercastdb.incrementItem((my_permid, permid), 'uploaded', up_kb, commit=False)
                changed = True
     
        # For the record: save KBs exchanged with non-tribler peers
        else:
            if down_kb > 0:
                new_value = bartercastdb.incrementItem((my_permid, 'non-tribler'), 'downloaded', down_kb, commit=False)
                changed = True
     
            if up_kb > 0:
                new_value = bartercastdb.incrementItem((my_permid, 'non-tribler'), 'uploaded', up_kb, commit=False)
                changed = True
                
        if changed:
            bartercastdb.commit()

    else:
        if DEBUG:
            print >> sys.stderr, "BARTERCAST: No bartercastdb instance"
Пример #17
0
 def _PostInit(self):
     # Do all init here
     self.backgroundColor = wx.WHITE
     self.dataBitmap = self.maskBitmap = None
     self.data = None
     self.mouseOver = False
     self.guiUtility = GUIUtility.getInstance()
     self.utility = self.guiUtility.utility
     self.Bind(wx.EVT_MOUSE_EVENTS, self.mouseAction)
     self.Bind(wx.EVT_LEFT_UP, self.guiUtility.buttonClicked)
     self.Bind(wx.EVT_PAINT, self.OnPaint)
     self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
     self.selected = False
     self.border = None
     #create the heart
     #I will use TasteHeart.BITMAPS to paint the right one
     self.peer_db = PeerDBHandler.getInstance()
     self.iconsManager = IconsManager.getInstance()
     self.superpeer_db = self.GetParent().parent.superpeer_db
Пример #18
0
    def OnAddEditFriend(self, event):
        name = self.name_text.GetValue()
        ip = str(self.ip_text.GetValue())
        b64permid = str(self.permid_text.GetValue())
        try:
            permid = base64.decodestring( b64permid+'\n' )
        except:
            print_exc()
            permid = ''
        #icon = self.icon_path.GetValue()
        try:
            port = int(self.port_text.GetValue())
        except:
            port = 0
            
        if len(name) == 0:
            self.show_inputerror(self.utility.lang.get('nicknameempty_error'))
        elif len(permid) == 0:
            self.show_inputerror(self.utility.lang.get('friendspermid_error'))
        elif port == 0:
            self.show_inputerror(self.utility.lang.get('friendsport_error'))
        else:
            fdb = FriendDBHandler.getInstance()
            pdb = PeerDBHandler.getInstance()
            
            #friend = {'permid':permid, 'ip':ip, 'port':port, 'name':name, 'icon':newiconfilename}
            #friend = {'permid':permid, 'ip':ip, 'port':port, 'name':name}
            friend = {'ip':ip, 'port':port, 'name':name}
            if self.editfriend is not None:
                if self.editfriend['permid'] != permid:
                    fdb.deleteFriend(self.editfriend['permid'])
                    pdb.deletePeer(self.editfriend['permid'])
                    
            #fdb.addExternalFriend(friend)
            pdb.addPeer(permid,friend)
            fdb.setFriend(permid)
            
            event.Skip()    # must be done, otherwise ShowModal() returns wrong error 
            self.Destroy()

        """            
Пример #19
0
    def OnAddEditFriend(self, event):
        name = self.name_text.GetValue()
        ip = str(self.ip_text.GetValue())
        b64permid = str(self.permid_text.GetValue())
        try:
            permid = base64.decodestring(b64permid + '\n')
        except:
            print_exc()
            permid = ''
        #icon = self.icon_path.GetValue()
        try:
            port = int(self.port_text.GetValue())
        except:
            port = 0

        if len(name) == 0:
            self.show_inputerror(self.utility.lang.get('nicknameempty_error'))
        elif len(permid) == 0:
            self.show_inputerror(self.utility.lang.get('friendspermid_error'))
        elif port == 0:
            self.show_inputerror(self.utility.lang.get('friendsport_error'))
        else:
            fdb = FriendDBHandler.getInstance()
            pdb = PeerDBHandler.getInstance()

            #friend = {'permid':permid, 'ip':ip, 'port':port, 'name':name, 'icon':newiconfilename}
            #friend = {'permid':permid, 'ip':ip, 'port':port, 'name':name}
            friend = {'ip': ip, 'port': port, 'name': name}
            if self.editfriend is not None:
                if self.editfriend['permid'] != permid:
                    fdb.deleteFriend(self.editfriend['permid'])
                    pdb.deletePeer(self.editfriend['permid'])

            #fdb.addExternalFriend(friend)
            pdb.addPeer(permid, friend)
            fdb.setFriend(permid)

            event.Skip(
            )  # must be done, otherwise ShowModal() returns wrong error
            self.Destroy()
        """            
Пример #20
0
class Peer(Thread):
    def __init__(self, testcase, port, secover):
        Thread.__init__(self)
        self.setDaemon(True)

        self.testcase = testcase

        self.doneflag = Event()
        config = {}
        config['timeout_check_interval'] = 100000
        config['timeout'] = 100000
        config['ipv6_enabled'] = 0
        config['minport'] = port
        config['maxport'] = port + 5
        config['random_port'] = 0
        config['bind'] = ''
        config['ipv6_binds_v4'] = 0
        config['max_message_length'] = 2**23
        config['state_dir'] = config['install_dir'] = tempfile.mkdtemp()
        config['peer_icon_path'] = 'icons'

        self.rawserver = RawServer(self.doneflag,
                                   config['timeout_check_interval'],
                                   config['timeout'],
                                   ipv6_enable=config['ipv6_enabled'],
                                   failfunc=self.report_failure,
                                   errorfunc=self.report_error)
        while 1:
            try:
                self.listen_port = self.rawserver.find_and_bind(
                    0,
                    config['minport'],
                    config['maxport'],
                    config['bind'],
                    reuse=True,
                    ipv6_socket_style=config['ipv6_binds_v4'],
                    randomizer=config['random_port'])
                print >> sys.stderr, "test: Got listen port", self.listen_port
                break
            except socketerror, e:
                self.report_failure(str(e))
                msg = "Couldn't not bind to listen port - " + str(e)
                self.report_failure(msg)
                return

        self.multihandler = MultiHandler(self.rawserver, self.doneflag)
        # Note: We don't want a singleton, we want
        # two different instances for peer1 and peer2
        self.secure_overlay = secover

        self.my_keypair = EC.gen_params(EC.NID_sect233k1)
        self.my_keypair.gen_key()
        self.my_permid = str(self.my_keypair.pub().get_der())

        self.session = FakeSession(self, self.my_keypair, self.my_permid,
                                   self.listen_port)
        self.peer_db = PeerDBHandler.getInstance(config)

        self.secure_overlay.register(self, config['max_message_length'])
        print >> sys.stderr, "Peer: Setting", self.secure_overlay.get_handler(
        ), "as handler at SocketHandler"
        self.rawserver.sockethandler.set_handler(
            self.secure_overlay.get_handler())
        self.secure_overlay.start_listening()

        # Stupid rawserver goes into very long wait if there are no short
        # term tasks. Emulate this
        self.rawserver.add_task(self.dummy_task, 0)