예제 #1
0
 def construct_hello(self):
     'Construct hello packet. Format: [0,{addr:sender_addr, pubID:sender_pubID}]'
     packet = p2p.Packet(
         "connect",
         dict(node=dict(address=self.address,
                        pubID=self.configs['node']['id'])))
     return packet
예제 #2
0
 def send_disconnect(self):
     'Construct disconnect packet and send. Format: ["disconnect",{addr:sender_addr, pubID:sender_pubID}]'
     packet = p2p.Packet(
         "disconnect",
         dict(node=dict(address=self.own_addr, pubID=self.own_id),
              reason='stopping peer!'))
     self.send(packet)
예제 #3
0
 def approve_conn(self, peer):
     num_peers = len(self.peers)
     max_peers = self.configs['p2p']['max_peers']
     if num_peers > max_peers:
         print("Too many connections! Disconnecting from peer %s" %
               peer.pubID)
         pk = dict(node=dict(address=self.address,
                             pubID=self.configs['node']['id']),
                   reason="too many peers")
         disconnect_packet = p2p.Packet("disconnect", pk)
         peer.send(disconnect_packet)
         return False
         #peer.stop()
     '''if peer in [p for p in self.peers]:
         print("Already connected to this peer! Disconnecting....")
         pk = dict(node=dict(address=self.address, pubID=self.configs['node']['id']), reason="duplicate hello")
         disconnect_packet = p2p.Packet("disconnect", pk)
         peer.send(disconnect_packet)
         return False'''
     return True
예제 #4
0
def run_pm_loop(pm, test_subject):
    '''
    Test 1: peer1 sending data to peer2
    start_time = time.time()
    count = 0
    print("running! time: %f" %start_time)
    while not pm.is_stopped:
        if test_subject == 'peer1':
            packet = p2p.Packet("transaction", dict(node=dict(address=pm.address, pubID=pm.configs['node']['id']),transaction=transaction1))
            pm.send(packet,pb2)
        if test_subject == 'peer2':
            if not pm.recv_queue.empty():
                count = count+1
                print("You've got mail! #of mails: %i" % len(pm.recv_queue))
                msg = pm.recv_queue.get()
                print("One mail from: %s \n There are %i messages left in inbox." % (msg['node']['pubID'], len(pm.recv_queue)))
                
                #print(msg['transaction'])
                #print("Broadcasting received message...")
                #packet = p2p.Packet("transaction", dict(node=dict(address=pm.address, pubID=pm.configs['node']['id']),transaction=msg['transaction']))
                #pm.broadcast(packet,excluded=[msg['node']['pubID']])
                
                print("peermanager stopped. Count: %i, time elapsed: %f" %(count, time.time()-start_time))
        gevent.sleep(0)
    '''

    # Another test: 4 peers broadcasting and receiving
    if test_subject == 'peer1':
        gevent.sleep(2)
        packet = p2p.Packet(
            "transaction",
            dict(node=dict(address=pm.address, pubID=pm.configs['node']['id']),
                 transaction=transaction1))
        pm.send(packet, pb2)
    if test_subject == 'peer2':
        gevent.sleep(1.2)
        packet = p2p.Packet(
            "transaction",
            dict(node=dict(address=pm.address, pubID=pm.configs['node']['id']),
                 transaction=transaction2))
        #pm.broadcast(packet)
        pm.send(packet, pb3)
    if test_subject == 'peer3':
        gevent.sleep(0.6)
        packet = p2p.Packet(
            "transaction",
            dict(node=dict(address=pm.address, pubID=pm.configs['node']['id']),
                 transaction=transaction3))
        #pm.broadcast(packet)
        pm.send(packet, pb4)
    if test_subject == 'peer4':
        packet = p2p.Packet(
            "transaction",
            dict(node=dict(address=pm.address, pubID=pm.configs['node']['id']),
                 transaction=transaction4))
        #pm.broadcast(packet)
        pm.send(packet, pb1)
    start_time = time.time()
    count = 0
    print("running! time: %f" % start_time)
    while not pm.is_stopped:
        # the same as: if keyboard interrupt: evt.set()
        if not pm.recv_queue.empty():
            count = count + 1
            print("You've got mail! #of mails: %i" % len(pm.recv_queue))
            msg = pm.recv_queue.get()
            print("One mail from: %s \n There are %i messages left in inbox." %
                  (msg['node']['pubID'], len(pm.recv_queue)))
            print(msg['transaction'])
            print("Broadcasting received message...")
            packet = p2p.Packet(
                "transaction",
                dict(node=dict(address=pm.address,
                               pubID=pm.configs['node']['id']),
                     transaction=msg['transaction']))
            pm.broadcast(packet, excluded=[msg['node']['pubID']])
            print("peermanager stopped. Count: %i, time elapsed: %f" %
                  (count, time.time() - start_time))
        gevent.sleep(0)
    '''
예제 #5
0
 def send_confirm(self):
     'Return confirm packet to ensure successful connection.'
     confirm_packet = p2p.Packet(
         "confirm",
         dict(node=dict(address=self.own_addr, pubID=self.own_id)))
     self.send(confirm_packet)
예제 #6
0
 def send_hello(self, serverid):
     'Construct hello packet and send. Format: ["connect",{addr:sender_addr, pubID:sender_pubID}]'
     packet = p2p.Packet(
         "connect", dict(node=dict(address=self.own_addr, pubID=serverid)))
     self.send(packet)