Package ivy :: Module ivy :: Class IvyServer
[hide private]
[frames] | no frames]

Class IvyServer

source code

SocketServer.ThreadingMixIn --+    
                              |    
SocketServer.BaseServer --+   |    
                          |   |    
     SocketServer.TCPServer --+    
                              |    
SocketServer.ThreadingTCPServer --+
                                  |
                                 IvyServer

An Ivy server is responsible for receiving and handling the messages that other clients send on an Ivy bus to a given agent.

An IvyServer has two important attributes: usesDaemons and server_termination.

MT-safety

All public methods (not starting with an underscore _) are MT-safe

Instance Methods [hide private]
 
__init__(self, agent_name, ready_msg='', app_callback=<function void_function at 0x293fd70>, die_callback=<function void_function at 0x293fd70>, usesDaemons=False)
Builds a new IvyServer.
source code
 
serve_forever(self)
Handle requests (calling handle_request()) until doomsday...
source code
 
isAlive(self) source code
 
handle_new_client(self, client)
finalisation de la connection avec le client TODO: peut-etre ajouter un flag (en cours de cnx) sur le client, qui empecherait l'envoi de msg.
source code
 
handle_die_message(self, msg_id, from_client=None) source code
 
handle_direct_msg(self, client, num_id, msg) source code
 
handle_regexp_change(self, client, event, id, regexp) source code
 
remove_client(self, ip, port, trigger_application_callback=True)
Removes a registered client
source code
 
bind_direct_msg(self, on_direct_msg_fct) source code
 
bind_regexp_change(self, on_regexp_change_callback) source code

Inherited from SocketServer.ThreadingMixIn: process_request, process_request_thread

Inherited from SocketServer.TCPServer: close_request, fileno, get_request, server_activate, server_bind, server_close

Inherited from SocketServer.BaseServer: finish_request, handle_error, handle_request, handle_timeout, shutdown, verify_request

Inherited from SocketServer.BaseServer (private): _handle_request_noblock

    Communication on the ivybus
 
start(self, ivybus=None)
Binds the server to the ivybus.
source code
 
stop(self)
Disconnects the server from the ivybus.
source code
 
send_msg(self, message)
Examine the message and choose to send a message to the clients that subscribed to such a msg
source code
 
send_direct_message(self, agent_name, num_id, msg) source code
 
send_ready_message(self, client) source code
 
handle_msg(self, client, idx, *params)
Simply call the function bound to the subscription id idx with the supplied parameters.
source code
    Inspecting the ivybus
 
get_clients(self)
Returns the list of the agent names of all connected clients
source code
 
_get_client(self, ip, port, socket=None, agent_id=None, agent_name=None)
Returns the corresponding client, and create a new one if needed.
source code
 
get_client_with_name(self, name)
Returns the list of the clients registered with a given agent-name
source code
    Our own subscriptions
 
_add_subscription(self, regexp, fct)
Registers a new regexp and binds it to the supplied fct.
source code
 
_remove_subscription(self, idx)
Unregisters the corresponding regexp
source code
 
_get_fct_for_subscription(self, idx)
this method is not MT-safe, callers must acquire the global lock
source code
 
get_subscriptions(self) source code
 
bind_msg(self, on_msg_fct, regexp)
Registers a new subscriptions, by binding a regexp to a function, so that this function is called whenever a message matching the regexp is received.
source code
 
unbind_msg(self, id)
Unbinds a subscription
source code
Class Variables [hide private]

Inherited from SocketServer.ThreadingMixIn: daemon_threads

Inherited from SocketServer.TCPServer: address_family, allow_reuse_address, request_queue_size, socket_type

Inherited from SocketServer.BaseServer: timeout

Instance Variables [hide private]
  port
tells on which port the TCP server awaits connection
  server_termination
a threading.Event object that is set on server shutdown.
  usesDaemons
whether the threads are daemonic or not.
Method Details [hide private]

__init__(self, agent_name, ready_msg='', app_callback=<function void_function at 0x293fd70>, die_callback=<function void_function at 0x293fd70>, usesDaemons=False)
(Constructor)

source code 

Builds a new IvyServer. A client only needs to call start() on the newly created instances to connect to the corresponding Ivy bus and to start communicating with other applications.

