class ModuleServiceMaker(object): implements(IServiceMaker, IPlugin) tapname = "module-loader" description = "module loader service" options = Options def __init__(self): """ Initialize the variables of this service and the logger. """ # Init service state self._stopping = False # Init variables self.service = None self.ipv8 = None self.my_peer = None self.discovery_community = None self.trustchain_community = None self.bus = None self.module_community = None self.cli = None self.rest_api = None # Setup logging root = logging.getLogger() root.setLevel(logging.INFO) stderr_handler = logging.StreamHandler(sys.stderr) stderr_handler.setLevel(logging.INFO) stderr_handler.setFormatter( logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")) root.addHandler(stderr_handler) def start(self, options, service): """ Main method to startup the cli and add a signal handler. """ msg("Service: Starting") self.service = service # State directory state_directory = options['statedir'] util.create_directory_if_not_exists(state_directory) # port network_port = options['port'] # Initial configuration configuration = get_default_configuration() configuration['address'] = "0.0.0.0" configuration['port'] = network_port configuration['keys'] = [{ 'alias': 'my peer', 'generation': u"curve25519", 'file': os.path.join(state_directory, u"ec.pem") }] configuration['logger'] = {'level': "ERROR"} configuration['overlays'] = [{ 'class': 'DiscoveryCommunity', 'key': "my peer", 'walkers': [{ 'strategy': 'RandomWalk', 'peers': -1, 'init': { 'timeout': 3.0 } }, { 'strategy': 'RandomChurn', 'peers': -1, 'init': { 'sample_size': 64, 'ping_interval': 1.0, 'inactive_time': 1.0, 'drop_time': 3.0 } }], 'initialize': {}, 'on_start': [('resolve_dns_bootstrap_addresses', )] }] configuration['overlays'] = [] # IPv8 instance self.ipv8 = IPv8(configuration) # Network port actual_network_port = self.ipv8.endpoint.get_address()[1] # Peer self.my_peer = self.ipv8.keys.get('my peer') # Trustchain community self.trustchain_community = TrustChainTestnetCommunity( self.my_peer, self.ipv8.endpoint, self.ipv8.network, working_directory=state_directory) self.ipv8.overlays.append(self.trustchain_community) self.ipv8.strategies.append((EdgeWalk(self.trustchain_community), 10)) # Event bus self.bus = EventBus() # module community self.module_community = ModuleCommunity( self.my_peer, self.ipv8.endpoint, self.ipv8.network, trustchain=self.trustchain_community, bus=self.bus, working_directory=state_directory, ipv8=self.ipv8, service=self.service) self.ipv8.overlays.append(self.module_community) self.ipv8.strategies.append((RandomWalk(self.module_community), 10)) # CLI self.cli = CLI(self, self.ipv8, self.module_community) def signal_handler(sig, _): msg("Service: Received shut down signal %s" % sig) if not self._stopping: self.stop() signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) self.rest_api = RESTManager(self.ipv8) self.rest_api.start(actual_network_port + 1000) self.rest_api.root_endpoint.putChild('module', ModuleRootEndpoint(self.ipv8)) StandardIO(self.cli) def stop(self): self._stopping = True self.ipv8.stop() reactor.stop() def makeService(self, options): """ Construct a IPv8 service. """ module_service = MultiService() module_service.setName("module-loader") reactor.callWhenRunning(self.start, options, module_service) return module_service
] configuration['overlays'] = [ o for o in configuration['overlays'] if o['class'] in requested_overlays ] # Give each peer a separate working directory working_directory_overlays = ['AttestationCommunity', 'IdentityCommunity'] for overlay in configuration['overlays']: if overlay['class'] in working_directory_overlays: overlay['initialize'] = {'working_directory': 'temp/state_%d' % i} # Start the IPv8 service ipv8 = IPv8(configuration) rest_manager = RESTManager(ipv8) rest_manager.start(14410 + i) # Print the peer for reference print("Starting peer", i) print("port", (14410 + i)) print("mid_b64", b64encode(ipv8.keys["anonymous id"].mid)) print("mid_hex", hexlify(ipv8.keys["anonymous id"].mid)) data[names[i - 1]] = { 'port': 14410 + i, 'mid_b64': b64encode(ipv8.keys["anonymous id"].mid), 'mid_hex': hexlify(ipv8.keys["anonymous id"].mid) } with open('temp/peers.json', 'w') as outfile: json.dump(data, outfile)