Пример #1
0
 def test_log(self):
     gLog.open("2", 10)
     gLog.print(
         """
                 Expected output:
                     Interleaved output from multiple threads
                     Each thread logs
                         (a) when it is starting
                         (b) multiple lines of output
                         (c) when it has ended
                     There should be NO BLANK LINES in the log and
                     each line should contain output from a single thread.
                     The line '....MIDPOINT...' appears half way through.
                     In the first half lines are about 90 characters long.
                     In the second half lines are twice as long.
         """
     )
     for digit in range(10):
         PrintManyLinesToLog(str(digit)).start()
     sleep(2)
     gLog.flush()
     gLog.print("...................... MIDPOINT.........................")
     for digit in range(10):
         PrintManyLinesToLog(str(digit) + str(digit) + str(digit)).start()
     sleep(5)
     gLog.close()
     raise Exception("EXCEPTION EXPECTED: Check correctness of output in file log_test.txt")
Пример #2
0
 def __init__(self, number, quSend, quRead):
     print("Initializing process {0}".format(number)); sys.stdout.flush()
     gLog.print("Initializing process {0}".format(number))
     self.number = number
     self.quSend = quSend
     self.quRead = quRead
     Process.__init__(self)
Пример #3
0
 def getNonblocking(self):
     try:
         msg = self.inQu.get(False) # non-blocking
         #gLog.print("MsgHandler {0}: getNonblocking message {1}".format(self.name, msg))
         return msg
     except multiprocessing.queues.Empty:
         gLog.print("MsgHandler {0}: getNonblocking empty qu exception)".format(self.name))
         raise multiprocessing.queues.Empty
Пример #4
0
 def getNonblocking(self):
     try:
         msg = self.msgHandler.getNonblocking()
         # gLog.print("Throttle {0}: getNonlocking msg {1}".format(self.name, msg))
         return msg
     except multiprocessing.queues.Empty:
         gLog.print("Throttle {0}: getNonlocking empty qu exception".format(self.name))
         raise multiprocessing.queues.Empty
Пример #5
0
 def startPumps(self):
     #gLog.print("MsgHandler {0}: trying to start pumps".format(self.name))
     internalQu = Queue()
     msgSock = MsgSocket(name = self.name)
     msgSock.connect(self.host, self.port)
     MsgInternalQuPump(name = self.name, internalQu = internalQu, inQuList = self.inQuList).start()
     MsgInQuPump(name = self.name, sock = msgSock, inQuList = self.inQuList).start()
     MsgOutQuPump(name = self.name, sock = msgSock, outQu = self.outQu, internalQu = internalQu).start()
     gLog.print("MsgHandler {0}: has started pumps".format(self.name))
Пример #6
0
 def __init__(self, name, socketToClient, clientHandlerFunction):
     Thread.__init__(self)
     assert(isinstance(name, str))
     assert(isinstance(socketToClient, socket))
     assert(clientHandlerFunction is not None)
     gLog.print("Client handler {0}: created for peer {1}".format(name, socketToClient.getpeername()))
     self.name = name
     self.socketToClient = socketToClient
     self.clientHandlerFunction = clientHandlerFunction
Пример #7
0
 def readLayout(self, fileName):
     gLog.print("Throttle {0}: sending DoReadLayoutMsg using file {1}".format(self.name, fileName))
     assert isinstance(fileName, str)
     self.addInterest(PutReadLayoutResponseMsg)
     self.put(DoReadLayoutMsg(fileName=fileName))
     msg = self.waitFor(PutReadLayoutResponseMsg(responseFlag=0, code=0))
     self.removeInterest(PutReadLayoutResponseMsg)
     sleep(3)
     return msg
Пример #8
0
 def run(self):
     gLog.print("MsgOutQuPump {0}: starting".format(self.name))
     while True:
         msg = self.outQu.get()
         if type(msg) in InQuListMsgs:
             #gLog.print("MsgOutQuPump {0}: getting ready to put {1}".format(self.name, msg))
             self.internalQu.put(msg)
         else:
             #gLog.print("MsgOutQuPump {0}: getting ready to send {1}".format(self.name, msg))
             self.sk.send(msg)
Пример #9
0
 def run(self):
     gLog.print("MsgInQuPump {0}: starting".format(self.name))
     while True:
         st = self.sk.receive()
         msg = makeMsgStr(st)
         for x in self.inQuList:
             q = x.inQu
             messageTypes = x.interests
             if type(msg) in messageTypes:
                 q.put(msg)
