Просмотр исходного кода

dnet: generalize rpc to work with remote hosts as well as localhost

lunar-mining 2 лет назад
Родитель
Сommit
98ca41b509
4 измененных файлов с 18 добавлено и 7 удалено
  1. 7 0
      bin/dnet/config.toml
  2. 7 5
      bin/dnet/main.py
  3. 1 0
      bin/dnet/model.py
  4. 3 2
      bin/dnet/rpc.py

+ 7 - 0
bin/dnet/config.toml

@@ -1,8 +1,15 @@
 [[nodes]]
 name = "darkirc"
+host = "localhost"
 port = 26660
 
 [[nodes]]
 name = "taud"
+host = "localhost"
 port = 23330
 
+[[nodes]]
+name = "agorism"
+host = "agorism.dev"
+port = 26660
+

+ 7 - 5
bin/dnet/main.py

@@ -32,15 +32,16 @@ class Dnetview:
         self.model = Model()
         self.view = View(self.model)
 
-    async def subscribe(self, rpc, name, port):
+    async def subscribe(self, rpc, name, host, port):
         info = {}
         while True:
             try:
-                #logging.debug(f"Start {name} RPC on port {port}")
-                await rpc.start("localhost", port)
+                await rpc.start(host, port)
+                logging.debug(f"Started {name} RPC on port {port}")
                 break
             # TODO: offline node handling
-            except OSError:
+            except Exception as e:
+                logging.debug(f"failed to connect {host}:{port} {e}")
                 pass
     
         data = await rpc._make_request("p2p.get_info", [])
@@ -79,7 +80,8 @@ class Dnetview:
             for i, node in enumerate(nodes):
                 rpc = JsonRpc()
                 subscribe = tg.create_task(self.subscribe(
-                            rpc, node['name'], node['port']))
+                            rpc, node['name'], node['host'],
+                            node['port']))
                 nodes = tg.create_task(self.update_info())
 
     async def update_info(self):

+ 1 - 0
bin/dnet/model.py

@@ -37,6 +37,7 @@ class Model:
         self.nodes[key] = value
 
     def handle_nodes(self, node):
+        logging.debug(node)
         channel_lookup = {}
         name = list(node.keys())[0]
         values = list(node.values())[0]

+ 3 - 2
bin/dnet/rpc.py

@@ -24,8 +24,9 @@ import asyncio
 
 class JsonRpc:
 
-    async def start(self, server, port):
-        reader, writer = await asyncio.open_connection(server, port)
+    async def start(self, host, port):
+        logging.info(f"trying to connect to {host}:{port}")
+        reader, writer = await asyncio.open_connection(host, port)
         self.reader = reader
         self.writer = writer