Esempio n. 1
0
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")
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
    sockets = []
    listOfIds = sys.argv[1:]

    for id in listOfIds:
        # connect to pi
        try:
            int(id)
            ip = str(id)
        except:
            ip = id

        try:
            soc1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            soc1.connect((ip, 9998))
            sockets.append(soc1)
            print("Connected to msg of " + str(ip))
        except:
            print("Unable to connect to msg of " + str(ip))

    while (1):
        try:
            readySockets = select.select(sockets, [], [])[0]
            for soc in readySockets:
                text = socket_receive_msg(soc)
                if text != "" and text != None:
                    print(text, end="")
        except Exception as inst:
            print(type(inst))
            print(inst.args)
            print(inst)
Esempio n. 5
0
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
Esempio n. 6
0
        log("Message port connected")

    except Exception as e:

        log("Connection to server failed")

        log(type(e))

        log(e)

    #main-loop, waiting for commands of server

    while (1):

        command = socket_receive_msg(server_cmd)

        if command == "start":

            try:

                subprocesses, running, handler = start(server_cmd, server_msg,
                                                       id)

            except Exception as e:

                log("start method failed")

                log(type(e))

                log(e)