def start_handler(server,processes,id): iterator = 1 pipes = [] open_pipes = [] #clear msg pipes for subprocesses os.system("rm -f -r temp/pipes_intern/*") #clear logfile directory, but keep daemon.log os.system("cd logfiles && ls * | grep -v daemon.log | xargs rm -rf") #create pipes for every subprocess in for pro in processes: #create stderr pipe err_name = "temp/pipes_intern/err_"+pro.name.split('.')[0]+"_"+str(iterator) os.mkfifo(err_name) pipes.append(err_name) #create out pipe out_name = "temp/pipes_intern/out_"+pro.name.split('.')[0]+"_"+str(iterator) os.mkfifo(out_name) pipes.append(out_name) #give name of pipes to process pro.stout = out_name pro.sterr = err_name #create logfile for module log_name = "logfiles/"+pro.name.split('.')[0]+"_"+str(iterator)+".log" os.system("touch "+log_name) #increment iterator iterator = iterator+1 #start handler as subprocess new_pid = os.fork() if new_pid == 0: open_pipes = [open(pipe,'r') for pipe in pipes] while True: ready_pipes = select.select(open_pipes,[],[])[0] for pipe in ready_pipes: #analyze information given by pipe pipe_name = pipe.name.split('/')[-1] pipe_name_splitted = pipe_name.split('_') msg_type = pipe_name_splitted[0] #rebuilding the module name even with '_' in the name module_name_list = pipe_name_splitted[1:-1] module_name = module_name_list[0] for name in module_name_list[1:]: module_name = module_name + '_' + name iterator = pipe_name_splitted[-1] #read from pipe data = pipe.readline() if data != "" and data != " ": #write to logfile log_name = "logfiles/"+module_name+"_"+str(iterator)+".log" file = open(log_name,'a') file.write(data) file.close() #send to debug window of server socket_send_msg(server,"Pi "+str(id)+", "+module_name+": "+data) return new_pid,processes
def is_online(sockets): if sockets != -1: for soc in sockets: ip = soc.getpeername() socket_send_msg(soc, "isonline") socket_receive_msg(soc) print(str(ip) + " is connected") else: print("Sockets not connected yet")
def connect(xmlfile, connected, sockets): if xmlfile == "": print("Set xml-file before connecting") return -1, 0 if connected == 1: for soc in sockets: socket_send_msg(soc, "reconnect") time.sleep(1) root_as_string = parse_xml(xmlfile) root = objectify.fromstring(root_as_string) nodes = [] cmd_sockets = [] for element in root.getchildren(): nodes.append(etree.tostring(element)) # do for every pi for node in nodes: # connect to pi ip = "" try: int(objectify.fromstring(node).get("pi_id")) ip = "10.1.1." + objectify.fromstring(node).get("pi_id") except Exception: ip = objectify.fromstring(node).get("pi_id") try: soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) soc.connect((ip, 9999)) cmd_sockets.append(soc) print("Connected to cmd of " + str(ip)) except Exception as e: print e print "Unable to connect to cmd of {}".format(ip) if len(cmd_sockets) < 1: return -1, 0 else: return cmd_sockets, 1
def start(xmlfile, sockets): if (xmlfile == "") or (sockets == -1): print("Set xml-file and connect before starting") return -1 print("Start: " + xmlfile) root_as_string = parse_xml(xmlfile) root = objectify.fromstring(root_as_string) nodes = [] for element in root.getchildren(): nodes.append(etree.tostring(element)) #do for every pi for node, soc in zip(nodes, sockets): try: int(objectify.fromstring(node).get("pi_id")) ip = "10.1.1." + objectify.fromstring(node).get("pi_id") #send 'start' command to pi and wait for 'ack' socket_send_msg(soc, "start") socket_receive_msg(soc) socket_send_msg(soc, node) socket_receive_msg(soc) print([ip + " is ready"]) except Exception: ip = objectify.fromstring(node).get("pi_id") #send 'start' command to pi and wait for 'ack' socket_send_msg(soc, "start") socket_receive_msg(soc) socket_send_msg(soc, node) socket_receive_msg(soc) print([ip + " is ready"]) for soc in sockets: socket_send_msg(soc, "start") return
def start(server_cmd,server_msg,id): #no subprocesses running subprocesses = [] #get objects from the server processes,connections_in,connections_out = get_objects(server_cmd) #initialize pipes init_pipes(processes) #initialize ingoing connections subprocesses = subprocesses + init_ingoing_connections(connections_in) #initialize outgoing connections subprocesses = subprocesses + init_outgoing_connections(connections_out) #open read pipes (non blocking!) processes = open_ingoing_pipes(processes) #start message handler handler,processes=start_handler(server_msg,processes,id) running = 1 #wait for starting signal from server (synchronization) try: socket_send_msg(server_cmd,"ready") socket_receive_msg(server_cmd) except Exception as e: log("synchronization with server failed") log(type(e)) log(e) #open write pipes processes = open_outgoing_pipes(processes) #run processes log("execute processes") subprocesses = subprocesses + execute_processes(processes) return subprocesses,running,handler
def exit_server(sockets): if sockets != -1: for soc in sockets: socket_send_msg(soc, "reconnect") sys.exit()
def get_objects(server_socket): processes = [] connections_in = [] connections_out = [] dir_iterator = 1 pipe_path = "/home/asn/asn_daemon/temp/pipes_system/fifo_" #send 'ack' to server to begin with transfer socket_send_msg(server_socket, "CMD ack") #receive data try: rootString = socket_receive_msg(server_socket) except Exception as e: log("Receiving data from server failed") log(type(e)) log(e) #parse data root = objectify.fromstring(rootString) #get Pi-id pi_id = root.get("pi_id") #loop through executables for p in root.getchildren(): input = [] output = [] parameter = '' #get name of executable name = p.get('executable') #get path slash = name.rfind('/') path = 'system/' + str(dir_iterator) if slash != -1: path = path + '/' + name[:slash] dir_iterator = dir_iterator + 1 #loop through parameters (input/output pipes and additional params) for atom in p.getchildren(): # collect input pipes if (atom.tag == 'input'): pipe_id = atom.get("pipe_id") pipe_name = pipe_path + str(pipe_id) input.append(pipe_name) #register ingoing nc connection if necessary if atom.get("source_pi_id") != pi_id: try: int(pi_id) connections_in.append( NCcon_c("10.1.1." + str(pi_id), (1000 + int(pipe_id)), pipe_name)) except Exception: connections_in.append( NCcon_c(str(pi_id), (1000 + int(pipe_id)), pipe_name)) # collect output fifos if (atom.tag == 'output'): pipe_id = atom.get("pipe_id") pipe_name = pipe_path + str(pipe_id) output.append(pipe_name) #register outgoing nc connection if necessary target_id = atom.get("target_pi_id") if target_id != pi_id: try: int(target_id) connections_out.append( NCcon_c("10.1.1." + target_id, (1000 + int(pipe_id)), pipe_name)) except Exception: connections_out.append( NCcon_c(target_id, (1000 + int(pipe_id)), pipe_name)) # collect parameter if (atom.tag == 'parameter'): param = atom.get("param") parameter = parameter + param + " " # append new process processes.append(process_c(input, output, path, name, parameter)) return processes, connections_in, connections_out
def gentleAbort(sockets): if sockets == -1: print("Sockets not connected yet") return for so in sockets: socket_send_msg(so, "gentleAbort")
elif command == "delete": delete_data() elif command == "reconnect": try: server_cmd, server_msg = reconnect(ip) except Exception as e: log("reconnect method failed") log(type(e)) log(e) elif command == "isonline": try: socket_send_msg(server_cmd, "online") except Exception as e: log("sending online status failed") log(type(e)) log(e)
def delete(sockets): if sockets == -1: print("Not connected") return for so in sockets: socket_send_msg(so, "delete")