@@ -23,7 +23,7 @@ def __init__(self, dispatcher: EventDispatcher):
2323 # Track pending binary requests by tag for proper response parsing
2424 self .pending_binary_requests : Dict [str , Dict [str , Any ]] = {} # tag -> {request_type, expires_at}
2525
26- def register_binary_request (self , prefix : str , tag : str , request_type : BinaryReqType , timeout_seconds : float ):
26+ def register_binary_request (self , prefix : str , tag : str , request_type : BinaryReqType , timeout_seconds : float , context = {} ):
2727 """Register a pending binary request for proper response parsing"""
2828 # Clean up expired requests before adding new one
2929 self .cleanup_expired_requests ()
@@ -32,7 +32,8 @@ def register_binary_request(self, prefix: str, tag: str, request_type: BinaryReq
3232 self .pending_binary_requests [tag ] = {
3333 "request_type" : request_type ,
3434 "pubkey_prefix" : prefix ,
35- "expires_at" : expires_at
35+ "expires_at" : expires_at ,
36+ "context" : context # optional info we want to keep from req to resp
3637 }
3738 logger .debug (f"Registered binary request: tag={ tag } , type={ request_type } , expires in { timeout_seconds } s" )
3839
@@ -519,6 +520,7 @@ async def handle_rx(self, data: bytearray):
519520 if tag in self .pending_binary_requests :
520521 request_type = self .pending_binary_requests [tag ]["request_type" ]
521522 pubkey_prefix = self .pending_binary_requests [tag ]["pubkey_prefix" ]
523+ context = self .pending_binary_requests [tag ]["context" ]
522524 del self .pending_binary_requests [tag ]
523525 logger .debug (f"Processing binary response for tag { tag } , type { request_type } , pubkey_prefix { pubkey_prefix } " )
524526
@@ -558,6 +560,40 @@ async def handle_rx(self, data: bytearray):
558560 )
559561 except Exception as e :
560562 logger .error (f"Error parsing binary ACL response: { e } " )
563+
564+ elif request_type == BinaryReqType .NEIGHBOURS :
565+ try :
566+ pk_plen = context ["pubkey_prefix_length" ]
567+ bbuf = io .BytesIO (response_data )
568+
569+ res = {
570+ "pubkey_prefix" : pubkey_prefix ,
571+ "tag" : tag
572+ }
573+ res .update (context ) # add context in result
574+
575+ res ["neighbours_count" ] = int .from_bytes (bbuf .read (2 ), "little" , signed = True )
576+ results_count = int .from_bytes (bbuf .read (2 ), "little" , signed = True )
577+ res ["results_count" ] = results_count
578+
579+ neighbours_list = []
580+
581+ for _ in range (results_count ):
582+ neighb = {}
583+ neighb ["pubkey" ] = bbuf .read (pk_plen ).hex ()
584+ neighb ["secs_ago" ] = int .from_bytes (bbuf .read (4 ), "little" , signed = True )
585+ neighb ["snr" ] = int .from_bytes (bbuf .read (1 ), "little" , signed = True ) / 4
586+ neighbours_list .append (neighb )
587+
588+ res ["neighbours" ] = neighbours_list
589+
590+ await self .dispatcher .dispatch (
591+ Event (EventType .NEIGHBOURS_RESPONSE , res , {"tag" : tag , "pubkey_prefix" : pubkey_prefix })
592+ )
593+
594+ except Exception as e :
595+ logger .error (f"Error parsing binary NEIGHBOURS response: { e } " )
596+
561597 else :
562598 logger .debug (f"No tracked request found for binary response tag { tag } " )
563599
@@ -623,7 +659,7 @@ async def handle_rx(self, data: bytearray):
623659 if len (pubkey ) < 32 :
624660 pubkey = pubkey [0 :8 ]
625661 else :
626- pubkey = pubkey [0 :32 ]
662+ pubkey = pubkey [0 :32 ]
627663
628664 ndr ["pubkey" ] = pubkey .hex ()
629665
0 commit comments