diff --git a/README.md b/README.md index b0c2f03..f0f36f3 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,10 @@ class MyOwnPeer2PeerNode (Node): def node_disconnect_with_outbound_node(self, connected_node): print("node wants to disconnect with oher outbound node: " + connected_node.id) - + + def node_disconnect_with_inbound_node(self, connected_node): + print("node wants to disconnect with other inbound node: " + connected_node.id) + def node_request_to_stop(self): print("node is requested to stop!") @@ -193,6 +196,9 @@ A node - ```` connected_node ```` - sends a message. At this moment the basic fu ### node_disconnect_with_outbound_node The application actively wants to disconnect the outbound node, a node with which we had made a connection in the past. You could send some last message to the node, that you are planning to disconnect, for example. +### node_disconnect_with_inbound_node +The application actively wants to disconnect the inbound node, a node that had made a connection with us in the past. You could send some last message to the node, that you are planning to disconnect, for example. + ### node_request_to_stop The main node, also the application, is stopping itself. Note that the variable connected_node is empty, while there is no connected node involved. diff --git a/p2pnetwork/node.py b/p2pnetwork/node.py index ae20192..5228137 100644 --- a/p2pnetwork/node.py +++ b/p2pnetwork/node.py @@ -202,7 +202,7 @@ def connect_with_node(self, host, port, reconnect=False): self.debug_print("TcpServer.connect_with_node: Could not connect with node. (" + str(e) + ")") return False - def disconnect_with_node(self, node): + def disconnect_with_node(self, node, strict_outbound=True): """Disconnect the TCP/IP connection with the specified node. It stops the node and joins the thread. The node will be deleted from the nodes_outbound list. Before closing, the method node_disconnect_with_outbound_node is invoked.""" @@ -210,6 +210,10 @@ def disconnect_with_node(self, node): self.node_disconnect_with_outbound_node(node) node.stop() + elif not strict_outbound and node in self.nodes_inbound: + self.node_disconnect_with_inbound_node(node) + node.stop() + else: self.debug_print("Node disconnect_with_node: cannot disconnect with a node with which we are not connected.") @@ -361,6 +365,13 @@ def node_disconnect_with_outbound_node(self, node): if self.callback is not None: self.callback("node_disconnect_with_outbound_node", self, node, {}) + def node_disconnect_with_inbound_node(self, node): + """This method is invoked just before the connection is closed with the outbound node. From the node + this request is created.""" + self.debug_print("node wants to disconnect with other inbound node: " + node.id) + if self.callback is not None: + self.callback("node_disconnect_with_inbound_node", self, node, {}) + def node_request_to_stop(self): """This method is invoked just before we will stop. A request has been given to stop the node and close all the node connections. It could be used to say goodbey to everyone."""