Skip to contentSkip to conversation

refactoring bb.server code

PATCH New Alex DAMIAN 1 participant 3 messages Apr 30, 2013

lore
Share & tools
lore
#0 lore raw

cover refactoring bb.server code

From: Alexandru DAMIAN <[email protected]>

This patchset refactors the bitbake server code as to use proper class inheritance, minimize code copy-pasta and maximize cod reuse. By creating a unified interface to server code I expect easier future development and minimizing the number of code changes for bug fixes.

Alexandru DAMIAN (2):
bitbake server: create common server infrastructure bitbake servers: use common server infrastructure

 bitbake/lib/bb/server/__init__.py |   95 +++++++++++++++++++++++++++++++++++++
 bitbake/lib/bb/server/none.py     |   38 +++++----------
 bitbake/lib/bb/server/process.py  |   54 ++++++++-------------
 bitbake/lib/bb/server/xmlrpc.py   |   40 +++++-----------
 4 files changed, 139 insertions(+), 88 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 1/2 bitbake server: create common server infrastructure

From: Alexandru DAMIAN <[email protected]>

In an attempt to minimize code duplication, create clear interfaces, and maximize code reuse through OOP, bb.server adds base classes for the BitBakeServer, BitBakeServerConnection and actual server implementations instructed in particular server types.

These classes document the minimum interfaces that the derived classes must implement, and provide boilerplate code.

---
 bitbake/lib/bb/server/__init__.py |   95 +++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
bitbake/lib/bb/server/__init__.py +95 -0
index e69de29..dcbae62 100644
@@ -0,0 +1,95 @@
+#
+# BitBake Base Server Code
+#
+# Copyright (C) 2006 - 2007  Michael 'Mickey' Lauer
+# Copyright (C) 2006 - 2008  Richard Purdie
+# Copyright (C) 2013         Alexandru Damian
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY 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.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+""" Base code for Bitbake server process
+
+Have a common base for that all Bitbake server classes ensures a consistent
+approach to the interface, and minimize risks associated with code duplication.
+
+"""
+
+"""  BaseImplServer() the base class for all XXServer() implementations.
+
+    These classes contain the actual code that runs the server side, i.e.
+    listens for the commands and executes them. Although these implementations
+    contain all the data of the original bitbake command, i.e the cooker instance,
+    they may well run on a different process or even machine.
+
+"""
+
+class BaseImplServer():
+    def __init__(self):
+        self._idlefuns = {}
+
+    def addcooker(self, cooker):
+        self.cooker = cooker
+
+    def register_idle_function(self, function, data):
+        """Register a function to be called while the server is idle"""
+        assert hasattr(function, '__call__')
+        self._idlefuns[function] = data
+
+
+
+""" BitBakeBaseServerConnection class is the common ancestor to all
+    BitBakeServerConnection classes.
+
+    These classes control the remote server. The only command currently
+    implemented is the terminate() command.
+
+"""
+
+class BitBakeBaseServerConnection():
+    def __init__(self, serverImpl):
+        pass
+
+    def terminate(self):
+        pass
+
+
+""" BitBakeBaseServer class is the common ancestor to all Bitbake servers
+
+    Derive this class in order to implement a BitBakeServer which is the
+    controlling stub for the actual server implementation
+
+"""
+class BitBakeBaseServer(object):
+    def initServer(self):
+        self.serverImpl = None  # we ensure a runtime crash if not overloaded
+        return
+
+    def addcooker(self, cooker):
+        self.cooker = cooker
+        self.serverImpl.addcooker(cooker)
+
+    def getServerIdleCB(self):
+        return self.serverImpl.register_idle_function
+
+    def saveConnectionDetails(self):
+        return
+
+    def detach(self):
+        return
+
+    def establishConnection(self):
+        raise   "Must redefine the %s.establishConnection()" % self.__class__.__name__
+
+    def launchUI(self, uifunc, *args):
+        raise   "Must redefine the %s.launchUI()" % self.__class__.__name__
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/2 bitbake servers: use common server infrastructure

In reply to Alex DAMIAN

From: Alexandru DAMIAN <[email protected]>

Modifies all the server implementations so that it uses and follows the common server infrastructure.

Also clears up convoluted code and adds usage of defined interfaces.

---
 bitbake/lib/bb/server/none.py    |   38 ++++++++-------------------
 bitbake/lib/bb/server/process.py |   54 +++++++++++++++-----------------------
 bitbake/lib/bb/server/xmlrpc.py  |   40 +++++++++-------------------
 3 files changed, 44 insertions(+), 88 deletions(-)
bitbake/lib/bb/server/none.py +11 -27
index f5fd4d4..83198d0 100644
@@ -34,6 +34,8 @@ DEBUG = False
 
 import inspect, select
 
+from . import BitBakeBaseServer, BitBakeBaseServerConnection, BaseImplServer
+
 class BitBakeServerCommands():
     def __init__(self, server):
         self.server = server
@@ -104,23 +106,18 @@ class BBUIEventQueue:
 def chldhandler(signum, stackframe):
     pass
 
-class BitBakeNoneServer():
+class NoneServer(BaseImplServer):
     # remove this when you're done with debugging
     # allow_reuse_address = True
 
     def __init__(self):
-        self._idlefuns = {}
+        BaseImplServer.__init__(self)
         self.commands = BitBakeServerCommands(self)
 
     def addcooker(self, cooker):
-        self.cooker = cooker
+        BaseImplServer.addcooker(cooker)
         self.commands.cooker = cooker
 
