Update of /cvsroot/freevo/freevo/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22407
Modified Files:
mcomm.py
Log Message:
improve interface to change to latest pyMbus version
Index: mcomm.py
===================================================================
RCS file: /cvsroot/freevo/freevo/src/mcomm.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** mcomm.py 2 Nov 2004 20:15:22 -0000 1.1
--- mcomm.py 4 Nov 2004 17:36:23 -0000 1.2
***************
*** 8,11 ****
--- 8,20 ----
# functions and classes for easier access.
#
+ # If not present, this module will generate a mbus configuration file in
+ # ~/.mbus with some basic mbus settings. All applications on the mbus need
+ # to have the same settings to communicate. All known mbus implementations
+ # read this file, if you have mbus applications with different users, please
+ # make sure the entries match. If your applications are on different hosts,
+ # change SCOPE=LINKLOCAL in the config file. Also make sure you have a valid
+ # route for the given address. If the default route is used, make sure the
+ # mbus messages are not eaten by an iptable rule.
+ #
# Note: this is a test only right now. To test some basic stuff, call
# './freevo recordserver' and
***************
*** 37,51 ****
# -----------------------------------------------------------------------------
# python imports
import os
import sys
! # mbus import
! from mbus.mbusguides import *
! # a list of our callbacks when an entity joins or leaves the bus
! _notification_callbacks = []
! class Entity:
"""
Wrapper class for an Entity. It is possible to act with this object like
--- 46,116 ----
# -----------------------------------------------------------------------------
+
+ __all__ = [ 'MException', 'RPCReturn', 'RPCError', 'RPCServer', 'get_address',
+ 'register_entity_notification', 'find' ]
+
+
# python imports
import os
import sys
+ import random
+ import string
+ import time
+ import traceback
! # notifier / mbus import
! import notifier
! import mbus
! # freevo imports
! import config
!
!
! # dict of all our mbus instances
! _instance_list = {}
!
! def instance(name='default'):
! """
! Return the mbus instance with the given name. Create the instance
! if it does not yet exist. Use the name 'default' to get the default
! instance for this application.
! """
! if _instance_list.has_key(name):
! return _instance_list[name]
! i = Instance(name)
! _instance_list[name] = i
! if i == 'default':
! _instance_list[i.module] = i
! return i
!
!
! class MException(Exception):
! """
! Mbus related exception
! """
! pass
!
!
! class RPCReturn(mbus.RPCReturn):
! """
! A positive rpc response. Sending this signals the caller that the call
! was sucessfull.
! """
! def __init__(self, result = [], description = 'success'):
! mbus.RPCReturn.__init__(self, result, 'OK', description)
!
!
! class RPCError(mbus.RPCReturn):
! """
! A positive rpc response. Sending this signals the caller that the call
! was unsucessfull for the given reason.
! """
! def __init__(self, description):
! mbus.RPCReturn.__init__(self, [], 'FAILED', description)
!
!
!
! class RemoteEntity:
"""
Wrapper class for an Entity. It is possible to act with this object like
***************
*** 54,69 ****
There are three different ways to call a function:
! 1. add a callback to the end of the paraneter list of a function call
! e.g.: Entity.function_to_call(arg1, arg2, callback)
2. add the callback as **args to the function:
! e.g.: Entity.function_to_call(arg2, arg2, callback=callback)
3. provide no callback function and the call will wait for the return
! e.g.: result = Entity.function_to_call(arg2, arg2)
"""
! def __init__(self, addr):
self.addr = addr
self.cmd = None
self.present = True
!
def __getattr__(self, attr):
"""
--- 119,145 ----
There are three different ways to call a function:
! 1. add a callback to the end of the parameter list of a function call
! e.g.: RemoteEntity.function_to_call(arg1, arg2, callback)
2. add the callback as **args to the function:
! e.g.: RemoteEntity.function_to_call(arg2, arg2, callback=callback)
3. provide no callback function and the call will wait for the return
! e.g.: result = RemoteEntity.function_to_call(arg2, arg2)
"""
! def __init__(self, addr, mbus_instance):
self.addr = addr
self.cmd = None
self.present = True
! self.mbus_instance = mbus_instance
!
!
! def matches(self, addr):
! """
! Return True if the given addr matches the address of the object.
! The given address can also be a subset of the objects address
! informations.
! """
! return mbus.isAddressedTo(addr, self.addr)
!
!
def __getattr__(self, attr):
"""
***************
*** 71,79 ****
the remote entity.
"""
! if attr in ('has_key', '__getitem__'):
return getattr(self.addr, attr)
self.cmd = attr.replace('_', '-')
return self.__call
def __callback(self, return_list, data = None):
"""
--- 147,156 ----
the remote entity.
"""
! if attr in ('has_key', ) or attr.startswith('__'):
return getattr(self.addr, attr)
self.cmd = attr.replace('_', '-')
return self.__call
+
def __callback(self, return_list, data = None):
"""
***************
*** 81,85 ****
"""
self.result = return_list
!
def __call(self, *args, **kargs):
"""
--- 158,163 ----
"""
self.result = return_list
!
!
def __call(self, *args, **kargs):
"""
***************
*** 99,109 ****
wait = True
self.result = None
! _mbus.sendRPC(self.addr, 'home-theatre.' + self.cmd, cmdargs, callback)
if self.result == True:
return True
while not self.result:
notifier.step(True, False)
! return self.result[0][1:]
!
def __eq__(self, obj):
"""
--- 177,209 ----
wait = True
self.result = None
! self.mbus_instance.sendRPC(self.addr, 'home-theatre.' + self.cmd,
! cmdargs, callback)
!
if self.result == True:
return True
+
+ # wait for return
while not self.result:
notifier.step(True, False)
!
! # check for errors / exceptions and raise them
! if isinstance(self.result, mbus.types.MError):
! raise MException(self.result.descr)
!
! status, args = self.result[0][1:]
! r = self.result[0][1:]
! if status[0] == 'FAILED':
! print status
! if status[1] in ('TypeError', ):
! raise eval(status[1])(status[2])
! else:
! raise MException('%s: %s' % (status[1], status[2]))
!
! # normal return handling
! if status[1] == 'OK':
! return True, args
! return False, args
!
!
def __eq__(self, obj):
"""
***************
*** 114,173 ****
return self.addr == obj
-
- def register_entity_notification(func):
- """
- Register to callback to get notification when an entity joins or leaves
- the mbus. The parameter of the callback is an 'Entity'. The attribute
- 'present' of this entity can be used to check if the entity joined the
- bus (present = True) or is gone now (present = False).
- """
- _notification_callbacks.append(func)
! def register_server(server):
"""
! Register a class as RPC Server. All functions starting with __rpc and
ending with __ are registered for the specific command.
"""
! for f in dir(server):
! if f.startswith('__rpc_'):
! _mbus.addRPCCallback('home-theatre.' + f[6:-2].replace('_', '-'),
! getattr(server, f))
!
! def error(description):
"""
! Returns a RPC error for server callbacks.
"""
! return RPCReturn( [], 'FAILED', description )
! def result(result, description = 'success'):
"""
! Returns a RPC result for server callbacks.
"""
! return RPCReturn( result, 'OK', description)
!
- # ---------- internal code ---------------------
! def _new_entity(maddr):
"""
! pyMbus callback for new entity
"""
! e = Entity(maddr)
! for c in _notification_callbacks:
! c(e)
! def _lost_entity(maddr):
"""
! pyMbus callback for lost entity
"""
! e = Entity(maddr)
! e.present = False
! for c in _notification_callbacks:
! c(e)
! # create mbus entity for this application
! _app = os.path.splitext(os.path.basename(sys.argv[0]))[0]
! _mbus = Guidelines( "app:%s lang:python" % _app )
! _mbus.newEntityFunction = _new_entity
! _mbus.lostEntityFunction = _lost_entity
--- 214,397 ----
return self.addr == obj
! class RPCServer:
"""
! A base class for a RPC Server. All functions starting with __rpc and
ending with __ are registered for the specific command.
"""
! def __init__(self, mbus_instance='default'):
! """
! Register all callbacks
! """
! add_callback = instance(mbus_instance).addRPCCallback
! for f in dir(self):
! if f.startswith('__rpc_'):
! cmdname = 'home-theatre.' + f[6:-2].replace('_', '-')
! add_callback(cmdname, getattr(self, f))
!
!
! def parse_parameter(self, val, pattern):
! """
! Parse the given parameter and send a TypeError if the parameters
! don't match the pattern.
! """
! if len(pattern) == len(val):
! if len(val) == 1:
! return val[0]
! return val
! if len(pattern) == 1:
! arg = '1 argument'
! else:
! arg = '%s arguments' % len(pattern)
! function = traceback.extract_stack(limit = 2)[0][2][6:-2]
! msg = '%s() takes %s (%s given)' % (function, arg, len(val))
! raise mbus.RPCException('TypeError', msg)
!
!
!
! def get_address(name):
"""
! Return a mbus address based on the freevo naming scheme.
"""
! if name == 'default':
! name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
! if name in ('main', 'freevo'):
! name = 'core'
! return mbus.MAddress({'type': 'home-theatre', 'module': name })
!
! def register_entity_notification(func):
"""
! Register to callback to get notification when an entity joins or leaves
! the mbus. The parameter of the callback is an 'RemoteEntity'. The attribute
! 'present' of this entity can be used to check if the entity joined the
! bus (present = True) or is gone now (present = False).
"""
! return instance().register_entity_notification(func)
! def find(name, timeout = 10000):
"""
! Wait for an entity matching 'name'. Return the entity or None if no
! entity is found after timeout. In most cases this function is needed
! for helpers connection to freevo or the recordserver.
"""
! return instance().find(name, timeout)
!
!
! class Instance(mbus.Guides):
"""
! Freevo mbus instance for mbus communication
"""
! def __init__(self, name='default'):
! """
! Create the mbus address and connect to the mbus
! """
! # build mbus name including app=freevo
! addr = get_address(name)
! addr['app'] = 'freevo'
! # init the mbus interface
! mbus.Guides.__init__( self, addr )
!
! # set callbacks
! self.newEntityFunction = self.new_entity
! self.lostEntityFunction = self.lost_entity
! self.errorFunction = None
! self.onErrorInvokeRPCCallback = True
!
! # application short name
! self.module = addr['module']
!
! # a list of our callbacks when an entity joins or leaves the bus
! self.__notification_callbacks = []
!
! # a list of all known entities
! self.__entities = []
!
!
! def register_entity_notification(self, func):
! """
! Register to callback to get notification when an entity joins or leaves
! the mbus. The parameter of the callback is an 'RemoteEntity'. The
! attribute 'present' of this entity can be used to check if the entity
! joined the bus (present = True) or is gone now (present = False).
! """
! self.__notification_callbacks.append(func)
!
!
! def find(self, name, timeout = 10000):
! """
! Wait for an entity matching 'name'. Return the entity or None if no
! entity is found after timeout. In most cases this function is needed
! for helpers connection to freevo or the recordserver.
! """
! addr = get_address(name)
! t1 = time.time()
! while 1:
! for e in self.__entities:
! if mbus.isAddressedTo(addr, e.addr):
! return e
! if t1 + float(timeout) / 1000 < time.time():
! return None
! notifier.step(True, False)
!
!
! def new_entity(self, maddr):
! """
! pyMbus callback for new entity
! """
! e = RemoteEntity(maddr, self)
! self.__entities.append(e)
! for c in self.__notification_callbacks:
! c(e)
!
!
! def lost_entity(self, maddr):
! """
! pyMbus callback for lost entity
! """
! for e in self.__entities:
! if e == maddr:
! self.__entities.remove(e)
! e.present = False
! for c in self.__notification_callbacks:
! c(e)
! break
!
!
! # Check for mbus configuration file. It is either ~/.mbus or defined
! # in the environment variable MBUS.
! mbus_config = os.path.expanduser('~/.mbus')
! if os.environ.has_key('MBUS'):
! mbus_config = os.environ['MBUS']
!
! if not os.path.isfile(mbus_config):
! # If the file does not exist, generate one with a random hash key
! # and a user defined port. For security reasons it is not allowed
! # to have read access to this file for 'others'
! tmp = open(os.path.join(config.SHARE_DIR, 'mbus.conf'))
! cfg = open(mbus_config, 'w')
! for line in tmp.readlines():
! if line.startswith('PORT='):
! # generate new user defined port
! line = 'PORT=%s\n' % (47000 + os.getuid())
! if line.startswith('HASHKEY='):
! # generate new random has key
! chars = string.ascii_letters + string.digits
! line = 'HASHKEY=(HMAC-MD5-96, '
! for i in range(16):
! line += chars[random.randint(0, len(chars)-1)]
! line += ')\n'
! # write line to config file
! cfg.write(line)
! tmp.close()
! cfg.close()
! os.chmod(mbus_config, 0600)
!
!
! if config.HELPER and not notifier.loop:
! # init the notifier (not done yet)
! notifier.init( notifier.GENERIC )
-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog