Skip to content

Commit 23e9398

Browse files
author
mx
committed
Merge branch 'Guy_Network'
2 parents 2ba328a + baaa48a commit 23e9398

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

Network/Network.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import socket
2+
import jsonpickle
3+
from threading import Thread
4+
from Node import Node
5+
6+
class Network:
7+
8+
def __init__(self, Node_):
9+
self.node = node_
10+
self.nodes = []
11+
self.blockchain = None
12+
node.init_node()
13+
14+
def set_blockchain(self, blockchain_): #Done
15+
self.blockchain = blockchain_
16+
17+
def broadcast_transaction(self, str_transaction_): #Done
18+
for nodeList in self.nodes:
19+
node.send("-t ", node.s, nodeList, str_transaction_)
20+
21+
def broadcast_block(self, str_block_): #Done
22+
for nodeList in self.nodes:
23+
node.send("-b ", node.s, nodeList, str_block_)
24+
25+
def broadcast_ask_chain(self): #Done
26+
for nodeList in self.nodes:
27+
node.send("-c ", node.s, nodeList, "")
28+
29+
#Une fonction pour broadcast ping
30+
31+
def receiv(self):
32+
print("ready to receiv")
33+
34+
while True:
35+
data, addr = mode.s.recvfrom(1024)
36+
curentNode = node(str(addr))
37+
38+
myData = str(data)
39+
40+
if myData[:3] == "-c ": #Done
41+
#Retourne le JSON de la chaine
42+
nodes.send("-ac", node.s, cureNode, self.blockchain.chain_for_network)
43+
44+
elif myData[:3] == "-n ": #Done
45+
#Parcourir la liste de noeud et les envois a l'emmeteur
46+
for nodeList in self.nodes:
47+
nodeToSend = jsonpickle.encode(nodeList)
48+
node.send("-an", node.s, cureNode, nodeToSend)
49+
50+
elif myData[:3] == "-t ": #Done
51+
#Reception d'une transaction
52+
self.blockchain.submit_block(myData[3:length(myData)])
53+
54+
elif myData[:3] == "-b ": #Done
55+
#Reception d'un block
56+
self.blockchain.submit_transaction(myData[3:length(myData)])
57+
58+
elif myData[:3] == "-p ": #Done
59+
#Repond present
60+
nodeToSend = jsonpickle.encode(self.node)
61+
node.send("-an", node, cureNode, nodeToSend)
62+
63+
elif myData[:3] == "-ac": #En suspend
64+
some = None
65+
66+
elif myData[:3] == "-an": #Done
67+
node = jsonpickle.decode(myData[3:length(myData)])
68+
69+
notFind = True
70+
for nodeList in self.nodes:
71+
if nodeList.host == node.host:
72+
notFind = False
73+
74+
if notFind:
75+
self.nodes.append(node)
76+
notFind = False
77+
78+
c.close()

Network/Node.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import socket
2+
from threading import Thread
3+
4+
class Node:
5+
def __init__(self, host_):
6+
self.host = host_ #Adresse du pc
7+
self.port = 5000
8+
self.s = None
9+
10+
def init_node(self):
11+
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
12+
self.s.bind((self.host, self.port))
13+
14+
def ping(self, node_to_ping):
15+
send("-p ", self.s, node_to_ping, "");
16+
17+
def send(self, command_, s_, node_, message_):
18+
server = (node_.host, node_.port) #Server
19+
self.s_.sendto(command_ + message_, servers)

Network/Node.pyc

1.55 KB
Binary file not shown.

Network/test.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import socket
2+
from threading import Thread
3+
4+
'''
5+
Faire la fonction de broadcast
6+
un tableau qui contiendra les IP des personnes du reseau et les gens qui ont deja ete connectes en p2p
7+
'''
8+
9+
def Main():
10+
host = '192.168.1.82' #Adresse du pc
11+
port = 5000
12+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
13+
s.bind((host,port))
14+
server = ('192.168.1.82',5001) #Adresse de l'autre pc
15+
16+
t1 = Thread(target=receiv, args=("ready to receive", s))
17+
t1.start()
18+
19+
message = raw_input()
20+
21+
while message != "q":
22+
23+
t2 = Thread(target=send, args=("ready to send", s, server, message))
24+
t2.start()
25+
26+
message = raw_input()
27+
28+
t1._Thread__stop()
29+
30+
def receiv(a, s):
31+
32+
print(a)
33+
while True:
34+
data, addr = s.recvfrom(1024)
35+
print (str(data))
36+
c.close()
37+
38+
def send(a_, s_, server_, message_):
39+
40+
s_.sendto("recu : " + message_, server_)
41+
42+
if __name__ == '__main__':
43+
Main()

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,14 @@ Work in progress. This is a toy blockchain to learn.
66

77
Because: "Prior to Python 3.7, dict was not guaranteed to be ordered, so inputs and outputs were typically scrambled unless collections.OrderedDict was specifically requested. Starting with Python 3.7, the regular dict became order preserving, so it is no longer necessary to specify collections.OrderedDict for JSON generation and parsing.". Source:
88
https://docs.python.org/fr/3/library/json.html
9+
10+
## Protocol to comunicate
11+
12+
### Command
13+
"-c " ask the blockchain to a node
14+
"-ac" is an answer to the command -c
15+
"-n " ask to a node his all connections
16+
"-an" is an answer to the command -n or -p
17+
"-t " specify that the node will receiv a transaction
18+
"-b " specify that the node will receiv a block
19+
"-p " it's a commande to know all the nodes in the same network

0 commit comments

Comments
 (0)