Denis Mazur 4 éve
szülő
commit
1925723d42
2 módosított fájl, 31 hozzáadás és 26 törlés
  1. 23 18
      hivemind/p2p/p2p_daemon_bindings/control.py
  2. 8 8
      hivemind/proto/p2pd.proto

+ 23 - 18
hivemind/p2p/p2p_daemon_bindings/control.py

@@ -115,32 +115,37 @@ class ControlClient:
                 self._write_task.cancel()
 
     async def _read_from_persistent_conn(self, reader: asyncio.StreamReader):
-        while True:
-            resp = p2pd_pb.PCResponse()
-            await read_pbmsg_safe(reader, resp)
+        with closing(reader):
+            while True:
+                resp = p2pd_pb.PCResponse()
+                await read_pbmsg_safe(reader, resp)
+
+                print("received response:", resp)
 
-            call_id = uuid.UUID(bytes=resp.callId)
+                call_id = uuid.UUID(bytes=resp.callId)
 
-            if resp.HasField("callUnaryResponse"):
-                if call_id in self.pending_calls and resp.callUnaryResponse.HasField("result"):
-                    self.pending_calls[call_id].set_result(resp.callUnaryResponse.result)
-                elif call_id in self.pending_calls and resp.callUnaryResponse.HasField("error"):
-                    remote_exc = P2PHandlerError(resp.callUnaryResponse.error.decode())
-                    self.pending_calls[call_id].set_exception(remote_exc)
-                else:
-                    logger.debug(f"received unexpected unary call")
+                if resp.HasField("callUnaryResponse"):
+                    print("HASHSAHAS")
+                    if call_id in self.pending_calls and resp.callUnaryResponse.HasField("response"):
+                        self.pending_calls[call_id].set_result(resp.callUnaryResponse.response)
+                    elif call_id in self.pending_calls and resp.callUnaryResponse.HasField("error"):
+                        remote_exc = P2PHandlerError(resp.callUnaryResponse.error.decode())
+                        self.pending_calls[call_id].set_exception(remote_exc)
+                    else:
+                        logger.debug(f"received unexpected unary call")
 
-            elif resp.HasField("requestHandling"):
-                handler_task = asyncio.create_task(self._handle_persistent_request(call_id, resp.requestHandling))
-                self.handler_tasks[call_id] = handler_task
+                elif resp.HasField("requestHandling"):
+                    handler_task = asyncio.create_task(self._handle_persistent_request(call_id, resp.requestHandling))
+                    self.handler_tasks[call_id] = handler_task
 
-            elif call_id in self.handler_tasks and resp.HasField("cancel"):
-                self.handler_tasks[call_id].cancel()
+                elif call_id in self.handler_tasks and resp.HasField("cancel"):
+                    self.handler_tasks[call_id].cancel()
 
     async def _write_to_persistent_conn(self, writer: asyncio.StreamWriter):
         with closing(writer):
             while True:
                 msg = await self.pending_messages.get()
+                print("writing to persicon:", msg)
                 await write_pbmsg(writer, msg)
 
     async def _handle_persistent_request(self, call_id: uuid.UUID, request: p2pd_pb.CallUnaryRequest):
@@ -150,7 +155,7 @@ class ControlClient:
         try:
             remote_id = PeerID(request.peer)
             response_payload: bytes = await self.unary_handlers[request.proto](request.data, remote_id)
-            response = p2pd_pb.CallUnaryResponse(result=response_payload)
+            response = p2pd_pb.CallUnaryResponse(response=response_payload)
 
         except Exception as e:
             response = p2pd_pb.CallUnaryResponse(error=repr(e).encode())

+ 8 - 8
hivemind/proto/p2pd.proto

@@ -7,7 +7,7 @@ syntax = "proto2";
 package p2pclient.p2pd.pb;
 
 message Request {
-   enum Type {
+  enum Type {
     IDENTIFY                 = 0;
     CONNECT                  = 1;
     STREAM_OPEN              = 2;
@@ -15,15 +15,12 @@ message Request {
     DHT                      = 4;
     LIST_PEERS               = 5;
     CONNMANAGER              = 6;
-    DISCONNECT               = 7;
+    DISCONNECT               = 7;      
     PUBSUB                   = 8;
 
     PERSISTENT_CONN_UPGRADE  = 9;
-    CALL_UNARY              = 10;
-    ADD_UNARY_HANDLER       = 11;
-    SEND_RESPONSE_TO_REMOTE = 12;
-
   }
+
   required Type type = 1;
 
   optional ConnectRequest connect = 2;
@@ -74,6 +71,7 @@ message PCResponse {
   }
 }
 
+
 message IdentifyResponse {
   required bytes id = 1;
   repeated bytes addrs = 2;
@@ -197,8 +195,10 @@ message CallUnaryRequest {
 }
 
 message CallUnaryResponse {
-  optional bytes result = 1;
-  optional bytes error = 2;
+  oneof result {
+    bytes response = 1;
+    bytes error = 2;
+  }
 }
 
 message AddUnaryHandlerRequest {