def onMessage(self, connMgr, message): try: # Display the XML representation of the received message libgmsec_python.logInfo("[ExampleCallback:onMessage] Received:\n" + message.toXML()) # Construct Reply subject. component = "" try: component = message.getStringField("COMPONENT").getValue() except Exception as e: libgmsec_python.logWarning( "COMPONENT field is not available: " + e.what()) # Set Status Code to indicate Successful Completion. # The GMSEC Interface Specification Document defines 6 # unique STATUS-CODE values: # 1 - Acknowledgement # 2 - Working/keep alive # 3 - Successful completion # 4 - Failed completion # 5 - Invalid request # 6 - Final message # If an asynchronous requestor is awaiting a reply, the # ReplyCallback in use will remain active for multiple # messages, so long as the messages it receives contain # a STATUS-CODE of either 1 or 2. status_code = "3" # Create the reply subject. # See API Interface Specificaiton Document for # more information on Reply Message subjects. # Generally speaking, they are composed # accordingly: # [Spec].[Mission].[Satellite ID]. # [Message Type].[Component Name].[Status Code] reply_subject = "GMSEC.MISSION.SAT_ID.RESP.REPLY_ASYNC." + status_code reply = libgmsec_python.Message(reply_subject, libgmsec_python.Message.REPLY) # Add fields to the reply message reply.addField("COMPONENT", component) reply.addField("ANSWER", "Sure looks like it!") # Display XML representation of reply message libgmsec_python.logInfo("Prepared Reply:\n" + reply.toXML()) # Send Reply connMgr.reply(message, reply) self.replySent = True except libgmsec_python.Exception as e: libgmsec_python.logError("[ExampleCallback::onMessage] " + e.what())
def publishTestMessage(connMgr, subject): i = 123 # Create a Message object message = libgmsec_python.Message(subject, libgmsec_python.Message.PUBLISH) # Add fields to the Message message.addField("F", False) message.addField(libgmsec_python.I32Field("I", i)) message.addField(libgmsec_python.U16Field("K", i)) message.addField("S", "This is a test") message.addField(libgmsec_python.F32Field("D", 1 + 1. / i)) message.addField(libgmsec_python.BinaryField("X", "JLMNOPQ", 7)) # Publish Message connMgr.publish(message) # Output the Message's XML string representation by invoking Log macro libgmsec_python.logInfo("Sent:\n" + message.toXML())
def buildMessage(config, numFields): msg = libgmsec_python.Message("GMSEC.SYSTEST.TEST.REQ.DIR.LONG", libgmsec_python.Message.PUBLISH, config) if (numFields > 0): # Add Fields for i in range(0, numFields): fieldName = "FIELD-" if i < 10: fieldName += "00" elif i < 100: fieldName += "0" else: fieldName += "" msg.addField(fieldName, i) return msg
def main(): if (len(sys.argv) <= 1): usageMessage = "usage: " + sys.argv[0] + " mw-id=<middleware ID>" print usageMessage return -1 # Load the command-line input into a GMSEC Config object # A Config object is basically a key-value pair map which is used to # pass configuration options into objects such as Connections, # ConnectionManagers, Subscribe and Publish function calls, Messages, # etc. config = libgmsec_python.Config() for arg in sys.argv[1:]: value = arg.split('=') config.addValue(value[0], value[1]) # Since this example program uses an invalid message, we ensure the # validation check is disabled. config.addValue("gmsec-msg-content-validate-all", "false") # If it was not specified in the command-line arguments, set LOGLEVEL # to 'INFO' and LOGFILE to 'stdout' to allow the program report output # on the terminal/command line initializeLogging(config) # Print the GMSEC API version number using the GMSEC Logging # interface # This is useful for determining which version of the API is # configured within the environment libgmsec_python.logInfo(libgmsec_python.ConnectionManager.getAPIVersion()) try: # Create a ConnectionManager object # This is the linchpin for all communications between the # GMSEC API and the middleware server connMgr = libgmsec_python.ConnectionManager(config) # Open the connection to the middleware libgmsec_python.logInfo( "Opening the connection to the middleware server") connMgr.initialize() # Output middleware client library version libgmsec_python.logInfo(connMgr.getLibraryVersion()) # Create a message # Disclaimer: This message is not based off of a Message # Definition outlined by the GMSEC Interface # Specification Document (ISD). It is meant to be an example # to demonstrate the various capabilities of the GMSEC Message # and Field classes. The GMSEC Team recommends that you map # your data into the Messages defined in the GMSEC ISD, as # doing so will make your software "GMSEC Compliant" resulting # in plug-and-play type functionality with other GMSEC # compliant software. message = libgmsec_python.Message(EXAMPLE_MESSAGE_SUBJECT, libgmsec_python.Message.PUBLISH) populateMessage(message) # Publish the message to the middleware bus connMgr.publish(message) # Display the XML string representation of the Message for # the sake of review libgmsec_python.logInfo("Published message: " + message.toXML()) # Disconnect from the middleware and clean up the Connection connMgr.cleanup() except libgmsec_python.Exception as e: libgmsec_python.logError(e.what()) return -1 return 0
def main(): if (len(sys.argv) <= 1): usageMessage = "usage: " + sys.argv[0] + " mw-id=<middleware ID>" print usageMessage return -1 # Load the command-line input into a GMSEC Config object # A Config object is basically a key-value pair map which is used to # pass configuration options into objects such as Connections, # ConnectionManagers, Subscribe and Publish function calls, Messages, # etc. config = libgmsec_python.Config() for arg in sys.argv[1:]: value = arg.split('=') config.addValue(value[0], value[1]) # Since this example program uses an invalid message, we ensure the # validation check is disabled. config.addValue("gmsec-msg-content-validate-all", "false") # If it was not specified in the command-line arguments, set LOGLEVEL # to 'INFO' and LOGFILE to 'stdout' to allow the program report output # on the terminal/command line initializeLogging(config) # Output GMSEC API version libgmsec_python.logInfo(libgmsec_python.ConnectionManager.getAPIVersion()) try: # Create the Connection connMgr = libgmsec_python.ConnectionManager(config) # Open the connection to the middleware libgmsec_python.logInfo( "Opening the connection to the middleware server") connMgr.initialize() # Output middleware client/wrapper version libgmsec_python.logInfo(connMgr.getLibraryVersion()) connMgr.subscribe(DEFAULT_REQUEST_SUBJECT) # Call receive() to synchronously retrieve a message that has # been received by the middleware client libraries # Timeout periods: # -1 - Wait forever # 0 - Return immediately # >0 - Time in milliseconds before timing out requestMsg = connMgr.receive(-1) # Example error handling for calling receive() with a timeout if (requestMsg): # Display the XML representation of the received message libgmsec_python.logInfo("Received a message\n" + requestMsg.toXML()) # Double-check the Message type to ensure that it is a request if (requestMsg.getKind() == libgmsec_python.Message.REQUEST): # Get the name of the component who issued the request component = 0 # Construct a Reply message try: compField = requestMsg.getStringField("COMPONENT") component = compField.getValue() except Exception as e: libgmsec_python.logError(e.what()) # Set Status Code to indicate Successful Completion. # The GMSEC Interface Specification Document defines 6 # unique STATUS-CODE values: # 1 - Acknowledgement # 2 - Working/keep alive # 3 - Successful completion # 4 - Failed completion # 5 - Invalid request # 6 - Final message # If an asynchronous requestor is awaiting a reply, the # ReplyCallback in use will remain active for multiple # messages, so long as the messages it receives contain # a STATUS-CODE of either 1 or 2. status_code = "3" # Set the reply subject. # See API Interface Specificaiton Document for # more information on Reply Message subjects. # Generally speaking, they are composed # accordingly: # [Spec].[Mission].[Satellite ID]. # [Message Type].[Component Name].[Status Code] reply_subject = "GMSEC.MISSION.SAT_ID.RESP.REPLY." + status_code # Create reply message replyMsg = libgmsec_python.Message( reply_subject, libgmsec_python.Message.REPLY) # Add fields to the reply message replyMsg.addField("ANSWER", "Yup, I'm here!") replyMsg.addField("COMPONENT", component) # Display XML representation of the reply message libgmsec_python.logInfo("Prepared Reply:\n" + replyMsg.toXML()) # Send Reply connMgr.reply(requestMsg, replyMsg) # Destroy request message to release its memory connMgr.release(requestMsg) connMgr.cleanup() except libgmsec_python.Exception as e: libgmsec_python.logError(e.what()) return -1 return 0
def main(): if(len(sys.argv) <= 1): usageMessage = "usage: " + sys.argv[0] + " mw-id=<middleware ID>" print usageMessage return -1 # Load the command-line input into a GMSEC Config object # A Config object is basically a key-value pair map which is used to # pass configuration options into objects such as Connections, # ConnectionManagers, Subscribe and Publish function calls, Messages, # etc. config = libgmsec_python.Config() for arg in sys.argv[1:]: value = arg.split('=') config.addValue(value[0], value[1]) # Since this example program uses an invalid message, we ensure the # validation check is disabled. config.addValue("gmsec-msg-content-validate-all", "false") # If it was not specified in the command-line arguments, set LOGLEVEL # to 'INFO' and LOGFILE to 'stdout' to allow the program report output # on the terminal/command line initializeLogging(config) # Print the GMSEC API version number using the GMSEC Logging # interface # This is useful for determining which version of the API is # configured within the environment libgmsec_python.logInfo(libgmsec_python.ConnectionManager.getAPIVersion()) try: # Create a ConnectionManager object # This is the linchpin for all communications between the # GMSEC API and the middleware server connMgr = libgmsec_python.ConnectionManager(config) # Open the connection to the middleware libgmsec_python.logInfo("Opening the connection to the middleware server") connMgr.initialize() # Output middleware client library version libgmsec_python.logInfo(connMgr.getLibraryVersion()) libgmsec_python.logInfo("Publishing two messages -- One will be received by the subscriber the other will be filtered out") # Create a message which will be received by the subscriber # in this set of example programs message = libgmsec_python.Message(EXAMPLE_MESSAGE_SUBJECT, libgmsec_python.Message.PUBLISH) populateMessage(message) # Publish the message to the middleware bus connMgr.publish(message) # Display the XML string representation of the Message for # the sake of review libgmsec_python.logInfo("Published message: " + message.toXML()) # Create a message which will NOT be received by the subscriber # in this set of example programs message = libgmsec_python.Message(FILTERED_MESSAGE_SUBJECT, libgmsec_python.Message.PUBLISH) populateMessage(message) # Publish the message to the middleware bus connMgr.publish(message) # Display the XML string representation of the Message for # the sake of review libgmsec_python.logInfo("Published message: " + message.toXML()) # Disconnect from the middleware and clean up the Connection connMgr.cleanup() except libgmsec_python.Exception as e: libgmsec_python.logError(e.what()) return -1 return 0
def main(): if (len(sys.argv) <= 1): usageMessage = "usage: " + sys.argv[0] + " mw-id=<middleware ID>" print usageMessage return -1 cb = AsyncStatusCheckCallback() # Load the command-line input into a GMSEC Config object # A Config object is basically a key-value pair map which is used to # pass configuration options into objects such as Connections, # ConnectionManagers, Subscribe and Publish function calls, Messages, # etc. config = libgmsec_python.Config() for arg in sys.argv[1:]: value = arg.split('=') config.addValue(value[0], value[1]) # Since this example program uses an invalid message, we ensure the # validation check is disabled. config.addValue("gmsec-msg-content-validate-all", "false") # If it was not specified in the command-line arguments, set LOGLEVEL # to 'INFO' and LOGFILE to 'stdout' to allow the program report output # on the terminal/command line initializeLogging(config) # Toggle the WebSphere MQ client libraries-level asynchronous publish # functionality on config.addValue("MW-ASYNC-PUBLISH", "true") # Toggle the periodic WebSphere MQ asynchronous publish status report # functionality on. The GMSEC API Connection will periodically fire # off an Event which will trigger the onEvent function of an # EventCallback registered to the event labeled, # Connection::WSMQ_ASYNC_STATUS_CHECK_EVENT config.addValue("MW-ASYNC-STATUS-CHECK", "true") # Additionally, the "MW-ASYNC-STATUS-CHECK-MESSAGE-INTERVAL" # configuration option can be used to instruct the GMSEC Connection # on how frequently (in number of publish operations) it should # periodically fire the WSMQ_ASYNC_STATUS_CHECK_EVENT. By default, it # will fire once every 100 messages. # For the sake of this example, we will use 500 messages. config.addValue("MW-ASYNC-STATUS-CHECK-MESSAGE-INTERVAL", "500") # Print the GMSEC API version number using the GMSEC Logging # interface libgmsec_python.logInfo(libgmsec_python.ConnectionManager.getAPIVersion()) try: # Create the Connection connMgr = libgmsec_python.ConnectionManager(config) # Connect connMgr.initialize() # Register the event callback with the connection to catch # the WebSphere asynchronous publish status events which are # eminated from the API connMgr.registerEventCallback( libgmsec_python.Connection.WSMQ_ASYNC_STATUS_CHECK_EVENT, cb) # Output middleware version libgmsec_python.logInfo("Middleware version = " + connMgr.getLibraryVersion()) libgmsec_python.logInfo("Publishing messages using the subject: " + EXAMPLE_MESSAGE_SUBJECT) # Create a GMSEC Message object message = libgmsec_python.Message(EXAMPLE_MESSAGE_SUBJECT, libgmsec_python.Message.PUBLISH) # Publish message as quickly as possible # (i.e. No sleep operation between each publish operation) count = 0 while (not cb.eventFired): # Populate the Message with fields, increment a # counter so that a publisher can track how many # messages were published (if they are interested) count += 1 populateMessage(message, count) # Publish the message to the middleware bus connMgr.publish(message) # Note: We do not recommend printing the output of a # message when publishing it while using the WebSphere # async publish functionality, as it is # counter-intuitive to take take up CPU resources # performing I/O operations when attempting to achieve # high message throughput rates. As such, if you want # to analyze the messages published by this program, # we recommend you execute GMSEC_API/bin/gmsub to # receive the messages. libgmsec_python.logInfo("Event detected, ending example demonstration") # Clean up the ConnectionManager before exiting the program connMgr.cleanup() except libgmsec_python.Exception as e: libgmsec_python.logError(e.what()) return -1 return 0
def main(): if(len(sys.argv) <= 1): usageMessage = "usage: " + sys.argv[0] + " mw-id=<middleware ID>" print usageMessage return -1 # Load the command-line input into a GMSEC Config object # A Config object is basically a key-value pair map which is used to # pass configuration options into objects such as Connections, # ConnectionManagers, Subscribe and Publish function calls, Messages, # etc. config = libgmsec_python.Config() for arg in sys.argv[1:]: value = arg.split('=') config.addValue(value[0], value[1]) # Since this example program uses an invalid message, we ensure the # validation check is disabled. config.addValue("gmsec-msg-content-validate-all", "false") # If it was not specified in the command-line arguments, set LOGLEVEL # to 'INFO' and LOGFILE to 'stdout' to allow the program report output # on the terminal/command line initializeLogging(config) # Output GMSEC API version libgmsec_python.logInfo(libgmsec_python.Connection.getAPIVersion()) try: # Create the Connection connMgr = libgmsec_python.ConnectionManager(config) # Connect connMgr.initialize() # output connection middleware version libgmsec_python.logInfo(connMgr.getLibraryVersion()) # Create request message requestMsg = libgmsec_python.Message(DEFAULT_REQUEST_SUBJECT, libgmsec_python.Message.REQUEST) # Add fields to request message requestMsg.addField("QUESTION", "Does the request/reply functionality still work?") requestMsg.addField("COMPONENT", "request_async") # Display XML representation of request message libgmsec_python.logInfo("Requesting:\n" + requestMsg.toXML()) cb = ExampleReplyCallback() connMgr.request(requestMsg, -1, cb) libgmsec_python.logInfo("Waiting for response...") # Loop while waiting for the asynchronous response until done while (cb.receivedReply == 0): libgmsec_python.TimeUtil.millisleep(100) if (cb.receivedReply): libgmsec_python.logInfo("Response Received!") else: libgmsec_python.logWarning("No response received") connMgr.cleanup() except libgmsec_python.Exception as e: libgmsec_python.logError(e.what()) return -1 return 0
def main(): if(len(sys.argv) <= 1): usageMessage = "usage: " + sys.argv[0] + " mw-id=<middleware ID>" print usageMessage return -1 # Load the command-line input into a GMSEC Config object # A Config object is basically a key-value pair map which is used to # pass configuration options into objects such as Connections, # ConnectionManagers, Subscribe and Publish function calls, Messages, # etc. config = libgmsec_python.Config() for arg in sys.argv[1:]: value = arg.split('=') config.addValue(value[0], value[1]) # Since this example program uses an invalid message, we ensure the # validation check is disabled. config.addValue("gmsec-msg-content-validate-all", "false") # Ensure that the open-response is enabled # Note: Other subscribing applications should set the configuration # option 'mw-expose-resp' to 'true' in order to receive exposed replies # By setting the configuration option 'GMSEC-REQ-RESP' to 'open-resp' # here, it automatically enables the 'mw-expose-resp' option. config.addValue("GMSEC-REQ-RESP", "OPEN-RESP") # If it was not specified in the command-line arguments, set LOGLEVEL # to 'INFO' and LOGFILE to 'stdout' to allow the program report output # on the terminal/command line initializeLogging(config) # Print the GMSEC API version number using the GMSEC Logging # interface # This is useful for determining which version of the API is # configured within the environment libgmsec_python.logInfo(libgmsec_python.ConnectionManager.getAPIVersion()) try: # Create the ConnectionManager connMgr = libgmsec_python.ConnectionManager(config) # Open the connection to the middleware connMgr.initialize() # Output middleware client library version libgmsec_python.logInfo(connMgr.getLibraryVersion()) # Subscribe to the bus in preparation to receive the # open-response message (Because it will not be routed # to the reqeust() call) reply_subject = OPEN_RESP_REPLY_SUBJECT + ".*" connMgr.subscribe(reply_subject) # Output information libgmsec_python.logInfo("Issuing a request using the subject '" + OPEN_RESP_REQUEST_SUBJECT + "'") # Create message requestMsg = libgmsec_python.Message(OPEN_RESP_REQUEST_SUBJECT, libgmsec_python.Message.REQUEST) # Add fields to message requestMsg.addField("QUESTION", "Is there anyone out there?") requestMsg.addField("COMPONENT", "request") # Display XML representation of request message libgmsec_python.logInfo("Sending request message:\n" + requestMsg.toXML()) # Send Request Message # Timeout periods: # -1 - Wait forever # 0 - Return immediately # >0 - Time in milliseconds before timing out replyMsg = connMgr.request(requestMsg, 1000, libgmsec_python.GMSEC_REQUEST_REPUBLISH_NEVER) # Example error handling for calling request() with a timeout if (replyMsg): # Display the XML string representation of the reply libgmsec_python.logInfo("Received replyMsg:\n" + replyMsg.toXML()) # Destroy the replyMsg message connMgr.release(replyMsg) # Disconnect from the middleware and clean up the Connection connMgr.cleanup() except libgmsec_python.Exception as e: libgmsec_python.logError(e.what()) return -1 return 0
def main(): if (len(sys.argv) <= 1): usageMessage = "usage: " + sys.argv[0] + " mw-id=<middleware ID>" print usageMessage return -1 # Load the command-line input into a GMSEC Config object # A Config object is basically a key-value pair map which is used to # pass configuration options into objects such as Connections, # ConnectionManagers, Subscribe and Publish function calls, Messages, # etc. config = libgmsec_python.Config() for arg in sys.argv[1:]: value = arg.split('=') config.addValue(value[0], value[1]) # Enable Message Binning config.addValue("GMSEC-USE-MESSAGE-BINS", "true") # Specify the number of messages to be aggregated prior to publishing # the aggregate message to the middleware server (This applies to all # of the messages which match the subject(s) provided in the # GMSEC-MSG-BIN-SUBJECT-N configuration parameters # Note: The aggregate message will be sent to the middleware server # immediately upon this many messages being published, regardless of # the value supplied for GMSEC-MSG-BIN-TIMEOUT. config.addValue("GMSEC-MSG-BIN-SIZE", "5") # Specify a timeout (in milliseconds) for the aggregate message to be # sent to the middleware server # Note: The aggregate message will be sent to the middleware server # after this period of time if the message bin does not fill up (per # the value provided for GMSEC-MSG-BIN-SIZE) prior to this timeout config.addValue("GMSEC-MSG-BIN-TIMEOUT", "5000") # Specify the subjects to aggreate messages for. # Note: Subscription wildcard syntax can be used here, and has been # supported since GMSEC API version 4.3. config.addValue("GMSEC-MSG-BIN-SUBJECT-1", "GMSEC.*.PUBLISH") # Specify any subjects that should be excluded from being aggregated # This is useful if a wildcard subscription is provided in one of the # GMSEC-MSG-BIN-SUBJECT-N parameters. config.addValue("GMSEC-MSG-BIN-EXCLUDE-SUBJECT-1", EXAMPLE_BIN_EXCLUDE_SUBJECT) # Since this example program uses an invalid message, we ensure the # validation check is disabled. config.addValue("gmsec-msg-content-validate-all", "false") # If it was not specified in the command-line arguments, set LOGLEVEL # to 'INFO' and LOGFILE to 'stdout' to allow the program report output # on the terminal/command line initializeLogging(config) # Print the GMSEC API version number using the GMSEC Logging # interface # This is useful for determining which version of the API is # configured within the environment libgmsec_python.logInfo(libgmsec_python.ConnectionManager.getAPIVersion()) try: # Create a ConnectionManager object # This is the linchpin for all communications between the # GMSEC API and the middleware server connMgr = libgmsec_python.ConnectionManager(config) # Open the connection to the middleware libgmsec_python.logInfo( "Opening the connection to the middleware server") connMgr.initialize() # Output middleware client library version libgmsec_python.logInfo(connMgr.getLibraryVersion()) # Create a message message = libgmsec_python.Message(EXAMPLE_MESSAGE_SUBJECT, libgmsec_python.Message.PUBLISH) for i in range(0, 5): populateMessage(message, i + 1) # Publish the message to the middleware bus connMgr.publish(message) # Display the XML string representation of the Message for # the sake of review libgmsec_python.logInfo("Published message: " + message.toXML()) # Create another message message = libgmsec_python.Message(EXAMPLE_BIN_EXCLUDE_SUBJECT, libgmsec_python.Message.PUBLISH) populateMessage(message, 1) # Publish the message to the middleware bus # Note: When calling publish(), if a message does NOT match # one of the subjects to be aggregated, it will be immediately # published connMgr.publish(message) # Display the XML string representation of the Message for # the sake of review libgmsec_python.logInfo("Published message: " + message.toXML()) # Disconnect from the middleware and clean up the Connection connMgr.cleanup() except libgmsec_python.Exception as e: libgmsec_python.logError(e.what()) return -1 return 0