Пример #10
0
 def __init__(self, name = None, sock = None, outQu = None, internalQu = None):
     Thread.__init__(self)
     assert(isinstance(name, str))
     assert(isinstance(sock, MsgSocket))
     assert(isinstance(outQu, multiprocessing.queues.Queue))
     assert(isinstance(internalQu, multiprocessing.queues.Queue))
     gLog.print("MsgOutQuPump {0}: initializing".format(name))
     self.name = name
     self.sk = sock
     self.outQu = outQu
     self.internalQu = internalQu
Пример #11
0
 def __init__(self, name = "1", host = None, port = None, clientHandlerFunction = None):
     Thread.__init__(self)
     assert(host is None or isinstance(name, str))
     assert(isinstance(host, str))
     assert(isinstance(port, int))
     assert(clientHandlerFunction is not None)
     gLog.print("Message server {0}: initializing at ({1}, {2})".format(name, host, port))
     self.name = name
     if host is None:
         host = socket.gethostname()
     self.host = host
     self.port = port
     self.clientHandlerFunction = clientHandlerFunction
     self.serverSocket = None
Пример #12
0
 def run(self):           
     gLog.print("MsgInternalQuPump {0}: starting".format(self.name))
     while True:
         msg = self.internalQu.get()  
         if isinstance(msg, RemoveAllInterestsMsg):
             gLog.print("MsgInternalQuPump {0}: removing all interests for inQuNum {1}".
                        format(self.name, msg.inQuNum))
             # if there is no entry for this inQuNum raise an exception
             # else remove all interests for this inQuNum
             if len(self.inQuList) <= msg.inQuNum:
                 raise Exception("MsgInternalQuPump {0}: inQuNum {1} is too large.".format(self.name, msg.inQuNum))
             else:
                 interestList = self.inQuList[msg.inQuNum].interests
                 for interest in interestList:
                     interestList.remove(interest)
         elif isinstance(msg, AddInterestMsg):
             gLog.print("MsgInternalQuPump {0}: adding interest {1} for inQuNum {2}".
                        format(self.name, msg.interest, msg.inQuNum))
             # if there is no entry for this inQuNum raise an exception
             # elif the inQuNum already has this interest pass
             # else add an interest for this inQuNum
             if len(self.inQuList) <= msg.inQuNum:
                 raise Exception("MsgInternalQuPump {0}: inQuNum {1} is too large.".format(self.name, msg.inQuNum))
             else:
                 x = self.inQuList[msg.inQuNum]
                 if msg.interest in x.interests:
                     pass
                     # The interest could have been have added at a higher level
                     #raise Exception("MsgInternalQuPump {0}: Can't add. inQuNum {1} already has {2}".
                     #                format(self.name, msg.inQuNum, msg.interest))
                 else:
                     x.interests.append(msg.interest)
         elif isinstance(msg, RemoveInterestMsg):
             gLog.print("MsgInternalQuPump {0}: removing interest {1} for inQuNum {2}".
                        format(self.name, msg.interest, msg.inQuNum))
             # if there is no entry for this inQuNum raise an exception
             # elif there is no matching interest raise an exception
             # else remove an interest for this inQuNum
             if len(self.inQuList) <= msg.inQuNum:
                 raise Exception("MsgInternalQuPump {0}: inQuNum {1} is too large.".format(self.name, msg.inQuNum))
             else:
                 x = self.inQuList[msg.inQuNum]
                 if not msg.interest in x.interests:
                     raise Exception("MsgInternalQuPump {0}: Can't remove. inQuNum {1} doesn't have {2}".
                                     format(self.name, msg.inQuNum, msg.interest))
                 else:
                     x.interests.remove(msg.interest)
         else:
             gLog.print("MsgInternalQuPump {0}: unrecognized message {1}}".format(self.name, msg))
Пример #13
0
 def receive(self):
     if len(self.inBuffer) < 2:
         buf = self.sock.recv(1024)
         for x in buf:
             self.inBuffer.append(x)
     strSize = self.inBuffer[0] + 128 * self.inBuffer[1]
     while strSize + 2 > len(self.inBuffer):
         buf = self.sock.recv(1024)         # WARNING: if length of buf is 0, then the connection has been broken
         for x in buf:
             self.inBuffer.append(x)
     strMsg = self.inBuffer[2:2 + strSize]
     self.inBuffer = self.inBuffer[2 + strSize:]
     msg = splitMsgStr(strMsg)
     gLog.print("    >>> Received    {0}    from peer {1}".format(msg, self.sock.getpeername()))
     return msg
