예제 #1
0
def startup_joinmarketd(host,
                        port,
                        usessl,
                        factories=None,
                        finalizer=None,
                        finalizer_args=None):
    """Start event loop for joinmarket daemon here.
    Args:
    port : port over which to serve the daemon
    finalizer: a function which is called after the reactor has shut down.
    finalizer_args : arguments to finalizer function.
    """
    startLogging(sys.stdout)
    if not factories:
        factories = [
            jmdaemon.JMDaemonServerProtocolFactory(),
            jmdaemon.P2EPDaemonServerProtocolFactory()
        ]
    for factory in factories:
        jmdaemon.start_daemon(host, port, factory, usessl, './ssl/key.pem',
                              './ssl/cert.pem')
        port += 1
    if finalizer:
        reactor.addSystemEventTrigger("after", "shutdown", finalizer,
                                      finalizer_args)
    reactor.run()
예제 #2
0
def start_reactor(host,
                  port,
                  factory,
                  ish=True,
                  daemon=False,
                  rs=True,
                  gui=False,
                  p2ep=False):  #pragma: no cover
    #(Cannot start the reactor in tests)
    #Not used in prod (twisted logging):
    #startLogging(stdout)
    usessl = True if jm_single().config.get("DAEMON",
                                            "use_ssl") != 'false' else False
    if daemon:
        try:
            from jmdaemon import JMDaemonServerProtocolFactory, start_daemon,\
                 P2EPDaemonServerProtocolFactory
        except ImportError:
            jlog.error("Cannot start daemon without jmdaemon package; "
                       "either install it, and restart, or, if you want "
                       "to run the daemon separately, edit the DAEMON "
                       "section of the config. Quitting.")
            return
        if not p2ep:
            dfactory = JMDaemonServerProtocolFactory()
        else:
            dfactory = P2EPDaemonServerProtocolFactory()
        orgport = port
        while True:
            try:
                start_daemon(host, port, dfactory, usessl, './ssl/key.pem',
                             './ssl/cert.pem')
                jlog.info("Listening on port " + str(port))
                break
            except Exception:
                jlog.warn("Cannot listen on port " + str(port) +
                          ", trying next port")
                if port >= (orgport + 100):
                    jlog.error(
                        "Tried 100 ports but cannot listen on any of them. Quitting."
                    )
                    sys.exit(EXIT_FAILURE)
                port += 1
    else:
        # if daemon run separately, and we do p2ep, we are using
        # the protocol server at port+1
        if p2ep:
            port += 1
    if usessl:
        ctx = ClientContextFactory()
        reactor.connectSSL(host, port, factory, ctx)
    else:
        reactor.connectTCP(host, port, factory)
    if rs:
        if not gui:
            reactor.run(installSignalHandlers=ish)
        if isinstance(jm_single().bc_interface, RegtestBitcoinCoreInterface):
            jm_single().bc_interface.shutdown_signal = True
예제 #3
0
 def start_daemon_on_port(p, f, name, port_offset):
     orgp = p[0]
     while True:
         try:
             start_daemon(host, p[0] - port_offset, f, usessl,
                          './ssl/key.pem', './ssl/cert.pem')
             jlog.info("{} daemon listening on port {}".format(
                 name, str(p[0] - port_offset)))
             break
         except Exception:
             jlog.warn("Cannot listen on port " +
                       str(p[0] - port_offset) + ", trying next port")
             if p[0] >= (orgp + 100):
                 jlog.error("Tried 100 ports but cannot "
                            "listen on any of them. Quitting.")
                 sys.exit(EXIT_FAILURE)
             p[0] += 1
     return p[0]