def bittorrent_store(self, message): ''' Saves the results of a bittorrent test ''' DATABASE.connect() if DATABASE.readonly: logging.warning('backend_neubot: readonly database') return table_bittorrent.insert(DATABASE.connection(), message)
def collect(self, m): btid = _make_btid(m["ident"]) if btid not in AUTH_PEERS: raise NegotiatorEOF() d = m["request_body"] result = AUTH_PEERS[btid] # # Note that the following is not a bug: it's just that # the server saves results using the point of view of the # client, i.e. upload_speed _is_ client's upload speed. # d["timestamp"] = result["timestamp"] d["upload_speed"] = result["upload_speed"] if privacy.collect_allowed(d): table_bittorrent.insert(DATABASE.connection(), d) # # After we've saved the result into the dictionary we # can add extra information we would like to return to # the client. # d["target_bytes"] = result["target_bytes"] m["response_body"] = d
def got_response_collecting(self, stream, request, response): LOG.complete() if self.success: # # Always measure at the receiver because there is more # information at the receiver and also to make my friend # Enrico happier :-P. # The following is not a bug: it's just that the server # returns a result using the point of view of the client, # i.e. upload_speed is _our_ upload speed. # m = json.loads(response.body.read()) self.my_side["upload_speed"] = m["upload_speed"] upload = utils.speed_formatter(m["upload_speed"]) STATE.update("test_upload", upload) if privacy.collect_allowed(self.my_side): table_bittorrent.insert(DATABASE.connection(), self.my_side) # Update the upstream channel estimate target_bytes = int(m["target_bytes"]) if target_bytes > 0: estimate.UPLOAD = target_bytes stream.close()
def got_response_collecting(self, stream, request, response): logging.info("BitTorrent: collecting ... done") if self.success: # # Always measure at the receiver because there is more # information at the receiver and also to make my friend # Enrico happier :-P. # The following is not a bug: it's just that the server # returns a result using the point of view of the client, # i.e. upload_speed is _our_ upload speed. # m = json.loads(response.body.read()) self.my_side["upload_speed"] = m["upload_speed"] upload = utils.speed_formatter(m["upload_speed"]) STATE.update("test_progress", "100%", publish=False) STATE.update("test_upload", upload) logging.info('BitTorrent: upload speed: %s', upload) if privacy.collect_allowed(self.my_side): if DATABASE.readonly: logging.warning('bittorrent_client: readonly database') else: table_bittorrent.insert(DATABASE.connection(), self.my_side) # Update the upstream channel estimate target_bytes = int(m["target_bytes"]) if target_bytes > 0: estimate.UPLOAD = target_bytes self.final_state = True stream.close()
def collect(self, stream, request_body): ''' Invoked when we must save the result of a session ''' sha1 = self._stream_to_sha1(stream) if sha1 not in self.peers: raise RuntimeError('Not authorized to collect') else: # Note: no more than one collect per session result = self.peers[sha1] del self.peers[sha1] # # Backward compatibility: the variable name changed from # can_share to can_publish after Neubot 0.4.5 # if 'privacy_can_share' in request_body: request_body['privacy_can_publish'] = request_body[ 'privacy_can_share'] del request_body['privacy_can_share'] # # Note that the following is not a bug: it's just that # the server saves results using the point of view of the # client, i.e. upload_speed _is_ client's upload speed. # request_body['timestamp'] = result['timestamp'] request_body['upload_speed'] = result['upload_speed'] if privacy.collect_allowed(request_body): table_bittorrent.insert(DATABASE.connection(), request_body) else: logging.warning('* bad privacy settings: %s', str(stream)) # # After we've saved the result into the dictionary we # can add extra information we would like to return to # the client. # request_body['target_bytes'] = result['target_bytes'] return request_body
def runTest(self): """Make sure bittorrent table works as expected""" connection = sqlite3.connect(":memory:") connection.row_factory = sqlite3.Row table_bittorrent.create(connection) table_bittorrent.create(connection) v = map(None, ResultIterator()) for d in v: table_bittorrent.insert(connection, d, override_timestamp=False) v1 = table_bittorrent.listify(connection) self.assertEquals(sorted(v), sorted(v1)) since = utils.timestamp() - 7 * 24 * 60 * 60 until = utils.timestamp() - 3 * 24 * 60 * 60 v2 = table_bittorrent.listify(connection, since=since, until=until) self.assertTrue(len(v2) < len(v)) table_bittorrent.prune(connection, until) self.assertTrue(len(table_bittorrent.listify(connection)) < len(v1))
def bittorrent_store(self, message): ''' Saves the results of a bittorrent test ''' table_bittorrent.insert(DATABASE.connection(), message)