Skip to content

Commit

Permalink
Merging development
Browse files Browse the repository at this point in the history
  • Loading branch information
macsnoeren committed Jun 28, 2021
2 parents 4ea78a8 + 72de129 commit bf0121e
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 102 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ from p2pnetwork.node import Node

class MyOwnPeer2PeerNode (Node):
# Python class constructor
def __init__(self, host, port):
super(MyOwnPeer2PeerNode, self).__init__(host, port, None)
def __init__(self, host, port, id=None, callback=None, max_connections=0):
super(MyOwnPeer2PeerNode, self).__init__(host, port, id, callback, max_connections)

def outbound_node_connected(self, connected_node):
print("outbound_node_connected: " + connected_node.id)
Expand Down Expand Up @@ -75,15 +75,15 @@ class MyOwnPeer2PeerNode (Node):
return MyOwnNodeConnection(self, connection, id, host, port)
````
### Extend class NodeConnection
The NodeConnection class only hold the TCP/IP connection with the other node, to manage the different connection to and from the main node. It does not implement application specific elements. Mostly, you will only need to extend the Node class. However, when you would like to create your own NodeConnection class you can do this. Make sure that you override ````create_new_connection(self, connection, id, host, port)```` in the class Node, to make sure you initiate your own NodeConnection class. The example below shows some example.
The NodeConnection class only hold the TCP/IP connection with the other node, to manage the different connections to and from the main node. It does not implement application specific elements. Mostly, you will only need to extend the Node class. However, when you would like to create your own NodeConnection class you can do this. Make sure that you override ````create_new_connection(self, connection, id, host, port)```` in the class Node, to make sure you initiate your own NodeConnection class. The example below shows some example.

````python
from p2pnetwork.node import Node

class MyOwnPeer2PeerNode (Node):
# Python class constructor
def __init__(self, host, port):
super(MyOwnPeer2PeerNode, self).__init__(host, port, None)
def __init__(self, host, port, id=None, callback=None, max_connections=0):
super(MyOwnPeer2PeerNode, self).__init__(host, port, id, callback, max_connections)

# Override event functions...

Expand All @@ -101,7 +101,7 @@ class MyOwnNodeConnection (NodeConnection):
super(MyOwnNodeConnection, self).__init__(main_node, sock, id, host, port)

# Check yourself what you would like to change and override! See the
# documentation
# documentation and code of the nodeconnection class.
````

### Using your new classes
Expand Down
21 changes: 11 additions & 10 deletions examples/MyOwnPeer2PeerNode.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
#######################################################################################################################
# Author: Maurice Snoeren #
# Version: 0.1 beta (use at your own risk) #
# Version: 0.2 beta (use at your own risk) #
# #
# MyOwnPeer2PeerNode is an example how to use the p2pnet.Node to implement your own peer-to-peer network node. #
# 28/06/2021: Added the new developments on id and max_connections
#######################################################################################################################
from p2pnetwork.node import Node

class MyOwnPeer2PeerNode (Node):

# Python class constructor
def __init__(self, host, port):
super(MyOwnPeer2PeerNode, self).__init__(host, port, None)
def __init__(self, host, port, id=None, callback=None, max_connections=0):
super(MyOwnPeer2PeerNode, self).__init__(host, port, id, callback, max_connections)
print("MyPeer2PeerNode: Started")

# all the methods below are called when things happen in the network.
# implement your network node behavior to create the required functionality.

def outbound_node_connected(self, node):
print("outbound_node_connected: " + node.id)
print("outbound_node_connected (" + self.id + "): " + node.id)

def inbound_node_connected(self, node):
print("inbound_node_connected: " + node.id)
print("inbound_node_connected: (" + self.id + "): " + node.id)

def inbound_node_disconnected(self, node):
print("inbound_node_disconnected: " + node.id)
print("inbound_node_disconnected: (" + self.id + "): " + node.id)

def outbound_node_disconnected(self, node):
print("outbound_node_disconnected: " + node.id)
print("outbound_node_disconnected: (" + self.id + "): " + node.id)

def node_message(self, node, data):
print("node_message from " + node.id + ": " + str(data))
print("node_message (" + self.id + ") from " + node.id + ": " + str(data))

def node_disconnect_with_outbound_node(self, node):
print("node wants to disconnect with other outbound node: " + node.id)
print("node wants to disconnect with oher outbound node: (" + self.id + "): " + node.id)

def node_request_to_stop(self):
print("node is requested to stop!")
print("node is requested to stop (" + self.id + "): ")

24 changes: 20 additions & 4 deletions examples/my_own_p2p_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

from MyOwnPeer2PeerNode import MyOwnPeer2PeerNode

node_1 = MyOwnPeer2PeerNode("127.0.0.1", 8001)
node_2 = MyOwnPeer2PeerNode("127.0.0.1", 8002)
node_3 = MyOwnPeer2PeerNode("127.0.0.1", 8003)
node_1 = MyOwnPeer2PeerNode("127.0.0.1", 8001, 1)
node_2 = MyOwnPeer2PeerNode("127.0.0.1", 8002, 2)
node_3 = MyOwnPeer2PeerNode("127.0.0.1", 8003, 3)

time.sleep(1)

Expand All @@ -27,12 +27,28 @@

node_1.connect_with_node('127.0.0.1', 8002)
node_2.connect_with_node('127.0.0.1', 8003)
node_3.connect_with_node('127.0.0.1', 8002)
node_3.connect_with_node('127.0.0.1', 8001)

time.sleep(2)

node_1.send_to_nodes("message: Hi there!")

time.sleep(2)

print("node 1 is stopping..")
node_1.stop()

time.sleep(20)

node_2.send_to_nodes("message: Hi there node 2!")
node_2.send_to_nodes("message: Hi there node 2!")
node_2.send_to_nodes("message: Hi there node 2!")
node_3.send_to_nodes("message: Hi there node 2!")
node_3.send_to_nodes("message: Hi there node 2!")
node_3.send_to_nodes("message: Hi there node 2!")

time.sleep(10)

time.sleep(5)

node_1.stop()
Expand Down
6 changes: 3 additions & 3 deletions examples/my_own_p2p_application_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def node_callback(event, main_node, connected_node, data):

# Just for test we spin off multiple nodes, however it is more likely that these nodes are running
# on computers on the Internet! Otherwise we do not have any peer2peer application.
node_1 = Node("127.0.0.1", 8001, node_callback)
node_2 = Node("127.0.0.1", 8002, node_callback)
node_3 = Node("127.0.0.1", 8003, node_callback)
node_1 = Node("127.0.0.1", 8001, callback=node_callback)
node_2 = Node("127.0.0.1", 8002, callback=node_callback)
node_3 = Node("127.0.0.1", 8003, callback=node_callback)

time.sleep(1)
#node_1.debug = True
Expand Down
2 changes: 1 addition & 1 deletion examples/my_own_p2p_application_using_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

node_1.connect_with_node('127.0.0.1', 8002)
node_2.connect_with_node('127.0.0.1', 8003)
node_3.connect_with_node('127.0.0.1', 8002)
node_3.connect_with_node('127.0.0.1', 8001)

time.sleep(2)

Expand Down
Loading

0 comments on commit bf0121e

Please sign in to comment.