MT-safety: both functions app_callback and die_callback must be prepared to be called concurrently

Parameters:
  • agent_name - : the client's agent name
  • ready_msg - : a message to send to clients when they connect
  • app_callback - : a function called each time a client connects or disconnects. This function is called with a single parameter indicating which event occured: IvyApplicationConnected or IvyApplicationDisconnected.
  • die_callback - : called when the IvyServer receives a DIE message
  • usesDaemons - : see above.
Overrides: SocketServer.BaseServer.__init__

See Also: bind_msg(), start()

serve_forever(self)

source code 

Handle requests (calling handle_request()) until doomsday... or until stop() is called.

This method is registered as the target method for the thread. It is also responsible for launching the UDP server in a separate thread, see UDP_init_and_listen for details.

You should not need to call this method, use start instead.

Overrides: SocketServer.BaseServer.serve_forever

start(self, ivybus=None)

source code 
Binds the server to the ivybus. The server remains connected until stop is called, or until it receives and accepts a 'die' message.
Raises:

stop(self)

source code 
Disconnects the server from the ivybus. It also sets the server_termination event.
Raises:

get_clients(self)

source code 
Returns the list of the agent names of all connected clients

See Also: get_client_with_name

_get_client(self, ip, port, socket=None, agent_id=None, agent_name=None)

source code 

Returns the corresponding client, and create a new one if needed.

If agent_id is not None, the method checks whether a client with the same id is already registered; if it exists, the method exits by returning None.

You should not need to call this, use get_client_with_name instead

get_client_with_name(self, name)

source code 
Returns the list of the clients registered with a given agent-name

See Also: get_clients

handle_new_client(self, client)

source code 
finalisation de la connection avec le client TODO: peut-etre ajouter un flag (en cours de cnx) sur le client, qui empecherait l'envoi de msg. etc. tant que la cnx. n'est pas confirmee

remove_client(self, ip, port, trigger_application_callback=True)

source code 

Removes a registered client

This method is responsible for calling server.app_callback

Note:

NO NETWORK CLEANUP IS DONE

Returns:
the removed client, or None if no such client was found

send_msg(self, message)

source code 
Examine the message and choose to send a message to the clients that subscribed to such a msg
Returns:
the number of clients to which the message was sent

_add_subscription(self, regexp, fct)

source code 
Registers a new regexp and binds it to the supplied fct. The id assigned to the subscription and returned by method is unique to that subscription for the life-time of the server object: even in the case when a subscription is unregistered, its id will _never_ be assigned to another subscription.
Returns:
the unique id for that subscription

_remove_subscription(self, idx)

source code 

Unregisters the corresponding regexp

Warning!

this method is not MT-safe, callers must acquire the global lock

Returns:
the regexp that has been removed
Raises:
  • KeyError - if no such subscription can be found

bind_msg(self, on_msg_fct, regexp)

source code 
Registers a new subscriptions, by binding a regexp to a function, so that this function is called whenever a message matching the regexp is received.
Parameters:
  • on_msg_fct - : a function accepting as many parameters as there is groups in the regexp. For example:

    • the regexp '^hello .*' corresponds to a function called w/ no parameter,
    • '^hello (.*)': one parameter,
    • '^hello=([^ ]*) from=([^ ]*)': two parameters
  • regexp - : (string) a regular expression
Returns:
the binding's id, which can be used to unregister the binding with unbind_msg()

unbind_msg(self, id)

source code 
Unbinds a subscription
Parameters:
  • id - the binding's id, as returned by bind_msg()
Returns:
the regexp corresponding to the unsubscribed binding
Raises:
  • KeyError - if no such subscription can be found

Instance Variable Details [hide private]

server_termination

a threading.Event object that is set on server shutdown. It can be used either to test whether the server has been stopped (server_termination.isSet()) or to wait until it is stopped (server_termination.wait()). Application code should not try to set the Event directly, rather it will call stop() to terminate the server.

usesDaemons

whether the threads are daemonic or not. Daemonic threads do not prevent python from exiting when the main thread stop, while non-daemonic ones do. Default is False. This attribute should be set through at __init__() time and should not be modified afterwards.