GET/PEEK not able to come out of the call

Help
Anonymous
2013-01-02
2013-06-12

  • Anonymous
    2013-01-02

    HI,

    We are using STAF queue service for communicating between client and server.
    OS ver. Windows 7, STAF ver. 3.4.10, Python 2.7.3
    The example communication script we are using is being attached down here.
    The problem we have is that, the script is being paused/suspended when we select the command window, and it is resuming after giving a keyboard interrupt(being pressed any key), probably I didn't explained well, but this is the same situation we have observed.

    ==============================================

    from PySTAF import *
    def registerHandle(handleName):
        # Create a non-static STAF handle
        stafHandle = STAFHandle(handleName + "non-static")
        # Create a static STAF handle
        result = stafHandle.submit("local",
                                   "HANDLE",
                                   "CREATE HANDLE NAME %s" % handleName);
        if (result.rc != STAFResult.Ok):
            print "Error on create handle request."
            print "Expected RC: 0, Result: <A Number>"
            print "Received RC: %d, Result: %s" % (result.rc, result.result)
            sys.exit(1)
        staticHandleNumber = int(result.result)
        print "Using STAF static handle %s" % (staticHandleNumber)
        # Now, create a STAFHandle for the static handle just created and use
        # it's queue as a "permanent" place to send messages to
        staticHandle = STAFHandle(staticHandleNumber, STAFHandle.Static)
        return stafHandle, staticHandle, staticHandleNumber
    def unregisterHandle(stafHandle, staticHandleNumber):
        stafHandle.submit("local",
                          "HANDLE",
                          "DELETE HANDLE %d" % staticHandleNumber);
        stafHandle.unregister()
    def getMsg(handle, fromMachine, fromHandle):
        result = handle.submit("%s" % fromMachine, "queue",
                               "GET NAME %s WAIT" % fromHandle)
        if result.rc != STAFResult.Ok:
            print "Could not get queued message from %s on %s" % (fromHandle, fromMachine)
            print "Received RC: %d, Result: %s" % (result.rc, result.result)
            sys.exit(1)
        else:
            print "Got queued message from %s on %s" % (fromHandle, fromMachine)
            msgMap = unmarshall(result.result).getRootObject()
        return msgMap['machine'], msgMap['message']
    def peekMsg(handle, fromMachine, fromHandle):
        result = handle.submit("%s" % fromMachine, "queue",
                               "PEEK NAME %s" % fromHandle)
        if result.rc != STAFResult.Ok:
            return -1
        else:
            return 0
    def sendMsg(handle, toMachine, toHandle, msgString):
        result = handle.submit("%s" % toMachine, "queue",
                               "QUEUE NAME %s MESSAGE %s" % (toHandle, wrapData(msgString)))
        if result.rc != STAFResult.Ok:
            print "ERROR: Queue message request failed with RC=%s Result=%s" % (result.rc, result.result)
            return -1
        else:
            if result.result == 0:
                print "Queue message request was queued to 0 handles."
                return -1
            else:
                print "Message was queued successfully to %s handle(s)" % (result.result)
                return 0
    

    ==============================================

    ==============================================

    import CommunicationModule
    from time import gmtime, strftime
    import time
    option = int(raw_input("1: Send\n2: Recieve\nOption: "))
    if option == 1:
        toMachine = raw_input("To IP address: ")
        while True:
            handle, staticHandle, staticHandleNumber = CommunicationModule.registerHandle('Server')
            toHandle = 'Client'
            dateTime = strftime("%d-%m-%Y-%H-%M-%S")
            print "Sending %s" % dateTime
            status = CommunicationModule.sendMsg(staticHandle, toMachine, toHandle, dateTime)
            CommunicationModule.unregisterHandle(handle, staticHandleNumber)
        pass
    elif option == 2:
        handle, staticHandle, staticHandleNumber = CommunicationModule.registerHandle('Client')
        fromHandle = 'Server'
        # continuosly poll for the messages
        while True:
            # peek on the handle's queue for messages one by one.
            status = CommunicationModule.peekMsg(staticHandle,
                                                 'local',
                                                 fromHandle)
            # got message, try to get
            if status == 0:
                # now getting the actual message
                machine, msgString = CommunicationModule.getMsg(staticHandle,
                                                                'local',
                                                                fromHandle)
                print "Recieved %s" % msgString
    else:
        print "invalid option, quiting."
    

    =======================================================

    This is what we doing exactly, we are running in different systems by making one server and one client for sending and receiving resp. sender requires IP addr. to be given when prompted to queue message to remote system(i.e., receiver). Please anybody there help with this issue.

     

  • Anonymous
    2013-01-02

    Forgot to mention that, the first piece of the code attached is CommunicationModule.py. And the second one the script we are using where we import CommunicationModule.

     
  • Sharon Lucas
    Sharon Lucas
    2013-01-02

    I don't really understand what the problem is.  Could you provide more information on what you think is not working correctly?

    Also, I really don't understand why you coded this the way you did.  Constantly submitting a PEEK request to the QUEUE service to see if there is a message on the QUEUE is very CPU intensive and wasteful.  Instead, don't submit a PEEK request to the QUEUE service; simply submit a GET WAIT request to the QUEUE service (within the loop).