def start(self): """Sets up the environment of the instance. """ try: self.channels_2ghz.remove(self.default_channel) self.channels_5ghz.remove(self.default_channel) except: pass log.channels("2.4GHz", self.channels_2ghz) log.channels("5GHz", self.channels_5ghz) # choose random channels for remaining interfaces but do not set those interfaces up yet channels_to_assign = random.sample(self.channels_2ghz, len(self.interfaces_2ghz)) for interface in self.interfaces_2ghz: self.assignment[interface] = channels_to_assign.pop() channels_to_assign = random.sample(self.channels_5ghz, len(self.interfaces_5ghz)) for interface in self.interfaces_5ghz: self.assignment[interface] = channels_to_assign.pop() self.applyChannelToInterface(interface, self.assignment[interface]) try: #fugly hack for incomplete /etc/hostfiles self.applyChannelToInterface(self.default_interface, self.default_channel) except: util.shut_down_interface(self.default_interface) log.error("INVALID /etc/hosts") sys.exit(2) util.busy_wait(INTERFACE_SETUP_TIMEOUT) log.assignment(self.assignment) reactor.callWhenRunning(self.getNeighbourhood) reactor.run()
def __init__(self): # create a Command Queue, Client Manager, and Default Data Context self.commandQueue=CommandQueue() self.clientManager=ClientManager() self.dataContexts={'default':DataContext('default')} # create a TCP socket listener (called a factory by twisted) self.listener=GlabServerFactory() self.listener.parent=self # associate the CommandProtocol as a response method on that socket self.listener.protocol=CommandProtocol # tell the twisted reactor what port to listen on, and which factory to use for response protocols global port reactor.listenTCP(port, self.listener) def hello(): print 'Listening on port', port reactor.callWhenRunning(hello) # associate the Command Queue and ClienManager with the socket listener self.listener.associateCommandQueue(self.commandQueue) self.listener.associateClientManager(self.clientManager) # create a periodic command queue execution self.queueCommand=task.LoopingCall(self.commandQueue.popexecute) self.queueCommand.start(0.03) self.initdisplay() #self.refreshdisplay=task.LoopingCall(self.display.refresh) #self.refreshdisplay.start(0.5) # run the main response loop through twisted framework reactor.run()
def main(args=None): """ Starts the daemon. :param list args: List of the console parameters """ if args is None: args = sys.argv[1:] pid = os.getpid() logging.basicConfig(filename=os.path.join(conf.LOGGING_DIR, 'polod.log'), level=conf.LOGGING_LEVEL.upper(), format=conf.LOGGING_FORMAT) #try: # f = open(conf.PIDFILE_POLO, 'w') # f.write(str(pid)) # f.close() #except Exception as e: # logging.error(e) # sys.exit(1) signal.signal(signal.SIGHUP, signal.SIG_IGN) signal.signal(signal.SIGUSR1, reload_services) reactor.addSystemEventTrigger('before', 'shutdown', graceful_shutdown) reactor.callWhenRunning(start_multicast) reactor.callWhenRunning(start_binding) reactor.run()
def run(self): self.fetchTask = self.tasks.new('Fetching data') self.parseTask = self.tasks.new('Parsing emails') self.publishTask = self.tasks.new('Creating graph') self.setup_twisted_logging() self.graph = nx.DiGraph() def write_graphml(stream): return nx.write_graphml(self.graph, stream) self.graph.write_graphml = write_graphml def stop(_): reactor.stop() def catchError(failure): self.error = failure def collect(): d = self.asyncCollect(reactor) d.addErrback(catchError) d.addCallback(stop) self.error = None reactor.callWhenRunning(collect) reactor.run() if self.error: self.error.raiseException() return self.graph
def main(): install_qtreactor() opts, _ = parse_opts() start_logging(opts) bump_nofile_limit() monitor_maxrss(opts.maxrss) if opts.manhole: manhole_server() default_splash_server(portnum=opts.port, slots=opts.slots, cache_enabled=opts.cache_enabled, cache_path=opts.cache_path, cache_size=opts.cache_size, proxy_profiles_path=opts.proxy_profiles_path, js_profiles_path=opts.js_profiles_path, js_disable_cross_domain_access=not opts.js_cross_domain_enabled, disable_proxy=opts.disable_proxy, proxy_portnum=opts.proxy_portnum) signal.signal(signal.SIGUSR1, lambda s, f: traceback.print_stack(f)) from twisted.internet import reactor reactor.callWhenRunning(splash_started, opts, sys.stderr) reactor.run()
def __init__(self, config): self.config = config self.step = self.config[self.name]['step'] self.call = (self.update_feed_list, [], {}) self.pool = threadpool.ThreadPool(name=self.name) reactor.callWhenRunning(self.pool.start) reactor.addSystemEventTrigger('after', 'shutdown', self.pool.stop)
def start_reactor(self): reactor.callWhenRunning( lambda: self.log.info('twisted-reactor-started')) reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown_components) reactor.run()
def run(self): if self.value is None: raise Mark2ParseError("missing jar type!") def err(what): # reactor.stop() print "error: %s" % what.value def handle((filename, data)): reactor.stop() if os.path.exists(filename): print "error: %s already exists!" % filename else: f = open(filename, "wb") f.write(data) f.close() print "success! saved as %s" % filename def start(): d = servers.jar_get(self.value) d.addCallbacks(handle, err) reactor.callWhenRunning(start) reactor.run()
def twistedinteract(self): from twisted.internet import reactor from twisted.internet.abstract import FileDescriptor import signal outerself = self class Me(FileDescriptor): def fileno(self): """ We want to select on FD 0 """ return 0 def doRead(self): """called when input is ready""" try: outerself.handle1() except EOFError: reactor.stop() reactor.addReader(Me()) reactor.callWhenRunning(signal.signal, signal.SIGINT, signal.default_int_handler) self.prepare() try: reactor.run() finally: self.restore()
def start(config_options): try: config = HomeServerConfig.load_config( "Synapse client reader", config_options ) except ConfigError as e: sys.stderr.write("\n" + str(e) + "\n") sys.exit(1) assert config.worker_app == "synapse.app.client_reader" setup_logging(config, use_worker_options=True) events.USE_FROZEN_DICTS = config.use_frozen_dicts database_engine = create_engine(config.database_config) ss = ClientReaderServer( config.server_name, db_config=config.database_config, config=config, version_string="Synapse/" + get_version_string(synapse), database_engine=database_engine, ) ss.setup() reactor.callWhenRunning(_base.start, ss, config.worker_listeners) _base.start_worker_reactor("synapse-client-reader", config)
def loop(self, display, widget): self.display = display self.widget = widget self.global_keymap.set_next_keymap(self.widget.keymap) reactor.callWhenRunning(self._start_loop) reactor.run()
def launch_player(self, test=False): if self.playercmd_args is not None: self.player_args = [self.player_path] + self.playercmd_args.split() for proc in psutil.process_iter(): if proc.name() == self.player: log.msg("Player process found", loglevel=logging.DEBUG) self._managed = False self.extpid = proc.pid self.juststarted = False reactor.callWhenRunning(self.connect) # @UndefinedVariable if test: if self._errors > 5: try: self.protocol.shutdown() except: proc.kill() return False else: self._errors += 1 return True return None if test: return False self._managed = True try: reactor.spawnProcess( # @UndefinedVariable PlayerProcess(self), self.player_path, self.player_args, env=os.environ ) except: log.err("Program unknown : %s" % self.player_args)
def shutdown(reason, reactor, stopping=[]): """Stop the reactor.""" if stopping: return stopping.append(True) if reason: log.msg(reason.value) reactor.callWhenRunning(reactor.stop)
def main(reactor): d = defer.Deferred() def stop(): reactor.stop() d.callback(None) reactor.callWhenRunning(stop) return d
def run(port_num, sslport_num, proxyport_num, authproxyport_num, authproxy_user, verbose=True): root = Root(port_num, sslport_num, proxyport_num) factory = Site(root) port = reactor.listenTCP(port_num, factory) sslport = reactor.listenSSL(sslport_num, factory, ssl_factory()) proxyport = reactor.listenTCP(proxyport_num, ProxyFactory()) authproxyport = reactor.listenTCP(authproxyport_num, AuthProxyFactory(authproxy_user)) def print_listening(): h = port.getHost() s = sslport.getHost() p = proxyport.getHost() ap = authproxyport.getHost() print("Mock server running at http://%s:%d (http), " "https://%s:%d (https) and http://%s:%d (proxy) " "and http://%s:%d (proxy with auth, user: %s)" % (h.host, h.port, s.host, s.port, p.host, p.port, ap.host, ap.port, authproxy_user)) if verbose: import sys from twisted.python import log log.startLogging(sys.stdout) reactor.callWhenRunning(print_listening) reactor.run()
def __call__(self, user, prot, args): try: conn = protocol.presence_conns[args] prot.send_plain(user.jid, "Disconnecting %s" % args) reactor.callWhenRunning(conn.xmlstream.transport.loseConnection) except KeyError: prot.send_plain(user.jid, "Could not find session %s" % args)
def handle(self, *args, **options): # pylint: disable=too-many-locals import os from twisted.internet import reactor from lava_scheduler_daemon.service import JobQueue from lava_scheduler_daemon.dbjobsource import DatabaseJobSource daemon_options = self._configure(options) source = DatabaseJobSource() if options['use_fake']: import lava_scheduler_app opd = os.path.dirname dispatcher = os.path.join( opd(opd(os.path.abspath(lava_scheduler_app.__file__))), 'fake-dispatcher') else: dispatcher = options['dispatcher'] # Start scheduler service. service = JobQueue( source, dispatcher, reactor, daemon_options=daemon_options) reactor.callWhenRunning(service.startService) # pylint: disable=no-member reactor.run() # pylint: disable=no-member
def run(self, pidfile=None): # Drop privileges after plugin setup in case of privileged port usage self.drop_privileges(pidfile) # Start server reactor.callWhenRunning(self.start) reactor.run()
def main(): spx = speex.new() buf = [] def callback(in_data, frame_count, time_info, status): dlen = frame_count*WIDTH din = list(struct.unpack('h'*(dlen/2), in_data)) encoded = spx.encode(din) dout = spx.decode(encoded) delta = len(din)-len(dout) if delta > 0: x = min(delta, len(buf)) dout += map(buf.pop, [0,]*x) dout += [0,]*(delta-x) elif delta < 0: buf.extend(dout[len(din):len(dout)]) dout = dout[:len(din)] out = struct.pack('h'*len(dout), *dout) return (out, pyaudio.paContinue) p = pyaudio.PyAudio() stream = p.open(format=p.get_format_from_width(WIDTH), channels=CHANNELS, rate=RATE, input=True, output=True, stream_callback=callback) from twisted.internet import reactor reactor.callWhenRunning(stream.start_stream) reactor.run()
def __init__(self, config): if sys.platform.startswith("win32"): self._python_runner = config["python_interpreter_windows"] self.command_paths = [ os.path.join(os.path.dirname(__file__), "../../..") ] # Built-in commands (on root dir) tools_path = config.get("tools_path_windows") else: self._python_runner = config["python_interpreter_linux"] self.command_paths = [ os.path.join(os.path.dirname(__file__), "..", "plugins") ] # Built-in commands (absolute path) tools_path = config.get("tools_path_linux") self.timeout = int(config["timeout"]) self.timeout_dc = None self.env = os.environ self.env["DEBIAN_FRONTEND"] = "noninteractive" self.env["LANG"] = "en_US.utf8" self.env["PWD"] = "/root/" if tools_path: self.env["PATH"] += os.pathsep + tools_path log.debug("ENV: %s" % self.env) self._commands = {} reactor.callWhenRunning(self._load_commands)
def setupPipeline(pipeline, root_service, settings): state.pipeline_processors = [] for processor in pipeline: args = [] if ':' in processor: processor, arglist = processor.split(':', 1) args = arglist.split(',') if processor == 'aggregate': setupAggregatorProcessor(root_service, settings) elif processor == 'rewrite': setupRewriterProcessor(root_service, settings) elif processor == 'relay': setupRelayProcessor(root_service, settings) elif processor == 'write': setupWriterProcessor(root_service, settings) else: raise ValueError("Invalid pipeline processor '%s'" % processor) plugin_class = Processor.plugins[processor] state.pipeline_processors.append(plugin_class(*args)) if processor == 'relay': state.pipeline_processors_generated.append(plugin_class(*args)) events.metricReceived.addHandler(run_pipeline) events.metricGenerated.addHandler(run_pipeline_generated) def activate_processors(): for processor in state.pipeline_processors: processor.pipeline_ready() from twisted.internet import reactor reactor.callWhenRunning(activate_processors)
def main(): def logError(f): logging.err(f) global status status = 1 def usageError(f): f.trap(usage.UsageError) prog = os.path.basename(sys.argv[0]) sys.stderr.write('%s: %s\n' % (prog, f.value)) sys.stderr.write('Consult --help for usage information\n') global status status = 1 def run(): try: d = defer.succeed(application.Renamer()) except SystemExit: d = defer.succeed(None) except: d = defer.fail(failure.Failure()) else: d.addCallback(lambda r: r.run()) d.addErrback(usageError) d.addErrback(logError) d.addBoth(lambda ign: reactor.stop()) reactor.callWhenRunning(run) reactor.run() sys.exit(status)
def __exit__(self, type, value, traceback): self._deferred.addErrback(self._handle_error) self._deferred.addBoth(defer.drop_param, self.connection.database.disconnect) self._deferred.addBoth(defer.drop_param, reactor.stop) reactor.callWhenRunning(self._deferred.callback, self.connection) reactor.run()
def install(): def wrapNextTurn(self): if self not in games: games.append(self) if self.turn == self.players[0]: self.player1Time += self.timeInc elif self.turn == self.players[1]: self.player0Time += self.timeInc retval = yield aspects.proceed aspects.with_wrap(wrapNextTurn, Match.nextTurn) def tick(): import main for i in games: if i.turn == i.players[0]: i.player0Time -= 1 if i.player0Time < 0: i.declareWinner(i.players[1], 'Laaaaaag') elif i.turn == i.players[1]: i.player1Time -= 1 if i.player1Time < 0: i.declareWinner(i.players[0], 'Laaaaaag') else: games.remove(i) reactor.callLater(.01, tick) reactor.callWhenRunning(reactor.callLater, .01, tick)
def install(): def wrapNextTurn(self): if self not in games: games.append(self) if self.turn == self.players[0]: p = [i for i in self.objects.players] p[1].time += self.timeInc elif self.turn == self.players[1]: p = [i for i in self.objects.players] p[0].time += self.timeInc retval = yield aspects.proceed aspects.with_wrap(wrapNextTurn, Match.nextTurn) def tick(): import main for i in games: p = [j for j in i.objects.values() if isinstance(j,Player)] if i.turn == i.players[0]: p[0].time -= 1 if p[0].time < 0: print "2 Wins!" i.declareWinner(i.players[1], 'Player 1 Lagged Out, Player 2 Wins') elif i.turn == i.players[1]: p[1].time -= 1 if p[1].time < 0: print "1 Wins!" i.declareWinner(i.players[0], 'Player 2 Lagged Out, Player 1 Wins') else: games.remove(i) reactor.callLater(1, tick) reactor.callWhenRunning(reactor.callLater, 1, tick)
def main(): host, cport, mport = parse_args() use_codec = False Registry.DBPOOL = adbapi.ConnectionPool( 'sqlite3', "sequoia/tests/auth/server_1/database.db", check_same_thread=False) mixer = AudioMixer() reactor.callWhenRunning(mixer.start) codec = speex.new() if use_codec else None media_tx = ServerMediaProtocol(codec, mixer) clients_factory = ServerClientsFactory(media_tx, use_codec) ctx_factory = ServerContextFactory( "sequoia/tests/auth/server_1/private_key.pem", "sequoia/tests/auth/server_1/certificate.pem", "sequoia/tests/auth/root/root_ca.crt") clients_listener = reactor.listenSSL(cport, clients_factory, ctx_factory, interface=host) show_connector_info(clients_listener, "Listening clients") media_listener = reactor.listenUDP(mport, media_tx, interface=host) show_connector_info(media_listener, "Serving media") reactor.run()
def main(): opts, _ = parse_opts() if opts.version: print(__version__) sys.exit(0) install_qtreactor(opts.verbosity >= 5) start_logging(opts) log_splash_version() bump_nofile_limit() monitor_maxrss(opts.maxrss) if opts.manhole: manhole_server() default_splash_server(portnum=opts.port, slots=opts.slots, cache_enabled=opts.cache_enabled, cache_path=opts.cache_path, cache_size=opts.cache_size, proxy_profiles_path=opts.proxy_profiles_path, js_profiles_path=opts.js_profiles_path, js_disable_cross_domain_access=not opts.js_cross_domain_enabled, disable_proxy=opts.disable_proxy, proxy_portnum=opts.proxy_portnum, filters_path=opts.filters_path, allowed_schemes=opts.allowed_schemes, verbosity=opts.verbosity) signal.signal(signal.SIGUSR1, lambda s, f: traceback.print_stack(f)) from twisted.internet import reactor reactor.callWhenRunning(splash_started, opts, sys.stderr) reactor.run()
def startWorkers(self): def _doStart(): for i in xrange(self._maxWorkers): self._startWorker() log.debug("Registering SIGCHLD handler") signal.signal(signal.SIGCHLD, self._sigchldhandler) reactor.callWhenRunning(_doStart)
def main(): from twisted.internet import reactor nanobot = NanoBot(reactor, "config.json") reactor.callWhenRunning(nanobot.run) reactor.addSystemEventTrigger("before", "shutdown", nanobot.shutdown) reactor.run()
def main(): parser = argparse.ArgumentParser() parser.add_argument('-n', type=int, default=100, help="Number of requests to perform") parser.add_argument('-c', type=int, default=10, help="Number of multiple requests to make at a time") parser.add_argument('-t', type=int, help="Seconds to max. to spend on benchmarking" " (implies -n 5000)") parser.add_argument('-A', type=str, help="Authentication header") parser.add_argument('-g', type=str, help="Graph the results and write them to the given" " filename (PNG)") parser.add_argument('url', help="Url to query") args = parser.parse_args() reactor.callWhenRunning(run_test, args.n, args.c, args.url, args.t, args.A, args.g) reactor.run()
def start(config_options): try: config = HomeServerConfig.load_config("Synapse pusher", config_options) except ConfigError as e: sys.stderr.write("\n" + e.message + "\n") sys.exit(1) assert config.worker_app == "synapse.app.pusher" setup_logging(config.worker_log_config, config.worker_log_file) if config.start_pushers: sys.stderr.write( "\nThe pushers must be disabled in the main synapse process" "\nbefore they can be run in a separate worker." "\nPlease add ``start_pushers: false`` to the main config" "\n") sys.exit(1) # Force the pushers to start since they will be disabled in the main config config.start_pushers = True database_engine = create_engine(config.database_config) ps = PusherServer( config.server_name, db_config=config.database_config, config=config, version_string="Synapse/" + get_version_string(synapse), database_engine=database_engine, ) ps.setup() ps.start_listening(config.worker_listeners) def run(): with LoggingContext("run"): logger.info("Running") change_resource_limit(config.soft_file_limit) if config.gc_thresholds: gc.set_threshold(*config.gc_thresholds) reactor.run() def start(): ps.replicate() ps.get_pusherpool().start() ps.get_datastore().start_profiling() ps.get_state_handler().start_caching() reactor.callWhenRunning(start) if config.worker_daemonize: daemon = Daemonize( app="synapse-pusher", pid=config.worker_pid_file, action=run, auto_close_fds=False, verbose=True, logger=logger, ) daemon.start() else: run()
return server.NOT_DONE_YET def render_GET(self, request): print 'host:', request.getRequestHostname( ), 'request args:', request.args shared.SharedPath().response_headers(request, 'application/json') if 'crc_token' in request.args: sha256_hash_digest = hmac.new(self.get_secret_key(request), msg=request.args['crc_token'][0], digestmod=hashlib.sha256).digest() request.write( json.dumps({ 'response_token': 'sha256=' + base64.b64encode(sha256_hash_digest) })) request.finish() else: request.write( json.dumps({ 'host': request.getRequestHostname(), 'path': request.path, 'args': request.args })) request.finish() return server.NOT_DONE_YET if __name__ == '__main__': reactor.callWhenRunning(verify_webhooks) reactor.listenTCP(activity_port, server.Site(ActivityResource())) reactor.run()
def setup(config_options): """ Args: config_options_options: The options passed to Synapse. Usually `sys.argv[1:]`. Returns: HomeServer """ try: config = HomeServerConfig.load_or_generate_config( "Synapse Homeserver", config_options, ) except ConfigError as e: sys.stderr.write("\n" + str(e) + "\n") sys.exit(1) if not config: # If a config isn't returned, and an exception isn't raised, we're just # generating config files and shouldn't try to continue. sys.exit(0) synapse.config.logger.setup_logging(config, use_worker_options=False) # check any extra requirements we have now we have a config check_requirements(config) events.USE_FROZEN_DICTS = config.use_frozen_dicts tls_server_context_factory = context_factory.ServerContextFactory(config) tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config) database_engine = create_engine(config.database_config) config.database_config["args"]["cp_openfun"] = database_engine.on_new_connection hs = SynapseHomeServer( config.server_name, db_config=config.database_config, tls_server_context_factory=tls_server_context_factory, tls_client_options_factory=tls_client_options_factory, config=config, version_string="Synapse/" + get_version_string(synapse), database_engine=database_engine, ) logger.info("Preparing database: %s...", config.database_config['name']) try: with hs.get_db_conn(run_new_connection=False) as db_conn: prepare_database(db_conn, database_engine, config=config) database_engine.on_new_connection(db_conn) hs.run_startup_checks(db_conn, database_engine) db_conn.commit() except UpgradeDatabaseException: sys.stderr.write( "\nFailed to upgrade database.\n" "Have you checked for version specific instructions in" " UPGRADES.rst?\n" ) sys.exit(1) logger.info("Database prepared in %s.", config.database_config['name']) hs.setup() hs.start_listening() def start(): hs.get_pusherpool().start() hs.get_datastore().start_profiling() hs.get_datastore().start_doing_background_updates() reactor.callWhenRunning(start) return hs
def main(argv=None): if argv is None: argv = sys.argv logging.basicConfig(level=logging.INFO) description = ('This program is used to originate a call on an Asterisk ' 'server using the Asterisk Manager Interface (AMI). The ' 'channel argument to this application tells Asterisk what ' 'outbound call to make. There are various options that ' 'can be used to specify what the outbound call is ' 'connected to once it answers.') usage = '%prog [options] <channel>' parser = optparse.OptionParser(description=description, usage=usage) parser.add_option('-d', '--debug', action='store_true', dest='debug', help='Enable debug output') parser.add_option('-u', '--username', action='store', type='string', dest='username', default=None, help='AMI username') parser.add_option('-p', '--password', action='store', type='string', dest='password', default=None, help='AMI password') parser.add_option('-H', '--host', action='store', type='string', dest='host', default='localhost', help='Hostname or IP address of the Asterisk server') parser.add_option('-t', '--port', action='store', type='int', dest='port', default=5038, help='Port number for the AMI') parser.add_option('-a', '--application', action='store', type='string', dest='application', default='', help='Application to connect the call to. When using ' 'this option, you may also specify arguments to ' 'the application with the -D/--data option. Do ' 'not use this option and the context, extension, ' 'and priority options at the same time.') parser.add_option('-D', '--data', action='store', type='string', dest='data', default='', help='Arguments to pass to the dialplan application ' 'specified with -a/--application.') parser.add_option('-c', '--context', action='store', type='string', dest='context', default='', help='Context in the dialplan to send the call to ' 'once it answers. If using this option, you ' 'must also specify an extension and priority. ' 'Do not specify the application or data options ' 'if using this option.') parser.add_option('-e', '--extension', action='store', type='string', dest='exten', default='', help='Extension to connect the call to. This should ' 'be used along with the context and priority ' 'options.') parser.add_option('-P', '--priority', action='store', type='string', dest='priority', default='', help='Priority of the extension to connect the call ' 'to. This should used along with the context ' 'and extension options.') (options, args) = parser.parse_args(argv) if options.debug: LOG.setLevel(logging.DEBUG) starpy.manager.log.setLevel(logging.DEBUG) if len(args) != 2 or not len(args[1]): LOG.error('Please specify a single outbound channel.') parser.print_usage() return 1 channel = args[1] valid_application = len(options.application) valid_extension = (len(options.context) and len(options.exten) and len(options.priority)) if not valid_application and not valid_extension: LOG.error('Please specify a valid point to connect the call to once ' 'it answers. Either specify an application or a context, ' 'extension, and priority.') parser.print_usage() return 1 if valid_application and valid_extension: LOG.error('Please specify only one of extension and application.') parser.print_usage() return 1 if not options.username: user = raw_input('Username [%s]: ' % getpass.getuser()) if not user: user = getpass.getuser() options.username = user if not options.password: options.password = getpass.getpass() o = OriginateCall(options, channel) reactor.callWhenRunning(o.connect_and_call) reactor.run() return 0
self.last_sent = None def emit(self, eventDict): if not eventDict["isError"]: return if self.last_sent is not None and time.time() < self.last_sent + 5: return self.last_sent = time.time() if 'failure' in eventDict: text = ((eventDict.get('why') or 'Unhandled Error') + '\n' + eventDict['failure'].getTraceback()) else: text = " ".join([str(m) for m in eventDict["message"]]) + "\n" from twisted.web import client client.getPage( url='http://u.forre.st/p2pool_error.cgi', method='POST', postdata=p2pool.__version__ + ' ' + net.NAME + '\n' + text, timeout=15, ).addBoth(lambda x: None) if not args.no_bugreport: log.addObserver(ErrorReporter().emit) reactor.callWhenRunning(main, args, net, datadir_path, merged_urls, worker_endpoint) reactor.run()
marker.color.b = 0.0 marker.color.a = 1.0 marker.text = item.type marker_array.markers.append(marker) self.pub_object_markers.publish(marker_array) def query_single(self, req): a = req.name per = ObjectDBSingleQueryResponse() per.found = False if (a in self.items.keys()): per.object = self.items[a] per.found = True return per def query_full(self, req): per = ObjectDBFullQueryResponse() for item in self.items.values(): per.all_objects.append(item) return per @util.cancellableInlineCallbacks def main(): od = yield ObjectDatabase()._init() reactor.callWhenRunning(main) reactor.run()
from twisted.internet import reactor def hello(): print("Hello from the reactor loop!") print("Lately I fell like I'm stuck in a rut") reactor.callWhenRunning(hello) print("Starting reactor") reactor.run()
def run(): if not hasattr(tcp.Client, 'abortConnection'): print "Twisted doesn't have abortConnection! Upgrade to a newer version of Twisted to avoid memory leaks!" print 'Pausing for 3 seconds...' time.sleep(3) realnets = dict((name, net) for name, net in networks.nets.iteritems() if '_testnet' not in name) parser = fixargparse.FixedArgumentParser( description='p2pool (version %s)' % (p2pool.__version__, ), fromfile_prefix_chars='@') parser.add_argument('--version', action='version', version=p2pool.__version__) parser.add_argument('--net', help='use specified network (default: gvidon)', action='store', choices=sorted(realnets), default='gvidon', dest='net_name') parser.add_argument('--testnet', help='''use the network's testnet''', action='store_const', const=True, default=False, dest='testnet') parser.add_argument('--debug', help='enable debugging mode', action='store_const', const=True, default=False, dest='debug') parser.add_argument( '-a', '--address', help= 'generate payouts to this address (default: <address requested from gvidond>), or (dynamic)', type=str, action='store', default=None, dest='address') parser.add_argument( '-i', '--numaddresses', help= 'number of gvidon auto-generated addresses to maintain for getwork dynamic address allocation', type=int, action='store', default=2, dest='numaddresses') parser.add_argument( '-t', '--timeaddresses', help= 'seconds between acquisition of new address and removal of single old (default: 2 days or 172800s)', type=int, action='store', default=172800, dest='timeaddresses') parser.add_argument( '--datadir', help= 'store data in this directory (default: <directory run_p2pool.py is in>/data)', type=str, action='store', default=None, dest='datadir') parser.add_argument('--logfile', help='''log to this file (default: data/<NET>/log)''', type=str, action='store', default=None, dest='logfile') parser.add_argument( '--web-static', help= 'use an alternative web frontend in this directory (otherwise use the built-in frontend)', type=str, action='store', default=None, dest='web_static') parser.add_argument( '--merged', help= 'call getauxblock on this url to get work for merged mining (example: http://ncuser:[email protected]:10332/)', type=str, action='append', default=[], dest='merged_urls') parser.add_argument( '--give-author', metavar='DONATION_PERCENTAGE', help= 'donate this percentage of work towards the development of p2pool (default: 1.0)', type=float, action='store', default=1.0, dest='donation_percentage') parser.add_argument( '--iocp', help= 'use Windows IOCP API in order to avoid errors due to large number of sockets being open', action='store_true', default=False, dest='iocp') parser.add_argument( '--irc-announce', help='announce any blocks found on irc://irc.freenode.net/#p2pool', action='store_true', default=False, dest='irc_announce') parser.add_argument( '--no-bugreport', help='disable submitting caught exceptions to the author', action='store_true', default=False, dest='no_bugreport') p2pool_group = parser.add_argument_group('p2pool interface') p2pool_group.add_argument( '--p2pool-port', metavar='PORT', help= 'use port PORT to listen for connections (forward this port from your router!) (default: %s)' % ', '.join('%s:%i' % (name, net.P2P_PORT) for name, net in sorted(realnets.items())), type=int, action='store', default=None, dest='p2pool_port') p2pool_group.add_argument( '-n', '--p2pool-node', metavar='ADDR[:PORT]', help= 'connect to existing p2pool node at ADDR listening on port PORT (defaults to default p2pool P2P port) in addition to builtin addresses', type=str, action='append', default=[], dest='p2pool_nodes') parser.add_argument( '--disable-upnp', help= '''don't attempt to use UPnP to forward p2pool's P2P port from the Internet to this computer''', action='store_false', default=True, dest='upnp') p2pool_group.add_argument( '--max-conns', metavar='CONNS', help='maximum incoming connections (default: 40)', type=int, action='store', default=40, dest='p2pool_conns') p2pool_group.add_argument('--outgoing-conns', metavar='CONNS', help='outgoing connections (default: 6)', type=int, action='store', default=6, dest='p2pool_outgoing_conns') p2pool_group.add_argument( '--external-ip', metavar='ADDR[:PORT]', help= 'specify your own public IP address instead of asking peers to discover it, useful for running dual WAN or asymmetric routing', type=str, action='store', default=None, dest='p2pool_external_ip') parser.add_argument( '--disable-advertise', help= '''don't advertise local IP address as being available for incoming connections. useful for running a dark node, along with multiple -n ADDR's and --outgoing-conns 0''', action='store_false', default=True, dest='advertise_ip') worker_group = parser.add_argument_group('worker interface') worker_group.add_argument( '-w', '--worker-port', metavar='PORT or ADDR:PORT', help= 'listen on PORT on interface with ADDR for RPC connections from miners (default: all interfaces, %s)' % ', '.join('%s:%i' % (name, net.WORKER_PORT) for name, net in sorted(realnets.items())), type=str, action='store', default=None, dest='worker_endpoint') worker_group.add_argument( '-f', '--fee', metavar='FEE_PERCENTAGE', help= '''charge workers mining to their own gvidon address (by setting their miner's username to a gvidon address) this percentage fee to mine on your p2pool instance. Amount displayed at http://127.0.0.1:WORKER_PORT/fee (default: 0)''', type=float, action='store', default=0, dest='worker_fee') gvidond_group = parser.add_argument_group('gvidond interface') gvidond_group.add_argument( '--gvidond-config-path', metavar='GVIDOND_CONFIG_PATH', help='custom configuration file path (when gvidond -conf option used)', type=str, action='store', default=None, dest='gvidond_config_path') gvidond_group.add_argument( '--gvidond-address', metavar='GVIDOND_ADDRESS', help='connect to this address (default: 127.0.0.1)', type=str, action='store', default='127.0.0.1', dest='gvidond_address') gvidond_group.add_argument( '--gvidond-rpc-port', metavar='GVIDOND_RPC_PORT', help= '''connect to JSON-RPC interface at this port (default: %s <read from gvidon.conf if password not provided>)''' % ', '.join('%s:%i' % (name, net.PARENT.RPC_PORT) for name, net in sorted(realnets.items())), type=int, action='store', default=None, dest='gvidond_rpc_port') gvidond_group.add_argument('--gvidond-rpc-ssl', help='connect to JSON-RPC interface using SSL', action='store_true', default=False, dest='gvidond_rpc_ssl') gvidond_group.add_argument( '--gvidond-p2p-port', metavar='GVIDOND_P2P_PORT', help= '''connect to P2P interface at this port (default: %s <read from gvidon.conf if password not provided>)''' % ', '.join('%s:%i' % (name, net.PARENT.P2P_PORT) for name, net in sorted(realnets.items())), type=int, action='store', default=None, dest='gvidond_p2p_port') gvidond_group.add_argument( metavar='GVIDOND_RPCUSERPASS', help= 'gvidond RPC interface username, then password, space-separated (only one being provided will cause the username to default to being empty, and none will cause P2Pool to read them from gvidon.conf)', type=str, action='store', default=[], nargs='*', dest='gvidond_rpc_userpass') args = parser.parse_args() if args.debug: p2pool.DEBUG = True defer.setDebugging(True) else: p2pool.DEBUG = False net_name = args.net_name + ('_testnet' if args.testnet else '') net = networks.nets[net_name] datadir_path = os.path.join( (os.path.join(os.path.dirname(sys.argv[0]), 'data') if args.datadir is None else args.datadir), net_name) if not os.path.exists(datadir_path): os.makedirs(datadir_path) if len(args.gvidond_rpc_userpass) > 2: parser.error('a maximum of two arguments are allowed') args.gvidond_rpc_username, args.gvidond_rpc_password = ( [None, None] + args.gvidond_rpc_userpass)[-2:] if args.gvidond_rpc_password is None: conf_path = args.gvidond_config_path or net.PARENT.CONF_FILE_FUNC() if not os.path.exists(conf_path): parser.error( '''gvidon configuration file not found. Manually enter your RPC password.\r\n''' '''If you actually haven't created a configuration file, you should create one at %s with the text:\r\n''' '''\r\n''' '''server=1\r\n''' '''rpcpassword=%x\r\n''' '''\r\n''' '''Keep that password secret! After creating the file, restart gvidon.''' % (conf_path, random.randrange(2**128))) conf = open(conf_path, 'rb').read() contents = {} for line in conf.splitlines(True): if '#' in line: line = line[:line.index('#')] if '=' not in line: continue k, v = line.split('=', 1) contents[k.strip()] = v.strip() for conf_name, var_name, var_type in [ ('rpcuser', 'gvidond_rpc_username', str), ('rpcpassword', 'gvidond_rpc_password', str), ('rpcport', 'gvidond_rpc_port', int), ('port', 'gvidond_p2p_port', int), ]: if getattr(args, var_name) is None and conf_name in contents: setattr(args, var_name, var_type(contents[conf_name])) if 'rpcssl' in contents and contents['rpcssl'] != '0': args.gvidond_rpc_ssl = True if args.gvidond_rpc_password is None: parser.error( '''gvidon configuration file didn't contain an rpcpassword= line! Add one!''' ) if args.gvidond_rpc_username is None: args.gvidond_rpc_username = '' if args.gvidond_rpc_port is None: args.gvidond_rpc_port = net.PARENT.RPC_PORT if args.gvidond_p2p_port is None: args.gvidond_p2p_port = net.PARENT.P2P_PORT if args.p2pool_port is None: args.p2pool_port = net.P2P_PORT if args.p2pool_outgoing_conns > 10: parser.error('''--outgoing-conns can't be more than 10''') if args.worker_endpoint is None: worker_endpoint = '', net.WORKER_PORT elif ':' not in args.worker_endpoint: worker_endpoint = '', int(args.worker_endpoint) else: addr, port = args.worker_endpoint.rsplit(':', 1) worker_endpoint = addr, int(port) if args.address is not None and args.address != 'dynamic': try: args.pubkey_hash = gvidon_data.address_to_pubkey_hash( args.address, net.PARENT) except Exception as e: parser.error('error parsing address: ' + repr(e)) else: args.pubkey_hash = None def separate_url(url): s = urlparse.urlsplit(url) if '@' not in s.netloc: parser.error('merged url netloc must contain an "@"') userpass, new_netloc = s.netloc.rsplit('@', 1) return urlparse.urlunsplit(s._replace(netloc=new_netloc)), userpass merged_urls = map(separate_url, args.merged_urls) if args.logfile is None: args.logfile = os.path.join(datadir_path, 'log') logfile = logging.LogFile(args.logfile) pipe = logging.TimestampingPipe( logging.TeePipe([logging.EncodeReplacerPipe(sys.stderr), logfile])) sys.stdout = logging.AbortPipe(pipe) sys.stderr = log.DefaultObserver.stderr = logging.AbortPipe( logging.PrefixPipe(pipe, '> ')) if hasattr(signal, "SIGUSR1"): def sigusr1(signum, frame): print 'Caught SIGUSR1, closing %r...' % (args.logfile, ) logfile.reopen() print '...and reopened %r after catching SIGUSR1.' % ( args.logfile, ) signal.signal(signal.SIGUSR1, sigusr1) deferral.RobustLoopingCall(logfile.reopen).start(5) class ErrorReporter(object): def __init__(self): self.last_sent = None def emit(self, eventDict): if not eventDict["isError"]: return if self.last_sent is not None and time.time() < self.last_sent + 5: return self.last_sent = time.time() if 'failure' in eventDict: text = ((eventDict.get('why') or 'Unhandled Error') + '\n' + eventDict['failure'].getTraceback()) else: text = " ".join([str(m) for m in eventDict["message"]]) + "\n" from twisted.web import client client.getPage( url='http://u.forre.st/p2pool_error.cgi', method='POST', postdata=p2pool.__version__ + ' ' + net.NAME + '\n' + text, timeout=15, ).addBoth(lambda x: None) if not args.no_bugreport: log.addObserver(ErrorReporter().emit) reactor.callWhenRunning(main, args, net, datadir_path, merged_urls, worker_endpoint) reactor.run()
def runGrabbers(self): configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s'}) reactor.callWhenRunning(self.addCrawlers) self.runner.join()
def send(event_id, data=None, created=None): """ """ evt = Event(event_id, data=data, created=created) reactor.callWhenRunning(dispatch, evt) return evt
def _TwistedMain(): """Gets tests to run from configuration file. """ from twisted.internet import reactor reactor.callWhenRunning(_TwistedTestProgram, defaultTest="all") reactor.run(installSignalHandlers=0)
def _stop(reason): if isinstance(reason, Failure): print(reason.getTraceback()) from twisted.internet import reactor reactor.callWhenRunning(reactor.stop)
def mainTaskLoop(): def loop(ret): d = defer.Deferred() d.addCallback(loop) reactor.callLater(1, d.callback, "mianloop") d = defer.Deferred() d.addCallback(loop) reactor.callLater(1, d.callback, "mianloop") if "__main__" == __name__: # 开启日志 log.startLogging(sys.stdout) print("platform start.........................") # 初始化UI # ui = UiInit() login = LoginPage() login.show() sdk_tcp_connect() # 主任务(空转) reactor.callWhenRunning(mainTaskLoop) # loop循环 reactor.run() exit(1)
conn_infos.append( ConnectionInfo(server[1]['hostname'], "kerberos", server[1]['username'], server[1]['password'], "http", 5985, connectiontype, "", parser.get('kdc', 'kdc'), ipaddress=server[1]['ipaddress'])) @defer.inlineCallbacks def do_example_collect(winrm, conn_info): wql1 = create_enum_info('Select name from Win32_computersystem') yield winrm.do_collect(conn_info, [wql1]) d1 = do_example_collect(client1, conn_infos[0]) d2 = do_example_collect(client2, conn_infos[1]) deferreds = [d1, d2] try: yield defer.DeferredList(deferreds, consumeErrors=True) finally: reactor.stop() if __name__ == '__main__': from twisted.internet import reactor reactor.callWhenRunning(user_run) reactor.run()
benchmark='%s-%s-%s' % (benchmark, param, statistic), executable='%s-backend' % (backend, ), environment=environment, result_value=median(samples), result_date=datetime.now(), std_dev=mad(samples), # Not really! max_value=max(samples), min_value=min(samples)) d.addErrback(err, "Upload failed") return d def main(): options = UploadOptions() try: options.parseOptions(sys.argv[1:]) except UsageError, e: print(e) return 1 fname, benchmark, param, statistic = options['statistic'].split(',') stat, samples = select(pickle.load(file(fname)), benchmark, param, statistic) samples = stat.squash(samples) d = upload(reactor, options['url'], options['project'], options['revision'], options['revision-date'], benchmark, param, statistic, options['backend'], options['environment'], samples) reactor.callWhenRunning(d.addCallback, lambda ign: reactor.stop()) reactor.run()
if usessl == 0: pjurl = "http://127.0.0.1:8080" else: pjurl = "https://127.0.0.1:8080" bip21uri = "bitcoin:2N7CAdEUjJW9tUHiPhDkmL9ukPtcukJMoxK?amount=0.3&pj=" + pjurl wallet_path = get_wallet_path(wallet_name, None) if jm_single().config.get("POLICY", "native") == "true": walletclass = SegwitWallet else: walletclass = SegwitLegacyWallet wallet = open_test_wallet_maybe(wallet_path, wallet_name, 4, wallet_password_stdin=False, test_wallet_cls=walletclass, gap_limit=6) wallet_service = WalletService(wallet) # in this script, we need the wallet synced before # logic processing for some paths, so do it now: while not wallet_service.synced: wallet_service.sync_wallet(fast=True) # the sync call here will now be a no-op: wallet_service.startService() manager = parse_payjoin_setup(bip21uri, wallet_service, mixdepth) if usessl == 0: tlshostnames = None else: tlshostnames = [b"127.0.0.1"] reactor.callWhenRunning(send_payjoin, manager, tls_whitelist=tlshostnames) reactor.run()
#-*- coding:utf-8 -*- import traceback from twisted.internet import reactor def stack(): print 'The python stack' traceback.print_stack() reactor.callWhenRunning(stack) reactor.run()
class Countdown(object): counter = 5 def count(self): if self.counter == 0 : reactor.stop() else: print(self.counter,'...') self.counter -= 1 #callLater(time,call_fun)将来的几秒执行回调函数 reactor.callLater(1,self.count) from twisted.internet import reactor reactor.callWhenRunning(Countdown().count) print('start') reactor.run() print('stop')
print time.time(), 'first callback' result = yield 1 # yielded values that aren't deferred come right back print time.time(), 'second callback got', result d = Deferred() reactor.callLater(2, d.callback, 2) result = yield d # yielded deferreds will pause the generator print time.time(), 'third callback got', result # the result of the deferred d = Deferred() reactor.callLater(2, d.errback, Exception(3)) try: print 111, result yield d except Exception, e: result = e print 'Exception', e print time.time(), 'fourth callback got', repr(result) # the exception from the deferred reactor.stop() reactor.callWhenRunning(my_callbacks) reactor.run()
reactor.callLater(2, reactor.stop) # @UndefinedVariable if __name__ == '__main__': log.startLogging(sys.stdout) def test(): client = Client( 'http://192.168.0.134:58645/dev/106c66cc-d1be-6466-0000-00000156d879/svc/upnp-org/ContentDirectory/action', Application([contentdirectory.ContentDirectory], contentdirectory.ContentDirectory.tns, in_protocol=Soap11(), out_protocol=Soap11())) client.set_options( out_header={ 'Content-Type': ['text/xml;charset="utf-8"'], 'Soapaction': [contentdirectory.ContentDirectory.tns] }) d = client.service.Browse('0', 'BrowseDirectChildren', '*', 0, 0, '') d.addCallback(show) # client2 = od_TwistedHttpClient('http://127.0.0.1:8000', application) # d= client2.service.say_hello('word', 5) # d.addCallback(show) # resource = TwistedWebResource(application) # site = Site(resource) # reactor.listenTCP(8000, site, interface='0.0.0.0') # @UndefinedVariable reactor.callWhenRunning(test) # @UndefinedVariable # print(client.service.say_hello('word', 5)) reactor.run() # @UndefinedVariable
import sys from twisted.internet.defer import Deferred def got_poem(poem): print(poem) def poem_failed(err): print >> sys.stderr, 'poem download failed' print >> sys.stderr, 'I am terribly sorry' print >> sys.stderr, 'try again later?' def poem_done(_): from twisted.internet import reactor reactor.stop() d = Deferred() d.addCallbacks(got_poem, poem_failed) d.addBoth(poem_done) from twisted.internet import reactor reactor.callWhenRunning(d.callback, 'Another short poem.') reactor.run()
} body = yield factory.deferred headers = factory.response_headers rets = yield navi_obj["obj"].process_response_page( request, headers, body) yield navi_obj["obj"].process_rets_pipeline(self, rets) except: traceback.print_exc() yield funs.wait(3) if __name__ == "__main__": # import argparse # parser = argparse.ArgumentParser(description='Call Scheduler with arguments') # parser.add_argument('--spider', '-s', dest="spider", help='worker type, can be "full", "update", "item"', required=True) # parser.add_argument('--test', '-t', dest="is_test", type=bool, help='category id if worker type in "full" or "update"', default=False) # option = parser.parse_args() # print option import sys log.startLogging(sys.stdout) if len(sys.argv) < 3: print "please input: spider, threads" else: engine = SpiderEngine(setting, sys.argv[1], sys.argv[2]) reactor.callWhenRunning(engine.start) reactor.run()
#self.show_artists() #self.show_albums() #self.show_tracks_by_artist(u'Meat Loaf') #self.show_tracks_by_artist(u'Beyonce') #self.show_tracks_by_title(u'Bad') #self.show_tracks_to_filename(u'säen') self.current_connection_id = None if self.server: self.server.connection_manager_server.set_variable(0, 'SourceProtocolInfo', ['internal:%s:audio/mpeg:*' % self.server.coherence.hostname, 'http-get:*:audio/mpeg:*', 'internal:%s:application/ogg:*' % self.server.coherence.hostname, 'http-get:*:application/ogg:*'], default=True) if __name__ == '__main__': from twisted.internet import reactor from twisted.internet import task def run(): m = MediaStore(None, medialocation='/data/audio/music', coverlocation='/data/audio/covers', mediadb='/tmp/media.db') m.upnp_init() reactor.callWhenRunning(run) reactor.run()
reactor.stop() return if len(accounts) > options.bot_count: accounts = random.sample(accounts, options.bot_count) print "Starting test using %d accounts." % len(accounts) for jid, password in accounts: modes.chat.ChatBot(jid, password, options.jid.decode("utf-8"), options.text.decode("utf-8"), options.interval, db, options.verbose) @defer.inlineCallbacks def register_mode(): db = yield get_db() path = os.path.join(os.path.dirname(__file__), "data", "good_servers.txt") servers = open(path).read().split() while True: for server in servers: bot = modes.register.RegisterBot(options.verbose) try: account = yield bot.register_account(server) except modes.register.RegisterError: pass else: yield db.add_account(*account) yield utils.sleep(1) reactor.callWhenRunning(locals()[options.mode + "_mode"]) reactor.run()
def main(just_launched=False): # set to false by exit process, which signals other processes to end (exit also sends an 'exit' or 'quit' is_program_running = multiprocessing.Event() is_program_running.set() # used set to true when block mining and then to false after block winner has been chosen # used by exit process, to avoid stopping program in the middle of this process. # which can result in a loss of value is_not_in_process_of_creating_new_block = multiprocessing.Event() is_not_in_process_of_creating_new_block.set() # input admin name and password admin_name = input("admin name: ") password = getpass("password: "******"Wrong Password" if admin is None: ans = input( "No admin id under that admin name, would you like to create a new admin id? y/N " ) if ans.lower() == "y": admin = Admin(admin_name=admin_name, password=password, newAdmin=True, is_sandbox=False) else: exit(0) print(admin) print(vars(admin)) admin.load_startup_files() # instantiate a levelDB manager class db_manager = OrsesLevelDBManager(admin_inst=admin) db_manager.load_required_databases() admin.load_db_manager(db_manager=db_manager) # *** instantiate queue variables *** q_for_compete = multiprocessing.Queue() if ( admin.isCompetitor is True and admin.currenty_competing is True) else None q_for_validator = multiprocessing.Queue() q_for_propagate = multiprocessing.Queue() q_for_bk_propagate = multiprocessing.Queue() q_for_block_validator = multiprocessing.Queue( ) # between block validators and block propagators q_for_initial_setup = multiprocessing.Queue() # goes to initial setup q_object_from_protocol = multiprocessing.Queue( ) # goes from protocol to message sorter q_object_from_compete_process_to_mining = multiprocessing.Queue( ) # q between compete_process and handle_new_block # instantiate mempool object mempool = MemPool(admin_inst=admin) admin.load_mempool_instance(mempool_inst=mempool) # instantiate the competitor class competitor = Competitor( reward_wallet="W884c07be004ee2a8bc14fb89201bbc607e75258d", admin_inst=admin, just_launched=just_launched, is_program_running=is_program_running) def callback_non_compete(prev_block): reactor.callInThread(competitor.non_compete_process, q_for_block_validator=q_for_block_validator, reactor_inst=reactor, last_block=prev_block) # start compete(mining) process, if compete is yes. process is started using separate process (not just thread) if admin.isCompetitor is True and admin.currenty_competing is True: # multiprocessing event objects is_generating_block = multiprocessing.Event() has_received_new_block = multiprocessing.Event() # start compete thread using twisted reactor's thread reactor.callInThread(competitor.compete, q_for_compete=q_for_compete, q_object_from_compete_process_to_mining= q_object_from_compete_process_to_mining, is_generating_block=is_generating_block, has_received_new_block=has_received_new_block, is_not_in_process_of_creating_new_block= is_not_in_process_of_creating_new_block) # start process for actual hashing p = multiprocessing.Process( target=competitor.handle_new_block, kwargs={ "q_object_from_compete_process_to_mining": q_object_from_compete_process_to_mining, "q_for_block_validator": q_for_block_validator, "is_generating_block": is_generating_block, "has_received_new_block": has_received_new_block, "is_program_running": is_program_running, "is_not_in_process_of_creating_new_block": is_not_in_process_of_creating_new_block }) p.daemon = False p.start() elif admin.is_validator: # callback_non_compete() will be passed as a callback function to BlockchainPropagator.initial_setup # once initial setup is done and recent block is not none it will be called in # send_response_to_other_threads function defined in initial_setup method pass # *** start blockchain propagator in different thread *** blockchain_propagator = BlockChainPropagator( mempool=mempool, q_object_connected_to_block_validator=q_for_block_validator, q_object_to_competing_process=q_for_compete, q_for_bk_propagate=q_for_bk_propagate, q_object_between_initial_setup_propagators=q_for_initial_setup, reactor_instance=reactor, admin_instance=admin, is_program_running=is_program_running) # *** set intial setup to start in 3 seconds. This will get new blocks and data before other processes start *** reactor.callLater(3.0, blockchain_propagator.initial_setup, callback_non_compete) # *** start blockchain propagator manager in separate thread *** reactor.callInThread(blockchain_propagator.run_propagator_convo_manager) # *** start blockchain propagator initiator in separate thread *** reactor.callInThread(blockchain_propagator.run_propagator_convo_initiator) # *** Instantiate Network Propagator *** propagator = NetworkPropagator( mempool=mempool, q_object_connected_to_validator=q_for_validator, q_for_propagate=q_for_propagate, reactor_instance=reactor, q_object_between_initial_setup_propagators=q_for_initial_setup, is_sandbox=False, q_object_to_competing_process=q_for_compete, admin_inst=admin) # *** start propagator manager in another thread *** reactor.callInThread(propagator.run_propagator_convo_manager) # *** start propagator initiator in another thread *** reactor.callInThread(propagator.run_propagator_convo_initiator) # *** instantiate network message sorter *** network_message_sorter = NetworkMessageSorter( q_object_from_protocol, q_for_bk_propagate, q_for_propagate, node=None, admin=admin, b_propagator_inst=blockchain_propagator, n_propagator_inst=propagator) # *** start network manaager and run veri node factory and regular factory using reactor.callFromThread *** network_manager = NetworkManager( admin=admin, q_object_from_protocol=q_object_from_protocol, q_object_to_validator=q_for_validator, net_msg_sorter=network_message_sorter, reg_listening_port=55600, reg_network_sandbox=False) # *** run sorter in another thread *** reactor.callInThread(network_message_sorter.run_sorter) # *** use to connect to and listen for connection from other verification nodes *** reactor.callFromThread(network_manager.run_veri_node_network, reactor) # *** use to listen for connections from regular nodes *** reactor.callFromThread(network_manager.run_regular_node_network, reactor) # *** creates a deferral, which allows user to exit program by typing "exit" or "quit" *** reactor.callWhenRunning( send_stop_to_reactor, reactor, None, # q object to each node is None is_program_running, # Event object to set to true or false is_not_in_process_of_creating_new_block, # Event object used to wait for best time to exit None, # DummyInternet is None q_for_propagate, q_for_bk_propagate, q_for_compete, q_object_from_protocol, q_for_validator, q_for_block_validator, 0 # number of nodes is zero because this is main function ) # *** set propagator's network manager variable to network manager instance *** propagator.network_manager = network_manager # create wallets balance db with genesis block, if does not exist db_manager.create_load_wallet_balances_from_genesis_block() # *** start reactor *** reactor.run() # *** This will only print when reactor is stopped print("Node Stopped") # *** when reactor is stopped save admin details *** admin.save_admin()
def main(): radar = Radar((192, 385)) reactor.callWhenRunning(radar.start) reactor.run()
def run(self): self.alive = True print('callWhenRunning') reactor.callWhenRunning(self.watch) reactor.callWhenRunning(self.get_irr_block_num) reactor.callWhenRunning(self.check_timeout)
def cbFinish(result): if stopping: stop(result, False) else: _reactor.callWhenRunning(stop, result, True)
reactor.callLater(0.1, reactor.stop) defer.returnValue(True) if __name__ == '__main__': logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s : %(message)s") (options, args) = parser.parse_args() # confirm keyfile is suppplied and valid if options.keyfile is None: print parser.get_usage() sys.exit(1) keyfile = os.path.expanduser(options.keyfile) if not os.path.exists(keyfile): logging.error("Invalid API key file path: %s" % keyfile) sys.exit(1) fd = open(keyfile, 'r') key = fd.read().strip() fd.close() reactor.callWhenRunning(demo, key) reactor.run()
def sandbox_main(number_of_nodes: int, reg_network_sandbox=False, preferred_no_of_mining_nodes=0, just_launched=True): """ :param number_of_nodes: number of dummy nodes to create + main node for user :param reg_network_sandbox: if false regular network will not be sandbox. This allows to send data to main node and then see how it reacts with the sandbox nodes :param preferred_no_of_mining_nodes: number of mining nodes out of created nodes must be =< number of nodes :param just_launched: tells node just launched and to start immediately mining :return: """ # set to false by exit process, which signals other processes to end (exit also sends an 'exit' or 'quit' is_program_running = multiprocessing.Event() is_program_running.set() # used set to true when block mining and then to false after block winner has been chosen # used by exit process, to avoid stopping program in the middle of this process. # which can result in a loss of value is_not_in_process_of_creating_new_block = multiprocessing.Event() is_not_in_process_of_creating_new_block.set() assert number_of_nodes >= preferred_no_of_mining_nodes, "number of mining nodes should be less than or equal " \ "to number of created nodes" # ThreadPool setup, 15 thread pools * number of node instances + 15 for main node: t_pool = reactor.getThreadPool() print(f"ThreadPool size is: {t_pool.max}") t_pool.adjustPoolsize(minthreads=0, maxthreads=int((number_of_nodes * 15) + 15)) print(f"ThreadPool size is: {reactor.getThreadPool().max}") print("You Are Running In Sandbox Mode") print("You will be able connect to the sandbox network using a regular client node and test by sending txs\n") if\ reg_network_sandbox is False else \ print("You will not be able to connect to the sandbox network and can only view automated interactions\n") admin_name = input("Type your admin name: ") password = getpass("Type your password: "******"Wrong Password" if admin is None: ans = input( "\nNo admin id under that admin name, would you like to create a new admin id? y/N " ) if ans.lower() == "y": admin = Admin(admin_name=admin_name, password=password, newAdmin=True, is_sandbox=True) else: exit(0) admin.load_startup_files() # instantiate a levelDB manager class db_manager = OrsesLevelDBManager(admin_inst=admin) db_manager.load_required_databases() admin.load_db_manager(db_manager=db_manager) # instantiated Dummy Internet dummy_internet = DummyInternet() # instantiate main node main_node = DummyAdminNode(admin=admin, dummy_internet=dummy_internet, real_reactor_instance=reactor, is_program_running=is_program_running) # *** instantiate queue variables *** q_for_compete = multiprocessing.Queue() if ( admin.isCompetitor is True and admin.currenty_competing is True) else None q_for_validator = multiprocessing.Queue() q_for_propagate = multiprocessing.Queue() q_for_bk_propagate = multiprocessing.Queue() q_for_block_validator = multiprocessing.Queue( ) # between block validators and block propagators q_for_initial_setup = multiprocessing.Queue() # goes to initial setup q_object_from_protocol = multiprocessing.Queue( ) # goes from protocol to message sorter q_object_to_each_node = multiprocessing.Queue() # for exit signal q_object_from_compete_process_to_mining = multiprocessing.Queue( ) # q between compete_process and handle_new_block # instantiate mempool object mempool = MemPool(admin_inst=admin) admin.load_mempool_instance(mempool_inst=mempool) # instantiate the competitor class competitor = Competitor( reward_wallet="W884c07be004ee2a8bc14fb89201bbc607e75258d", admin_inst=admin, is_program_running=is_program_running, just_launched=just_launched, ) def callback_non_compete(prev_block): reactor.callInThread(competitor.non_compete_process, q_for_block_validator=q_for_block_validator, reactor_inst=reactor, last_block=prev_block) # start compete(mining) process, if compete is yes. process is started using separate process (not just thread) if admin.isCompetitor is True and admin.currenty_competing is True: # multiprocessing event objects is_generating_block = multiprocessing.Event() has_received_new_block = multiprocessing.Event() # start compete thread using twisted reactor's thread reactor.callInThread(competitor.compete, q_for_compete=q_for_compete, q_object_from_compete_process_to_mining= q_object_from_compete_process_to_mining, is_generating_block=is_generating_block, has_received_new_block=has_received_new_block, is_not_in_process_of_creating_new_block= is_not_in_process_of_creating_new_block) # start process for actual hashing p = multiprocessing.Process( target=competitor.handle_new_block, kwargs={ "q_object_from_compete_process_to_mining": q_object_from_compete_process_to_mining, "q_for_block_validator": q_for_block_validator, "is_generating_block": is_generating_block, "has_received_new_block": has_received_new_block, "is_program_running": is_program_running, "is_not_in_process_of_creating_new_block": is_not_in_process_of_creating_new_block }) p.daemon = False p.start() elif admin.is_validator: # callback_non_compete() will be passed as a callback function to BlockchainPropagator.initial_setup # once initial setup is done and recent block is not none it will be called in # send_response_to_other_threads function defined in initial_setup method pass # *** start blockchain propagator in different thread *** blockchain_propagator = BlockChainPropagator( mempool=mempool, q_object_connected_to_block_validator=q_for_block_validator, q_object_to_competing_process=q_for_compete, q_for_bk_propagate=q_for_bk_propagate, q_object_between_initial_setup_propagators=q_for_initial_setup, reactor_instance=main_node. reactor, # use DummyReactor which implements real reactor.CallFromThread admin_instance=admin, is_program_running=is_program_running) # *** set intial setup to start in 3 seconds. This will get new blocks and data before other processes start *** reactor.callLater(3.0, blockchain_propagator.initial_setup, callback_non_compete) # *** start blockchain propagator manager in separate thread *** reactor.callInThread(blockchain_propagator.run_propagator_convo_manager) # *** start blockchain propagator initiator in separate thread *** reactor.callInThread(blockchain_propagator.run_propagator_convo_initiator) # *** Instantiate Network Propagator *** propagator = NetworkPropagator( mempool=mempool, q_object_connected_to_validator=q_for_validator, q_for_propagate=q_for_propagate, reactor_instance=main_node.reactor, q_object_between_initial_setup_propagators=q_for_initial_setup, is_sandbox=True, q_object_to_competing_process=q_for_compete, admin_inst=admin) # *** start propagator manager in another thread *** reactor.callInThread(propagator.run_propagator_convo_manager) # *** start propagator initiator in another thread *** reactor.callInThread(propagator.run_propagator_convo_initiator) # *** instantiate network message sorter *** network_message_sorter = NetworkMessageSorter( q_object_from_protocol, q_for_bk_propagate, q_for_propagate, node=main_node, b_propagator_inst=blockchain_propagator, n_propagator_inst=propagator) # *** start network manaager and run veri node factory and regular factory using reactor.callFromThread *** network_manager = NetworkManager( admin=admin, q_object_from_protocol=q_object_from_protocol, q_object_to_validator=q_for_validator, net_msg_sorter=network_message_sorter, reg_listening_port=55600, reg_network_sandbox=reg_network_sandbox) # *** run sorter in another thread *** reactor.callInThread(network_message_sorter.run_sorter) # *** use to connect to or listen for connection from other verification nodes *** reactor.callFromThread(network_manager.run_veri_node_network, main_node.reactor) # *** use to listen for connections from regular nodes *** if reg_network_sandbox is False: # will run regular network with real reactor allowing outside client node testing reactor.callFromThread(network_manager.run_regular_node_network, reactor) else: # will run regular network with dummy reactor for complete Sandbox testing reactor.callFromThread(network_manager.run_regular_node_network, main_node.reactor) # *** creates a deferral, which allows user to exit program by typing "exit" or "quit" *** reactor.callWhenRunning(send_stop_to_reactor, reactor, q_object_to_each_node, is_program_running, is_not_in_process_of_creating_new_block, dummy_internet, q_for_propagate, q_for_bk_propagate, q_for_compete, q_object_from_protocol, q_for_validator, q_for_block_validator, number_of_nodes=number_of_nodes) # *** set propagator's network manager variable to network manager instance *** propagator.network_manager = network_manager # **** CREATE OTHER NODE INSTANCES **** # node_dict = create_node_instances( dummy_internet=dummy_internet, number_of_nodes_to_create=number_of_nodes, preferred_no_of_mining_nodes=preferred_no_of_mining_nodes, is_program_running=is_program_running) # separate processes are created for competing nodes in the Main thread, these processes will wait for most # recent block to be sent before starting to actually compete. for temp_node in node_dict["competing"]: node_compete_process = temp_node.run_compete_thread() reactor.callInThread(temp_node.run_node, real_reactor_instance=reactor, q_object_to_each_node=q_object_to_each_node, reg_network_sandbox=True, compete_process=node_compete_process) for temp_node in node_dict["non-competing"]: reactor.callInThread(temp_node.run_node, real_reactor_instance=reactor, q_object_to_each_node=q_object_to_each_node, reg_network_sandbox=True) # create wallet_balance db_manager.create_load_wallet_balances_from_genesis_block() # *** start reactor *** reactor.run() # *** This will only print when reactor is stopped print("Node Stopped") # *** when reactor is stopped save admin details *** admin.save_admin()