Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!")

Expand Down Expand Up @@ -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.

Expand Down
13 changes: 12 additions & 1 deletion p2pnetwork/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,18 @@ 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."""
if node in self.nodes_outbound:
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.")

Expand Down Expand Up @@ -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."""
Expand Down