The QAMProxy sends method-calls to a QAMServer-instance and receives the results. It is possible to register a callback-method on the proxy. This method is called when a result for a specific method-call is available.
The QAMProxy consists of the following classes:
This class manages the start of:
- ConsumerThread
- PublisherThread
- CallbackObserverThread
The PublisherThread runs in an infinite-loop and sends continuously messages to the QAMServer. The messages which are send to the QAMServer are stored in a method_queue.
The ConsumerThread receives continuously messages from the QAMServer and signals that a message has arrived. If a callback is registered the ConsumerThread signals the CallbackObserverThread that a message has arrived, else it signals the QAMProxy that the message has arrived.
The CallbackObserverThread checks continuously if it is time to execute a callback-method. A callback-method is executed if a callback-method is registered and the ConsumerThread signals that a result has arrived.
The QAMProxy consists of the following Exceptions:
An example-usage of the QAMProxy. The code shows how to call a method that is registered on QAMServer.
>>> from qam.qam_proxy import QAMProxy, QAMMethodNotFoundException, QAMException
>>> qam_proxy = QAMProxy(hostname="localhost",
... port=5672,
... username='guest',
... password='guest',
... vhost='/',
... server_id='qamserver',
... client_id='qamproxy')
...
>>> result = qam_proxy.add(2,3) # name of method registered on QAMServer
>>> qam_proxy.close()
An example-usage of the QAMProxy with callback functions.
>>> from qam.qam_proxy import QAMProxy, QAMMethodNotFoundException, QAMException
>>> qam_proxy = QAMProxy(hostname="localhost",
... port=5672,
... username='guest',
... password='guest',
... vhost='/',
... server_id='qamserver',
... client_id='qamproxy')
...
... # defining functions for callback
>>> def success(arg):
... print arg
>>> def error(arg):
... # if an error occours on QAMServer
... print arg
>>> uid = qam_proxy.callback(success, error).adder_function(2,4)
>>> while True:
... state = qam_proxy.get_callback_state(uid)
... if state == 2 :
... # execution of callback finished
... break
...
>>> qam_proxy.close()
An example-usage of the QAMProxy for calling a registered class-instance on QAMServer.
>>> from qam.qam_proxy import QAMProxy, QAMMethodNotFoundException, QAMException
>>> qam_proxy = QAMProxy(hostname="localhost",
... port=5672,
... username='guest',
... password='guest',
... vhost='/',
... server_id='qamserver',
... client_id='qamproxy')
...
>>> result = qam_proxy.testclass.adder_function(2,4)
>>> qam_proxy.close()
The class Callback is callable. It is used to realize the callback-mechanism.
Parameters: |
|
---|
This method is for setting a timeout for a asynchronous remote method call. If the timeout happens before the callback function is called a QAMTimeoutException will be passed to the registered error function.
Parameters: | timeout (integer) – the timeout in seconds |
---|
The CallbackObserverThread checks continuosly if for an registered callback-function a result arrived. If the result arrived the specific callback-function is being executed by the ExecuteCallbackThread.
Parameters: | parent – see parent |
---|
Class-variables of CallbackObserverThread:
This method changes the state of a specific callback-item. Possible values for the state are:
- 0 = waiting for processing
- 1 = processing
- 2 = finished
Parameters: |
|
---|
It is a critical section so it is necessary to lock this section.
This method checks if a timeout has occoured and calls the error method with a QAMTimeoutException
Parameters: |
|
---|
The exit method terminates the CallbackObseverThread.
This function returns an item from the callback_uid_dict, identified by the given uid.
Parameters: | uid (string) – uid of the desired item |
---|---|
Return type: | dictionary, {“id”: string, “state”: integer} |
It is a critical section so it is necessary to lock this section.
This method initializes a new callback-item with an unique uid. The given id is being stored and the state is set to WAITING. The new item is stored in the callback_uid_dict.
Parameters: | id (integer) – id of the item in the result_list |
---|
This method removes an item from the callback_uid_dict. This is the case, when a callback is already executed.
Parameters: | uid (string) – uid of the desired item |
---|
It is a critical section so it is necessary to lock this section.
For the execution of the callback-function a new ExecuteCallbackThread is being started. This thread finally executes the method.
The ConsumerThread receives messages from the QAMServer. When a message arrives, the result is stored in the result_list, identified by the id. Further the specific entry in the result_list gets the state DONE.
Structure of the received message:
- “id”: integer
- “result”: result of method-call
- “exception”: bool
- “internal_exception”: integer
Parameters: |
|
---|
Class-variables of ConsumerThread:
The exit method terminates the ConsumerThread and closes the amqp-consumer and the amqp-connection of the ConsumerThread.
This function frees a specific result_list “slot”.
Parameters: | id (integer) – id of the slot to free |
---|
This method assigns a free slot to a new request. The new request is appended to result_list with the state WAITING.
Parameters: |
|
---|---|
Return type: | id of the assigned slot |
This function is a high level function for getting the result of the remote method call. It makes several checks to validate the result against Exceptions and raises them if any occoured
Parameters: | id (integer) – id for the desired result |
---|---|
Return type: | result for a specific id |
This function returns the result from the result_list, for a specific id.
Parameters: | id (integer) – id for the desired result_list “slot” |
---|---|
Return type: | result for a specific id |
This function checks, if a desired result from the QAMServer is already received.
Parameters: | id (integer) – id for checking the state in the result_list. |
---|---|
Return type: | True if the message is already received, else False |
Messages are received from QAMServer and stored in the result_list. T he state in the result_list for specific results is set DONE.
The ExecuteCallbackThread executes a registered callback-function with the results received from the QAMServer as parameter.
Parameters: |
|
---|
Class-variables of ExecuteCallbackThread:
The run method executes the given callback-function.
The PublisherThread takes messages from the method_queue and sends them to the QAMServer. The method_queue is filled by qam.qam_proxy.QAMProxy.__request and by qam.qam_proxy.Callback.__request.
Parameters: |
|
---|
Class-variables of PublisherThread:
A new message is appended to the method_queue. All messages that are stored in the method_queue are sent to the QAMServer.
Parameters: | message (dictionary) – the message which should be appended. |
---|
The exit method terminates the PublisherThread and closes the publisher and the amqp-connection
The run method sends messages which are stored in the method_queue to the QAMServer.
Internal QAMServer Exception.
Internal QAMServer Exception, which is generated if the method called by the QAMProxy doesn’t exist on the QAMServer.
The role of the QAMProxy is sending messages to a specific QAMServer. Instead of waiting on the message it is also possible to register a callback-method which is being called if a result from QAMServer has arrived. The QAMProxy manages the start of ConsumerThread, PublisherThread an CallbackObserverThread.
Parameters: |
|
---|
Class-variables of QAMProxy:
The callback-function registers a callback-method and an exception-method if an exception-method is given.
Parameters: |
|
---|---|
Return type: | callable qam.qam_proxy.Callback-instance |
The PublisherThread, the ConsumerThread and the CallbackObserverTrhead are being stopped (kill-signal set to True).
Returns the state of the callback method registered for a specific uid.
Return type: | state of the callback:
|
---|
This function returns the number of callacks which are waiting for a result from QAMServer.
Return type: | number of callbacks not executed yet |
---|
The set_timeout-function sets a timeout in seconds and returns a callable class where the remote function can be called. If a timeout occurs a QAMTimeoutException is raised.
Parameters: | timeout (integer) – timeout value in seconds |
---|---|
Return type: | callable qam.qam_proxy.SynchronTimeout-instance |
Internal QAMServer Exception, which is generated if the method called by the QAMProxy produces a timeout.
The class SynchronTimeout is callable. It is used to realize the synchronous Timeout-mechanism.
Parameters: |
|
---|