-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
213 lines (183 loc) · 5.96 KB
/
utils.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
import zmq
import random
from threading import Thread,Semaphore
import dill
import os
from more_itertools import chunked
import time
import socket
from PyQt5.QtNetwork import QNetworkInterface
transports = frozenset(['udp', 'tcp', 'ipc', 'inproc'])
#Ports
BROADCAST_PORT = 6666
#KEYWORDS
ADRESS = '0'
TYPE = '1'
MESSAGE = '2'
PORT = '3'
SIZE = '4'
NAME = '5'
MAPF = '6'
REDF = '7'
CLIENT = '8'
BOSS = '9'
FILE = '10'
#GLOBAL VALUES
SIZEOFTASK = 100
TIMEOUT = 6000
RETRYTIME = 3
def map1(key,value):
res = []
for word in value.split(' '):
res.append((word, 1))
return res
def reduce1(key,values):
res = 0
for elem in values:
res += int(elem)
return res
def sendMessage(_type,_message,_dadress,_dport,_adress,_port ,c = None, _size = 0 , _mapf = None , _redf = None, _name = '',_client = None,_file= None):
s = c.socket(zmq.PUSH)
pull_addr = zmq_addr(_dport,transport = 'tcp',host = _dadress)
print(pull_addr)
s.connect(pull_addr)
print('here sending')
data = {
ADRESS : _adress,
PORT : _port ,
TYPE : _type,
MESSAGE : _message,
SIZE : _size ,
MAPF : _mapf ,
REDF : _redf ,
NAME : _name ,
CLIENT : _client,
FILE : _file
}
dataSend = dill.dumps(data)
s.send(dataSend,zmq.NOBLOCK)
def sendTask(_message,_dadress,_dport,_adress,_port , _size = 0 ,c = None, _mapf = None , _redf = None, _name = '',_client = None):
s = c.socket(zmq.PUSH)
s1 = c.socket(zmq.PULL)
s1_port = s1.bind_to_random_port('tcp://' + _adress)
pol = zmq.Poller()
pol.register(s1 , zmq.POLLIN)
pull_addr = zmq_addr(_dport,transport = 'tcp',host = _dadress)
print(pull_addr)
s.connect(pull_addr)
data = {
ADRESS : _adress,
PORT : s1_port ,
TYPE : 'task',
MESSAGE : _message,
SIZE : _size ,
MAPF : _mapf ,
REDF : _redf ,
NAME : _name ,
CLIENT : _client
}
dataSend = dill.dumps(data)
s.send(dataSend,zmq.NOBLOCK)
for i in range(0,RETRYTIME):
print(f'Retrytime {i}')
ans = dict(pol.poll(TIMEOUT))
if ans != {} :
data = s1.recv()
data = dill.loads(data)
return True , data[MESSAGE] , data[TYPE]
time.sleep(2)
return False , None , None
def pubMessage(s,_type,_message,_adress,_port , _size = 0 , _mapf = None , _redf = None, _name = '',_client = None, _file = None):
data = {
ADRESS : _adress,
PORT : _port ,
TYPE : _type,
MESSAGE : _message,
SIZE : _size ,
MAPF : _mapf ,
REDF : _redf ,
NAME : _name ,
CLIENT : _client,
FILE : _file
}
dataSend = dill.dumps(data)
s.send(dataSend,zmq.NOBLOCK)
def zmq_addr(port, transport=None, host=None):
if host is None:
host = '127.0.0.1'
if transport is None:
transport = 'tcp'
assert transport in transports
return '{transport}://{host}:{port}'.format(
transport = transport,
host = host,
port = port,
)
def waiting_to_broadcast(pull_port):
#Get My Adress
address = QNetworkInterface.allAddresses()
ip = address[2].toString()
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(('',BROADCAST_PORT))
while True:
data, addr = client.recvfrom(1024)
print(addr)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
while True:
try:
sock.connect((addr[0], addr[1]))
break
except (ConnectionRefusedError, OSError):
print("Can't connect to " + str((addr[0], addr[1])))
break
try:
sock.send(('tcp://' + ip +':' +str(pull_port)).encode())
except (BrokenPipeError, ConnectionResetError, ConnectionAbortedError, ConnectionError, ConnectionRefusedError):
continue
client.close()
def do_broadcast(b_Port):
address = QNetworkInterface.allAddresses()
ip = address[2].toString()
# Sending broadcast
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# Set a timeout so the socket does not block
rport = None
while True:
rport = random.randint(3000, 5000)
try :
client.bind((ip, rport))
break
except OSError :
pass
message = b'hello Server'
for i in range(RETRYTIME * 3):
if i > 0:
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind((ip, rport))
client.sendto(message, ('<broadcast>', b_Port))
print('I send the broadcast message')
# Then we listen in a TCP socket
# Listening response
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Set timeout
try:
client.bind((ip, rport))
except OSError:
return
client.settimeout(3)
client.listen(1000)
try:
s, addr_info = client.accept()
s_ip = s.recv(1024).decode()
if s_ip != '':
return s_ip
except:
print('ERROR', i)
client.close()
continue
return ''