def __init__(self, batchsize, buffersizeA, buffersizeB, buffersizeC):
        self.batchsize = batchsize
        self.buffersizeA = buffersizeA
        self.buffersizeB = buffersizeB
        self.buffersizeC = buffersizeC
        self.samples = []

        for i, seed in enumerate(seeds):
            # print('Running {}th simulation at {}'.format(i, str(time() - start_time)))
            factory = Factory(11, 8, self.batchsize, self.buffersizeA,
                              self.buffersizeB, self.buffersizeC, seed, duration, silent=True)
            factory.play()
            factory.start()

            factory.update_stats()
            sample = Sample(seed,
                            factory.stats['time'],
                            factory.stats['total produced'],
                            factory.stats['average produced'],
                            factory.stats['average throughput'])

            self.samples.append(sample)

        self.mean_produced, self.deviation_produced, *self.confidence_interval_produced = (
            self.mean_confidence_interval([float(s.total_produced) for s in self.samples])
        )

        self.mean_throughput, self.deviation_throughput, *self.confidence_interval_throughput = (
            self.mean_confidence_interval([float(s.average_throughput) for s in self.samples])
        )
Exemple #2
0
class Khashmir:
    """
    Khashmir API
    """
    def __init__(self, host, port, dbDir = 'khashmir', ipv6_enable = False, upnp = 0, natpmp = False):
        self.host = host
        self.port = port
        self.factory = Factory(host, port, dbDir, ipv6_enable, upnp, natpmp)        
        
    def start(self):
        """
        Start DHT
        """
        if DEBUG:
            print "DHT: Starting"
        self.factory.start()
            
    def close(self):
        """
        Close DHT 
        """
        if DEBUG:
            print "DHT: Closing"
        self.factory.rawserver.add_task_prio(self.factory._close)

    def stats(self):
        """
        Returns the number of contacts in our routing table
        """
        return self.factory.stats()  

    def addContacts(self, nodes):
        for node in nodes:
            self.addContact(node[0], node[1])

    def addContact(self, host, port):
        """
        Ping this node and add the contact info to the table on pong!
        """
        if DEBUG:
            print "DHT: Adding contact - ", host, port
        return self.factory.addContact(host, port)
    
    def getPeers(self, infohash, callback, searchlocal = True, donecallback = None):
        """
        NOTE: USE getPeersAndAnnounce
        Returns the peers found for the infohash in global table
        callback will be called with a list of values for each peer that returns unique values
        final callback will be an empty list
        """
        if DEBUG:
            print "DHT: getPeers"
        self.factory.rawserver.add_task(self.factory.getPeers, 0, [infohash, callback, searchlocal, donecallback])

    def announcePeer(self, infohash, port, callback=None):
        """
        NOTE: USE getPeersAndAnnounce
        Announce that you're downloading the a torrent on a port
        @param callback - indicates nodes we got a response from
        """
        if DEBUG:
            print "DHT: announcePeer"
        self.factory.rawserver.add_task(self.factory.announcePeer, 0, [infohash, port, callback])

    def getPeersAndAnnounce(self, infohash, port, callback, searchlocal = True):
        """
        This is the method that is actually used ;)
        A combination of getPeers and announcePeer
        NOTE: keys expire every 30 minutes, so calling this method sooner is a good idea.
        """
        if DEBUG:
            print "DHT: getPeersAndAnnounce"

        def callback_wrapper(peers):
            if DEBUG:
                print "DHT: got %d peers" % len(peers)
            callback(peers)
            
        self.factory.rawserver.add_task(self.factory.getPeersAndAnnounce, 0, [infohash, port, callback_wrapper, searchlocal])