Пример #14
0
 def __init__(self, name = None, sock = None, inQuList = None):
     Thread.__init__(self)
     assert(isinstance(name, str))
     assert(isinstance(sock, MsgSocket))
     assert(isinstance(inQuList, list))
     for x in inQuList:
         assert(isinstance(x, InQuListEntry))
         assert(isinstance(x.inQu, multiprocessing.queues.Queue))
         assert(isinstance(x.interests, list))
         for y in x.interests:
             assert(y in ControllerInMsgs)
     gLog.print("MsgInQuPump {0}: initializing".format(name))
     self.name = name
     self.sk = sock
     self.inQuList = inQuList
Пример #15
0
 def initTrain(self, address, position):
     gLog.print("Throttle {0}: sending DoLocoInitMsg".format(self.name))
     assert 0 <= address <= 9999
     assert isinstance(position, list) or isinstance(position, tuple)
     self.put(DoLocoInitMsg(address=address, sensors=position))
     responseMsg = None
     if position:
         self.addInterest(PutInitOutcomeMsg)
         responseMsg = self.waitFor(PutInitOutcomeMsg(physAdd=address, physSlot=0, virtAdd=0, virtSlot=0))
         self.removeInterest(PutInitOutcomeMsg)
         if responseMsg.physSlot > 120:
             self.virtSlot = None
         else:
             self.virtSlot = responseMsg.virtSlot
     return responseMsg
Пример #16
0
 def getPath(self, pathKind, preSensor, fromSensor, toSensor, sensorsToExclude):
     gLog.print("Throttle {0}: sending GetPathMsg".format(self.name))
     self.addInterest(PutPathMsg)
     self.put(
         GetPathMsg(
             slot=self.virtSlot,
             pathKind=pathKind,
             preSensor=preSensor,
             fromSensor=fromSensor,
             toSensor=toSensor,
             sensorsToExclude=sensorsToExclude,
         )
     )
     msg = self.waitFor(PutPathMsg(sensors=[]))
     self.removeInterest(PutPathMsg)
     return msg.sensors
Пример #17
0
    def __init__(self, name = None, comPkg = None):
        gLog.print("MsgHandler {0}: initializing".format(name))
        assert(isinstance(name, str))

        assert(isinstance(comPkg, CommunicationsPackage))
        inQu = comPkg.inQu
        inQuNum = comPkg.inQuNum
        outQu = comPkg.outQu

        assert(isinstance(inQu, multiprocessing.queues.Queue))
        assert(isinstance(outQu, multiprocessing.queues.Queue))
        assert(0 <= inQuNum)
        self.name = name
        self.inQu = inQu
        self.inQuNum = inQuNum
        self.outQu = outQu
Пример #18
0
 def __init__(self, name = None, host = None, port = None, inQuList = None, outQu = None):
     gLog.print("MsgPumpHandler {0}: initializing".format(name))
     assert(isinstance(name, str))
     assert(isinstance(host, str))
     assert(port > 1000)
     assert(isinstance(inQuList, list))
     for x in inQuList:
         assert(isinstance(x, InQuListEntry))
         assert(isinstance(x.inQu, multiprocessing.queues.Queue))
         assert(isinstance(x.interests, list))
         #print(str(x))
         for y in x.interests:
             assert(y in ControllerInMsgs)
     assert(isinstance(outQu, multiprocessing.queues.Queue))
     self.name = name
     self.host = host
     self.port = port
     self.inQuList = inQuList
     self.outQu = outQu
Пример #19
0
    def __init__(self, number, quSend, quRead):
        print("Initializing gui {0}".format(number)); sys.stdout.flush()
        gLog.print("Initializing gui {0}".format(number))
        self.number = number
        self.quSend = quSend
        self.quRead = quRead

        EasyFrame.__init__(self, title = "GUI " + str(number))

        # Label and field for the input
        self.addLabel(text = "Input", row = 0, column = 0)
        self.inputField = self.addTextField(text = "", row = 0, column = 1)

        # Text area for the output
        self.outputArea = self.addTextArea(text = "", row = 1, column = 0, columnspan = 2, width = 50, height = 15)

        # The command button
        self.button = self.addButton(text = "Send", row = 2, column = 0, columnspan = 2, command = self.sendMessage)

        # Set up the queue reader for this window
        quReaderThread = QuReaderThread(self, self.quRead)
        quReaderThread.start()
