示例#1
0
    def connection_ready(self, stream):
        LOG.progress()

        m = compat.RendezvousRequest()
        m.accept.append("speedtest")
        m.accept.append("bittorrent")
        m.version = CONFIG["rendezvous.client.version"]

        request = Message()
        request.compose(method="GET", pathquery="/rendezvous",
          mimetype="text/xml", keepalive=False, host=self.host_header,
          body=marshal.marshal_object(m, "text/xml"))

        stream.send_request(request)
示例#2
0
 def got_response(self, stream, request, response):
     if self.finished:
         stream.close()
         return
     LOG.progress()
     if response.code not in ("200", "206"):
         stream.close()
         self.cleanup("bad response code")
     else:
         try:
             self.child.got_response(stream, request, response)
         except (KeyboardInterrupt, SystemExit):
             raise
         except:
             LOG.exception()
             stream.close()
             self.cleanup("unexpected exception")
         else:
             self.streams.append(stream)
             self.update()
示例#3
0
    def connection_ready(self, stream):
        LOG.progress()

        m = compat.RendezvousRequest()
        m.accept.append("speedtest")
        m.accept.append("bittorrent")
        m.version = CONFIG["rendezvous.client.version"]
        m.privacy_informed = CONFIG["privacy.informed"]
        m.privacy_can_collect = CONFIG["privacy.can_collect"]
        m.privacy_can_share = CONFIG["privacy.can_publish"]  # XXX

        request = Message()
        request.compose(
            method="GET",
            pathquery="/rendezvous",
            mimetype="text/xml",
            keepalive=False,
            host=self.host_header,
            body=marshal.marshal_object(m, "text/xml"),
        )

        stream.send_request(request)
示例#4
0
import logging
import sys

if __name__ == "__main__":
    sys.path.insert(0, ".")

from neubot.log import LOG
from neubot.log import _log_info
from neubot import compat

if __name__ == "__main__":
    # Make sure the hackish name substitution works
    assert(logging.info == _log_info)

    LOG.start("Testing the in-progress feature")
    LOG.progress("...")
    LOG.progress()
    LOG.complete("success!")

    logging.info("INFO w/ logging.info")
    # The following should work because it should not interpolate
    logging.debug("DEBUG w/ logging.debug", "ciao")
    logging.warning("WARNING w/ logging.warning")
    logging.error("ERROR w/ logging.error")

    LOG.verbose()

    logging.info("INFO w/ logging.info")
    logging.debug("DEBUG w/ logging.debug")
    logging.warning("WARNING w/ logging.warning")
    logging.error("ERROR w/ logging.error")
示例#5
0
    def update(self):
        if self.finished:
            return

        #
        # Decide whether we can transition to the next phase of
        # the speedtest or not.  Fall through to next request if
        # needed, or return to the caller and rewind the stack.
        #

        ostate = self.state

        if not self.state:
            self.state = "negotiate"
            del QUEUE_HISTORY[:]

        elif self.state == "negotiate":
            if self.conf.get("speedtest.client.unchoked", False):
                LOG.complete("authorized to take the test\n")
                self.state = "latency"
            elif "speedtest.client.queuepos" in self.conf:
                queuepos = self.conf["speedtest.client.queuepos"]
                LOG.complete("waiting in queue, pos %s\n" % queuepos)
                STATE.update("negotiate", {"queue_pos": queuepos})
                QUEUE_HISTORY.append(queuepos)

        elif self.state == "latency":
            tries = self.conf.get("speedtest.client.latency_tries", 10)
            if tries == 0:
                # Calculate average latency
                latency = self.conf["speedtest.client.latency"]
                latency = sum(latency) / len(latency)
                self.conf["speedtest.client.latency"] = latency
                # Advertise the result
                STATE.update("test_latency", utils.time_formatter(latency))
                LOG.complete("done, %s\n" % utils.time_formatter(latency))
                self.state = "download"
            else:
                self.conf["speedtest.client.latency_tries"] = tries - 1

        elif self.state in ("download", "upload"):
            if len(self.streams) == self.conf.get("speedtest.client.nconn", 1):

                # Calculate average speed
                speed = self.conf["speedtest.client.%s" % self.state]
                elapsed = (max(map(lambda t: t[1], speed)) -
                  min(map(lambda t: t[0], speed)))
                speed = sum(map(lambda t: t[2], speed)) / elapsed
                LOG.progress(".[%s,%s]." % (utils.time_formatter(elapsed),
                       utils.speed_formatter(speed)))

                #
                # O(N) loopless adaptation to the channel w/ memory
                # TODO bittorrent/peer.py implements an enhanced version
                # of this algorithm, with a cap to the max number of
                # subsequent tests.  In addition to that, the bittorrent
                # code also anticipates the update of target_bytes.
                #
                if elapsed > LO_THRESH:
                    ESTIMATE[self.state] *= TARGET/elapsed
                    self.conf["speedtest.client.%s" % self.state] = speed
                    # Advertise
                    STATE.update("test_%s" % self.state,
                      utils.speed_formatter(speed))
                    LOG.complete("done, %s\n" % utils.speed_formatter(speed))
                    if self.state == "download":
                        self.state = "upload"
                    else:
                        self.state = "collect"
                elif elapsed > LO_THRESH/3:
                    del self.conf["speedtest.client.%s" % self.state]
                    ESTIMATE[self.state] *= TARGET/elapsed
                else:
                    del self.conf["speedtest.client.%s" % self.state]
                    ESTIMATE[self.state] *= 2

            else:
                # Wait for all pending requests to complete
                return

        elif self.state == "collect":
            LOG.complete()
            self.cleanup()
            return

        else:
            raise RuntimeError("Invalid state")

        #
        # Perform state transition and run the next phase of the
        # speedtest.  Not all phases need to employ all the connection
        # with the upstream server.
        #

        if self.state == "negotiate":
            ctor, justone = ClientNegotiate, True
        elif self.state == "latency":
            ctor, justone = ClientLatency, True
        elif self.state == "download":
            ctor, justone = ClientDownload, False
        elif self.state == "upload":
            ctor, justone = ClientUpload, False
        elif self.state == "collect":
            ctor, justone = ClientCollect, True
        else:
            raise RuntimeError("Invalid state")

        if ostate != self.state:
            self.child = ctor(self.poller)
            self.child.configure(self.conf)
            self.child.host_header = self.host_header
            if self.state not in ("negotiate", "collect"):
                if ostate == "negotiate" and self.state == "latency":
                    STATE.update("test_latency", "---", publish=False)
                    STATE.update("test_download", "---", publish=False)
                    STATE.update("test_upload", "---", publish=False)
                    STATE.update("test", "speedtest")
            else:
                STATE.update(self.state)
            LOG.start("* speedtest: %s" % self.state)
        elif self.state == "negotiate":
            LOG.start("* speedtest: %s" % self.state)

        while self.streams:
            #
            # Override child Time-To-Connect (TTC) with our TTC
            # so for the child it's like it really performed the
            # connect(), not us.
            #
            self.child.rtts = self.rtts
            self.child.connection_ready(self.streams.popleft())
            if justone:
                break