11import os
2+ import json
23import uuid
4+ import time
35import asyncio
46import logging
57
@@ -20,6 +22,9 @@ def __init__(self, config=None, node_id=None, router_cls=None,
2022 self .work_manager = (work_manager_cls or WorkManager )(self )
2123 self .connections = dict ()
2224 self .controller_connections = []
25+ self .connection_manifest_path = os .path .join (self .config .server .data_dir ,
26+ self .node_id ,
27+ "connection_manifest" )
2328 self .stop = False
2429
2530 def _find_node_id (self ):
@@ -32,12 +37,40 @@ def _find_node_id(self):
3237 ofs .write (f'\n RECEPTOR_NODE_ID={ node_id } \n ' )
3338 return str (node_id )
3439
40+ def get_connection_manifest (self ):
41+ if not os .path .exists (self .connection_manifest_path ):
42+ return []
43+ try :
44+ fd = open (self .connection_manifest_path , "r" )
45+ manifest = json .load (fd )
46+ return manifest
47+ except Exception as e :
48+ logger .warn ("Failed to read connection manifest: {}" .format (e ))
49+ return []
50+
51+ def update_connection_manifest (self , connection ):
52+ manifest = self .get_connection_manifest ()
53+ found = False
54+ for node in manifest :
55+ if node ["id" ] == connection .id_ :
56+ node ["last" ] = time .time ()
57+ found = True
58+ break
59+ if not found :
60+ node .append (dict (id = connection .id_ ,
61+ last = time .time ()))
62+ fd = open (self .connection_manifest_path , "w" )
63+ json .dump (manifest , fd )
64+ fd .close ()
65+
66+
3567 def update_connections (self , connection ):
3668 self .router .register_edge (connection .id_ , self .node_id , 1 )
3769 if connection .id_ in self .connections :
3870 self .connections [connection .id_ ].append (connection )
3971 else :
4072 self .connections [connection .id_ ] = [connection ]
73+ self .update_connection_manifest (connection )
4174
4275 def add_connection (self , id_ , protocol_obj ):
4376 buffer_mgr = self .config .components .buffer_manager
@@ -47,6 +80,7 @@ def add_connection(self, id_, protocol_obj):
4780
4881 def remove_connection (self , conn ):
4982 notify_protocols = []
83+ self .update_connection_manifest (conn )
5084 for connection_node in self .connections :
5185 if conn in self .connections [connection_node ]:
5286 logger .info ("Removing connection {} for node {}" .format (conn , connection_node ))
0 commit comments