-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer.py
208 lines (172 loc) · 4.96 KB
/
peer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from Node import Peer
import socket as s
import threading as th
import json
import sys
import time
import random
HOST=s.gethostbyname(s.gethostname())
P=s.socket(s.AF_INET,s.SOCK_DGRAM)
P.setsockopt(s.SOL_SOCKET,s.SO_REUSEADDR,1)
P.bind((HOST,6970))
peer_obj=Peer()
def inter_peer(seeder_chain,soc,i,ip):
print(seeder_chain,ip,"IN INTER_PEER",sep="\t")
"""
HOW TO AUTHENTICATE THE NEW SOCKET AS ORIGINAL PEER AND NO ONE ELSE?
"""
try:
soc.sendto(json.dumps(peer_obj.number).encode(),tuple(ip))
data,ip=soc.recvfrom(1024)
except:
print("Error connecting to peer",ip)
msg=b"DORA RORA RORA"
while True:
try:
soc.sendto(msg,ip)
print(soc.recvfrom(1024),"FROM CHOTU "+str(i),sep="\t")
except OSError as e:
print("THREAD ERROR OCCURED",e)
break
while True:
try:
"""
GET TOTAL BLOCKS NUMBER
"""
number=10
required_blocks=set(range(number)).difference(seeder_chain["Data"])
req=random.choice(required_blocks)
soc.sendto(str(req).encode(),ip)
seeder_chain["Data"].append(req)
data,_=soc.recvfrom(1024)
data=data.decode()
"""
Data will contain block number
"""
while data!="OVERIDA".encode():
try:
data,_=soc.recvfrom(1024)
except:
print("FIle sending error. Fix SHIT")
"""
LOAD FILE INTO VARIABLE
"""
file=b"DORA RARA RARA"
ctr=int(len(block)/1024)+1
"""
FURTHER PART OF CHECKING THE BLOCK TRANSFERS
history=[False]*ctr
After a block has successfully transfered to the peer, history[block_number]=True
If error happens the history can be used to resend the block
"""
while ctr!=0:
"""
LOAD BLOCK INTO VARIABLE
"""
block=file
try:
soc.sendto(block,ip)
except:
"""
ADDING THE ERROR AND INDEX OF ERROR BlOCK IN HISTORY
"""
pass
except:
pass
def process_chain(chain,soc):
chain,ip_table=chain["CHAIN"],chain["IP_TABLE"]
for i in map(str,range(peer_obj.number+1,peer_obj.size+1)):
if(i not in peer_obj.fellow_peer.keys() and chain[i]['Vac']!=-1):
peer_obj.fellow_peer[i]=[]
peer_obj.fellow_peer[i].append(s.socket(s.AF_INET,s.SOCK_DGRAM))
peer_obj.fellow_peer[i][-1].setsockopt(s.SOL_SOCKET,s.SO_REUSEADDR,1)
peer_obj.fellow_peer[i].append(th.Thread(target=inter_peer,args=(chain[i],peer_obj.fellow_peer[i][0],i,ip_table[int(i)-1],)))
peer_obj.fellow_peer[i][-1].start()
def seeder(S,ip,number):
print("INSIDE SEEDER THREAD",ip,number,sep='\t')
msg="MUDA MUDA MUDA".encode()
S.sendto("I AM HERE!".encode(),ip)
size,_=S.recvfrom(1024)
print(size,"SIZE")
size=json.loads(size)
peer_obj.update_data(size,number)
chain,_=S.recvfrom(1024)
chain=json.loads(chain.decode())
peer_obj.seeder_chain["CHAIN"],peer_obj.seeder_chain["IP_TABLE"]=json.loads(chain[0]),json.loads(chain[1])
print(peer_obj.seeder_chain,"INITIAL D")
t1=time.time()
ctr=0
while True:
try:
ans,_=S.recvfrom(1024)
print(ans)
if(time.time()-t1>=5):
peer_obj.seeder_chain["CHAIN"][str(peer_obj.number)]['Data'].append(ctr)
ctr+=1
t1=time.time()
if(ans=="CHAIN".encode()):
S.sendto(json.dumps(peer_obj.seeder_chain["CHAIN"][str(peer_obj.number)]).encode(),ip)
chain,_=S.recvfrom(2048)
chain=json.loads(chain.decode())
peer_obj.seeder_chain["CHAIN"],peer_obj.seeder_chain["IP_TABLE"]=json.loads(chain[0]),json.loads(chain[1])
print(peer_obj.seeder_chain,"NEW CHAIN")
process_chain(peer_obj.seeder_chain,S)
else:
S.sendto(msg,ip)
time.sleep(0.2)
except OSError as e:
print(e)
def peer(data,addr,soc):
msg=b"DORA RORA RORA"
while True:
try:
soc.sendto(msg,addr)
print(soc.recvfrom(1024))
time.sleep(1)
except:
break
"""
FIND SEEDER SHIT
"""
Seeder=(HOST,6969)
seeder_soc=s.socket(s.AF_INET,s.SOCK_DGRAM)
seeder_soc.setsockopt(s.SOL_SOCKET,s.SO_REUSEADDR,1)
try:
P.sendto("Connect".encode(),Seeder)
data,ip=P.recvfrom(1024)
data=json.loads(data)
print(data,"Seeder answer loaded",type(data))
while type(data)!=int:
P.sendto("Connect".encode(),data)
data,ip=P.recvfrom(1024)
data=json.loads(data)
seeder_thread=th.Thread(target=seeder,args=(seeder_soc,ip,data,))
seeder_thread.start()
seeder_thread_active=True
except:
pass
print("SEEDER DONE")
while True:
try:
data,addr=P.recvfrom(1024)
data=json.loads(data.decode())
print(data,type(data),sep="\t")
if(addr not in peer_obj.fellow_peer.keys() and type(data)==int): #### IF INT THEN FELLOW PEER
"""
IMPLEMENT PEER_OBJ.FELLOW_PEER SHIT AS DONE ABOVE FOR PEER AND ENABLE COMMUNICATION
"""
print("FELLOW_PEER")
socketo=s.socket(s.AF_INET,s.SOCK_DGRAM)
socketo.setsockopt(s.SOL_SOCKET,s.SO_REUSEADDR,1)
peer_thread=th.Thread(target=peer,args=(data,addr,socketo,))
peer_thread.start()
peer_obj.fellow_peer[data]=[socketo,peer_thread]
if(addr[0] not in peer_obj.fellow_peer.keys() and type(data)==str):##### IF STR THEN DOWN LEVEL PEER
print("DOWN LEVEL PEER")
except OSError as e:
print(e)
pass
"""
NOTES FOR FUTURE IMPLEMENTATION
1. Add break statement at the end of except statement in thread
"""