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

Module ivy

source code

Using an IvyServer

The following code is a typical example of use:

from ivy.ivy import IvyServer

class MyAgent(IvyServer):
  def __init__(self, name):
    IvyServer.__init__(self,'MyAgent')
    self.name = name
    self.start('127.255.255.255:2010')
    self.bind_msg(self.handle_hello, 'hello .*')
    self.bind_msg(self.handle_button, 'BTN ([a-fA-F0-9]+)')

  def handle_hello(self, agent):
    print '[Agent %s] GOT hello from %r'%(self.name, agent)

  def handle_button(self, agent, btn_id):
    print '[Agent %s] GOT BTN button_id=%s from %r'%(self.name, btn_id, agent)
    # let's answer!
    self.send_msg('BTN_ACK %s'%btn_id)

a=MyAgent('007')

Implementation details

An Ivy client is made of several threads:

Copyright (c) 2005-2008 Sebastien Bigaret <sbigaret@users.sourceforge.net>

Classes [hide private]
  IvyProtocolError
  IvyMalformedMessage
  IvyIllegalStateError
  IvyClient
Represents a client connected to the bus.
  IvyServer
An Ivy server is responsible for receiving and handling the messages that other clients send on an Ivy bus to a given agent.
  IvyHandler
An IvyHandler is associated to one IvyClient connected to our server.
  IvyTimer
An IvyTimer object is responsible for calling a function regularly.
Functions [hide private]
 
trace(*args, **kw) source code
 
void_function(*arg, **kw)
A function that accepts any number of parameters and does nothing
source code
 
UDP_init_and_listen(broadcast_addr, port, socket_server)
Called by an IvyServer at startup; the method is responsible for:
source code
 
decode_msg(msg)
Returns: msg_id, numerical_id, parameters
source code
 
encode_message(msg_type, numerical_id, params='')
params is string -> added as-is params is list -> concatenated, separated by ARG_END
source code
 
is_multicast(ip)
Tells whether the specified ip is a multicast address or not
source code
 
decode_ivybus(ivybus=None)
Transforms the supplied string into the corrersponding broadcast address and port
source code
Variables [hide private]
  IvyRegexpAdded = 3
  IvyRegexpRemoved = 4
  NOT_INITIALIZED = 0
  INITIALIZATION_IN_PROGRESS = 1
  INITIALIZED = 2
    Messages types
  BYE = 0
  ADD_REGEXP = 1
  MSG = 2
  ERROR = 3
  DEL_REGEXP = 4
  END_INIT = 5
  END_REGEXP = 5
  START_INIT = 6
  START_REGEXP = 6
  DIRECT_MSG = 7
  DIE = 8
    Separators
  ARG_START = '\x02'
  ARG_END = '\x03'
    Misc. constants
  DEFAULT_IVYBUS = '127:2010'
  PROTOCOL_VERSION = 3
  DEFAULT_TTL = 64
  IvyApplicationConnected = 1
  IvyApplicationDisconnected = 2
  IVY_SHOULD_NOT_DIE = 'Ivy Application Should Not Die'
    Objects and functions related to logging
  ivylogger = logging.getLogger('Ivy')
  ivy_loghdlr = logging.StreamHandler()
  ivy_logformatter = logging.Formatter('%(asctime)s %(levelname)...
Function Details [hide private]

UDP_init_and_listen(broadcast_addr, port, socket_server)

source code 

Called by an IvyServer at startup; the method is responsible for:

  • sending the initial UDP broadcast message,
  • waiting for incoming UDP broadcast messages being sent by new clients connecting on the bus. When it receives such a message, a connection is established to that client and that connection (a socket) is then passed to the IvyServer instance.
Parameters:
  • broadcast_addr - the broadcast address used on the Ivy bus
  • port - the port dedicated to the Ivy bus
  • socket_server - instance of an IvyServer handling communications for our client.

decode_msg(msg)

source code 
Returns:
msg_id, numerical_id, parameters
Raises:

is_multicast(ip)

source code 
Tells whether the specified ip is a multicast address or not
Parameters:
  • ip - an IPv4 address in dotted-quad string format, for example 192.168.2.3

decode_ivybus(ivybus=None)

source code 
Transforms the supplied string into the corrersponding broadcast address and port
Parameters:
  • ivybus - if None or empty, defaults to environment variable IVYBUS
Returns:

a tuple made of (broadcast address, port number). For example:

>>> print decode_ivybus('192.168.12:2010')
('192.168.12.255', 2010)

Variables Details [hide private]

ivy_logformatter

Value:
logging.Formatter('%(asctime)s %(levelname)s %(message)s')