-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP_Socket.py
234 lines (202 loc) · 8.23 KB
/
UDP_Socket.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import threading
import socket
import sys
import time
import PacketsHandler
from Packets import ReportPacket
from Packets import BeaconPacket
from Packets import MicStatusPacket
from Packets import Packets
import node_variables
from configparser import ConfigParser
import init_config
from queue import Queue
config = ConfigParser()
config.read('config.ini')
ttl = 1
# --- Thread Receiver Udp Packets --- #
class ThreadReceiverUdpPackets(threading.Thread):
def __init__(self, threadID, name, port):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.port = port
def run(self):
print("Starting " + self.name)
while True:
try:
#UdpSocketReceiver( config.get(socket.gethostname(),'IpStation') , int(config['GENERAL']['Port']) )
UdpSocketReceiverFromNode(self.port)
except:
print("Error on thread " + self.name+" - Restarting")
time.sleep(3)
def UdpSocketReceiverFromNode(port):
# Create a UDP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = (init_config.GetIp(config['GENERAL']['StationInterface']),
port)
s.bind(server_address)
while True:
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("####### Node is listening #######")
data, address = s.recvfrom(8192)
#s.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
#print("\n\n 2. Node received: ", data.decode('utf-8'), "\n\n")
PacketsHandler.PacketHandler(data, address)
class ThreadReceiverUdpPacketsFromController(threading.Thread):
def __init__(self, threadID, name, ip, port):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.port = port
self.ip = ip
def run(self):
print("Starting " + self.name)
while True:
try:
#UdpSocketReceiver( config.get(socket.gethostname(),'IpStation') , int(config['GENERAL']['Port']) )
UdpSocketReceiverFromController(self.ip, self.port)
except:
print("Error on thread " + self.name+" - Restarting")
time.sleep(3)
def UdpSocketReceiverFromController(ip, port):
# Create a UDP socket
#sC = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sC = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
#server_address = (ip, port)
sC.bind(('', port))
while True:
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("####### Node is listening #######")
data, address = sC.recvfrom(4096)
#s.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
#print("\n\n 2. Node received: ", data.decode('utf-8'), "\n\n")
PacketsHandler.PacketHandler(data, address)
# --- Thread Beacon Udp Packets --- #
class ThreadBeacon(threading.Thread):
def __init__(self, threadID, name, beacon, ip, port):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.beacon = beacon
self.port = port
self.ip = ip
def run(self):
print("Starting " + self.name)
while True:
try:
SendUdpPacketBeacon(self.beacon, self.ip, self.port)
except:
print("Error on thread " + self.name+" - Restarting")
time.sleep(3)
def SendUdpPacketBeacon(beacon, ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
# Let's send data through UDP protocol
while True:
s.sendto(beacon, (ip, port))
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("\n\n 1. Node Send Beacon: ", beacon, "\n\n")
# close the socketù
print("Beacon Send!")
time.sleep(int(config['GENERAL']['BeaconSleep']))
# --- Thread Report Udp Packets --- #
class ThreadReport(threading.Thread):
def __init__(self, threadID, name, port, s_address, d_address, neighbours):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.port = port
self.s_address = s_address
self.d_address = d_address
self.neighbours=neighbours
def run(self):
print("Starting " + self.name)
while True:
try:
SendUdpPacketReport(self.port, self.s_address, self.d_address, self.neighbours)
except:
print("Error on thread " + self.name+" - Restarting")
time.sleep(3)
def SendUdpPacketReport(port, src, dst, queue):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
# Let's send data through UDP protocol
neighbours=[]
while True:
if not queue.empty():
neighbours=queue.get()
pckReort = ReportPacket(config['GENERAL']['NetId'], dst, src,
config['GENERAL']['TTL'], dst,
", ".join(neighbours))
bytesToSend = pckReort.getBytesFromPackets()
s.sendto(bytesToSend, (dst, port))
if config['DEBUG']['PRINT_LOGS'] is True:
print("\n\n 1. Node Send Report : ", bytesToSend, "\n\n")
# close the socket
print("Unicast Report Send!")
time.sleep(int(config['GENERAL']['ReportSleep']))
def SendUdpPacketMicStatus(port, src, dst, action):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
# Let's send data through UDP protocol
pckReort = MicStatusPacket(config['GENERAL']['NetId'], dst, src,
config['GENERAL']['TTL'], dst,
action)
bytesToSend = pckReort.getBytesFromPackets()
s.sendto(bytesToSend, (dst, port))
if config['DEBUG']['PRINT_LOGS'] is True:
print("\n\n 1. Node Send MicStatus : ", bytesToSend, "\n\n")
# close the socket
print("Unicast MicStatus Sent!")
# --- Generic Function For Send Udp Packets --- #
def SendUdpPacketUnicast(data, address, port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
s.sendto(data, (address, port))
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("\n\n 1. Packet sent: ", data, "\n\n")
class ThreadPrintInfoNode(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print("Starting " + self.name)
while True:
try:
#UdpSocketReceiver( config.get(socket.gethostname(),'IpStation') , int(config['GENERAL']['Port']) )
PrintBasicInfo(1, 0)
except:
print("Error on thread " + self.name+" - Restarting")
time.sleep(3)
def PrintBasicInfo(NeighborInfo, OtherInfo):
while True:
if config.getboolean('DEBUG','PRINT_LOGS') is True:
print("\n[B-INFO] Network ID: ",
config.get(socket.gethostname(), 'NetId'))
print("[B-INFO] Node ID: ", config.get(socket.gethostname(), 'Id'))
print("[B-INFO] SINK: ", config.get(socket.gethostname(), 'Sink'))
print("[B-INFO] Ip Station: ", node_variables.IpStation)
print("[B-INFO] Ip Client: ", node_variables.IpClient)
print("[B-INFO] Default Gateway: ",
node_variables.IpDefaultGateway)
if (NeighborInfo == 1):
print("[Nei-INFO] Network ID: ", node_variables.list_neighbor)
if (OtherInfo == 1):
print(" --- ")
print("\n")
time.sleep(int(config['GENERAL']['InfoSleep']))
class ThreadRefreshARP(threading.Thread):
def __init__(self, threadID, name, neighbours):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.neighbours= neighbours
def run(self):
while True:
try:
print("Starting " + self.name)
self.neighbours.put(init_config.RefreshARP())
time.sleep(int(config['GENERAL']['ScanNetSleep']))
except:
print("Error on thread " + self.name+" - Restarting")
time.sleep(3)