44import time
55import asyncio
66import logging
7+ import copy
78
89from .config import ReceptorConfig
910from .router import MeshRouter
@@ -37,6 +38,26 @@ def _find_node_id(self):
3738 ofs .write (f'\n RECEPTOR_NODE_ID={ node_id } \n ' )
3839 return str (node_id )
3940
41+ async def watch_expire (self ):
42+ while True :
43+ logger .info ("Checking expirations" )
44+ current_manifest = self .get_connection_manifest ()
45+ for connection in current_manifest :
46+ buffer = self .config .components .buffer_manager .get_buffer_for_node (connection ["id" ], self )
47+ for ident , message in buffer :
48+ message_actual = json .loads (message )
49+ logger .info ("Examining {}" .format (message_data ))
50+ if "expire_time" in message_data and message_data ['expire_time' ] < time .time ():
51+ logger .info ("Expiring message {}:{}" .format (ident , connection ["id" ]))
52+ expired_message = buffer .read_message (ident , remove = True )
53+ # TODO: Do something with expired message
54+ if connection ["last" ] + 86400 < time .time ():
55+ logger .info ("Expiring connection {}" .format (connection ["id" ]))
56+ write_manifest = copy .copy (current_manifest )
57+ write_manifest .remove (connection )
58+ self .write_connection_manifest (write_manifest )
59+ await asyncio .sleep (600 )
60+
4061 def get_connection_manifest (self ):
4162 if not os .path .exists (self .connection_manifest_path ):
4263 return []
@@ -48,43 +69,46 @@ def get_connection_manifest(self):
4869 logger .warn ("Failed to read connection manifest: {}" .format (e ))
4970 return []
5071
72+ def write_connection_manifest (self , manifest ):
73+ fd = open (self .connection_manifest_path , "w" )
74+ json .dump (manifest , fd )
75+ fd .close ()
76+
5177 def update_connection_manifest (self , connection ):
5278 manifest = self .get_connection_manifest ()
5379 found = False
5480 for node in manifest :
55- if node ["id" ] == connection . id_ :
81+ if node ["id" ] == connection :
5682 node ["last" ] = time .time ()
5783 found = True
5884 break
5985 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 ()
86+ manifest .append (dict (id = connection .id_ ,
87+ last = time .time ()))
88+ self .write_connection_manifest (manifest )
6589
66-
6790 def update_connections (self , connection ):
6891 self .router .register_edge (connection .id_ , self .node_id , 1 )
6992 if connection .id_ in self .connections :
7093 self .connections [connection .id_ ].append (connection )
7194 else :
7295 self .connections [connection .id_ ] = [connection ]
73- self .update_connection_manifest (connection )
96+ self .update_connection_manifest (connection . id_ )
7497
7598 def add_connection (self , id_ , protocol_obj ):
7699 buffer_mgr = self .config .components .buffer_manager
77100 conn = Connection (id_ , protocol_obj , buffer_mgr , self )
78101 self .update_connections (conn )
79102 return conn
80103
81- def remove_connection (self , conn ):
104+ def remove_connection (self , protocol_obj ):
82105 notify_protocols = []
83- self .update_connection_manifest (conn )
106+ # self.update_connection_manifest(conn)
84107 for connection_node in self .connections :
85- if conn in self .connections [connection_node ]:
86- logger .info ("Removing connection {} for node {}" .format (conn , connection_node ))
87- self .connections [connection_node ].remove (conn )
108+ if protocol_obj in self .connections [connection_node ]:
109+ logger .info ("Removing connection {} for node {}" .format (protocol_obj , connection_node ))
110+ self .update_connection_manifest (connection_node )
111+ self .connections [connection_node ].remove (protocol_obj )
88112 self .router .update_node (self .node_id , connection_node , 100 )
89113 self .router .debug_router ()
90114 notify_protocols += self .connections [connection_node ]
0 commit comments