当前位置: 首页>>代码示例>>Python>>正文


Python Network.get_local_height方法代码示例

本文整理汇总了Python中network.Network.get_local_height方法的典型用法代码示例。如果您正苦于以下问题:Python Network.get_local_height方法的具体用法?Python Network.get_local_height怎么用?Python Network.get_local_height使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在network.Network的用法示例。


在下文中一共展示了Network.get_local_height方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import get_local_height [as 别名]
class NetworkServer:

    def __init__(self, config):
        self.network = Network(config)
        self.network.trigger_callback = self.trigger_callback
        self.network.start()
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.daemon_port = config.get('daemon_port', DAEMON_PORT)
        self.socket.bind(('', self.daemon_port))
        self.socket.listen(5)
        self.socket.settimeout(1)
        self.running = False
        # daemon terminates after period of inactivity
        self.timeout = config.get('daemon_timeout', 5*60)
        self.lock = threading.RLock()

        # each GUI is a client of the daemon
        self.clients = []
        # daemon needs to know which client subscribed to which address

    def stop(self):
        with self.lock:
            self.running = False

    def add_client(self, client):
        for key in ['status','banner','updated','servers','interfaces']:
            value = self.get_status_value(key)
            client.queue.put({'method':'network.status', 'params':[key, value]})
        with self.lock:
            self.clients.append(client)


    def remove_client(self, client):
        with self.lock:
            self.clients.remove(client)
        print_error("client quit:", len(self.clients))

    def get_status_value(self, key):
        if key == 'status':
            value = self.network.connection_status
        elif key == 'banner':
            value = self.network.banner
        elif key == 'updated':
            value = (self.network.get_local_height(), self.network.get_server_height())
        elif key == 'servers':
            value = self.network.get_servers()
        elif key == 'interfaces':
            value = self.network.get_interfaces()
        return value

    def trigger_callback(self, key):
        value = self.get_status_value(key)
        print_error("daemon trigger callback", key, len(self.clients))
        for client in self.clients:
            client.queue.put({'method':'network.status', 'params':[key, value]})

    def main_loop(self):
        self.running = True
        t = time.time()
        while self.running:
            try:
                connection, address = self.socket.accept()
            except socket.timeout:
                if not self.clients:
                    if time.time() - t > self.timeout:
                        print_error("Daemon timeout")
                        break
                else:
                    t = time.time()
                continue
            t = time.time()
            client = ClientThread(self, self.network, connection)
            client.start()
        print_error("Daemon exiting")
开发者ID:ramontayag,项目名称:electrum,代码行数:77,代码来源:daemon.py

示例2: Daemon

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import get_local_height [as 别名]
class Daemon(DaemonThread):
    def __init__(self, config, fd):

        DaemonThread.__init__(self)
        self.config = config
        if config.get("offline"):
            self.network = None
        else:
            self.network = Network(config)
            self.network.start()
        self.gui = None
        self.wallets = {}
        # Setup server
        cmd_runner = Commands(self.config, None, self.network)
        host = config.get("rpchost", "localhost")
        port = config.get("rpcport", 0)
        server = SimpleJSONRPCServer((host, port), logRequests=False, requestHandler=RequestHandler)
        os.write(fd, repr((server.socket.getsockname(), time.time())))
        os.close(fd)
        server.timeout = 0.1
        for cmdname in known_commands:
            server.register_function(getattr(cmd_runner, cmdname), cmdname)
        server.register_function(self.run_cmdline, "run_cmdline")
        server.register_function(self.ping, "ping")
        server.register_function(self.run_daemon, "daemon")
        server.register_function(self.run_gui, "gui")
        self.server = server

    def ping(self):
        return True

    def run_daemon(self, config):
        sub = config.get("subcommand")
        assert sub in ["start", "stop", "status"]
        if sub == "start":
            response = "Daemon already running"
        elif sub == "status":
            if self.network:
                p = self.network.get_parameters()
                response = {
                    "path": self.network.config.path,
                    "server": p[0],
                    "blockchain_height": self.network.get_local_height(),
                    "server_height": self.network.get_server_height(),
                    "nodes": self.network.get_interfaces(),
                    "connected": self.network.is_connected(),
                    "auto_connect": p[4],
                    "wallets": {k: w.is_up_to_date() for k, w in self.wallets.items()},
                }
            else:
                response = "Daemon offline"
        elif sub == "stop":
            self.stop()
            response = "Daemon stopped"
        return response

    def run_gui(self, config_options):
        config = SimpleConfig(config_options)
        if self.gui:
            if hasattr(self.gui, "new_window"):
                path = config.get_wallet_path()
                self.gui.new_window(path, config.get("url"))
                response = "ok"
            else:
                response = "error: current GUI does not support multiple windows"
        else:
            response = "Error: Electrum is running in daemon mode. Please stop the daemon first."
        return response

    def load_wallet(self, path, get_wizard=None):
        if path in self.wallets:
            wallet = self.wallets[path]
        else:
            storage = WalletStorage(path)
            if get_wizard:
                if storage.file_exists:
                    wallet = Wallet(storage)
                    action = wallet.get_action()
                else:
                    action = "new"
                if action:
                    wizard = get_wizard()
                    wallet = wizard.run(self.network, storage)
                else:
                    wallet.start_threads(self.network)
            else:
                wallet = Wallet(storage)
                wallet.start_threads(self.network)
            if wallet:
                self.wallets[path] = wallet
        return wallet

    def run_cmdline(self, config_options):
        config = SimpleConfig(config_options)
        cmdname = config.get("cmd")
        cmd = known_commands[cmdname]
        path = config.get_wallet_path()
        wallet = self.load_wallet(path) if cmd.requires_wallet else None
        # arguments passed to function
        args = map(lambda x: config.get(x), cmd.params)
#.........这里部分代码省略.........
开发者ID:prusso,项目名称:electrum,代码行数:103,代码来源:daemon.py


注:本文中的network.Network.get_local_height方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。