Example #1
0
    def start(self,
              response=None,
              mechanism=None,
              locale="en_US",
              tune_params=None,
              username=None,
              password=None,
              client_properties=None,
              connection_options=None,
              sasl_options=None):
        self.mechanism = mechanism
        self.response = response
        self.username = username
        self.password = password
        self.locale = locale
        self.tune_params = tune_params
        self.client_properties = get_client_properties_with_defaults(
            provided_client_properties=client_properties)
        self.sasl_options = sasl_options
        self.socket = connect(self.host, self.port, connection_options)
        self.conn = Connection(self.socket, self.spec)
        self.peer = Peer(self.conn, ClientDelegate(self), Session)

        self.conn.init()
        self.peer.start()
        self.wait()
        self.channel(0).connection_open(self.vhost)
Example #2
0
 def __init__(self, peer_dict, torrent, tracker):
     self.tracker = tracker
     self.torrent = torrent
     Peer.__init__(self, peer_dict, torrent, peer_dict['id'])
     self.peers = []
     self.done = False
     self.piece_dict = { i : None for i in range(self.torrent.no_of_subpieces)}
    def __init__(self, srcRoom, dstRoom, pillow):
        Peer.__init__(self, srcRoom)
        
        from wallaby.pf.room import House
        self._dstRoom = House.get(dstRoom)

        self._catch(pillow, self._message)
Example #4
0
    def __init__(self):
        host_name = socket.gethostname()
        if os.path.isdir("sdfs/"):
            shutil.rmtree("sdfs/")
        os.system("mkdir sdfs")
        self.membership_manager = Peer(host_name)
        self.membership_manager.start()

        self.padding = "jyuan18?yixinz6"

        self.file_receive_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.file_receive_socket.bind(("0.0.0.0",2333))

        self.file_dict = {"filename":[1,2,3,4,5]}

        self.file_dict_lock = threading.Lock()
        self.file_to_node = {"filename":["ip1","ip2","ip3"]}

        self.ip = socket.gethostname()
        self.file_receive_socket.listen(100)
        logging.basicConfig(filename='vm1.log', level=logging.INFO)
        threading.Thread(target=self.receive_file).start()

        if self.ip == "fa18-cs425-g26-01.cs.illinois.edu":
            Master = master(self.membership_manager)
            threading.Thread(target=Master.listen, args=(Master.op_listen_socket, False)).start()
            threading.Thread(target=Master.listen, args=(Master.ack_listen_socket, True)).start()
Example #5
0
  def start(self, response=None, mechanism=None, locale="en_US", tune_params=None,
            username=None, password=None,
            client_properties=None, connection_options=None, sasl_options = None,
            channel_options=None):
    if response is not None and (username is not None or password is not None):
      raise RuntimeError("client must not specify both response and (username, password).")
    if response is not None:
      self.response = response
      authzid, self.username, self.password = response.split("\0")
    else:
      self.username = username
      self.password = password
    self.mechanism = mechanism
    self.locale = locale
    self.tune_params = tune_params
    self.client_properties=get_client_properties_with_defaults(provided_client_properties=client_properties, version_property_key="version")
    self.sasl_options = sasl_options
    self.socket = connect(self.host, self.port, connection_options)
    self.conn = Connection(self.socket, self.spec)
    self.peer = Peer(self.conn, ClientDelegate(self), Session, channel_options)

    self.conn.init()
    self.peer.start()
    self.wait()
    self.channel(0).connection_open(self.vhost)
 def record_cert_handler(self, conn: Peer, msg) -> bool:
     if conn.add_peer_cert(msg.decode("utf-8")):
         conn.send_data(Peer.CERT_RESPONSE_VALID, self.encrypt.get_signed_cert_str())
         return False
     else:
         conn.send_data(Peer.CERT_RESPONSE_INVALID, "")
         return True
    def file_request_handler(self, conn: Peer, file_name) -> bool:
        # print("Received", file_name)

        plain_file_name = self.encrypt.get_private_key().decrypt(
            file_name,
            padding.OAEP(
                mgf=padding.MGF1(algorithm=hashes.SHA256()),
                algorithm=hashes.SHA256(),
                label=None))
        print("File request for:", plain_file_name)

        with open(plain_file_name.decode("utf-8"), 'rb') as f:
            for piece in read_in_chunks(f):
                message = piece
                if conn.peer_cert:
                    public_key = conn.peer_cert.public_key()
                    ciphertext = public_key.encrypt(
                        message,
                        padding.OAEP(
                            mgf=padding.MGF1(algorithm=hashes.SHA256()),
                            algorithm=hashes.SHA256(),
                            label=None))
                    # print(ciphertext)
                    conn.send_data(Peer.RESPONSE_FILE, ciphertext)
                else:
                    print("ERROR: PEER CERTIFICATE NOT AVAILABLE.")
        return True
    def __handle_peer(self, client_sock):
        """
        handle_peer( new socket connection ) -> ()
        Dispatches messages from the socket connection
        """

        self.__debug('New child ' + str(threading.currentThread().getName()))
        self.__debug('Connected ' + str(client_sock.getpeername()))

        host, port = client_sock.getpeername()
        conn = Peer(port, client_sock)
        while True:
            try:
                msg_type, msg_data = conn.recv_data()
                if msg_type:
                    msg_type = msg_type.upper()
                if msg_type not in self.handlers:
                    self.__debug('Peer msg not handled') # : %s: %s' % (msg_type, msg_data))
                    break
                else:
                    self.__debug('Handling peer msg') # : %s: %s' % (msg_type, msg_data))
                    disconnect = self.handlers[msg_type](conn, msg_data)
                    if disconnect:
                        break
            except KeyboardInterrupt:
                raise
            except:
                traceback.print_exc()

        self.__debug('Disconnecting ' + str(client_sock.getpeername()))
        conn.close()