Пример #20
0
 def connect(self, host, port):
     assert(isinstance(host, str))
     assert(port > 0)
     gLog.print("MsgSocket {0}: is trying to connect to ({1}, {2})".format(self.name, host, port))
     self.inBuffer = bytearray()
     self.sock = socket()
     for i in range(5):
         if not self.sock.connect_ex((host, port)):
             gLog.print("MsgSocket {0}: has connected to peer {1}".format(self.name, self.sock.getpeername()))
             return
         else:
             gLog.print("MsgSocket {0}: failed to connect to ({1}, {2})".format(self.name, host, port))
             sleep(1)
     st = "MsgSocket {0}: gave up trying to connect to ({1}, {2})".format(self.name, host, port)
     gLog.print(st)
     raise Exception(st)
Пример #21
0
 def run(self):
     gLog.print("Starting " + self.character)
     tempStr = ""
     for i in range(80):
         tempStr = tempStr + self.character
     for i in range(50):
         self.lineCount += 1
         gLog.print(tempStr + ": count = " + str(self.lineCount))
     gLog.print("Ending " + self.character)
Пример #22
0
    def __init__(self, name=None, comPkg=None):
        gLog.print("Throttle {0}: initializing".format(name))

        assert isinstance(name, str)

        assert isinstance(comPkg, CommunicationsPackage)
        inQu = comPkg.inQu
        inQuNum = comPkg.inQuNum
        outQu = comPkg.outQu

        assert isinstance(inQu, multiprocessing.queues.Queue)
        assert inQuNum >= 0
        assert isinstance(outQu, multiprocessing.queues.Queue)

        self.name = name
        self.virtSlot = None
        self.direction = kForward
        self.lights = kOff
        self.horn = kOff
        self.bell = kOff
        self.mute = kOff
        self.F5 = 0  # Flip this to close next turnout
        self.F6 = 0  # Flip this ot throw next turnout
        self.msgHandler = MsgHandler(name=self.name, comPkg=comPkg)
Пример #23
0
    def kill(self, name="all"):
        """
        I don't want to make this function static. It is easier to use as a member function."

        Usage (only uses first three letters of name)
        If name unrecognized, then defaults to "all"
          kill("simulator")
          kill("controller")
          kill("ut4")
          kill("RBLDisplay")
          kill("adminthrottle")
          kill("all")
        """

        # todo this doesn't work under Windows 8 at school. For instance, get the message:
        # todo ERROR: The process "StartController" not found.

        if name.lower()[:3] == "sim":
            name = "RailroadBig"
        elif name.lower()[:3] == "con":
            name = "StartController"
        elif name.lower()[:3] == "ut4":
            name = "Throttle"
        elif name.lower()[:3] == "rbl":
            name = "RBLDisplay"
        elif name.lower()[:3] == "adm":
            name = "adminthrottle"
        elif name.lower()[:3] == "all":
            name = "all"
        else:
            name = "all"
        if name != "all":
            if "XP" in platform.platform():
                killThis = "tskill " + name
                subprocess.call(killThis, shell=True)
            else:
                killThis = "taskkill /T /IM " + name
                subprocess.call(killThis, shell=True)

            gLog.print("sak: " + killThis)
        else:
            if "XP" in platform.platform():
                subprocess.call("tskill RailroadBig", shell=True)
                subprocess.call("tskill StartController", shell=True)
                subprocess.call("tskill Throttle", shell=True)
                subprocess.call("tskill RBLDisplay", shell=True)
                subprocess.call("tskill adminthrottle", shell=True)
                gLog.print("sak: tskill RailroadBig, StartController, Throttle, RBLDisplay, AdminThrottle")
            else:
                subprocess.call("taskkill /T /IM RailroadBig", shell=True)
                subprocess.call("taskkill /T /IM StartController", shell=True)
                subprocess.call("taskkill /T /IM Throttle", shell=True)
                subprocess.call("taskkill /T /IM RBLDisplay", shell=True)
                subprocess.call("taskkill /T /IM adminthrottle", shell=True)
                gLog.print("sak: taskkill /T /IM RailroadBig, StartController, Throttle, RBLDisplay, AdminThrottle ")
Пример #24
0
 def run(self):
     gLog.print("Message server {0}: running".format(self.name))
     self.serverSocket = socket()
     self.serverSocket.bind((self.host, self.port))           # bind
     self.serverSocket.listen(5)                              # listen
     gLog.print("Message server {0}: now listening at ({1}, {2})".format(self.name, self.host, self.port))
     while True:
         socketToClient, address = self.serverSocket.accept() # accept
         gLog.print("Message server {0}: just created a connection to peer at {1}".
                    format(self.name, socketToClient.getpeername()))
         ClientHandlerThread("1", socketToClient, self.clientHandlerFunction).start()