-    def register_idle_function(self, function, data):
-        """Register a function to be called while the server is idle"""
-        assert hasattr(function, '__call__')
-        self._idlefuns[function] = data
-
     def idle_commands(self, delay):
         #print "Idle queue length %s" % len(self._idlefuns)
         #print "Idle timeout, running idle functions"
@@ -159,9 +156,9 @@ class BitBakeNoneServer():
             except:
                 pass
 
-class BitBakeServerConnection():
-    def __init__(self, server):
-        self.server = server.server
+class BitBakeNoneServerConnection(BitBakeBaseServerConnection):
+    def __init__(self, serverImpl):
+        self.server = serverImpl
         self.connection = self.server.commands
         self.events = bb.server.none.BBUIEventQueue(self.server)
         for event in bb.event.ui_queue:
@@ -177,25 +174,12 @@ class BitBakeServerConnection():
         except:
             pass
 
-class BitBakeServer(object):
+class BitBakeServer(BitBakeBaseServer):
     def initServer(self):
-        self.server = BitBakeNoneServer()
-
-    def addcooker(self, cooker):
-        self.cooker = cooker
-        self.server.addcooker(cooker)
-
-    def getServerIdleCB(self):
-        return self.server.register_idle_function
-
-    def saveConnectionDetails(self):
-        return
-
-    def detach(self):
-        return
+        self.serverImpl = NoneServer()
 
     def establishConnection(self):
-        self.connection = BitBakeServerConnection(self)
+        self.connection = BitBakeNoneServerConnection(self.serverImpl)
         return self.connection
 
     def launchUI(self, uifunc, *args):
bitbake/lib/bb/server/process.py +21 -33

121 lines not on this page · raw message

bitbake/lib/bb/server/xmlrpc.py +12 -28
index 61bd57a..9028c39 100644
@@ -41,6 +41,8 @@ DEBUG = False
 from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
 import inspect, select
 
+from . import BitBakeBaseServer, BitBakeBaseServerConnection, BaseImplServer
+
 if sys.hexversion < 0x020600F0:
     print("Sorry, python 2.6 or later is required for bitbake's XMLRPC mode")
     sys.exit(1)
@@ -159,7 +161,7 @@ class BitBakeServerCommands():
         """
         return True
 
-class BitBakeXMLRPCServer(SimpleXMLRPCServer):
+class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer):
     # remove this when you're done with debugging
     # allow_reuse_address = True
 
@@ -167,17 +169,17 @@ class BitBakeXMLRPCServer(SimpleXMLRPCServer):
         """
         Constructor
         """
+        BaseImplServer.__init__(self)
         SimpleXMLRPCServer.__init__(self, interface,
                                     requestHandler=SimpleXMLRPCRequestHandler,
                                     logRequests=False, allow_none=True)
-        self._idlefuns = {}
         self.host, self.port = self.socket.getsockname()
         #self.register_introspection_functions()
         self.commands = BitBakeServerCommands(self)
         self.autoregister_all_functions(self.commands, "")
 
     def addcooker(self, cooker):
-        self.cooker = cooker
+        BaseImplServer.addcooker(cooker)
         self.commands.cooker = cooker
 
     def autoregister_all_functions(self, context, prefix):
@@ -190,10 +192,6 @@ class BitBakeXMLRPCServer(SimpleXMLRPCServer):
             if name.startswith(prefix):
                 self.register_function(method, name[len(prefix):])
 
-    def register_idle_function(self, function, data):
-        """Register a function to be called while the server is idle"""
-        assert hasattr(function, '__call__')
-        self._idlefuns[function] = data
 
     def serve_forever(self):
         bb.cooker.server_main(self.cooker, self._serve_forever)
@@ -241,14 +239,10 @@ class BitBakeXMLRPCServer(SimpleXMLRPCServer):
         self.server_close()
         return
 
-class BitbakeServerInfo():
-    def __init__(self, host, port):
-        self.host = host
-        self.port = port
 
-class BitBakeServerConnection():
-    def __init__(self, serverinfo, clientinfo=("localhost", 0)):
-        self.connection = _create_server(serverinfo.host, serverinfo.port)
+class BitBakeXMLRPCServerConnection(BitBakeBaseServerConnection):
+    def __init__(self, serverImpl, clientinfo=("localhost", 0)):
+        self.connection = _create_server(serverImpl.host, serverImpl.port)
         self.events = uievent.BBUIEventQueue(self.connection, clientinfo)
         for event in bb.event.ui_queue:
             self.events.queue_event(event)
@@ -266,27 +260,17 @@ class BitBakeServerConnection():
         except:
             pass
 
-class BitBakeServer(object):
+class BitBakeServer(BitBakeBaseServer):
     def initServer(self, interface = ("localhost", 0)):
-        self.server = BitBakeXMLRPCServer(interface)
-
-    def addcooker(self, cooker):
-        self.cooker = cooker
-        self.server.addcooker(cooker)
-
-    def getServerIdleCB(self):
-        return self.server.register_idle_function
-
-    def saveConnectionDetails(self): 
-        self.serverinfo = BitbakeServerInfo(self.server.host, self.server.port)
+        self.serverImpl = XMLRPCServer(interface)
 
     def detach(self):
         daemonize.createDaemon(self.server.serve_forever, "bitbake-cookerdaemon.log")
         del self.cooker
-        del self.server
+        del self.serverImpl
 
     def establishConnection(self):
-        self.connection = BitBakeServerConnection(self.serverinfo)
+        self.connection = BitBakeXMLRPCServerConnection(self.serverImpl)
         return self.connection
 
     def launchUI(self, uifunc, *args):
Signature · 1 line
1.7.10.4