Author: dmeyer
Date: Thu Jan 26 20:54:04 2006
New Revision: 7879
Added:
trunk/core/src/ipc/status.py
Log:
add mbus.status handling
Added: trunk/core/src/ipc/status.py
==============================================================================
--- (empty file)
+++ trunk/core/src/ipc/status.py Thu Jan 26 20:54:04 2006
@@ -0,0 +1,131 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# status.py - ipc handling for mbus status information
+# -----------------------------------------------------------------------------
+# $Id$
+#
+# FIXME: send last status on mbus.register
+#
+# -----------------------------------------------------------------------------
+# Freevo - A Home Theater PC framework
+# Copyright (C) 2002-2006 Krister Lagerstrom, Dirk Meyer, et al.
+#
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer: Dirk Meyer <[EMAIL PROTECTED]>
+#
+# Please see the file doc/CREDITS for a complete list of authors.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
+# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# -----------------------------------------------------------------------------
+
+
+# kaa imports
+from kaa.notifier import OneShotTimer, Signal
+
+
+class Status(object):
+ """
+ Status object handling the local status of an entity and (optional)
+ monitor remote entities.
+ """
+ def __init__(self, instance):
+ self.instance = instance
+ self.dict = dict()
+ self.send = OneShotTimer(self.send_update).start
+ self.signals = {'changed': Signal() }
+ self.instance.signals['register'].connect(self.register)
+ self.instance.signals['new-entity'].connect(self.new_entity)
+ self.monitoring = False
+
+ def set(self, key, value):
+ """
+ Set key to value and send event notifications.
+ """
+ if not self.dict.has_key(key) or self.dict[key] != value:
+ self.dict[key] = value
+ self.send(0.1)
+
+
+ def get(self, key):
+ """
+ Get the value for the given key.
+ """
+ return self.dict.get(key)
+
+
+ def monitor(self):
+ """
+ Monitor all other entities for status information. Each entity will
+ have a status variable to access that data.
+ """
+ if self.monitoring:
+ # already active
+ return
+ self.monitoring = True
+ # connect to event
+ self.instance.events['mbus.status'].connect(self.update)
+ # register to mbus.status for all already known entities
+ for entity in self.instance.get_entities():
+ entity.register('mbus.status')
+
+
+ def new_entity(self, entity):
+ """
+ Internal callback for new entities.
+ """
+ entity.status = {}
+ if self.monitoring:
+ # register to mbus.status
+ entity.register('mbus.status')
+ if 'mbus.status' in entity.registrations:
+ # is registered (already), send last status
+ entity.send('mbus.status', *self.dict.items())
+
+
+ def send_update(self):
+ """
+ Send update to registered remote entities.
+ """
+ self.instance.send_event('mbus.status', *self.dict.items())
+
+
+ def register(self, entity, prefix):
+ """
+ Callback for new register requests.
+ """
+ if prefix == 'mbus.register':
+ # send last status on register
+ entity.send('mbus.status', *self.dict.items())
+
+
+ def update(self, info):
+ """
+ Internal callback for updates.
+ """
+ info.source.status = dict(info)
+ self.signals['changed'].emit(info.source)
+
+
+def ipc_connect(instance):
+ """
+ Connect a new status object to an ipc instance.
+ """
+ status = Status(instance)
+ return 'status', dict(
+ set=status.set,
+ get=status.get,
+ monitor=status.monitor,
+ signals=status.signals)
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog