-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
281 lines (228 loc) · 8.39 KB
/
data.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import json
import os
import socket
import netifaces as ni
import subprocess
import _thread
import time
from general import my_recv, my_send, send_folder, receive_folder
BUFFER_SIZE = 1024
server_ip = '192.168.43.154'
server_port = int(input('Enter port'))
folder_names = os.listdir(os.getcwd() + '/actual/data/input')
all_folders = []
for name in folder_names:
folder_info = {
'folder': name,
'status': 'no',
'uid': '',
'success': 'yes'
}
all_folders.append(folder_info)
# print(all_folders)
class DataServer:
def __init__(self, ip, hostname, port):
self.ip = ip
self.hostname = hostname
self.type = 'DataServer'
self.port = port
def create_init_message(self_hostname, self_ip, self_port):
return json.dumps({
'type': 'server_metadata',
'data_server_host_name': self_hostname,
'data_server_ip': self_ip,
'data_server_port': self_port
}).encode('UTF-8')
def init_self(port):
#self_ip = ni.ifaddresses('wlp3s0')[ni.AF_INET][0]['addr']
self_ip = '10.31.0.5'
self_hostname = socket.gethostname()
data_server = DataServer(self_ip, self_hostname, port)
return data_server
def receive_file(sock, file_size, file_name, chunk_size):
file = open(file_name, "wb")
while file_size > 0:
current = chunk_size
if file_size < chunk_size:
current = file_size
print(file_size)
file_data = sock.recv(current)
file_size -= len(file_data)
while not file_data:
file_data = sock.recv(current)
file.write(file_data)
file.close()
return
def send_coordinator_init_message():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server_ip, server_port))
server_query = my_recv(s)
print('Request from server : ' + str(server_query))
if server_query['type'] == 'question' and server_query['question'] == 'role':
my_send(s, {'type': 'question',
'role': 'data_server'})
server_q = my_recv(s)
if server_q['type'] == 'request' and server_q['file_type'] == 'code':
send_folder_old('/actual/code', s, type='actual_codes')
else:
my_send(s, {'type': 'question',
'error': 'invalid_query'})
return s
message_types = {
'GET_DATA': ''
}
# def send_file(path, client_sock, type):
# print('Sending file')
# try:
# file = open(path, 'r')
# data = file.read(BUFFER_SIZE)
# my_send(client_sock, {
# 'type': type,
# 'file_names': '0',
# 'file_sizes': '100',
# 'num_of_bytes': len(data),
# 'chunk_size': BUFFER_SIZE,
# })
# while data:
# client_sock.send(data)
# data = file.read(BUFFER_SIZE)
# file.close()
# client_sock.close()
# except IOError:
# client_sock.send(json.dumps({'error': str(IOError.filename)}).encode('UTF-8'))
def get_file_sizes(file_names, path):
size_list = []
for file in file_names:
size_list.append(os.stat(os.getcwd() + path + '/{}'.format(file)).st_size)
return size_list
def get_file_types(file_names):
types = []
for file in file_names:
types.append(file.split('.')[1])
return types
def send_input(connection, path, data_json):
type = data_json['type']
print('input path is : ' + path)
found = False
index = 0
for each in all_folders:
if each['status'] == 'no':
found = True
break
index += 1
if found:
path = path + '/' + all_folders[index]['folder']
all_folders[index]['status'] = 'processing'
all_folders[index]['uid'] = str(data_json['client_id']) + '_' + str(data_json['number'])
send_folder(connection, path, type)
else:
print('All folders sent')
def send_folder_old(path, client_sock, type):
print('Sending files in the folder')
print(path)
print(type)
try:
file_names = os.listdir(os.getcwd() + path)
file_sizes = get_file_sizes(file_names, path)
file_types = get_file_types(file_names)
my_send(client_sock, {
'type': type,
'file_name': file_names,
'file_size': file_sizes,
'chunk_size': BUFFER_SIZE,
'file_type': file_types
})
ack_json = json.loads(client_sock.recv(BUFFER_SIZE))
if ack_json['type'] == 'acknowledge_actual_codes':
total_files = len(os.listdir(os.getcwd() + path))
for idx, filename in enumerate(os.listdir(os.getcwd() + path)):
print('File {0} of {1}'.format(idx, total_files))
file = open(os.getcwd() + path + '/' + filename, 'r')
data = file.read(BUFFER_SIZE)
while data:
client_sock.send(data.encode('UTF-8'))
# print(data)
data = file.read(BUFFER_SIZE)
file.close()
print('----------Done sending file {}'.format(filename))
ack_j = my_recv(client_sock)
if ack_j['type'] == 'file_received':
continue
else:
print('ACK NOT RECVD')
break
else:
print('------------------Did not receive ack------------------')
except IOError:
print(IOError.strerror)
print('send_folder()')
def execute_code(s, filename):
code = subprocess.Popen(["python", filename], stdout=subprocess.PIPE)
while code.returncode is None:
# output = code.stdout.readline()
# s.send(output)
code.poll()
s.send("Done".encode('utf-8'))
debug_receive = 'request received : '
def request_handler(name, delay):
print('started thread')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('192.168.43.245', 7800))
s.listen(100)
while True:
conn, addr = s.accept()
print(addr)
req = conn.recv(BUFFER_SIZE).decode('UTF-8')
print(req)
json_data = json.dumps(all_folders)
conn.send(('HTTP/1.1 200 OK\n'
'Connection: close\n'
'Content-Type: application/json\n'
'Access-Control-Allow-Origin: *\n'
'Access-Control-Expose-Headers: Access-Control-Allow-Origin\n'
'Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept\n'
'\n' +
json_data).encode('utf-8'))
_thread.start_new_thread(request_handler, ('akshay', 1))
# data_server = init_self(7443)
s = send_coordinator_init_message()
while True:
print('\n\nWaiting for connection...')
# client_sock, address = socket_obj.accept()
data_json = my_recv(s)
# print(data_json)
if data_json:
if data_json['type'] == 'get_input':
# print(debug_receive + 'input')
print('get_input')
send_input(s, os.getcwd() + '/actual/data/input', data_json)
elif data_json['type'] == 'output':
# print(debug_receive + 'output')
folder_info = data_json
uid = str(folder_info['client_id']) + '_' + str(folder_info['number'])
print('uid is : ' + uid)
# all_folders
for folders in all_folders:
print(folders)
if folders['uid'] == uid:
print('changing folders dict : ' + str(folders))
folders['status'] = 'done'
print('output folder received : ' + str(uid))
break
my_send(s, {'type': 'acknowledge_output'})
recv_json = my_recv(s)
receive_folder(s, 'output/' + str(folder_info['client_id']) + '_' + str(folder_info['number']),
recv_json, type='acknowledge_output_files')
elif data_json['type'] == 'request':
print(debug_receive + 'actual code')
if data_json['file_type'] == 'code':
send_folder_old('/actual/code', s, type='actual_codes')
elif data_json['type'] == 'question':
print(debug_receive + 'role')
if data_json['question'] == 'role':
s.send(json.dumps({'type': 'question',
'role': 'data_server'}).encode('UTF-8'))
print('Task Completed')
print(all_folders)
else:
print('In else')