Author: dmeyer
Date: Wed Apr 5 19:29:56 2006
New Revision: 1383
Modified:
trunk/beacon/bin/beacon
trunk/beacon/src/__init__.py
trunk/beacon/src/client.py
trunk/beacon/src/server.py
Log:
support only one db at server side
Modified: trunk/beacon/bin/beacon
==============================================================================
--- trunk/beacon/bin/beacon (original)
+++ trunk/beacon/bin/beacon Wed Apr 5 19:29:56 2006
@@ -22,6 +22,7 @@
print 'options:'
print '--start start beacon'
print '--stop stop running beacon'
+ print '--db directory directory of the database (default ~/.beacon)'
print '--daemon detach for parent process'
print '--autoshutdown stop server when no clients are connected'
print '--logfile file use file for logging'
@@ -45,8 +46,8 @@
try:
# read arguments
- opts = [ 'start', 'stop', 'daemon', 'autoshutdown', 'logfile=', 'verbose=',
- 'help', 'search', 'monitor' ]
+ opts = [ 'start', 'stop', 'db=', 'daemon', 'autoshutdown', 'logfile=',
+ 'verbose=', 'help', 'search', 'monitor' ]
opts, args = getopt.getopt(sys.argv[1:], 'h', opts)
except getopt.GetoptError:
usage(1)
@@ -56,12 +57,15 @@
detach = False
shutdown = False
monitor = False
+database = os.path.expanduser("~/.beacon")
for o, a in opts:
if o in ('--start', '--stop', '--search'):
if mode:
usage(1)
mode = o[2:]
+ if o == '--db':
+ database = a
if o == '--daemon':
detach = True
if o == '--autoshutdown':
@@ -129,7 +133,7 @@
query[key] = value
try:
- kaa.beacon.connect(os.path.expanduser("~/.beacon"))
+ kaa.beacon.connect()
except:
print 'beacon server not running'
sys.exit(1)
@@ -238,32 +242,17 @@
log.info('connect to thumbnailer')
kaa.beacon.thumbnail.thumbnail.connect()
-try:
- ipc = kaa.ipc.IPCServer('beacon')
-except IOError, e:
- kaa.beacon.thumbnail.thumbnail.disconnect()
- log.error('beacon: %s' % e)
- time.sleep(0.1)
- sys.exit(0)
-
-log.info('start beacon')
-ipc.register_object(kaa.beacon.server.connect, 'beacon')
-ipc.register_object(kaa.notifier.shutdown, 'shutdown')
-ipc.signals["client_closed"].connect(kaa.beacon.server._client_closed)
-
-if os.environ.get('BEACON_DATABASE'):
- log.info('connect to %s', os.environ['BEACON_DATABASE'])
- server = kaa.beacon.server.connect(os.environ['BEACON_DATABASE'])
- if os.environ.get('BEACON_MONITOR'):
- for dirname in os.environ.get('BEACON_MONITOR').split(':'):
- log.info('monitor %s', dirname)
- server.monitor_dir(dirname)
+server = kaa.beacon.server.Server(database)
+if os.environ.get('BEACON_MONITOR'):
+ for dirname in os.environ.get('BEACON_MONITOR').split(':'):
+ log.info('monitor %s', dirname)
+ server.monitor_dir(dirname)
# start garbage collector
kaa.notifier.Timer(garbage_collect).start(10)
if shutdown:
log.info('set autoshutdown timer to 3 seconds')
- kaa.beacon.server.autoshutdown(3)
+ server.autoshutdown(3)
kaa.notifier.loop()
log.info('stop beacon')
Modified: trunk/beacon/src/__init__.py
==============================================================================
--- trunk/beacon/src/__init__.py (original)
+++ trunk/beacon/src/__init__.py Wed Apr 5 19:29:56 2006
@@ -44,24 +44,18 @@
# connected client object
_client = None
-def connect(database=None):
+def connect():
"""
- Connect to the beacon database dir given by 'database'. Id 'database' is
None, the
- client will only connect to the thumbnailer. A beacon server must be
running.
+ Connect to the beacon. A beacon server must be running.
"""
global _client
if _client:
return _client
- log.info('connect to thumbnailer')
+ log.info('beacon connect')
thumbnail.connect()
-
- if not database:
- return None
-
- log.info('connect to %s' % database)
- _client = Client(database)
+ _client = Client()
return _client
Modified: trunk/beacon/src/client.py
==============================================================================
--- trunk/beacon/src/client.py (original)
+++ trunk/beacon/src/client.py Wed Apr 5 19:29:56 2006
@@ -60,15 +60,14 @@
Beacon client. This client uses the db read only and needs a server on
the same machine doing the file scanning and changing of the db.
"""
- def __init__(self, db):
- db = os.path.abspath(db)
+ def __init__(self):
# monitor function from the server to start a new monitor for a query
- self._server = kaa.ipc.IPCClient('beacon').get_object('beacon')(db)
- self._server_monitor = self._server.monitor
+ self._server = kaa.ipc.IPCClient('beacon').get_object('beacon')
+ self._monitor = self._server.monitor
# read only version of the database
- self.database = Database(db, self)
+ self.database = Database(self._server.get_database(), self)
# connect to server notifications
- self.id = self._server.connect(self, self._beacon_notify)
+ self.id = self._server.connect(self)
# internal list of active queries
self._queries = []
# internal list of items to update
@@ -85,7 +84,7 @@
q._monitor = False
self._queries = []
self._server = None
- self._server_monitor = None
+ self._monitor = None
self.database = None
@@ -120,8 +119,7 @@
q = copy.copy(query._query)
if 'parent' in q:
q['parent'] = q['parent']._beacon_id
- self._server_monitor(self.id, query.id, q,
- __ipc_noproxy_args=True, __ipc_oneway=True)
+ self._monitor(self.id, query.id, q, __ipc_noproxy_args=True,
__ipc_oneway=True)
def _beacon_request(self, filename):
@@ -132,7 +130,7 @@
__ipc_noproxy_args=True)
- def _beacon_notify(self, id, msg, *args, **kwargs):
+ def notify(self, id, msg, *args, **kwargs):
"""
Internal notification callback from the server. The Monitor does not
has a reference to the Query because this would result in circular
Modified: trunk/beacon/src/server.py
==============================================================================
--- trunk/beacon/src/server.py (original)
+++ trunk/beacon/src/server.py Wed Apr 5 19:29:56 2006
@@ -39,9 +39,9 @@
import logging
# kaa imports
-from kaa import ipc
+import kaa.ipc
from kaa.weakref import weakref
-from kaa.notifier import OneShotTimer, Timer
+from kaa.notifier import OneShotTimer, Timer, Callback
# kaa.beacon imports
import parser
@@ -51,8 +51,6 @@
# get logging object
log = logging.getLogger('beacon')
-# ipc debugging
-# ipc.DEBUG = 1
class Server(object):
"""
@@ -60,6 +58,20 @@
scanning / monitoring of queries.
"""
def __init__(self, dbdir):
+ log.info('start beacon')
+ try:
+ self.ipc = kaa.ipc.IPCServer('beacon')
+ except IOError, e:
+ kaa.beacon.thumbnail.thumbnail.disconnect()
+ log.error('beacon: %s' % e)
+ time.sleep(0.1)
+ sys.exit(0)
+
+ self.ipc.register_object(self, 'beacon')
+ self.ipc.register_object(Callback(sys.exit, 0), 'shutdown')
+ self.ipc.signals["client_closed"].connect(self.disconnect)
+
+ self._dbdir = dbdir
self._db = Database(dbdir, None)
self._next_client = 0
@@ -118,6 +130,62 @@
self._db.commit()
+ def get_database(self):
+ """
+ Return the database directory of the server.
+ """
+ return self._dbdir
+
+
+ def connect(self, client):
+ """
+ Connect a new client to the server.
+ """
+ self._next_client += 1
+ self._clients.append((self._next_client, client, client.notify, []))
+ for device, directory, name in self._db.get_mountpoints():
+ client.database.add_mountpoint(device, directory)
+ client.database.set_mountpoint(directory, name)
+ return self._next_client
+
+
+ def disconnect(self, client):
+ """
+ IPC callback when a client is lost.
+ """
+ for client_info in self._clients[:]:
+ if kaa.ipc.get_ipc_from_proxy(client_info[1]) == client:
+ log.warning('disconnect client')
+ for m in client_info[3]:
+ m.stop()
+ self._clients.remove(client_info)
+
+
+ def autoshutdown(self, timeout):
+ """
+ Start autoshutdown.
+ """
+ if hasattr(self, '_autoshutdown_timer'):
+ return
+ self._autoshutdown_timer = timeout
+ Timer(self._autoshutdown, timeout).start(1)
+
+
+ def _autoshutdown(self, timeout):
+ """
+ Timer callback for autoshutdown.
+ """
+ print self._autoshutdown_timer
+ if len(self._clients) > 0:
+ self._autoshutdown_timer = timeout
+ return True
+ self._autoshutdown_timer -= 1
+ if self._autoshutdown_timer == 0:
+ log.info('beacon timeout')
+ sys.exit(0)
+ return True
+
+
def register_file_type_attrs(self, name, **kwargs):
"""
Register new attrs and types for files. The basics are already
@@ -198,18 +266,6 @@
return False
- def connect(self, client, callback):
- """
- Connect a new client to the server.
- """
- self._next_client += 1
- self._clients.append((self._next_client, client, callback, []))
- for device, directory, name in self._db.get_mountpoints():
- client.database.add_mountpoint(device, directory)
- client.database.set_mountpoint(directory, name)
- return self._next_client
-
-
def update(self, items):
"""
Update items from the client.
@@ -239,58 +295,3 @@
Debug in __del__.
"""
return 'del', self
-
-
-# internal list of server
-_server = {}
-_num_client = 0
-
-def connect(dbdir):
- """
- Connect to a server object. Each server object handles one db dir.
- Different clients can use the same server object.
- """
- log.info('connect to %s' % dbdir)
-
- global _num_client
- _num_client += 1
-
- # TODO: delete databases not used anymore
-
- if not dbdir in _server:
- log.info('create server object')
- server = Server(dbdir)
- # TODO: use weakref
- _server[dbdir] = server
- return _server[dbdir]
-
-
-def _client_closed(client):
- global _num_client
- for server in _server.values():
- for client_info in server._clients:
- if ipc.get_ipc_from_proxy(client_info[1]) == client:
- log.warning('disconnect client')
- for m in client_info[3]:
- m.stop()
- server._clients.remove(client_info)
- _num_client -= 1
-
-
-def autoshutdown_step(timeout):
- global shutdown_timer
- if _num_client > 0:
- shutdown_timer = timeout
- return True
- shutdown_timer -= 1
- if shutdown_timer == 0:
- log.info('beacon timeout')
- sys.exit(0)
- return True
-
-
-def autoshutdown(timeout):
- global shutdown_timer
- shutdown_timer = timeout
- Timer(autoshutdown_step, timeout).start(1)
-
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog