qam_server

The QAM Server executes remote method calls received from a QAMProxy.

The QAMServer consists of the following classes:

qam.qam_server.QAMServer :
The QAMServer class provides the basic functions for registering methods and class-instances which could be executed and called by the server. Further the QAMServer receives messages (remote-method-calls) from an amqp-queue and passes them the ExecuteMethodThread which executes one method-call.
qam.qam_server.ExecuteMethodThread :
The ExecuteMethodThread is called by the QAMServer and executes one method-call. The QAMServer receives the method-calls and then the QAMServer starts an ExecuteMethodThread for executing the method.
qam.qam_server.PublisherThread :
The PublisherThread sends the result for one method-call back to QAMProxy. The resuls are stored in a queue. The PublisherThread takes continuously a result from the queue and sends it. If the queue is empty, the PublisherThread waits, until a new result is available for sending.

The QAMServer consists of the following Exceptions:

qam.qam_server.QAMMEthodNotFoundException :
This exception signals, that the method the QAMProxy wanted to call isn’t registered on the QAMServer. The exception is sent back to QAMProxy as result.

Usage

Register methods

An example-usage of the QAMServer. The code shows how to register a function.

>>> from qam.qam_server import QAMServer
>>> qam_server = QAMServer(hostname="localhost",
...                    port=5672,
...                    username='guest',
...                    password='guest',
...                    vhost='/',
...                    server_id='qamserver')
...
>>> def adder_function(x, y):    
...    return x + y
...
>>> qam_server.register_function(adder_function, 'add')
...
... # it is also possible to register the adder_function as follows:
... # qam_server.register_function(adder_function)
... # the method-name for registering in this case is adder_function.__name__
...
>>> qam_server.serve()

Register class-instances

An example-usage of the QAMServer. The code shows how to register class-instance.

>>> from qam.qam_server import QAMServer
>>> qam_server = QAMServer(hostname="localhost",
...                    port=5672,
...                    username='guest',
...                    password='guest',
...                    vhost='/',
...                    server_id='qamserver')
...
>>> class TestClass():
...    def __init__(self):
...        pass
...    def adder_function(self,a,b):
...        return a+b
...
>>> testclass = TestClass()
>>> qam_server.register_class(testclass,'testclass')
>>> qam_server.serve()

Classes

class qam.qam_server.ExecuteMethodThread(result_queue, message_data, method_dict, instance_dict, serializer)

The role of the ExecuteMethodThread is calling defined methods and creating messages out of the results, which could be send. Messages are stored in the a queue (result_queue) for further processing.

Parameters:
  • result_queue – see result_queue
  • message_data – see message_data
  • method_dict – see method_dict
  • instance_dict – see instance_dict

Class-variables of ExecuteMethodThread:

result_queue : list
result_queue from parent QAMServer. Results are stored in this list, after executing a specified method.
message_data : dictionary
sturcture of the message.
method_dict : dictionary
method_dict from parent QAMServer. All registered methods are stored in here.
instance_dict : dictionary
instance_dict from parent QAMServer. All registered instances are stored in here.
run()

The run-method takes messages, and processes them. Content of a message is a simple method call, or a method-call of a given instance. For calling the method, it is necessary, that the message is registered on the QAMServer. A method-call of a non-registered method or instance.method throws an exception. The result of the call is packed into a message and stored in a queue (result_queue).

exception qam.qam_server.MethodNotFoundException

Internal QAMServer Exception, which is thrown, if a non-registered method is being called.

class qam.qam_server.PublisherThread(amqp_credentials, result_queue, server_id, serializer)

The PublisherThread sends given messages from a queue back to the caller proxy.

Parameters:
  • amqp_credentials – see amqp_credentials
  • result_queue – see result_queue
  • server_id – see server_id

Class-variables of PublisherThread:

amqp_credentials : qam.amqp_credentials.AMQPCredentials
all necessary stuff for creating a amqp-connection ist stored in here.
result_queue : list
result_queue from parent QAMServer. Messages are taken from this queue and sent back to the caller proxy.
server_id : string
id for QAMServer.
amqpconn : carrot.connection.AMQPConnection
AMQP-Connection for the PublisherThread
kill : bool
The kill signal tells the PublisherThread to stop sending messages.
close()

The PublisherThread is being stopped (kill-signal is set to True)

run()

Messages from the given queue (result_queue) are sent back to a specified amqp-queue.

class qam.qam_server.QAMServer(hostname='localhost', port=5672, username='guest', password='guest', vhost='/', server_id='testqamserver', serializer='pickle', ssl=False)

The QAMServer class provides the basic functions for registering methods and class-instances which could be executed and called by the server. Further the QAMServer receives messages (remote-method-calls) from an amqp-queue and passes them to a thread, which executes the remote-method-calls.

Parameters:
  • hostname – hostname of the amqp-server
  • port – port of the amqp-server
  • username – username for amqp-server
  • password – password for amqp-server
  • vhost – virtual host on amqp-server
  • server_id – see server_id

Class-variables of QAMServer:

server_id : string
id for QAMServer.
method_dict : dictionary
stores method-registrations {method_name: method}
instance_dict : dictionary
stores class-instance-registrations {instance_name: instance}
result_queue : list
stores result-messages processed from ExecutionThread
kill : bool
tells the serve-function to terminate
finished : bool
tells the close-function wether the serve-function has finished or not
closed : bool
tells the destructor wether the amqp-connection is closed or not
close()

The consumer, the publisher and the amqp-connection are being closed. The serve-function terminates (kill-signal is set to True).

register_class(instance, name)

Class-instances which should be available for calling, are registered with this function. Each class-instance is being stored in a dictionary (instance_dict), where the key is the name given and the value is the instance itself. No methods for this instance are stored because they could be reached with the function getattr for any given class.

Parameters:
  • instance – class-instance which should be registered
  • name – with this name the instance is being registered
register_function(method, name=None)

Methods that should be available on the server for calling, are registered with this function. Each method is being stored in a dictionary (method_dict), where the name of the method is the key, and the value is the method itself.

Parameters:
  • method – method to register on server
  • name (String or None) – if a name is given, the method is being registered with this name, else the method is registered with method.__name__
serve()

The serve-function fetches messages (method-calls) from the amqp-queue. Each message is being executed by the ExecuteMethodThread, who calls the function and returns the result.

Table Of Contents

Previous topic

QAM - A python RPC Framework using AMQP

Next topic

qam_proxy

This Page