def assess(connection, address): current_dir = os.getcwd() if HAS_SAMPLE: sizes = [] # To get sizes of each file for each in SAMPLE_FILE: file_info = os.stat(ASSESS_DIR + '/' + each) file_size = file_info.st_size sizes.append(file_size) msg = { 'type': 'assess', 'file_size': sizes, 'chunk_size': BUFFER_SIZE, 'file_name': SAMPLE_FILE, 'file_type': SAMPLE_TYPE } my_send(connection, msg) print('waiting for acknowledge') response = my_recv(connection) if response['type'] == 'acknowledge_assess': for each in range(len(SAMPLE_FILE)): file_name = SAMPLE_FILE[each] file_type = SAMPLE_TYPE[each] f = open(ASSESS_DIR + file_name, 'rb') file_size = sizes[each] chunk_size = BUFFER_SIZE while file_size > 0: print(file_size) current = chunk_size if file_size < chunk_size: current = file_size # print(current) msg = f.read(current) file_size -= current connection.send(msg) # temp = connection.recv(2).decode('UTF-8') # print(temp) # if temp != 'ok': # print('FAIL') print('Done:' + file_name) response = my_recv(connection) if not (response['type'] == 'file_received' and response['file_name'] == file_name): print('Failure') else: print('Success') else: print('Didnt got response') else: msg = {'type': 'error', 'error': 'Sample file not found'} my_send(connection, msg)
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
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 run(self): global HAS_SAMPLE global DATA_IP global DATA_THREAD_ID global DATA_PORT msg = {'type': 'question', 'question': 'role'} my_send(self.connection, data=msg) response = my_recv(self.connection) role = response['role'] self.role = role print(response) print(role) if role == 'data_server': DATA_IP = self.address DATA_THREAD_ID = self.threadID msg = {'type': 'request', 'file_type': 'code'} # print(self.address) my_send(self.connection, data=msg) response = my_recv(self.connection) type = response['type'] file_name = response['file_name'] file_size = response['file_size'] chunk_size = response['chunk_size'] msg = {'type': 'acknowledge_' + type} my_send(self.connection, data=msg) for i in range(len(file_name)): file = open('code/' + file_name[i], "wb") bytes_received = file_size[i] while bytes_received > 0: current = chunk_size if bytes_received < chunk_size: current = bytes_received print(bytes_received) # s.settimeout(TIMEOUT) file_data = self.connection.recv(current) # print('--------' + str(len(file_data))) bytes_received -= len(file_data) file.write(file_data) file.close() file_response = { "type": "file_received", "file_name": file_name[i] } # print(self.address) my_send(self.connection, data=file_response) print("received " + file_name[i]) global HAS_CODE HAS_CODE = True while True: # print(task_queue.empty()) if not task_queue.empty(): each_task = task_queue.get() if each_task['type'] == 'send_output': each_task['type'] = 'output' # print(self.address) my_send(self.connection, data=each_task) response = my_recv(self.connection) type = response['type'] if type == 'get_input': msg = {'type': 'acknowledge_' + type} # print(self.address) my_send(self.connection, data=msg) file_names = response['file_name'] file_size = response['file_size'] chunk_size = response['chunk_size'] path = 'input/' + str( each_task['client_id']) + '/' + str( each_task['number']) + '/' if not os.path.exists(path): os.makedirs(path) # receive_folder(self.connection, path, response) for i in range(len(file_names)): file = open(path + file_names[i], "wb") bytes_received = file_size[i] while bytes_received > 0: current = chunk_size if bytes_received < chunk_size: current = bytes_received print(bytes_received) # s.settimeout(TIMEOUT) file_data = self.connection.recv(current) # print('--------' + str(len(file_data))) bytes_received -= len(file_data) file.write(file_data) file.close() file_response = { "type": "file_received", "file_name": file_names[i] } # print(self.address) my_send(self.connection, data=file_response) print("received " + file_names[i] + ' for client/number ' + str(each_task['client_id']) + '/' + str(each_task['number'])) folder_path = os.getcwd() + '/input/' + str(each_task['client_id']) + '/' + \ str(each_task['number']) + '/' done_task = { str(each_task['client_id']) + '_' + str(each_task['number']): { 'file_names': file_names, 'folder_path': folder_path, 'status': 'done' } } done_task_list.append(done_task) elif type == 'acknowledge_output': msg = {'type': type} # print(self.address) # my_send(self.connection, data=msg) each_task['chunk_size'] = BUFFER_SIZE send_folder(self.connection, each_task['path'], 'output_files') # my_recv(self.connection) print("folder sent! path: " + each_task['path']) # self.connection.close() else: # assess(self.connection, self.address) final_answer = 'yes' send_code_files(connection=self.connection) while final_answer == 'yes': request = {'type': 'question', 'question': 'input_data'} # print(self.address) my_send(self.connection, request) response = my_recv(self.connection) if response['type'] == 'response_input': final_answer = response['response'] if response['response'] == 'yes': print('Got yes') my_dict = { 'client_id': self.threadID, 'number': self.number, 'type': 'get_input' } task_queue.put(my_dict) exists = False msg = str(self.threadID) + '_' + str(self.number) index = -1 while not exists: length = done_task_list.__len__() for i in range(length): if msg in done_task_list[i]: exists = True index = i break current_task = done_task_list[index][msg] send_folder(self.connection, current_task['folder_path'], 'actual_input') recv_response = my_recv(self.connection) while not recv_response: recv_response = my_recv(self.connection) if recv_response['type'] == 'finished': temp_response = { 'type': 'acknowledge_' + recv_response['type'] } # print(self.address) my_send(self.connection, temp_response) if recv_response['status'] == 'success': path = os.getcwd() + '/output/' + str( self.threadID) + '_' + str(self.number) response = my_recv(self.connection) response[ 'type'] = 'acknowledge_' + response['type'] receive_folder(self.connection, path, response) task_json = { 'client_id': self.threadID, 'number': self.number, 'type': 'send_output', 'path': path } task_queue.put(task_json) print('Inserted into task') else: print('Got type not finished') self.number += 1 else: print('got NO') self.connection.close() return 1
'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)