-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added query feature to bandwidth accounting
- Loading branch information
Showing
9 changed files
with
283 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[Unit] | ||
Description="Bandwidth crawler" | ||
After=network-online.target | ||
|
||
[Service] | ||
Type=simple | ||
User=crawler | ||
Group=crawler | ||
Restart=always | ||
Environment=PYTHONPATH=./src/pyipv8:./src/tribler-common:./src/tribler-core | ||
WorkingDirectory=/opt/tribler | ||
ExecStart=/usr/bin/python3 src/tribler-core/run_bandwidth_crawler.py --statedir /var/lib/crawler $EXTRA_CRAWLER_ARGS | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
This executable script starts a Tribler instance and joins the BandwidthAccountingCommunity. | ||
""" | ||
import argparse | ||
import sys | ||
from asyncio import ensure_future, get_event_loop | ||
from pathlib import Path | ||
|
||
from ipv8.loader import IPv8CommunityLoader | ||
|
||
from tribler_core.config.tribler_config import TriblerConfig | ||
from tribler_core.modules.bandwidth_accounting.database import BandwidthDatabase | ||
from tribler_core.modules.bandwidth_accounting.settings import BandwidthAccountingSettings | ||
from tribler_core.modules.ipv8_module_catalog import BandwidthCommunityLauncher | ||
from tribler_core.session import Session | ||
|
||
|
||
class PortAction(argparse.Action): | ||
def __call__(self, parser, namespace, values, option_string=None): | ||
if not 0 < values < 2**16: | ||
raise argparse.ArgumentError(self, "Invalid port number") | ||
setattr(namespace, self.dest, values) | ||
|
||
|
||
class BandwidthCommunityCrawlerLauncher(BandwidthCommunityLauncher): | ||
|
||
def get_kwargs(self, session): | ||
settings = BandwidthAccountingSettings() | ||
settings.outgoing_query_interval = 5 | ||
database = BandwidthDatabase(session.config.get_state_dir() / "sqlite" / "bandwidth.db", | ||
session.trustchain_keypair.pub().key_to_bin(), store_all_transactions=True) | ||
|
||
return { | ||
"database": database, | ||
"settings": settings, | ||
"max_peers": -1 | ||
} | ||
|
||
|
||
async def start_crawler(config): | ||
session = Session(config) | ||
|
||
# We use our own community loader | ||
loader = IPv8CommunityLoader() | ||
session.ipv8_community_loader = loader | ||
loader.set_launcher(BandwidthCommunityCrawlerLauncher()) | ||
|
||
await session.start() | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description=('Start a crawler in the bandwidth accounting community')) | ||
parser.add_argument('--statedir', '-s', default='bw_crawler', type=str, help='Use an alternate statedir') | ||
parser.add_argument('--restapi', '-p', default=8085, type=str, help='Use an alternate port for the REST API', | ||
action=PortAction, metavar='{0..65535}') | ||
args = parser.parse_args(sys.argv[1:]) | ||
|
||
config = TriblerConfig(args.statedir, config_file=Path(args.statedir) / 'triblerd.conf') | ||
config.set_state_dir(Path(args.statedir).absolute()) | ||
config.set_tunnel_community_enabled(False) | ||
config.set_libtorrent_enabled(False) | ||
config.set_bootstrap_enabled(False) | ||
config.set_chant_enabled(False) | ||
config.set_torrent_checking_enabled(False) | ||
config.set_api_http_enabled(True) | ||
config.set_api_http_port(args.restapi) | ||
|
||
loop = get_event_loop() | ||
coro = start_crawler(config) | ||
ensure_future(coro) | ||
loop.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/tribler-core/tribler_core/modules/bandwidth_accounting/settings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class BandwidthAccountingSettings: | ||
""" | ||
This class contains several settings related to the bandwidth accounting mechanism. | ||
""" | ||
outgoing_query_interval: int = 30 # The interval at which we send out queries to other peers, in seconds. | ||
max_tx_returned_in_query: int = 10 # The maximum number of bandwidth transactions to return in response to a query. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.