ppenet / ppenet / host.py

import socket
from Queue import Queue
import protocol
from peer import Peer, State

HOST_ANY = 0
HOST_BROADCAST = 0xFFFFFFFF
PORT_ANY = 0

BANDWIDTH_THROTTLE_INTERVAL = 1000
DEFAULT_MTU = 1400
RECEIVE_BUFFER_SIZE = 256 * 1024
SEND_BUFFER_SIZE = 256 * 1024


class Host(object):
    def __init__(self, address=None, peer_count=0, channel_limit=0,
                 incoming_bandwidth=0, outgoing_bandwidth=0):
        self.socket = s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        if address is not None:
            address = socket.gethostbyname(address[0]), address[1:]
            s.bind(address)
        self.address = address
        # disable blocking read / write
        s.setblocking(0)
        # enable sending / receiving broadcast packets
        s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        # set buffers size
        s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, RECEIVE_BUFFER_SIZE)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, SEND_BUFFER_SIZE)
        self.peers = []
        if not peer_count:
            self.peer_count = protocol.MAXIMUM_PEER_ID
        else:
            self.peer_count = min(peer_count, protocol.MAXIMUM_PEER_ID)
        if not channel_limit:
            self.channel_limit = protocol.MAXIMUM_CHANNEL_COUNT
        else:
            self.channel_limit = max(min(channel_limit,
                                         protocol.MAXIMUM_CHANNEL_COUNT),
                                     protocol.MAXIMUM_CHANNEL_COUNT)
        self.incoming_bandwidth = incoming_bandwidth
        self.outgoing_bandwidth = outgoing_bandwidth
        self.bandwidth_throttle_epoch = 0
        self.recalculate_bandwidth_limits = 0
        self.mtu = self.DEFAULT_MTU
        self.commands = []
        self.command_count = 0
        self.buffer = bytearray()
        self.buffer_count = 0
        self.checksum = None
        self.received_address = (HOST_ANY, PORT_ANY)
        self.received_data = None
        self.total_sent_data = 0
        self.total_sent_packets = 0
        self.total_received_data = 0
        self.total_received_packets = 0
        self.compressor = None
        self.dispatch_queue = Queue()

    def __del__(self):
        for peer in self.peers:
            peer.reset()
        self.socket.close()

    def __repr__(self):
        return '%s(%s, %d, %d, %d, %d)' % (
            self.__name__, self.address, self.peer_count, self.channel_limit,
            self.incoming_bandwidth, self.outgoing_bandwidth)

    def __str__(self):
        return '%s %s sent: %d recv: %d' % (
            self.__name__, self.address,
            self.total_sent_packets, self.total_received_packets)

    def connect(self, address, channel_count=0, data=0):
        for peer in self.peers:
            if peer.state == State.DISCONNECTED:
                break
        else:  # No empty slot for new peer
            return
        return self.peers.append(Peer(self, address, channel_count, data))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.