def run(self):
        rid = str(self.roach_num)
        ithread = self.roach_num
        cfg = self.cfg
        lock = self.lock
        cond = self.cond
        states = self.states
        results = self.results
        fpga = []
        locked = False
        acc_len = 25
        roach_cfg = Bpsr.getROACHConfig()
        roach_name = roach_cfg["ROACH_IP_" + rid]
        os.chdir(cfg["SERVER_STATS_DIR"])

        try:

            Dada.logMsg(1, DL, "roachThread[" + rid + "]: starting")

            # acquire lock to wait for commands setup
            locked = lock.acquire()
            Dada.logMsg(2, DL, "roachThread[" + rid + "]: lock acquired")

            while (states[ithread] != QUIT):
                while (states[ithread] == IDLE):
                    Dada.logMsg(
                        2, DL,
                        "roachThread[" + rid + "]: waiting for not IDLE")
                    locked = False
                    cond.wait()
                    locked = True
                if (states[ithread] == QUIT):
                    Dada.logMsg(1, DL,
                                "roachThread[" + rid + "]: quit requested")
                    lock.release()
                    loacked = False
                    return

                # we have been given a command to perform (i.e. not QUIT or IDLE)
                task = states[ithread]
                Dada.logMsg(2, DL,
                            "roachThread[" + rid + "]: TASK=" + str(task))

                Dada.logMsg(2, DL, "roachThread[" + rid + "]: lock.release()")
                lock.release()
                locked = False

                result = "fail"

                if (task == TASK_STATE):
                    Dada.logMsg(2, DL,
                                "roachThread[" + rid + "]: state request")
                    if (fpga != []):
                        result = "ok"

                elif (task == TASK_CONFIG):
                    Dada.logMsg(2, DL,
                                "roachThread[" + rid + "]: perform config")
                    result, fpga = Bpsr.configureRoach(DL, acc_len, rid, cfg)
                    if (result != "ok"):
                        Dada.logMsg(
                            -2, DL,
                            "roachThread[" + rid + "]: roach not ready")
                        fpga = []

                elif (task == TASK_ARM):
                    Dada.logMsg(2, DL, "roachThread[" + rid + "]: perform arm")
                    if (fpga != []):
                        result = Bpsr.armRoach(DL, fpga)

                elif (task == TASK_SET_LEVELS):
                    Dada.logMsg(2, DL,
                                "roachThread[" + rid + "]: perform set levels")
                    if (fpga != []):
                        result = Bpsr.setLevels(DL, fpga)

                elif (task == TASK_BRAMPLOT):
                    if (fpga != []):
                        Dada.logMsg(
                            2, DL,
                            "roachThread[" + rid + "]: perform bramplot")
                        time_str = Dada.getCurrentDadaTime()
                        result = Bpsr.bramplotRoach(DL, fpga, time_str,
                                                    roach_name)
                    else:
                        Dada.logMsg(
                            -1, DL,
                            "roachThread[" + rid + "]: not connected to FPGA")

                else:
                    Dada.logMsg(
                        2, DL,
                        "roachThread[" + rid + "]: unrecognised task!!!")

                # now that task is done, re-acquire lock
                Dada.logMsg(2, DL, "roachThread[" + rid + "]: lock.acquire()")
                locked = lock.acquire()
                Dada.logMsg(2, DL,
                            "roachThread[" + rid + "]: setting state = IDLE")
                states[ithread] = IDLE
                results[ithread] = result
                Dada.logMsg(2, DL,
                            "roachThread[" + rid + "]: cond.notifyAll()")
                cond.notifyAll()

        except:

            print '-' * 60
            traceback.print_exc(file=sys.stdout)
            print '-' * 60

            quit_event.set()
            if (not locked):
                Dada.logMsg(0, DL,
                            "roachThread[" + rid + "]: except: lock.acquire()")
                locked = lock.acquire()
            Dada.logMsg(
                0, DL,
                "roachThread[" + rid + "]: except: setting state = ERROR")
            states[ithread] = ERROR
            results[ithread] = "fail"
            Dada.logMsg(2, DL,
                        "roachThread[" + rid + "]: except: cond.notifyAll()")
            cond.notifyAll()
            Dada.logMsg(2, DL,
                        "roachThread[" + rid + "]: except: lock.release()")
            lock.release()
            Dada.logMsg(2, DL, "roachThread[" + rid + "]: except: exiting")
            return

        # we have been asked to exit the rthread
        Dada.logMsg(1, DL, "roachThread[" + rid + "]: end of thread")
    def run(self):
        rid = str(self.roach_num)
        ithread = self.roach_num
        cfg = self.cfg
        lock = self.lock
        cond = self.cond
        states = self.states
        results = self.results
        responses = self.responses
        fpga = []
        locked = False
        acc_len = 25
        roach_cfg = Hispec.getROACHConfig()
        roach_name = roach_cfg["ROACH_" + rid]
        beam_name = roach_cfg["BEAM_" + rid]

        # os.chdir(cfg["SERVER_STATS_DIR"])

        try:

            Dada.logMsg(1, DL, "[" + rid + "] roachThread: starting")

            # acquire lock to wait for commands setup
            locked = lock.acquire()
            Dada.logMsg(3, DL, "[" + rid + "] roachThread: lock acquired")

            while (states[ithread] != QUIT):
                while (states[ithread] == IDLE):
                    Dada.logMsg(
                        3, DL,
                        "[" + rid + "] roachThread: waiting for not IDLE")
                    locked = False
                    cond.wait()
                    locked = True
                if (states[ithread] == QUIT):
                    Dada.logMsg(1, DL,
                                "[" + rid + "] roachThread: quit requested")
                    Hispec.stopTX(DL, fpga)
                    lock.release()
                    locked = False
                    return

                # we have been given a command to perform (i.e. not QUIT or IDLE)
                task = states[ithread]
                Dada.logMsg(3, DL,
                            "[" + rid + "] roachThread: TASK=" + str(task))

                Dada.logMsg(3, DL, "[" + rid + "] roachThread: lock.release()")
                lock.release()
                locked = False

                result = "fail"
                response = ""

                if (task == TASK_STATE):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: state request")
                    if (fpga != []):
                        result = "ok"

                elif (task == TASK_CONFIG):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform config")
                    result, fpga = Hispec.configureRoach(DL, acc_len, rid, cfg)
                    if (result != "ok"):
                        Dada.logMsg(
                            -2, DL,
                            "[" + rid + "] roachThread: roach not ready")
                        fpga = []

                elif (task == TASK_ARM):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform arm")
                    result = Hispec.rearm(DL, fpga)

                elif (task == TASK_START_TX):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform start tx")
                    result = Hispec.startTX(DL, fpga)

                elif (task == TASK_STOP_TX):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform stop tx")
                    result = Hispec.stopTX(DL, fpga)

                elif (task == TASK_LEVELS):
                    Dada.logMsg(
                        3, DL, "[" + rid + "] roachThread: perform set levels")
                    if (fpga != []):
                        result, response = Hispec.setLevels(DL, fpga, rid)
                        Dada.logMsg(
                            2, DL, "[" + rid + "] roachThread: result=" +
                            result + " response=" + response)

                elif (task == TASK_BRAMPLOT):
                    if (fpga != []):
                        Dada.logMsg(
                            3, DL,
                            "[" + rid + "] roachThread: perform bramplot")
                        time_str = Dada.getCurrentDadaTime()
                        result = Hispec.bramplotRoach(DL, fpga, time_str,
                                                      beam_name)
                    else:
                        Dada.logMsg(
                            -1, DL,
                            "[" + rid + "] roachThread: not connected to FPGA")

                elif (task == TASK_BRAMDISK):
                    if (fpga != []):
                        Dada.logMsg(
                            3, DL,
                            "[" + rid + "] roachThread: perform bramdisk")
                        time_str = Dada.getCurrentDadaTime()
                        result = Hispec.bramdiskRoach(DL, fpga, time_str,
                                                      beam_name)
                    else:
                        Dada.logMsg(
                            -1, DL,
                            "[" + rid + "] roachThread: not connected to FPGA")

                else:
                    Dada.logMsg(
                        2, DL,
                        "[" + rid + "] roachThread: unrecognised task!!!")

                # now that task is done, re-acquire lock
                Dada.logMsg(3, DL, "[" + rid + "] roachThread: lock.acquire()")
                locked = lock.acquire()

                Dada.logMsg(3, DL,
                            "[" + rid + "] roachThread: setting state = IDLE")
                states[ithread] = IDLE
                results[ithread] = result
                responses[ithread] = response
                Dada.logMsg(3, DL,
                            "[" + rid + "] roachThread: cond.notifyAll()")
                cond.notifyAll()

        except:

            print '-' * 60
            traceback.print_exc(file=sys.stdout)
            print '-' * 60

            quit_event.set()
            if (not locked):
                Dada.logMsg(
                    0, DL, "[" + rid + "] roachThread: except: lock.acquire()")
                locked = lock.acquire()
            Dada.logMsg(
                0, DL,
                "[" + rid + "] roachThread: except: setting state = ERROR")
            states[ithread] = ERROR
            results[ithread] = "fail"
            responses[
                ithread] = "exception ocurred in roachThread " + roach_name + ":" + beam_name
            Hispec.stopTx(DL, fpga)
            Dada.logMsg(2, DL,
                        "[" + rid + "] roachThread: except: cond.notifyAll()")
            cond.notifyAll()
            Dada.logMsg(2, DL,
                        "[" + rid + "] roachThread: except: lock.release()")
            lock.release()
            Dada.logMsg(2, DL, "[" + rid + "] roachThread: except: exiting")
            return

        # we have been asked to exit the rthread
        Dada.logMsg(1, DL, "[" + rid + "] roachThread: end of thread")
        Hispec.stopTx(DL, fpga)
    def run(self):
        rid = str(self.roach_num)
        ithread = self.roach_num
        cfg = self.cfg
        lock = self.lock
        cond = self.cond
        states = self.states
        args = self.args
        results = self.results
        responses = self.responses
        fpga = []
        locked = False
        acc_len = 25
        roach_cfg = Bpsr.getROACHConfig()
        roach_name = roach_cfg["ROACH_" + rid]
        beam_name = roach_cfg["BEAM_" + rid]
        os.chdir(cfg["SERVER_STATS_DIR"])

        try:

            Dada.logMsg(1, DL, "[" + rid + "] roachThread: starting")

            Dada.logMsg(2, DL,
                        "[" + rid + "] roachThread: connectRoach(" + rid + ")")
            result, fpga = Bpsr.connectRoach(DL, rid)
            if (result != "ok"):
                raise NameError('Could not connect to Roach')

            # acquire lock to wait for commands setup
            locked = lock.acquire()
            Dada.logMsg(3, DL, "[" + rid + "] roachThread: lock acquired")

            while (states[ithread] != QUIT):
                while (states[ithread] == IDLE):
                    Dada.logMsg(
                        3, DL,
                        "[" + rid + "] roachThread: waiting for not IDLE")
                    locked = False
                    cond.wait()
                    locked = True
                if (states[ithread] == QUIT):
                    Dada.logMsg(1, DL,
                                "[" + rid + "] roachThread: quit requested")
                    spec.stopTX(DL, fpga)
                    lock.release()
                    locked = False
                    return

                # we have been given a command to perform (i.e. not QUIT or IDLE)
                task = states[ithread]
                arg = args[ithread]
                Dada.logMsg(
                    3, DL, "[" + rid + "] roachThread: TASK=" + str(task) +
                    " ARG=" + str(arg))

                Dada.logMsg(3, DL, "[" + rid + "] roachThread: lock.release()")
                lock.release()
                locked = False

                result = "fail"
                response = ""

                if (task == TASK_STATE):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: state request")
                    if (fpga != []):
                        result = "ok"

                elif (task == TASK_CONFIG):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform config")

                    result, response = spec.programRoach(DL, fpga, rid)
                    if (result == "ok"):
                        result, response = spec.configureRoach(
                            DL, fpga, rid, cfg)
                        if (result == "ok"):
                            result, response = spec.accLenRoach(
                                DL, fpga, acc_len, rid)
                            if (result == "ok"):
                                Dada.logMsg(
                                    2, DL, "[" + rid +
                                    "] roachThread: config finished")
                            else:
                                Dada.logMsg(
                                    -2, DL, "[" + rid +
                                    "] roachThread: accLenRoach failed " +
                                    response)
                        else:
                            Dada.logMsg(
                                -2, DL, "[" + rid +
                                "] roachThread: configureRoach failed " +
                                response)
                    else:
                        Dada.logMsg(
                            -2, DL, "[" + rid +
                            "] roachThread: programRoach failed " + response)

                elif (task == TASK_ACCLEN):
                    acc_len = int(arg)
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform accLen")
                    result, response = spec.accLenRoach(DL, fpga, acc_len, rid)

                elif (task == TASK_ARM):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform arm")
                    result = spec.rearm(DL, fpga)

                elif (task == TASK_START_TX):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform start tx")
                    result = spec.startTX(DL, fpga)

                elif (task == TASK_STOP_TX):
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform stop tx")
                    result = spec.stopTX(DL, fpga)

                elif (task == TASK_LEVELS):
                    Dada.logMsg(
                        3, DL, "[" + rid + "] roachThread: perform set levels")
                    if (fpga != []):
                        # test if all our beam/pols are active first
                        pol1, pol2 = Bpsr.getActivePolConfig(beam_name)

                        Dada.logMsg(
                            2, DL, "[" + rid + "] roachThread: setLevels(" +
                            str(pol1) + ", " + str(pol2) + ")")
                        result, response = spec.setLevels(
                            DL, fpga, pol1, pol2, rid)
                        Dada.logMsg(
                            2, DL, "[" + rid + "] roachThread: result=" +
                            result + " response=" + response)

                elif (task == TASK_SETGAINS):
                    new_cross_gain = int(arg)
                    new_bit_window = 3
                    Dada.logMsg(3, DL,
                                "[" + rid + "] roachThread: perform setGains")
                    result, response = spec.setComplexGains(
                        DL, fpga, rid, new_cross_gain, new_bit_window)

                elif (task == TASK_BRAMPLOT):
                    if (fpga != []):
                        Dada.logMsg(
                            3, DL,
                            "[" + rid + "] roachThread: perform bramplot")
                        time_str = Dada.getCurrentDadaTime()
                        result = spec.bramplotRoach(DL, fpga, time_str,
                                                    beam_name)
                    else:
                        Dada.logMsg(
                            -1, DL,
                            "[" + rid + "] roachThread: not connected to FPGA")

                elif (task == TASK_BRAMDISK):
                    if (fpga != []):
                        Dada.logMsg(
                            3, DL,
                            "[" + rid + "] roachThread: perform bramdisk")
                        time_str = Dada.getCurrentDadaTime()
                        result = spec.bramdiskRoach(DL, fpga, time_str,
                                                    beam_name)
                    else:
                        Dada.logMsg(
                            -1, DL,
                            "[" + rid + "] roachThread: not connected to FPGA")

                else:
                    Dada.logMsg(
                        2, DL,
                        "[" + rid + "] roachThread: unrecognised task!!!")

                # now that task is done, re-acquire lock
                Dada.logMsg(3, DL, "[" + rid + "] roachThread: lock.acquire()")
                locked = lock.acquire()

                Dada.logMsg(3, DL,
                            "[" + rid + "] roachThread: setting state = IDLE")
                states[ithread] = IDLE
                results[ithread] = result
                responses[ithread] = response
                Dada.logMsg(3, DL,
                            "[" + rid + "] roachThread: cond.notifyAll()")
                cond.notifyAll()

        except:

            print '-' * 60
            traceback.print_exc(file=sys.stdout)
            print '-' * 60

            quit_event.set()
            if (not locked):
                Dada.logMsg(
                    0, DL, "[" + rid + "] roachThread: except: lock.acquire()")
                locked = lock.acquire()
            Dada.logMsg(
                0, DL,
                "[" + rid + "] roachThread: except: setting state = ERROR")
            states[ithread] = ERROR
            results[ithread] = "fail"
            responses[
                ithread] = "exception ocurred in roachThread " + roach_name + ":" + beam_name
            spec.stopTX(DL, fpga)
            Dada.logMsg(2, DL,
                        "[" + rid + "] roachThread: except: cond.notifyAll()")
            cond.notifyAll()
            Dada.logMsg(2, DL,
                        "[" + rid + "] roachThread: except: lock.release()")
            lock.release()
            Dada.logMsg(2, DL, "[" + rid + "] roachThread: except: exiting")
            return

        # we have been asked to exit the rthread
        Dada.logMsg(1, DL, "[" + rid + "] roachThread: end of thread")
        spec.stopTX(DL, fpga)