def _main(): last = '' file_flag = False while True: file_flag = False raw_data = input() if raw_data == 'restart': msw_queue.put(0) break if raw_data == 'exit': msw_queue.put(1) break if raw_data == 'update': raw_data = 'update:compress;update_to:*' if raw_data == '1': raw_data = 'ffmpeg:autostart' if raw_data == '2': raw_data = 'ffmpeg:enable;to:*,server:miku' if raw_data == 'r': raw_data = last last = raw_data if raw_data[:6] == '(file)': # like "(file)log: filename.exe" raw_data = raw_data[6:] file_flag = True first_index, last_index = find_index(raw_data) app = raw_data[:first_index] body = raw_data[last_index:] ihead = {} if ';' in body and ':' in body: ihead_index = body.index(';') ihead_str = body[ihead_index + 1:] body = body[:ihead_index] ihead_list = ihead_str.split(',') for key_value in ihead_list: key, value = key_value.split(':') ihead[key] = value app = app.replace(' ', '') dp = Datapack(head={'from': __name__}) dp.head.update(ihead) dp.app = app if file_flag: dp.method = 'file' dp.body = b'' dp.head['filename'] = body else: dp.body = body.encode() send_queue.put(dp) print('Command has been sent', dp)
def send_request(self): if self.status: dp = Datapack(head={'from': __name__}) dp.method = 'get' dp.app = 'ffmpeg' dp.head['to'] = self.server send_queue.put(dp)
def process_command(self, dp): if dp.body == b'status': result = '' result += 'Online %s' % str(self.id_dict) + '\n' result += 'proxydict %s' % str(self.proxydict) + '\n' result += 'conflist %s' % str(self.conflist) + '\n' result += 'conflist_pass %s' % str(self.conflist_pass) + '\n' result += 'netlist %s' % str(self.netlist) + '\n' result += 'netlist_pass %s' % str(self.netlist_pass) + '\n' result += 'mhtlist %s' % str(self.mhtlist) + '\n' result += 'mhtlist_pass %s' % str(self.mhtlist_pass) ndp = dp.reply() ndp.body = result.encode() send_queue.put(ndp) elif dp.body == b'mht' and dp.method == 'get': ndp = dp.reply() data_dict = {} connection_list = [] with self.lock: for id in self.id_dict: connections = self.id_dict[id] for connection in connections: ip, port = connection.conn.getpeername() port = int(connection.listen_port) connection_list.append((ip, port)) for addr in self.conflist: if not addr in connection_list: connection_list.append(addr) for addr in self.conflist_pass: if not addr in connection_list: connection_list.append(addr) data_dict['mht'] = connection_list data_dict['proxy'] = self.proxydict ndp.body = json.dumps(data_dict).encode() send_queue.put(ndp) elif dp.method == 'reply': mhtstr = dp.body.decode() data_dict = json.loads(mhtstr) mhtlist = data_dict['mht'] with self.lock: for addr in mhtlist: addr = (addr[0], addr[1]) if not self.check_in_list(addr): self.mhtlist.append(addr) self.proxydict.update(data_dict['proxy']) else: print('Received unknown command', dp)
def start_mht(self): while True: dp = Datapack(head={'from': __name__}) dp.head['to'] = '*' dp.app = 'net' dp.method = 'get' dp.body = b'mht' #print('Send mht request', dp) send_queue.put(dp) time.sleep(10)
def main(): while True: dp = receive_queue.get() command = dp.body.decode() try: result = subprocess.check_output(command, shell=True) except Exception as e: result = 'Command %s error, %s: %s' % (command, type(e), str(e)) result = result.encode() ndp = dp.reply() ndp.body = try_decode_and_encode(result) send_queue.put(ndp)
def convert_func(self): # run as client for _ in range(2): self.send_request() while self.status or not self.convert_task_queue.empty(): dp = receive_queue.get() if dp.method == 'post' and dp.body == b'disable': self.status = 0 elif dp.method == 'post' and dp.body == b'status': result = 'Working as client, queue size: %s' % str( self.convert_task_queue.qsize()) ndp = dp.reply() ndp.body = result.encode() send_queue.put(ndp) elif dp.method == 'file': self.convert_task_queue.put(dp)
def convert_task_func(self): while True: dp = self.convert_task_queue.get() filename = dp.head['filename'] output_filename = filename[:-4] + '.mkv' output_filename = output_filename.replace('ffmpeg_tmp', 'ffmpeg_finished') os.system('ffmpeg -i "' + os.path.normpath(filename) + '" -c:a libopus -ab 64k \ -c:v libx265 -s 1280x720 -y "' + os.path.normpath(output_filename) + '"') os.remove(filename) ndp = dp.reply() ndp.head['filename'] = output_filename ndp.head['old_filename'] = filename ndp.method = 'file' ndp.delete = True send_queue.put(ndp) self.send_request()
def main(): while True: dp = receive_queue.get() if dp.method == 'post': if dp.body == b'compress': # compressing file print('Starting update') compress = Compresser() compress.start_compress() print('Compress finished') # getting to destination to = dp.head.get('update_to') if not to: print('unable to locate update_to') continue # sending file ndp = Datapack(head={'from':__name__}) ndp.method = 'file' ndp.app = 'update' ndp.head['filename'] = 'res/update.tar.xz' ndp.head['to'] = to send_queue.put(ndp) elif dp.method == 'file': print('Starting update local file') with tarfile.open(dp.head['filename'], 'r:xz') as f: f.extractall() #os.remove(dp.head['filename']) # restart msw program msw_queue.put(0)
def receive(self): still_need = 0 while True: try: data = self.conn.recv(BUFFSIZE) except ConnectionResetError: break except Exception as e: print('Connection recv error %s: %s' % (type(e), str(e))) break if not data: break self.buff += data if not still_need: dp = Datapack() dp.encode_data = self.buff try: self.buff = dp.decode(only_head=True) if dp.method == 'file': create_floder(dp.head['filename']) create_floder('tmp/' + dp.head['filename']) self.f = open('tmp/' + dp.head['filename'], 'ab') if dp.method == 'file' and os.path.exists(dp.head['filename']): os.remove(dp.head['filename']) except Exception as e: print('Decode head failed %s: %s' % (type(e), str(e))) print(self.buff) break length = int(dp.head.get('length')) still_need = length if still_need > len(self.buff): # writing tmp data if dp.method == 'file': still_need -= self.f.write(self.buff) else: dp.body += self.buff still_need -= len(self.buff) self.buff = b'' # empty buff because all tmp data has been write else: # download complete setuation if dp.method == 'file': self.f.write(self.buff[:still_need]) self.f.close() self.f = None else: dp.body = self.buff[:still_need] self.buff = self.buff[still_need:] still_need = 0 # bleow code are using to process datapack if dp.method == 'file': os.rename('tmp/' + dp.head['filename'], dp.head['filename']) print('Received file %s from %s' % (dp.head['filename'], self.id), dp) send_queue.put(dp) # below code are using to closed connection if self.f: self.f.close() self.f = None self.conn.close() self.netowrk_controller.del_connection(self)
def mainloop(self): _create_floder('res/ffmpeg_tmp') _create_floder('res/ffmpeg_finished') _create_floder('res/ffmpeg_task') _create_floder('res/ffmpeg_old') _create_floder('res/ffmpeg_complet') while True: dp = receive_queue.get() if dp.method == 'post' and dp.body == b'concat': self.org_filename = dp.head['filename'] self.object_filename = self.org_filename[:-4] + '.mkv' self.concat_func() if dp.method == 'post' and dp.body == b'autostart': filelist = os.listdir('res/ffmpeg_task') self.tasklist = [] for file in filelist: if len(file) > 3: ext = file[-4:] if ext in ['.mp4', '.MP4', '.mkv', '.MKV']: self.tasklist.append('res/ffmpeg_task/' + file) dp = Datapack() dp.app = 'ffmpeg' dp.body = b'start' dp.head['filename'] = self.tasklist.pop(0) self.autostart = dp.head['filename'] send_queue.put(dp) if dp.method == 'post' and dp.body == b'start': # config ffmpeg is server or client if dp.head.get('concat'): if dp.head['concat'] == 'true': self.concat = True elif dp.head['concat'] == 'false': self.concat = False else: print('unknown concat value') continue else: self.concat = True if self.concat: self.org_filename = dp.head['filename'] self.object_filename = 'res/ffmpeg_complet/' + os.path.basename( self.org_filename)[:-4] + '.mkv' if self.concat: ndp = dp.reply() ndp.body = 'Spliting file %s' % dp.head['filename'] ndp.body = ndp.body.encode() send_queue.put(ndp) cmd = 'ffmpeg -i "' + os.path.normpath( dp.head['filename']) + '" -c copy \ -f segment -segment_time 20 -reset_timestamps 1 -y \ "res/ffmpeg_tmp/' + '%d' + '.mkv"' os.system(cmd) self.run_as_server() if self.concat: self.concat_func() print('All process finished') elif dp.method == 'post' and dp.body == b'enable': # clinet mode self.status = 1 self.server = dp.head['server'] self.convert_func() elif dp.method == 'post' and dp.body == b'status': result = 'ffmpeg not working' ndp = dp.reply() ndp.body = result.encode() send_queue.put(ndp) elif dp.method == 'get': # let other client disable ndp = dp.reply() ndp.method = 'post' ndp.body = b'disable' print('let %s disabled' % dp.head['id']) send_queue.put(ndp)
def run_as_server(self): _padding_to_convert = os.listdir('res/ffmpeg_tmp') padding_to_convert = [] for file in _padding_to_convert: file = 'res/ffmpeg_tmp/' + file padding_to_convert.append(file) already_in_convert = [] finished_convert = [] # outputfilename while True: dp = receive_queue.get() if dp.method == 'post' and dp.body == b'status': result = '' result += 'padding_to_convert ' + str( padding_to_convert) + '\n' result += 'already_in_convert ' + str( already_in_convert) + '\n' result += 'finished_convert ' + str(finished_convert) + '\n' result += 'convert_task_queue size ' + str( self.convert_task_queue.qsize()) ndp = dp.reply() ndp.body = result.encode() send_queue.put(ndp) elif dp.method == 'post' and dp.body == b'reset': padding_to_convert = already_in_convert already_in_convert = [] elif dp.method == 'post' and dp.body == b'stop': break elif dp.method == 'post' and dp.body == b'pause': self.pause = True elif dp.method == 'post' and dp.body == b'continue': self.pause = False elif dp.method == 'get': if self.pause: ndp = dp.reply() ndp.method = 'post' ndp.body = b'disable' send_queue.put(ndp) continue if padding_to_convert: filename = padding_to_convert.pop(0) already_in_convert.append(filename) print('%s get %s to convert' % (dp.head['id'], filename), dp) ndp = dp.reply() ndp.method = 'file' ndp.head['filename'] = filename send_queue.put(ndp) else: if not already_in_convert: # finished break else: # waiting for final convert ndp = dp.reply() ndp.method = 'post' ndp.body = b'disable' send_queue.put(ndp) elif dp.method == 'file': old_filename = dp.head['old_filename'] filename = dp.head['filename'] os.remove(old_filename) already_in_convert.remove(old_filename) finished_convert.append(filename) total = len(padding_to_convert) + len( already_in_convert) + len(finished_convert) print('Processing...(%d) %d/%d %s' % \ (len(already_in_convert), \ len(finished_convert), \ total, \ str(round(len(finished_convert)/total*100, 2)))) if not padding_to_convert and not already_in_convert: # final process break print('Mapreduce finished')