Example #9
0
 def test_create_bitfield(self):
     client.num_pieces = 8
     mypeer = Peer('127.0.0.1', 6881, client)
     mypeer.act_on_messages((BitfieldMsg('\x00'),))
     self.assertEqual(mypeer.bitfield[0], 0)
     mypeer.act_on_messages((BitfieldMsg('\xff'),))
     self.assertEqual(mypeer.bitfield[0], 1)
Example #10
0
 def __init__(self):
     Thread.__init__(self)
     self.id = "1000-" + str(random.randint(0, 100))
     self.peer = Peer(self.id, self.resource_loaded)
     self.playlist_uri = "http://cdn.bem.tv/stream/soccer2sec/soccer/playlist.m3u8"
     self.player_buffer = []
     self.start()
Example #11
0
def main():    
    app = QApplication(sys.argv)

    peer = Peer(1, "TestPeer", "127.0.0.1", 8888)
    peer.show()


    sys.exit(app.exec_())
Example #12
0
 def __init__(self, peer_dict, torrent, tracker):
     self.tracker = tracker
     self.torrent = torrent
     Peer.__init__(self, peer_dict, torrent, peer_dict['id'])
     self.peers = []
     self.done = False
     # The piece dictionary is to make sure I don't make multiple requests for the same piece to different peers
     self.piece_dict = { i : None for i in range(self.torrent.no_of_subpieces)}
Example #13
0
 def __init__(self):
     super(PeerApplication, self).__init__()
     os.system("pyuic5 -x GUI/gui.ui -o GUI/gui.py")  # compile the gui
     from GUI.gui import Ui_MainWindow  # import the newly compiled gui
     self.ui = Ui_MainWindow()  # create an instance of the GUI
     self.ui.setupUi(self)
     self.configure()
     self.peer = Peer()
     self.show()
Example #14
0
    def _start_peer(self, connection, address, remote_pubkey=None):
        log.debug('new connect', connection=connection, incoming=bool(not remote_pubkey))
        # create peer
        peer = Peer(self, connection, remote_pubkey=remote_pubkey)
        log.debug('created new peer', peer=peer)
        self.peers.append(peer)

        # loop
        peer.start()
        log.debug('peer started', peer=peer)
 def _startNewPeers(self):
     """Connect with new peers up until MAX_PEERS total connections"""
     for (ip_address, port) in self._peer_addresses:
             if len(self._peers) < self.MAX_PEERS:
                 p = Peer(self, ip_address, port)
                 t = Thread(target=p.execute)
                 self._peers.append(p)
                 print "MANAGER CREATING PEER: ", p.ID
                 # t.start()
                 p.execute()
Example #16
0
 def __init__(self, ip: str, id: int, api_url='R'):
     if len(ip) < 5:  # port only
         self.__IP = '127.0.0.1:' + ip
     else:
         self.__IP = ip
     self.__ID = id
     self.__socket.bind('tcp://' + self.__IP)
     Peer.__init__(self, ip, id, api_url)
     print('Validator constructed')
     self.join()
     self.listen()
Example #17
0
 def __init__(self, peer_dict, torrent, tracker):
     self.tracker = tracker
     self.torrent = torrent
     Peer.__init__(self, peer_dict, torrent, peer_dict['id'])
     self.peers = []
     self.done = False
     # The piece dictionary is to make sure I don't make multiple requests for the same piece to different peers
     self.piece_dict = {
         i: None
         for i in range(self.torrent.no_of_subpieces)
     }
Example #18
0
 def scanPorts(self):
     host = input()
     print("[+] Scanning Ports for other bots")
     ports = range(45454, 50000)
     for port in ports:
         try:
             if self.connectTo(Peer(host, port)):
                 self.addNewPeer(Peer(host, port))
                 print("[+] Found a new bot at", host + ":" + str(port))
         except:
             pass
Example #19
0
    def _start_peer(self, connection, address, remote_pubkey=None):
        # create peer
        peer = Peer(self, connection, remote_pubkey=remote_pubkey)
        log.debug('created new peer', peer=peer, fno=connection.fileno())
        self.peers.append(peer)

        # loop
        peer.start()
        log.debug('peer started', peer=peer, fno=connection.fileno())
        assert not connection.closed
        return peer