Пример #25
0
 def close(self):
     gLog.print("MsgSocket {0}: closing ".format(self.name))
     self.sock.close()
Пример #26
0
 def __init__(self, myGui, myQu):
     print("Initializing a queue reader"); sys.stdout.flush()
     gLog.print("Initializing a queue reader")
     Thread.__init__(self)
     self.myGui = myGui
     self.myQu = myQu
Пример #27
0
 def run(self):
     gLog.open(str(self.number))
     print("Running process {0}".format(self.number)); sys.stdout.flush()
     gLog.print("Running process {0}".format(self.number))
     TheGui(self.number, self.quSend, self.quRead).mainloop()
Пример #28
0
 def run(self):
     msgSocketToClient = MsgSocket()
     msgSocketToClient.attach(self.socketToClient)
     gLog.print("Client handler {0}: running with peer {1} and function {2}"
                .format(self.name, self.socketToClient.getpeername(), self.clientHandlerFunction))
     self.clientHandlerFunction(msgSocketToClient)
Пример #29
0
    def start(self, name, ip="", port="", trace="", logs="", layoutFile=""):
        """
        Usage (only uses first three letters of name)
          start("simulator")
          start("controller", ip = "127.0.0.1", port = "1234", trace = "yes")
          start("ut4", ip = "127.0.0.1", port = "1234")
          start("RBLDisplay", ip = "127.0.0.1", port = "1235")
          start("adminthrottle",  ip = "127.0.0.1", port = "1235", layoutFile = "layout.xml", logs = "no")
        """
        startThis = ""
        # start("simulator")
        if name.lower()[:3] == "sim":
            startThis = "start " + self.sakPath + "/RailroadBig.exe"
            subprocess.call(startThis, shell=True)
            sleep(1)

        # start("controller", ip = "127.0.0.1", port = "1234", trace = "yes")
        elif name.lower()[:3] == "con":
            if ip == "":
                ip = "127.0.0.1"  # default to local host
            if port == "":
                port = "1234"  # default to the simulator
            if trace == "" or trace.lower()[:1] == "y":
                trace = "yes"  # default to trace on
            else:
                trace = "no"
            startThis = "start " + self.sakPath + "/StartController.exe" + \
                        " IP " + ip + \
                        " PORT " + port + \
                        " TRACE " + trace
            subprocess.call(startThis, shell=True)
            sleep(3)

        # start("ut4", ip = "127.0.0.1", port = "1234")
        elif name.lower()[:3] == "ut4":
            if ip == "":
                ip = "127.0.0.1"  # default to local host
            if port == "":
                port = "1234"  # default to the simulator
            startThis = "start " + self.sakPath + "/Throttle.exe" + \
                        " IP " + ip + \
                        " PORT " + port
            subprocess.call(startThis, shell=True)

        # start("RBLDisplay", ip = "127.0.0.1", port = "1235")
        elif name.lower()[:3] == "rbl":
            if ip == "":
                ip = "127.0.0.1"  # default to local host
            if port == "":
                port = "1235"  # default to the controller
            startThis = "start " + self.sakPath + "/RBLDisplay.exe" + \
                        " IP " + ip + \
                        " PORT " + port
            subprocess.call(startThis, shell=True)

        # start("adminthrottle",  ip = "127.0.0.1", port = "1235", layoutFile = "layout.xml", logs = "no")
        elif name.lower()[:3] == "adm":
            if ip == "":
                ip = "127.0.0.1"  # default to local host
            if port == "" or port == "1235":
                port = "1235"  # default to controller
                mode = "controller"  # default to controller mode
            else:
                mode = "standalone"  # default to standalone mode
            if layoutFile == "":
                layoutFile = "layout.xml"  # default to layout.xml
            if logs == "" or logs.lower()[:1] == "n":
                logs = "no"  # default to logs off
            else:
                logs = "yes"

            startThis = "start " + self.sakPath + "/adminthrottle.exe" + \
                        " IP " + ip + \
                        " PORT " + port + \
                        " MODE " + mode + \
                        " KEYBOARDLOG " + logs + \
                        " ADMINLOG " + logs
            if mode == "controller":
                startThis = startThis + " LAYOUTFILE " + layoutFile

            subprocess.call(startThis, shell=True)

        # Log it
        gLog.print("sak: " + startThis)
Пример #30
0
 def setPath(self, st):
     self.sakPath = st
     gLog.print("sak: path is {0}  ".format(st))