diff options
Diffstat (limited to 'bitbake-dev/lib/bb/server')
-rw-r--r-- | bitbake-dev/lib/bb/server/__init__.py | 1 | ||||
-rw-r--r-- | bitbake-dev/lib/bb/server/none.py | 164 | ||||
-rw-r--r-- | bitbake-dev/lib/bb/server/xmlrpc.py | 50 |
3 files changed, 145 insertions, 70 deletions
diff --git a/bitbake-dev/lib/bb/server/__init__.py b/bitbake-dev/lib/bb/server/__init__.py index 0dd04cf72..1a732236e 100644 --- a/bitbake-dev/lib/bb/server/__init__.py +++ b/bitbake-dev/lib/bb/server/__init__.py @@ -1 +1,2 @@ import xmlrpc +import none diff --git a/bitbake-dev/lib/bb/server/none.py b/bitbake-dev/lib/bb/server/none.py index ef061bc5d..ebda11158 100644 --- a/bitbake-dev/lib/bb/server/none.py +++ b/bitbake-dev/lib/bb/server/none.py @@ -1,5 +1,5 @@ # -# BitBake XMLRPC Server +# BitBake 'dummy' Passthrough Server # # Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer # Copyright (C) 2006 - 2008 Richard Purdie @@ -31,8 +31,11 @@ in the server's main loop. """ +import time import bb +from bb.ui import uievent import xmlrpclib +import pickle DEBUG = False @@ -44,31 +47,19 @@ class BitBakeServerCommands(): self.cooker = cooker self.server = server - def registerEventHandler(self, host, port): - """ - Register a remote UI Event Handler - """ - s = xmlrpclib.Server("http://%s:%d" % (host, port), allow_none=True) - return bb.event.register_UIHhandler(s) - - def unregisterEventHandler(self, handlerNum): - """ - Unregister a remote UI Event Handler - """ - return bb.event.unregister_UIHhandler(handlerNum) - def runCommand(self, command): """ Run a cooker command on the server """ + #print "Running Command %s" % command return self.cooker.command.runCommand(command) def terminateServer(self): """ Trigger the server to quit """ - self.server.quit = True - print "Server (cooker) exitting" + self.server.server_exit() + #print "Server (cooker) exitting" return def ping(self): @@ -77,63 +68,84 @@ class BitBakeServerCommands(): """ return True -class BitBakeXMLRPCServer(SimpleXMLRPCServer): +eventQueue = [] + +class BBUIEventQueue: + class event: + def __init__(self, parent): + self.parent = parent + @staticmethod + def send(event): + bb.server.none.eventQueue.append(pickle.loads(event)) + @staticmethod + def quit(): + return + + def __init__(self, BBServer): + self.eventQueue = bb.server.none.eventQueue + self.BBServer = BBServer + self.EventHandle = bb.event.register_UIHhandler(self) + + def getEvent(self): + if len(self.eventQueue) == 0: + return None + + return self.eventQueue.pop(0) + + def waitEvent(self, delay): + event = self.getEvent() + if event: + return event + self.BBServer.idle_commands(delay) + return self.getEvent() + + def queue_event(self, event): + self.eventQueue.append(event) + + def system_quit( self ):
+ bb.event.unregister_UIHhandler(self.EventHandle) + +class BitBakeServer(): # remove this when you're done with debugging # allow_reuse_address = True - def __init__(self, cooker, interface = ("localhost", 0)): - """ - Constructor - """ - SimpleXMLRPCServer.__init__(self, interface, - requestHandler=SimpleXMLRPCRequestHandler, - logRequests=False, allow_none=True) + def __init__(self, cooker): self._idlefuns = {} - self.host, self.port = self.socket.getsockname() - #self.register_introspection_functions() - commands = BitBakeServerCommands(self, cooker) - self.autoregister_all_functions(commands, "") - - def autoregister_all_functions(self, context, prefix): - """ - Convenience method for registering all functions in the scope - of this class that start with a common prefix - """ - methodlist = inspect.getmembers(context, inspect.ismethod) - for name, method in methodlist: - if name.startswith(prefix): - self.register_function(method, name[len(prefix):]) + self.commands = BitBakeServerCommands(self, cooker) def register_idle_function(self, function, data): """Register a function to be called while the server is idle""" assert callable(function) self._idlefuns[function] = data - def serve_forever(self): - """ - Serve Requests. Overloaded to honor a quit command - """ - self.quit = False - while not self.quit: - #print "Idle queue length %s" % len(self._idlefuns) - if len(self._idlefuns) == 0: - self.timeout = None - else: - self.timeout = 0 - self.handle_request() - #print "Idle timeout, running idle functions" - for function, data in self._idlefuns.items(): - try: - retval = function(self, data, False) - if not retval: - del self._idlefuns[function] - except SystemExit: - raise - except: - import traceback - traceback.print_exc() - pass + def idle_commands(self, delay): + #print "Idle queue length %s" % len(self._idlefuns) + #print "Idle timeout, running idle functions" + #if len(self._idlefuns) == 0: + nextsleep = delay + for function, data in self._idlefuns.items(): + try: + retval = function(self, data, False) + #print "Idle function returned %s" % (retval) + if retval is False: + del self._idlefuns[function] + elif retval is True: + nextsleep = None + elif nextsleep is None: + continue + elif retval < nextsleep: + nextsleep = retval + except SystemExit: + raise + except: + import traceback + traceback.print_exc() + pass + if nextsleep is not None: + #print "Sleeping for %s (%s)" % (nextsleep, delay) + time.sleep(nextsleep) + def server_exit(self): # Tell idle functions we're exiting for function, data in self._idlefuns.items(): try: @@ -141,5 +153,29 @@ class BitBakeXMLRPCServer(SimpleXMLRPCServer): except: pass - self.server_close() - return +class BitbakeServerInfo(): + def __init__(self, server): + self.server = server + self.commands = server.commands + +class BitBakeServerFork(): + def __init__(self, serverinfo, command, logfile): + serverinfo.forkCommand = command + serverinfo.logfile = logfile + +class BitBakeServerConnection(): + def __init__(self, serverinfo): + self.server = serverinfo.server + self.connection = serverinfo.commands + self.events = bb.server.none.BBUIEventQueue(self.server) + + def terminate(self): + try: + self.events.system_quit() + except: + pass + try: + self.connection.terminateServer() + except: + pass + diff --git a/bitbake-dev/lib/bb/server/xmlrpc.py b/bitbake-dev/lib/bb/server/xmlrpc.py index c5937abd6..b4d69f7be 100644 --- a/bitbake-dev/lib/bb/server/xmlrpc.py +++ b/bitbake-dev/lib/bb/server/xmlrpc.py @@ -33,6 +33,8 @@ import bb import xmlrpclib +from bb import daemonize +from bb.ui import uievent DEBUG = False @@ -114,26 +116,34 @@ class BitBakeServer(SimpleXMLRPCServer): Serve Requests. Overloaded to honor a quit command """ self.quit = False + self.timeout = 0 # Run Idle calls for our first callback while not self.quit: #print "Idle queue length %s" % len(self._idlefuns) - if len(self._idlefuns) == 0: - self.timeout = None - else: - self.timeout = 0 self.handle_request() #print "Idle timeout, running idle functions" + nextsleep = None for function, data in self._idlefuns.items(): try: retval = function(self, data, False) - if not retval: + if retval is False: del self._idlefuns[function] + elif retval is True: + nextsleep = 0 + elif nextsleep is 0: + continue + elif nextsleep is None: + nextsleep = retval + elif retval < nextsleep: + nextsleep = retval except SystemExit: raise except: import traceback traceback.print_exc() pass - + if nextsleep is None and len(self._idlefuns) > 0: + nextsleep = 0 + self.timeout = nextsleep # Tell idle functions we're exiting for function, data in self._idlefuns.items(): try: @@ -143,3 +153,31 @@ class BitBakeServer(SimpleXMLRPCServer): self.server_close() return + +class BitbakeServerInfo(): + def __init__(self, server): + self.host = server.host + self.port = server.port + +class BitBakeServerFork(): + def __init__(self, serverinfo, command, logfile): + daemonize.createDaemon(command, logfile) + +class BitBakeServerConnection(): + def __init__(self, serverinfo): + self.connection = xmlrpclib.Server("http://%s:%s" % (serverinfo.host, serverinfo.port), allow_none=True) + self.events = uievent.BBUIEventQueue(self.connection) + + def terminate(self): + # Don't wait for server indefinitely + import socket + socket.setdefaulttimeout(2) + try: + self.events.system_quit() + except: + pass + try: + self.connection.terminateServer() + except: + pass + |