Example #20
0
    def _start_peer(self, connection, address, remote_pubkey=None):
        log.debug('new connect',
                  connection=connection,
                  incoming=bool(not remote_pubkey))
        # create peer
        peer = Peer(self, connection, remote_pubkey=remote_pubkey)
        log.debug('created new peer', peer=peer)
        self.peers.append(peer)

        # loop
        peer.start()
        log.debug('peer started', peer=peer)
Example #21
0
class Player(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.id = "1000-" + str(random.randint(0, 100))
        self.peer = Peer(self.id, self.resource_loaded)
        self.playlist_uri = "http://cdn.bem.tv/stream/soccer2sec/soccer/playlist.m3u8"
        self.player_buffer = []
        self.start()

    def run(self):
        segments = m3u8.load(self.playlist_uri).segments
        self.segment_duration = segments[0].duration
        self.playlist = [str(seg).split("\n")[1] for seg in segments]
        self.running = True
        self.startup()
        self.start_polling_and_fetching_chunks()

    def startup(self):
        logger.info(self.id, "Player: Buffering")
        for segment in self.playlist[7:]:
            self.request_resource(segment)

    def start_polling_and_fetching_chunks(self):
        logger.info(
            self.id, "Player: Fetching playlist and chunks every " +
            str(self.segment_duration) + "seconds")
        while self.running:
            time.sleep(self.segment_duration)
            last_segments = [
                str(seg).split("\n")[1]
                for seg in m3u8.load(self.playlist_uri).segments
            ][7:]
            [
                self.request_resource(seg) for seg in last_segments
                if seg not in self.player_buffer
            ]

    def request_resource(self, segment):
        #        logger.info(self.id, "Player: requesting " + segment);
        self.peer.download_chunk(segment)

    def resource_loaded(self, segment):
        #        logger.info(self.id, "Player: resource loaded " + segment);
        self.player_buffer.append(segment)

    def connect(self, other_player):
        self.peer.add_peer(other_player.peer)

    def shutdown(self):
        #        logger.info(self.id, "Player Stats:" + str(self.peer.stats)  + " Player buffer: " + str(sorted(self.player_buffer)))
        self.running = False
Example #22
0
def main():
    # Read options from command line
    opts = readOptions(sys.argv[1:])

    # Init peer
    mypeer = Peer(opts.id, opts.maxpeers, opts.port, opts.ttl, opts.verbose)

    # Add list of known peers
    for p in opts.peers:
        pfull = p.split(':')
        mypeer.addpeer(pfull[0], pfull[1], int(pfull[2]))

    # Add handlers
    for m in messages:
        mypeer.addhandler(m, messages[m].handler)

    # Add router
    mypeer.addrouter(distancerouter)

    # Start peer server in a separate thread
    t = threading.Thread( target = mypeer.start, args = [opts.backlog, opts.timeout] )
    t.daemon = True
    t.start()
    print(f'Peer {mypeer.host}:{mypeer.port} started')

    # Start client
    menu()
    try:
        for line in sys.stdin:
            line = line.rstrip().split(' ', 2)
            msgtype = line[0].upper()
            target = line[1]
            if msgtype not in messages or not messages[msgtype].iscallable:
                print('Wrong message type. Please try again.')
                continue
            data = ''
            if len(line) == 3:
                data = line[2]
            if messages[msgtype].isasync:
                # Need to add own info as sender for async messages, so receiver can respond
                data = json.dumps({'data': data, 'sender': mypeer.peerid})
            res = mypeer.sendtopeer(target, msgtype, data, not messages[msgtype].isasync)
            for r in res:
                if r[0] == 'ERRO':
                    print(f'{r[1]}')
                    break
                if r[0] == 'RESP':
                    print(f'[{target}] -> {r[1]}')
                    continue
                print('Unable to process response.')
                continue
            menu()

    except KeyboardInterrupt:
        exit()
Example #23
0
	def __init__(self, port, id, host = "127.0.0.1", bootstrap = True):
		Peer.__init__(self, id, port, host, False, True)
		handlers = {"TEST": self.__handle_test, 
					"PING": self.__handle_ping, 
					"PONG": self.__handle_pong,
					"BROADCAST": self.__handle_broadcast,
					"ADVERTISE": self.__handle_advt_peers,
					"ADVERTISE_REPLY": self.__handle_add_to_peers}
		for h in handlers:
			self.add_handler(h, handlers[h])

		self.recvd_messages = []

		if bootstrap:
			self.bootstrap(BOOTSTRAP_LIST)
 def create_peers(self):
     peers = []
     for p in self.tracker.parse_peer_address():
         if p[0] == self.ip:
             continue
         peers.append(Peer(p[0], p[1], self))
     return peers
Example #25
0
    def __init__(self, config_file):
        print "Initialize the RouteOPS Server"
        self.relax_announce = True
        # Init the SDNRoute Server
        ## Parse Config
        self.sdnrouter = parse_config(config_file)
        self.peer = Peer()
        # MongoIPC agent<->server
        self.sdnrouter.server_ipc = \
            lib.MongoIPC.MongoIPCMessageService(MONGO_ADDRESS, MONGO_DB_NAME, SERVER_ID, Thread, time.sleep)
        self.sdnrouter.server_ipc.listen(AGENT_SERVER_CHANNEL, False)
        self.sdnrouter.server_ipc.listen(SERVER_PROXY_CHANNEL, False)

        try:
            pass
            #db = mongo.Connection(MONGO_ADDRESS)[MONGO_DB_NAME]
            #self.sdnrouter.db_peers = db[TABLE_PEERS]
            #for participant in self.sdnrouter.participants:
            #    res = None
            #    res = self.sdnrouter.db_peers.find_one({'IP': self.sdnrouter.participants[participant]['IP']})
            #    if res:
            #        pass
            #    else:
            #        self.sdnrouter.db_peers.insert(self.sdnrouter.participants[participant])
        except Queue.Empty:
            pass
        except:
            pass

        self.run = True
Example #26
0
 def __init__(self):
     Thread.__init__(self)
     self.id = "eb5bbe4c-3b4a-41a8-9092-" + str(random.randint(0, 100))
     self.peer = Peer(self.id, self.resource_loaded)
     self.playlist_uri = "http://cdn.bem.tv/stream/soccer4sec/soccer/playlist.m3u8"
     self.player_buffer = []
     self.start()
    def handlePeerArrival(self, name, connection):
        request = connection.recv(1024)
        request = request.decode()
        print(F"Incoming Request: {request}")

        if (request[:3] == "NEW"
            ):  # NEW command is for new peers connecting to the server
            peerID = (len(self.connectedPeers) + 1)
            connection.send(f"{peerID}".encode())
            information = request[4:].split(":")
            # we save the information of the new peer and store a Peer object inside the dirctionary
            self.connectedPeers[peerID] = Peer(str(information[0]),
                                               portNumber=int(information[1]))

        elif (request[:3] == "REQ"):  # REQ command is for requesting a file
            requestedFile = request[4:]
            if Server.fileExists(f"Server_files/{requestedFile}"):
                self.sendFile("", connection,
                              f"./Server_files/{requestedFile}")

            elif Tracker.manifestExists(requestedFile):
                self.sendManifestFile(connection, requestedFile)
            else:
                connection.send("ERR".encode())
            connection.close()

        for peer in self.connectedPeers:
            print(
                f"Name: {self.connectedPeers[peer].name} - Port: {self.connectedPeers[peer].portNo}"
            )
Example #28
0
    def got_handshake(self, data):
        if options.verbose > 2:
            logging.info('got handshake %s' % [data])
        self.handshake = parse_handshake(data)
        if options.verbose > 1:
            logging.info('parsed handshake %s' % [self.handshake])
        if self.handshake:
            self.peerid = self.handshake['peerid']
            self.peer = Peer.instantiate({'peerid':self.peerid})
            self.infohash = self.handshake['infohash']
            if not self.torrent:

                # check if this torrent is started, and in the current client's list of torrents!

                #self.torrent = Torrent.instantiate( binascii.hexlify(self.handshake['infohash']) )
                self.torrent = Torrent.instantiate( self.handshake['infohash'] )
                self.torrent.connections.append(self)
                logging.info('connection has torrent %s with hash %s%s' % (self.torrent, [self.torrent.hash], ' (with metadata)' if self.torrent.meta else ''))
                if not self._sent_handshake:
                    self.send_handshake()
                if not self._sent_extension_handshake:
                    self.send_extension_handshake()
                if self.torrent and self.torrent.meta:
                    self.send_bitmask()
                self.get_more_messages()
            else:
                self.get_more_messages()
        else:
            logging.info('invalid/unrecognized handshake')
            self.stream.close()
Example #29
0
  def start(self, response=None, mechanism=None, locale="en_US", tune_params=None,
            username=None, password=None,
            client_properties=None, connection_options=None, sasl_options = None,
            channel_options=None):
    if response is not None and (username is not None or password is not None):
      raise RuntimeError("client must not specify both response and (username, password).")
    if response is not None:
      self.response = response
      authzid, self.username, self.password = response.split("\0")
    else:
      self.username = username
      self.password = password
    self.mechanism = mechanism
    self.locale = locale
    self.tune_params = tune_params
    self.client_properties=get_client_properties_with_defaults(provided_client_properties=client_properties, version_property_key="version")
    self.sasl_options = sasl_options
    self.socket = connect(self.host, self.port, connection_options)
    self.conn = Connection(self.socket, self.spec)
    self.peer = Peer(self.conn, ClientDelegate(self), Session, channel_options)

    self.conn.init()
    self.peer.start()
    self.wait()
    self.channel(0).connection_open(self.vhost)
Example #30
0
 def __init__(self, bootstrap, miner, difficulty):
     """Allocate the backend storage of the high level API, i.e.,
     your blockchain. Depending whether or not the miner flag has
     been specified, you should allocate the mining process.
     """
     self._peer = Peer(bootstrap)
     self._blockchain = Blockchain()
Example #31
0
 def run(self):
     while True:
         try:
             msg, addr = self.hQueue.get(False)
             newPeer = Peer(msg, addr[0], addr[1])
             # Create a new peer based on the username, which is given by the text before a semicolon, and the address.
             exists = self.existInList(newPeer)
             # If the peer exists, an index is returned. Otherwise, -1 is returned and we need to add the peer.
             if exists == 0:
                 continue
             elif exists < 0:
                 self.peerlist.append(newPeer)
                 print("\n" + newPeer.username + " has come online.")
                 self.timers.append(Timer(15.0, self.delete_peer,
                                          [newPeer]))
                 self.timers[-1].start()
             else:
                 try:
                     self.timers[exists - 1].cancel()
                     # If the peer already exists, cancel the current timer
                     self.timers[exists - 1] = Timer(
                         15.0, self.delete_peer, [newPeer])
                     # and start a new one
                     self.timers[exists - 1].start()
                 except IndexError as ie:
                     print(ie)
         except Empty:
             continue
Example #32
0
    def add_peers(self, message_body):
        for peer_data in message_body.values():
            try:
                if peer_data['id'] != self.my_id and peer_data[
                        'id'] not in self.peers:
                    tcp_connection = TcpConnection()
                    tcp_connection.connect(peer_data['ip'], peer_data['port'])
                    tcp_connection.send(
                        (19).to_bytes(CODE_SIZE, byteorder='big') +
                        (self.info_hash + self.my_id).encode('utf-8'))

                    if int.from_bytes(tcp_connection.receive(),
                                      byteorder='big') == 11:
                        peer = Peer.start(peer_data['id'], tcp_connection,
                                          self.actor_ref, self.file_object,
                                          self.pieces_map)
                        self.peers[peer_data['id']] = {
                            'actor_ref': peer,
                            'downloaded_bytes': 0,
                            'state': 'not_interested'
                        }
                        print(
                            colored(
                                f"added new peer with id: {peer_data['id']}",
                                'yellow'))
                        if len(self.peers) == 1:
                            self.timer = Timer(TIMER_INTERVAL, self.actor_ref)
                            self.timer.daemon = True
                            self.timer.start()
            except:
                pass
Example #33
0
 def __init__(self, data: dict):
     self.failure_reason = data.get('failure reason')
     self.warning_message = data.get('warning message')
     self.interval = data.get('interval')
     self.min_interval = data.get('min interval')
     self.tracker_id = data.get('tracker id')
     self.complete = data.get('complete')
     self.incomplete = data.get('incomplete')
     peers = data.get('peers')
     if isinstance(peers, list):
         self.peers = [Peer(peer) for peer in peers]
     elif isinstance(peers, bytes):
         peers = [peers[i:i + 6] for i in range(len(peers))]
         self.peers = [Peer(peer) for peer in peers]
     else:
         self.peers = None
    def handle_commands(self, command):

        if (command in ['help', '?', '/?', 'h']):
            self.cli.log('\nAvailable commands:')

            self.cli.log('  peer add HOST:PORT')
            self.cli.log('  peer remove HOST:PORT')

            self.cli.log('  track list')
            self.cli.log('  track get SHORT_HASH')
            return

        elif (command == 'exit' or command == 'quit'):
            self.cli.log('Exiting...')

            # Do some work to notify neighboring nodes that you're going down?

            sys.exit()

        tokens = command.split()
        if (len(tokens) == 0):
            return

        if (tokens[0] == 'peer'):
            if (len(tokens) < 3 or tokens[1] not in ['add', 'remove']):
                self.cli.log('usage: peer add|remove HOST:PORT')

            else:
                host, port = tokens[2].split(':')
                peer = Peer(self.cli, host, port)
                self.peer_manipulate(peer, tokens[1])

        elif (tokens[0] == 'track'):
            if (len(tokens) < 2 or tokens[1] not in ['list', 'get']
                    or (tokens[1] == 'get' and len(tokens) < 3)):
                self.cli.log('Usage:')
                self.cli.log('  track list')
                self.cli.log('  track get SHORT_HASH')

            elif (tokens[1] == 'list'):

                self.do_track_list_update()

            elif (tokens[1] == 'get'):

                short_hash = tokens[2]

                if (len(short_hash) != constant.HASH_LEN):
                    self.cli.log(
                        f'SHORT_HASH should be the {constant.HASH_LEN} character prefix to the left of the track'
                    )
                    return

                track = self.all_tracks_sh[short_hash]
                track.download(self.cli)

                self.update_tracks()

        else:
            self.cli.log('Invalid command. Type "help" for available commands')
Example #35
0
 def create_peers(self):
     peers = []
     for p in self.tracker.peer_list:
         if p[0] == self.torrent.ip:
             continue
         peers.append(Peer(p[0], p[1], self))
     return peers
Example #36
0
  def __init__(self):
    init_balances = {}
    for i in xrange(Parameters.num_peers):
      init_balances["P_" + str(i)] = Parameters.start_balance

    self.gen_block = Block("B_-1", 0, init_balances, {}, {}, "None")
    self.nodes = [Peer("P_" + str(i), self.get_delay, self.gen_block) for i in xrange(Parameters.num_peers)]
    self.node_is_slow = dict()
    self.network_graph = self.generate_graph()
    self.assign_neighbours()
    for i in xrange(Parameters.num_peers):
      pid = "P_" + str(i)
      self.node_is_slow[pid] = (random.random() < Parameters.z)
    # testing str of peers.
    t = threading.Timer(5, self.nodes[0].write_to_file) 
    t.start()
    #for tree generation
    if _ete2_import:
      self.nst = [NodeStyle() for i in xrange(Parameters.num_peers)]
      self.fnst = [NodeStyle() for i in xrange(Parameters.num_peers)]
      for i in xrange(Parameters.num_peers):
        self.nst[i]["bgcolor"] = "#" + str(hex(random.randint(128,255)))[2:] + str(hex(random.randint(128,255)))[2:] + str(hex(random.randint(128,255)))[2:]
      for i in xrange(Parameters.num_peers):
        self.fnst[i]["size"] = 15
        self.fnst[i]["fgcolor"] = self.nst[i]["bgcolor"]      
      self.ts = TreeStyle()
      # self.ts.mode = "c" #circle
      self.ts.show_leaf_name = False
      def my_layout(node):
        F = TextFace(node.name, tight_text=True)
        add_face_to_node(F, node, column=0, position="branch-right")
      self.ts.layout_fn = my_layout
Example #37
0
async def download():

    print(
        "Proceso de descarga inicializado. \n\nEs posible que se deba esperar un tiempo prologando para la obtencion de pieces para la descarga.\n"
    )

    peer_addresses = await Tracker(torrent).begin_connection()

    # asignacion de la clase encargada de gestionar las piezas, como la descarga que sera utilizada por cada
    # valido para hacer la descarga

    pieces = Pieces(torrent, torrent_writer.get_received_pieces_queue())

    # lista de peers intanciados con su parametro de torrent, puerto, host
    # y descarga previamente creada.

    peers = []

    for host, port in peer_addresses:
        peers.append(Peer(torrent, host, port, pieces, torrent_writer))

    # lista de ejecucion de la descarga realizada para cada peer,
    # con su espera hasta que el mismo responda.

    for peer in peers:
        peer.pieces.peerList = peers
        await peer.download()
Example #38
0
	def __init__(self):
		super(MainInterface, self).__init__()
		self.peer = Peer()
		self.peer.set_parent(self)
		self.peer.start()
		self.initUI()
		self.initSignals()
		self.initData()
Example #39
0
def get_peers(peer_val, elts, length):
    peers = []
    if (isinstance(peer_val, basestring)):
        # Compact response (binary)
        i = 0
        while (i < len(peer_val)):
            curr_peer = peer_val[i:i + 6]
            ip_addr = socket.inet_ntoa(curr_peer[0:4])
            port = int(unpack("!H", curr_peer[4:6])[0])
            peers.append(Peer(ip_addr, port, elts, length))
            i += 6
        return peers
    else:
        # Dictionary repsonse???
        for peer in peer_val:
            peers.append(Peer(peer['ip'], peer['port'], elts, length))
        return peers
Example #40
0
def managed_peer(name, env):
    p = Peer(name, env)
    p.services.append(ConnectionManager(p))
    p.services.append(PeerRequestHandler())
    p.services.append(PingHandler())
    p.services.append(Downtime(env, p))
    p.services.append(Slowdown(env, p))
    return p
Example #41
0
File: dak.py Project: 0xNaN/dak
def main():
    parser = argparse.ArgumentParser(description='a simple Kademlia implementation')
    parser.add_argument('-p', '--port', help = "specify the port to use")
    parser.add_argument('--to', help = "specify the destination of a request. e.g: <address>:<port>, <port>")
    parser.add_argument('--listen', action = 'store_true', help = "keep up the peer to listen incoming requests")
    parser.add_argument('--ping', action = 'store_true', help = "perform a PING request on the target peer")
    parser.add_argument('--local-find-node', metavar = "NODE_ID", help = "perform a FIND-NODE request on a local peer")
    args = parser.parse_args()

    node = Node(20)
    port = random.randint(2000, 8000) if args.port == None else int(args.port)
    peer = Peer(node, port)

    log('Peer started at port {}'.format(port))
    log('Node id {}'.format(node.getid()))

    toAddr = ""
    if(args.to != None):
        if args.to.__contains__(':'):
            toAddr, toPort = args.to.split(":")
        else:
            toPort = args.to
        toPort = int(toPort)

    if(args.ping and args.to != None):
        peer.ping(toAddr, toPort)

    if(args.local_find_node != None and args.to != None):
        peer.local_find_node(args.local_find_node, toPort)

    if(args.listen):
        peer.listen()
Example #42
0
 def add_peer(self, connection, host, port):
     # FIXME: should check existance first
     connection.settimeout(.1)
     try:
         peer = Peer(connection, host, port)
         peer.start()
         with self.lock:
             self.connected_peers.add(peer)
         logger.debug(
             "new TCP connection {0} {1}:{2}"
             .format(connection, host, port))
     except BaseException as e:
         logger.error(
             "cannot start TCP session \"{0}\" {1}:{2} "
             .format(str(e), host, port))
         traceback.print_exc(file=sys.stdout)
         connection.close()
         time.sleep(0.1)
     return peer
Example #43
0
    def new_connection(self, addr, sock):
        print 'connection from %s' % addr
        if addr in self.enemies:
            return

        elif addr in self.peers:
            self.peers[addr].recv_connect(sock)

        else:
            self.peers[addr] = Peer.from_socket(self, addr, sock)
Example #44
0
class Player(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.id = "eb5bbe4c-3b4a-41a8-9092-" + str(random.randint(0, 100))
        self.peer = Peer(self.id, self.resource_loaded)
        self.playlist_uri = "http://cdn.bem.tv/stream/soccer4sec/soccer/playlist.m3u8"
        self.player_buffer = []
        self.start()

    def run(self):
        segments = m3u8.load(self.playlist_uri).segments
        self.segment_duration = segments[0].duration
        self.playlist = [str(seg).split("\n")[1] for seg in segments]
        self.running = True
        self.startup()
        self.start_polling_and_fetching_chunks()

    def startup(self):
        logger.info(self.id ,"Player: Buffering")
        for segment in self.playlist[7:]:
            self.request_resource(segment)

    def start_polling_and_fetching_chunks(self):
        logger.info(self.id, "Player: Fetching playlist and chunks every " + str(self.segment_duration) +  "seconds")
        while self.running:
            time.sleep(self.segment_duration)
            last_segments = [str(seg).split("\n")[1] for seg in m3u8.load(self.playlist_uri).segments][7:]
            [self.request_resource(seg) for seg in last_segments if seg not in self.player_buffer]

    def request_resource(self, segment):
        logger.info(self.id, "Player: requesting " + segment);
        self.peer.download_chunk(segment)

    def resource_loaded(self, segment):
        logger.info(self.id, "Player: resource loaded " + segment);
        self.player_buffer.append(segment)

    def connect(self, other_player):
        self.peer.add_peer(other_player.peer)

    def shutdown(self):
        logger.info(self.id, "Player Stats:" + str(self.peer.stats)  + " Player buffer: " + str(sorted(self.player_buffer)))
        self.running = False
Example #45
0
    def add_friend(self, addr, name):
        if addr in self.enemies:
            self.enemies.remove(addr)

        if addr not in self.friends:
            self.friends[addr] = name

            if addr not in self.peers:
                self.peers[addr] = Peer.from_addr(self, addr, name)

        return name, self.peers[addr]
Example #46
0
  def __init__(self, maxPeers, serverPort):
 
 
      Peer.__init__(self, maxPeers, serverPort)
      
      self.files = {}  # available files: name --> peerid mapping
  
      self.addRouter(self.__router)
  
      handlers = {LISTPEERS : self.__handle_listpeers,
              INSERTPEER : self.__handle_insertpeer,
              PEERNAME: self.__handle_peername,
              QUERY: self.__handle_query,
              QRESPONSE: self.__handle_qresponse,
              FILEGET: self.__handle_fileget,
              PEERQUIT: self.__handle_quit,
              SUPERPEER: self.__handle_superpeer
             }
      for mt in handlers:
          self.addHandler(mt, handlers[mt])
Example #47
0
def main():

    torrent = Torrent('data/ubuntu.torrent')

    td = deepcopy(torrent.torrent_file_dict)

    td[b'info'][b'pieces'] = '20 byte pieces hashes go here...'

    pp(td)
    print('Number of pieces in the file:', torrent.num_pieces)
    print('Size of download in bytes: ', torrent.get_download_length())

    input()

    peers_info = PeersGetter(torrent).parse_peers()

    writers = []
    readers = []

    peers_connected_to_count = 0

    # Build connected peers
    for peer_info in peers_info:
        if peers_connected_to_count > 3:
            break
        peers_connected_to_count += 1

        peer = Peer(torrent, peer_info=peer_info)
        peer.connect()

        if peer.is_connected:
            writers.append(peer)
            readers.append(peer)


    while not torrent.is_download_finished():
        # print(
        #     'Downloading... Writers: {} Readers: {}'.format(
        #         len(readers), len(writers)
        #     )
        # )

        to_read, to_write, errors = select.select(readers, writers, readers)

        for peer in to_read:
            peer.read()

        for peer in to_write:
            peer.write()

        for peer in errors:
            readers.remove(peer)
            writers.remove(peer)
Example #48
0
  def start(self, response, mechanism="AMQPLAIN", locale="en_US", tune_params=None):
    self.mechanism = mechanism
    self.response = response
    self.locale = locale
    self.tune_params = tune_params

    self.socket = connect(self.host, self.port)
    self.conn = Connection(self.socket, self.spec)
    self.peer = Peer(self.conn, ClientDelegate(self), Session)

    self.conn.init()
    self.peer.start()
    self.wait()
    self.channel(0).connection_open(self.vhost)
Example #49
0
    def handle_pex(self):
        info = bencode.bdecode(self.request.payload[1:])
        if 'added' in info:
            num_added = len(info['added'])/6

        peers = []
        for i in range(num_added):
            peerinfo = info['added'][6*i:6*(i+1)]
            peer = Peer.instantiate( {'compact':decode_peer(peerinfo)} )
            peers.append(peer)

        if 'added_f' in info:
            for i in range(num_added):
                flags = list(bin(ord(info['added_f'][i]))[2:])
                flags = map(lambda x:x=='1', flags)
                peers[i].add_flags(flags)

        self.request.connection.torrent.handle_pex(peers, raw=info)
Example #50
0
	for edge in edgeSet:
		str += edge + os.linesep
	str += "}"
	return str


parser = argparse.ArgumentParser(description='Simple XML-RPC Peer')
parser.add_argument('IP', type=str, help='IP Address to listen on')
parser.add_argument('port', type=int, help='Port to listen on')
parser.add_argument('name', type=str, help='My name')
parser.add_argument('limit', type=str, help='My peer limit')
parser.add_argument('-late', default=.0, type=float, help='Artifical Latency in RPC requests')

args = parser.parse_args()

thisPeer = Peer(args.name, args.IP, args.port, args.limit, args.late)
thisPeer.addResource(args.name, args.name + ' is interactive peer')

while True:
	do = input('>')

	# PEERS
	match = re.match("plist", do)
	if match:
		pprint.pprint(thisPeer.listPeers())
		continue

	match = re.match("hello", do)
	if match:
		thisPeer.multicast()
		thisPeer.startListeningForMulticast()
Example #51
0
 def __init__(self, peer_dict, torrent, brain):
     Peer.__init__(self, peer_dict, torrent, brain.my_id)
     self.brain = brain
	def __init__(self, name, transport=None):
		Peer.__init__(self, name, transport)
		self.age = 0
Example #53
0
 def test_init(self):
     mypeer = Peer('127.0.0.1', 6881, client)
     mypeer.buf = ''
     self.assertEqual(mypeer.buf, '')
Example #54
0
 def receive_incoming_connection(self, s, ip, port):
     logging.info('receiving incoming connection from %s:%d', ip, port)
     p = Peer((ip, port), client=self)
     p.respond(s)
     self.pending_connections.append(p)
Example #55
0
 def test_buffer_plus_message_unchoke(self):
     mypeer = Peer('127.0.0.1', 6881, client)
     mypeer.buf = ''
     mypeer.process_and_act_on_incoming_data('\x00\x00\x00\x01\x01\xff')
     self.assertEqual(mypeer.buf, '\xff')
     self.assertFalse(mypeer.peer_is_choking_client)
Example #56
0
 def test_init_nonempty(self):
     mypeer = Peer('127.0.0.1', 6881, client)
     mypeer.buf = '\x00'
     self.assertEqual(mypeer.buf, '\x00')
Example #57
0
			str += "\"" + node + "\"" + getColour(heatmap, heatmap[node]) + ";" + os.linesep
	for edge in edgeSet:
		str += edge + os.linesep
	str += "}"
	return str


parser = argparse.ArgumentParser(description='Simple XML-RPC Peer')
parser.add_argument('IP', type=str, help='IP Address to listen on')
parser.add_argument('port', type=int, help='Port to listen on')
parser.add_argument('name', type=str, help='My name')
parser.add_argument('limit', type=str, help='My peer limit')

args = parser.parse_args()

thisPeer = Peer(args.name, args.IP, args.port, args.limit)
thisPeer.addResource(args.name, args.name + ' is interactive peer')

while True:
	do = input()

	# PEERS
	match = re.match("plist", do)
	if match:
		pprint.pprint(thisPeer.listPeers())
		continue

	match = re.match("hello", do)
	if match:
		thisPeer.multicast()
		thisPeer.startListeningForMulticast()
Example #58
0
 def _start_peer(self, connection, ip, port):
     peer = Peer(connection, ip, port)
     peer.start()
     return peer
Example #59
0
 def test_buffer_plus_message_keep_alive(self):
     mypeer = Peer('127.0.0.1', 6881, client)
     mypeer.buf = ''
     mypeer.process_and_act_on_incoming_data('\x00\x00\x00\x00\x00')
     self.assertEqual(mypeer.buf, '\x00')