def run(self):
        self.quit_event = quit_event
        cond = self.cond
        lock = self.lock
        states = self.states
        results = self.results
        responses = self.responses
        cfg = self.cfg

        try:

            Dada.logMsg(2, DL, "commandThread: starting")

            # open a socket to receive commands via socket
            hostname = cfg["SERVER_HOST"]
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            Dada.logMsg(
                2, DL, "commandThread: binding to " + hostname + ":" +
                cfg["IBOB_MANAGER_PORT"])
            sock.bind((hostname, int(cfg["IBOB_MANAGER_PORT"])))

            # listen for at most 2 connections at a time (TCS and self)
            Dada.logMsg(3, DL, "commandThread: sock.listen(2)")
            sock.listen(2)

            can_read = [sock]
            can_write = []
            can_error = []
            timeout = 1

            valid_commands = dict({ "QUIT":QUIT, \
                                    "EXIT":TASK_EXIT, \
                                    "HELP":TASK_HELP, \
                                    "STATE":TASK_STATE, \
                                    "CONFIG":TASK_CONFIG, \
                                    "ARM":TASK_ARM, \
                                    "START_TX":TASK_START_TX, \
                                    "STOP_TX":TASK_STOP_TX, \
                                    "LEVELS":TASK_LEVELS, \
                                    "BRAMPLOT":TASK_BRAMPLOT, \
                                    "BRAMDISK":TASK_BRAMDISK})

            # keep listening
            # while ((not quit_event.isSet()) or (len(can_read) > 1)):
            while (not quit_event.isSet()):

                Dada.logMsg(
                    3, DL, "commandThread: calling select len(can_read)=" +
                    str(len(can_read)))
                timeout = 1
                did_read, did_write, did_error = select.select(
                    can_read, can_write, can_error, timeout)
                Dada.logMsg(
                    3, DL,
                    "commandThread: read=" + str(len(did_read)) + " write=" +
                    str(len(did_write)) + " error=" + str(len(did_error)))

                # if we did_read
                if (len(did_read) > 0):
                    for handle in did_read:
                        if (handle == sock):
                            (new_conn, addr) = sock.accept()
                            Dada.logMsg(
                                1, DL,
                                "commandThread: accept connection from " +
                                repr(addr))
                            # add the accepted connection to can_read
                            can_read.append(new_conn)
                            new_conn.send(
                                "Welcome to the HISPEC Roach Manager\r\n")

                        # an accepted connection must have generated some data
                        else:
                            message = handle.recv(4096)
                            message = message.strip()
                            Dada.logMsg(
                                3, DL,
                                "commandThread: message='" + message + "'")
                            if (len(message) == 0):
                                Dada.logMsg(
                                    1, DL, "commandThread: closing connection")
                                handle.close()
                                for i, x in enumerate(can_read):
                                    if (x == handle):
                                        del can_read[i]
                            else:

                                message = message.upper()
                                if ((message == "BRAMPLOT")
                                        or (message == "BRAMDISK")
                                        or (message == "STATE")):
                                    qdl = 2
                                else:
                                    qdl = 1

                                Dada.logMsg(qdl, DL, "<- " + message)

                                if (message in valid_commands.keys()):

                                    command = valid_commands[message]
                                    Dada.logMsg(
                                        3, DL, "commandThread: " + message +
                                        " was valid, index=" + str(command))

                                    if (command == QUIT):
                                        quit_event.set()

                                        # remove the server socket from the list of can_reads
                                        for i, x in enumerate(can_read):
                                            if (x == sock):
                                                Dada.logMsg(
                                                    2, DL,
                                                    "commandThread: removed sock from can_read"
                                                )
                                                del can_read[i]

                                        Dada.logMsg(
                                            2, DL,
                                            "commandThread: closing server socket [1]"
                                        )
                                        sock.close()
                                        sock = []

                                    # request to close the socket
                                    elif (command == TASK_EXIT):
                                        for i, x in enumerate(can_read):
                                            if (x == handle):
                                                Dada.logMsg(
                                                    2, DL,
                                                    "commandThread: removed curent handle from can_read"
                                                )
                                                del can_read[i]
                                                handle.close()
                                                handle = []

                                    elif (command == TASK_HELP):
                                        handle.send("Available commands:\r\n")
                                        handle.send(
                                            "  help    print these commands\r\n"
                                        )
                                        handle.send(
                                            "  exit    close current connection\r\n"
                                        )
                                        handle.send(
                                            "  quit    stop the server script\r\n"
                                        )
                                        handle.send(
                                            "  config  program all roaches\r\n"
                                        )
                                        handle.send(
                                            "  state   report state of all roaches\r\n"
                                        )
                                        handle.send(
                                            "  levels  perform level setting\r\n"
                                        )
                                        handle.send(
                                            "  arm     start 10GbE packets, reset seq no, returning UTC_START\r\n"
                                        )
                                        handle.send(
                                            "  stop    stop 10GbE packets\r\n")

                                    else:
                                        # now process this message
                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: lock.acquire()")
                                        lock.acquire()
                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: lock acquired")

                                        for i in range(n_roach):
                                            roach_states[i] = command
                                        roach_state = command
                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: states set to " +
                                            message)

                                        # all commands should happen as soon as is practical, expect
                                        # for the arm, which should ocurr very close to 0.5 seconds
                                        # through a second. This command should also return the UTC time
                                        # corresponding to the expected start
                                        if (command == TASK_ARM):
                                            # busy sleep until the next second ticks over
                                            curr_time = int(time.time())
                                            next_time = curr_time
                                            Dada.logMsg(
                                                2, DL,
                                                "commandThread: waiting for 1 second boundary"
                                            )
                                            while (curr_time == next_time):
                                                next_time = int(time.time())
                                            Dada.logMsg(
                                                2, DL,
                                                "commandThread: sleeping 0.5 seconds"
                                            )
                                            time.sleep(0.5)
                                            utc_start = Dada.getUTCDadaTime(1)
                                            Dada.logMsg(
                                                2, DL,
                                                "commandThread: UTC_START=" +
                                                utc_start)

                                        # activate threads
                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: cond.notifyAll()")
                                        cond.notifyAll()
                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: lock.release()")
                                        lock.release()

                                        # wait for all roaches to finished the command
                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: lock.acquire()")
                                        lock.acquire()
                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: lock acquired")

                                        command_result = ""
                                        command_response = ""

                                        while (roach_state == command):
                                            Dada.logMsg(
                                                3, DL,
                                                "commandThread: checking all roaches for IDLE"
                                            )

                                            n_idle = 0
                                            n_error = 0
                                            n_running = 0
                                            n_ok = 0
                                            n_fail = 0

                                            for i in range(n_roach):
                                                Dada.logMsg(
                                                    3, DL,
                                                    "commandThread: testing roach["
                                                    + str(i) + "]")

                                                # check the states of each roach thread
                                                if (roach_states[i] == IDLE):
                                                    n_idle += 1
                                                    # check the return values of this roach
                                                    Dada.logMsg(
                                                        3, DL,
                                                        "commandThread: roach_results["
                                                        + str(i) + "] = " +
                                                        roach_results[i])
                                                    if (roach_results[i] ==
                                                            "ok"):
                                                        n_ok += 1
                                                    else:
                                                        n_fail += 1
                                                elif (roach_states[i] == ERROR
                                                      ):
                                                    Dada.logMsg(
                                                        -1, DL,
                                                        "commandThread: roach["
                                                        + str(i) +
                                                        "] thread failed")
                                                    n_error += 1
                                                else:
                                                    n_running += 1

                                            # if all roach threads are idle, we are done - extract the results
                                            if (n_idle == n_roach):

                                                roach_state = IDLE
                                                if (n_ok == n_roach):
                                                    command_result = "ok"
                                                else:
                                                    command_result = "fail"
                                                for i in range(n_roach):
                                                    command_response = command_response + roach_cfg[
                                                        "BEAM_" + str(i)] + ":"
                                                    if (roach_responses[i] !=
                                                            ""):
                                                        command_response += roach_responses[
                                                            i] + " "
                                                    else:
                                                        command_response += roach_results[
                                                            i] + " "

                                            elif (n_error > 0):
                                                Dada.logMsg(
                                                    2, DL,
                                                    "commandThread: roach thread error"
                                                )
                                                command_result = "fail"
                                                command_response = str(
                                                    n_error
                                                ) + " roach threads failed"
                                                roach_state = ERROR

                                            else:
                                                Dada.logMsg(
                                                    3, DL,
                                                    "commandThread: NOT all IDLE, cond.wait()"
                                                )
                                                cond.wait()

                                        if (command == TASK_ARM):
                                            if (command_response != ""):
                                                command_response = command_response + "\n" + utc_start
                                            else:
                                                command_response = utc_start

                                        if (command_response != ""):
                                            handle.send(command_response +
                                                        "\r\n")
                                            Dada.logMsg(
                                                qdl, DL,
                                                "-> " + command_response)

                                        handle.send(command_result + "\r\n")
                                        Dada.logMsg(qdl, DL,
                                                    "-> " + command_result)

                                        Dada.logMsg(
                                            3, DL,
                                            "commandThread: lock.release()")
                                        lock.release()

                                else:
                                    Dada.logMsg(
                                        2, DL,
                                        "commandThread: unrecognised command")
                                    Dada.logMsg(1, DL, " -> fail")
                                    handle.send("fail\r\n")

        except:
            Dada.logMsg(
                -2, DL,
                "commandThread: exception caught: " + str(sys.exc_info()[0]))
            print '-' * 60
            traceback.print_exc(file=sys.stdout)
            print '-' * 60

        for i, handle in enumerate(can_read):
            Dada.logMsg(2, DL,
                        "commandThread: closing can_read[" + str(i) + "]")
            handle.close
            del can_read[i]

        if (not sock == []):
            Dada.logMsg(2, DL, "commandThread: closing server socket [2]")
            sock.close()

        Dada.logMsg(2, DL, "commandThread: exiting")
Esempio n. 2
0
Dada.logMsg(1, dl, "main: connectRoach")
result, fpga = fullpol.connectRoach(dl, rid)
if (not result == "ok"):
  Dada.logMsg(0, dl, "["+rid+"] main: could not connect to ROACH")
  sys.exit(1)

Dada.logMsg(1, dl, "main: setLevels")
result, response = fullpol.levelSetRoach(dl, fpga, rid)
Dada.logMsg(1, dl, "main: " + result + " " + response)

Dada.logMsg(1, dl, "main: crossLevelSetRoach")
result, response = fullpol.crossLevelSetRoach(dl, fpga, rid)
Dada.logMsg(1, dl, "main: " + result + " " + response)

curr_time = int(time.time())
next_time = curr_time
Dada.logMsg(2, dl, "main: waiting for 1 second boundary")
while (curr_time == next_time):
  next_time = int(time.time())

Dada.logMsg(2, dl, "main: sleeping 0.5 seconds")
time.sleep(0.5)
utc_start = Dada.getUTCDadaTime(1)
Dada.logMsg(2, dl, "main: UTC_START=" + utc_start)

fpga.write_int('reg_arm',0)
fpga.write_int('reg_arm',1)

print utc_start