diff options
Diffstat (limited to 'bitbake/lib/bb/server')
-rw-r--r-- | bitbake/lib/bb/server/__init__.py | 96 | ||||
-rw-r--r-- | bitbake/lib/bb/server/process.py | 252 | ||||
-rw-r--r-- | bitbake/lib/bb/server/xmlrpc.py | 383 |
3 files changed, 731 insertions, 0 deletions
diff --git a/bitbake/lib/bb/server/__init__.py b/bitbake/lib/bb/server/__init__.py new file mode 100644 index 0000000000..da5e480740 --- /dev/null +++ b/bitbake/lib/bb/server/__init__.py | |||
@@ -0,0 +1,96 @@ | |||
1 | # | ||
2 | # BitBake Base Server Code | ||
3 | # | ||
4 | # Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer | ||
5 | # Copyright (C) 2006 - 2008 Richard Purdie | ||
6 | # Copyright (C) 2013 Alexandru Damian | ||
7 | # | ||
8 | # This program is free software; you can redistribute it and/or modify | ||
9 | # it under the terms of the GNU General Public License version 2 as | ||
10 | # published by the Free Software Foundation. | ||
11 | # | ||
12 | # This program is distributed in the hope that it will be useful, | ||
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | # GNU General Public License for more details. | ||
16 | # | ||
17 | # You should have received a copy of the GNU General Public License along | ||
18 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
20 | |||
21 | """ Base code for Bitbake server process | ||
22 | |||
23 | Have a common base for that all Bitbake server classes ensures a consistent | ||
24 | approach to the interface, and minimize risks associated with code duplication. | ||
25 | |||
26 | """ | ||
27 | |||
28 | """ BaseImplServer() the base class for all XXServer() implementations. | ||
29 | |||
30 | These classes contain the actual code that runs the server side, i.e. | ||
31 | listens for the commands and executes them. Although these implementations | ||
32 | contain all the data of the original bitbake command, i.e the cooker instance, | ||
33 | they may well run on a different process or even machine. | ||
34 | |||
35 | """ | ||
36 | |||
37 | class BaseImplServer(): | ||
38 | def __init__(self): | ||
39 | self._idlefuns = {} | ||
40 | |||
41 | def addcooker(self, cooker): | ||
42 | self.cooker = cooker | ||
43 | |||
44 | def register_idle_function(self, function, data): | ||
45 | """Register a function to be called while the server is idle""" | ||
46 | assert hasattr(function, '__call__') | ||
47 | self._idlefuns[function] = data | ||
48 | |||
49 | |||
50 | |||
51 | """ BitBakeBaseServerConnection class is the common ancestor to all | ||
52 | BitBakeServerConnection classes. | ||
53 | |||
54 | These classes control the remote server. The only command currently | ||
55 | implemented is the terminate() command. | ||
56 | |||
57 | """ | ||
58 | |||
59 | class BitBakeBaseServerConnection(): | ||
60 | def __init__(self, serverImpl): | ||
61 | pass | ||
62 | |||
63 | def terminate(self): | ||
64 | pass | ||
65 | |||
66 | |||
67 | """ BitBakeBaseServer class is the common ancestor to all Bitbake servers | ||
68 | |||
69 | Derive this class in order to implement a BitBakeServer which is the | ||
70 | controlling stub for the actual server implementation | ||
71 | |||
72 | """ | ||
73 | class BitBakeBaseServer(object): | ||
74 | def initServer(self): | ||
75 | self.serverImpl = None # we ensure a runtime crash if not overloaded | ||
76 | self.connection = None | ||
77 | return | ||
78 | |||
79 | def addcooker(self, cooker): | ||
80 | self.cooker = cooker | ||
81 | self.serverImpl.addcooker(cooker) | ||
82 | |||
83 | def getServerIdleCB(self): | ||
84 | return self.serverImpl.register_idle_function | ||
85 | |||
86 | def saveConnectionDetails(self): | ||
87 | return | ||
88 | |||
89 | def detach(self): | ||
90 | return | ||
91 | |||
92 | def establishConnection(self, featureset): | ||
93 | raise "Must redefine the %s.establishConnection()" % self.__class__.__name__ | ||
94 | |||
95 | def endSession(self): | ||
96 | self.connection.terminate() | ||
diff --git a/bitbake/lib/bb/server/process.py b/bitbake/lib/bb/server/process.py new file mode 100644 index 0000000000..302ee5fc8a --- /dev/null +++ b/bitbake/lib/bb/server/process.py | |||
@@ -0,0 +1,252 @@ | |||
1 | # | ||
2 | # BitBake Process based server. | ||
3 | # | ||
4 | # Copyright (C) 2010 Bob Foerster <robert@erafx.com> | ||
5 | # | ||
6 | # This program is free software; you can redistribute it and/or modify | ||
7 | # it under the terms of the GNU General Public License version 2 as | ||
8 | # published by the Free Software Foundation. | ||
9 | # | ||
10 | # This program is distributed in the hope that it will be useful, | ||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | # GNU General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU General Public License along | ||
16 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | |||
19 | """ | ||
20 | This module implements a multiprocessing.Process based server for bitbake. | ||
21 | """ | ||
22 | |||
23 | import bb | ||
24 | import bb.event | ||
25 | import itertools | ||
26 | import logging | ||
27 | import multiprocessing | ||
28 | import os | ||
29 | import signal | ||
30 | import sys | ||
31 | import time | ||
32 | import select | ||
33 | from Queue import Empty | ||
34 | from multiprocessing import Event, Process, util, Queue, Pipe, queues, Manager | ||
35 | |||
36 | from . import BitBakeBaseServer, BitBakeBaseServerConnection, BaseImplServer | ||
37 | |||
38 | logger = logging.getLogger('BitBake') | ||
39 | |||
40 | class ServerCommunicator(): | ||
41 | def __init__(self, connection, event_handle, server): | ||
42 | self.connection = connection | ||
43 | self.event_handle = event_handle | ||
44 | self.server = server | ||
45 | |||
46 | def runCommand(self, command): | ||
47 | # @todo try/except | ||
48 | self.connection.send(command) | ||
49 | |||
50 | if not self.server.is_alive(): | ||
51 | raise SystemExit | ||
52 | |||
53 | while True: | ||
54 | # don't let the user ctrl-c while we're waiting for a response | ||
55 | try: | ||
56 | if self.connection.poll(20): | ||
57 | return self.connection.recv() | ||
58 | else: | ||
59 | bb.fatal("Timeout while attempting to communicate with bitbake server") | ||
60 | except KeyboardInterrupt: | ||
61 | pass | ||
62 | |||
63 | def getEventHandle(self): | ||
64 | return self.event_handle.value | ||
65 | |||
66 | class EventAdapter(): | ||
67 | """ | ||
68 | Adapter to wrap our event queue since the caller (bb.event) expects to | ||
69 | call a send() method, but our actual queue only has put() | ||
70 | """ | ||
71 | def __init__(self, queue): | ||
72 | self.queue = queue | ||
73 | |||
74 | def send(self, event): | ||
75 | try: | ||
76 | self.queue.put(event) | ||
77 | except Exception as err: | ||
78 | print("EventAdapter puked: %s" % str(err)) | ||
79 | |||
80 | |||
81 | class ProcessServer(Process, BaseImplServer): | ||
82 | profile_filename = "profile.log" | ||
83 | profile_processed_filename = "profile.log.processed" | ||
84 | |||
85 | def __init__(self, command_channel, event_queue, featurelist): | ||
86 | BaseImplServer.__init__(self) | ||
87 | Process.__init__(self) | ||
88 | self.command_channel = command_channel | ||
89 | self.event_queue = event_queue | ||
90 | self.event = EventAdapter(event_queue) | ||
91 | self.featurelist = featurelist | ||
92 | self.quit = False | ||
93 | |||
94 | self.quitin, self.quitout = Pipe() | ||
95 | self.event_handle = multiprocessing.Value("i") | ||
96 | |||
97 | def run(self): | ||
98 | for event in bb.event.ui_queue: | ||
99 | self.event_queue.put(event) | ||
100 | self.event_handle.value = bb.event.register_UIHhandler(self) | ||
101 | |||
102 | bb.cooker.server_main(self.cooker, self.main) | ||
103 | |||
104 | def main(self): | ||
105 | # Ignore SIGINT within the server, as all SIGINT handling is done by | ||
106 | # the UI and communicated to us | ||
107 | self.quitin.close() | ||
108 | signal.signal(signal.SIGINT, signal.SIG_IGN) | ||
109 | while not self.quit: | ||
110 | try: | ||
111 | if self.command_channel.poll(): | ||
112 | command = self.command_channel.recv() | ||
113 | self.runCommand(command) | ||
114 | if self.quitout.poll(): | ||
115 | self.quitout.recv() | ||
116 | self.quit = True | ||
117 | |||
118 | self.idle_commands(.1, [self.event_queue._reader, self.command_channel, self.quitout]) | ||
119 | except Exception: | ||
120 | logger.exception('Running command %s', command) | ||
121 | |||
122 | self.event_queue.close() | ||
123 | bb.event.unregister_UIHhandler(self.event_handle.value) | ||
124 | self.command_channel.close() | ||
125 | self.cooker.shutdown(True) | ||
126 | |||
127 | def idle_commands(self, delay, fds = []): | ||
128 | nextsleep = delay | ||
129 | |||
130 | for function, data in self._idlefuns.items(): | ||
131 | try: | ||
132 | retval = function(self, data, False) | ||
133 | if retval is False: | ||
134 | del self._idlefuns[function] | ||
135 | nextsleep = None | ||
136 | elif retval is True: | ||
137 | nextsleep = None | ||
138 | elif isinstance(retval, float): | ||
139 | if (retval < nextsleep): | ||
140 | nextsleep = retval | ||
141 | elif nextsleep is None: | ||
142 | continue | ||
143 | else: | ||
144 | fds = fds + retval | ||
145 | except SystemExit: | ||
146 | raise | ||
147 | except Exception: | ||
148 | logger.exception('Running idle function') | ||
149 | del self._idlefuns[function] | ||
150 | self.quit = True | ||
151 | |||
152 | if nextsleep is not None: | ||
153 | select.select(fds,[],[],nextsleep) | ||
154 | |||
155 | def runCommand(self, command): | ||
156 | """ | ||
157 | Run a cooker command on the server | ||
158 | """ | ||
159 | self.command_channel.send(self.cooker.command.runCommand(command)) | ||
160 | |||
161 | def stop(self): | ||
162 | self.quitin.send("quit") | ||
163 | self.quitin.close() | ||
164 | |||
165 | class BitBakeProcessServerConnection(BitBakeBaseServerConnection): | ||
166 | def __init__(self, serverImpl, ui_channel, event_queue): | ||
167 | self.procserver = serverImpl | ||
168 | self.ui_channel = ui_channel | ||
169 | self.event_queue = event_queue | ||
170 | self.connection = ServerCommunicator(self.ui_channel, self.procserver.event_handle, self.procserver) | ||
171 | self.events = self.event_queue | ||
172 | |||
173 | def sigterm_terminate(self): | ||
174 | bb.error("UI received SIGTERM") | ||
175 | self.terminate() | ||
176 | |||
177 | def terminate(self): | ||
178 | def flushevents(): | ||
179 | while True: | ||
180 | try: | ||
181 | event = self.event_queue.get(block=False) | ||
182 | except (Empty, IOError): | ||
183 | break | ||
184 | if isinstance(event, logging.LogRecord): | ||
185 | logger.handle(event) | ||
186 | |||
187 | signal.signal(signal.SIGINT, signal.SIG_IGN) | ||
188 | self.procserver.stop() | ||
189 | |||
190 | while self.procserver.is_alive(): | ||
191 | flushevents() | ||
192 | self.procserver.join(0.1) | ||
193 | |||
194 | self.ui_channel.close() | ||
195 | self.event_queue.close() | ||
196 | self.event_queue.setexit() | ||
197 | |||
198 | # Wrap Queue to provide API which isn't server implementation specific | ||
199 | class ProcessEventQueue(multiprocessing.queues.Queue): | ||
200 | def __init__(self, maxsize): | ||
201 | multiprocessing.queues.Queue.__init__(self, maxsize) | ||
202 | self.exit = False | ||
203 | |||
204 | def setexit(self): | ||
205 | self.exit = True | ||
206 | |||
207 | def waitEvent(self, timeout): | ||
208 | if self.exit: | ||
209 | sys.exit(1) | ||
210 | try: | ||
211 | if not self.server.is_alive(): | ||
212 | self.setexit() | ||
213 | return None | ||
214 | return self.get(True, timeout) | ||
215 | except Empty: | ||
216 | return None | ||
217 | |||
218 | def getEvent(self): | ||
219 | try: | ||
220 | if not self.server.is_alive(): | ||
221 | self.setexit() | ||
222 | return None | ||
223 | return self.get(False) | ||
224 | except Empty: | ||
225 | return None | ||
226 | |||
227 | |||
228 | class BitBakeServer(BitBakeBaseServer): | ||
229 | def initServer(self): | ||
230 | # establish communication channels. We use bidirectional pipes for | ||
231 | # ui <--> server command/response pairs | ||
232 | # and a queue for server -> ui event notifications | ||
233 | # | ||
234 | self.ui_channel, self.server_channel = Pipe() | ||
235 | self.event_queue = ProcessEventQueue(0) | ||
236 | self.serverImpl = ProcessServer(self.server_channel, self.event_queue, None) | ||
237 | self.event_queue.server = self.serverImpl | ||
238 | |||
239 | def detach(self): | ||
240 | self.serverImpl.start() | ||
241 | return | ||
242 | |||
243 | def establishConnection(self, featureset): | ||
244 | |||
245 | self.connection = BitBakeProcessServerConnection(self.serverImpl, self.ui_channel, self.event_queue) | ||
246 | |||
247 | _, error = self.connection.connection.runCommand(["setFeatures", featureset]) | ||
248 | if error: | ||
249 | logger.error("Unable to set the cooker to the correct featureset: %s" % error) | ||
250 | raise BaseException(error) | ||
251 | signal.signal(signal.SIGTERM, lambda i, s: self.connection.sigterm_terminate()) | ||
252 | return self.connection | ||
diff --git a/bitbake/lib/bb/server/xmlrpc.py b/bitbake/lib/bb/server/xmlrpc.py new file mode 100644 index 0000000000..10d4b5c779 --- /dev/null +++ b/bitbake/lib/bb/server/xmlrpc.py | |||
@@ -0,0 +1,383 @@ | |||
1 | # | ||
2 | # BitBake XMLRPC Server | ||
3 | # | ||
4 | # Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer | ||
5 | # Copyright (C) 2006 - 2008 Richard Purdie | ||
6 | # | ||
7 | # This program is free software; you can redistribute it and/or modify | ||
8 | # it under the terms of the GNU General Public License version 2 as | ||
9 | # published by the Free Software Foundation. | ||
10 | # | ||
11 | # This program is distributed in the hope that it will be useful, | ||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | # GNU General Public License for more details. | ||
15 | # | ||
16 | # You should have received a copy of the GNU General Public License along | ||
17 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | |||
20 | """ | ||
21 | This module implements an xmlrpc server for BitBake. | ||
22 | |||
23 | Use this by deriving a class from BitBakeXMLRPCServer and then adding | ||
24 | methods which you want to "export" via XMLRPC. If the methods have the | ||
25 | prefix xmlrpc_, then registering those function will happen automatically, | ||
26 | if not, you need to call register_function. | ||
27 | |||
28 | Use register_idle_function() to add a function which the xmlrpc server | ||
29 | calls from within server_forever when no requests are pending. Make sure | ||
30 | that those functions are non-blocking or else you will introduce latency | ||
31 | in the server's main loop. | ||
32 | """ | ||
33 | |||
34 | import bb | ||
35 | import xmlrpclib, sys | ||
36 | from bb import daemonize | ||
37 | from bb.ui import uievent | ||
38 | import hashlib, time | ||
39 | import socket | ||
40 | import os, signal | ||
41 | import threading | ||
42 | try: | ||
43 | import cPickle as pickle | ||
44 | except ImportError: | ||
45 | import pickle | ||
46 | |||
47 | DEBUG = False | ||
48 | |||
49 | from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler | ||
50 | import inspect, select, httplib | ||
51 | |||
52 | from . import BitBakeBaseServer, BitBakeBaseServerConnection, BaseImplServer | ||
53 | |||
54 | class BBTransport(xmlrpclib.Transport): | ||
55 | def __init__(self, timeout): | ||
56 | self.timeout = timeout | ||
57 | self.connection_token = None | ||
58 | xmlrpclib.Transport.__init__(self) | ||
59 | |||
60 | # Modified from default to pass timeout to HTTPConnection | ||
61 | def make_connection(self, host): | ||
62 | #return an existing connection if possible. This allows | ||
63 | #HTTP/1.1 keep-alive. | ||
64 | if self._connection and host == self._connection[0]: | ||
65 | return self._connection[1] | ||
66 | |||
67 | # create a HTTP connection object from a host descriptor | ||
68 | chost, self._extra_headers, x509 = self.get_host_info(host) | ||
69 | #store the host argument along with the connection object | ||
70 | self._connection = host, httplib.HTTPConnection(chost, timeout=self.timeout) | ||
71 | return self._connection[1] | ||
72 | |||
73 | def set_connection_token(self, token): | ||
74 | self.connection_token = token | ||
75 | |||
76 | def send_content(self, h, body): | ||
77 | if self.connection_token: | ||
78 | h.putheader("Bitbake-token", self.connection_token) | ||
79 | xmlrpclib.Transport.send_content(self, h, body) | ||
80 | |||
81 | def _create_server(host, port, timeout = 60): | ||
82 | t = BBTransport(timeout) | ||
83 | s = xmlrpclib.ServerProxy("http://%s:%d/" % (host, port), transport=t, allow_none=True) | ||
84 | return s, t | ||
85 | |||
86 | class BitBakeServerCommands(): | ||
87 | |||
88 | def __init__(self, server): | ||
89 | self.server = server | ||
90 | self.has_client = False | ||
91 | |||
92 | def registerEventHandler(self, host, port): | ||
93 | """ | ||
94 | Register a remote UI Event Handler | ||
95 | """ | ||
96 | s, t = _create_server(host, port) | ||
97 | |||
98 | # we don't allow connections if the cooker is running | ||
99 | if (self.cooker.state in [bb.cooker.state.parsing, bb.cooker.state.running]): | ||
100 | return None | ||
101 | |||
102 | self.event_handle = bb.event.register_UIHhandler(s) | ||
103 | return self.event_handle | ||
104 | |||
105 | def unregisterEventHandler(self, handlerNum): | ||
106 | """ | ||
107 | Unregister a remote UI Event Handler | ||
108 | """ | ||
109 | return bb.event.unregister_UIHhandler(handlerNum) | ||
110 | |||
111 | def runCommand(self, command): | ||
112 | """ | ||
113 | Run a cooker command on the server | ||
114 | """ | ||
115 | return self.cooker.command.runCommand(command, self.server.readonly) | ||
116 | |||
117 | def getEventHandle(self): | ||
118 | return self.event_handle | ||
119 | |||
120 | def terminateServer(self): | ||
121 | """ | ||
122 | Trigger the server to quit | ||
123 | """ | ||
124 | self.server.quit = True | ||
125 | print("Server (cooker) exiting") | ||
126 | return | ||
127 | |||
128 | def addClient(self): | ||
129 | if self.has_client: | ||
130 | return None | ||
131 | token = hashlib.md5(str(time.time())).hexdigest() | ||
132 | self.server.set_connection_token(token) | ||
133 | self.has_client = True | ||
134 | return token | ||
135 | |||
136 | def removeClient(self): | ||
137 | if self.has_client: | ||
138 | self.server.set_connection_token(None) | ||
139 | self.has_client = False | ||
140 | if self.server.single_use: | ||
141 | self.server.quit = True | ||
142 | |||
143 | # This request handler checks if the request has a "Bitbake-token" header | ||
144 | # field (this comes from the client side) and compares it with its internal | ||
145 | # "Bitbake-token" field (this comes from the server). If the two are not | ||
146 | # equal, it is assumed that a client is trying to connect to the server | ||
147 | # while another client is connected to the server. In this case, a 503 error | ||
148 | # ("service unavailable") is returned to the client. | ||
149 | class BitBakeXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): | ||
150 | def __init__(self, request, client_address, server): | ||
151 | self.server = server | ||
152 | SimpleXMLRPCRequestHandler.__init__(self, request, client_address, server) | ||
153 | |||
154 | def do_POST(self): | ||
155 | try: | ||
156 | remote_token = self.headers["Bitbake-token"] | ||
157 | except: | ||
158 | remote_token = None | ||
159 | if remote_token != self.server.connection_token and remote_token != "observer": | ||
160 | self.report_503() | ||
161 | else: | ||
162 | if remote_token == "observer": | ||
163 | self.server.readonly = True | ||
164 | else: | ||
165 | self.server.readonly = False | ||
166 | SimpleXMLRPCRequestHandler.do_POST(self) | ||
167 | |||
168 | def report_503(self): | ||
169 | self.send_response(503) | ||
170 | response = 'No more client allowed' | ||
171 | self.send_header("Content-type", "text/plain") | ||
172 | self.send_header("Content-length", str(len(response))) | ||
173 | self.end_headers() | ||
174 | self.wfile.write(response) | ||
175 | |||
176 | |||
177 | class XMLRPCProxyServer(BaseImplServer): | ||
178 | """ not a real working server, but a stub for a proxy server connection | ||
179 | |||
180 | """ | ||
181 | def __init__(self, host, port): | ||
182 | self.host = host | ||
183 | self.port = port | ||
184 | |||
185 | class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer): | ||
186 | # remove this when you're done with debugging | ||
187 | # allow_reuse_address = True | ||
188 | |||
189 | def __init__(self, interface): | ||
190 | """ | ||
191 | Constructor | ||
192 | """ | ||
193 | BaseImplServer.__init__(self) | ||
194 | if (interface[1] == 0): # anonymous port, not getting reused | ||
195 | self.single_use = True | ||
196 | # Use auto port configuration | ||
197 | if (interface[1] == -1): | ||
198 | interface = (interface[0], 0) | ||
199 | SimpleXMLRPCServer.__init__(self, interface, | ||
200 | requestHandler=BitBakeXMLRPCRequestHandler, | ||
201 | logRequests=False, allow_none=True) | ||
202 | self.host, self.port = self.socket.getsockname() | ||
203 | self.connection_token = None | ||
204 | #self.register_introspection_functions() | ||
205 | self.commands = BitBakeServerCommands(self) | ||
206 | self.autoregister_all_functions(self.commands, "") | ||
207 | self.interface = interface | ||
208 | self.single_use = False | ||
209 | |||
210 | def addcooker(self, cooker): | ||
211 | BaseImplServer.addcooker(self, cooker) | ||
212 | self.commands.cooker = cooker | ||
213 | |||
214 | def autoregister_all_functions(self, context, prefix): | ||
215 | """ | ||
216 | Convenience method for registering all functions in the scope | ||
217 | of this class that start with a common prefix | ||
218 | """ | ||
219 | methodlist = inspect.getmembers(context, inspect.ismethod) | ||
220 | for name, method in methodlist: | ||
221 | if name.startswith(prefix): | ||
222 | self.register_function(method, name[len(prefix):]) | ||
223 | |||
224 | |||
225 | def serve_forever(self): | ||
226 | # Start the actual XMLRPC server | ||
227 | bb.cooker.server_main(self.cooker, self._serve_forever) | ||
228 | |||
229 | def _serve_forever(self): | ||
230 | """ | ||
231 | Serve Requests. Overloaded to honor a quit command | ||
232 | """ | ||
233 | self.quit = False | ||
234 | while not self.quit: | ||
235 | fds = [self] | ||
236 | nextsleep = 0.1 | ||
237 | for function, data in self._idlefuns.items(): | ||
238 | try: | ||
239 | retval = function(self, data, False) | ||
240 | if retval is False: | ||
241 | del self._idlefuns[function] | ||
242 | elif retval is True: | ||
243 | nextsleep = 0 | ||
244 | elif isinstance(retval, float): | ||
245 | if (retval < nextsleep): | ||
246 | nextsleep = retval | ||
247 | else: | ||
248 | fds = fds + retval | ||
249 | except SystemExit: | ||
250 | raise | ||
251 | except: | ||
252 | import traceback | ||
253 | traceback.print_exc() | ||
254 | pass | ||
255 | |||
256 | socktimeout = self.socket.gettimeout() or nextsleep | ||
257 | socktimeout = min(socktimeout, nextsleep) | ||
258 | # Mirror what BaseServer handle_request would do | ||
259 | try: | ||
260 | fd_sets = select.select(fds, [], [], socktimeout) | ||
261 | if fd_sets[0] and self in fd_sets[0]: | ||
262 | self._handle_request_noblock() | ||
263 | except IOError: | ||
264 | # we ignore interrupted calls | ||
265 | pass | ||
266 | |||
267 | # Tell idle functions we're exiting | ||
268 | for function, data in self._idlefuns.items(): | ||
269 | try: | ||
270 | retval = function(self, data, True) | ||
271 | except: | ||
272 | pass | ||
273 | self.server_close() | ||
274 | return | ||
275 | |||
276 | def set_connection_token(self, token): | ||
277 | self.connection_token = token | ||
278 | |||
279 | class BitBakeXMLRPCServerConnection(BitBakeBaseServerConnection): | ||
280 | def __init__(self, serverImpl, clientinfo=("localhost", 0), observer_only = False, featureset = []): | ||
281 | self.connection, self.transport = _create_server(serverImpl.host, serverImpl.port) | ||
282 | self.clientinfo = clientinfo | ||
283 | self.serverImpl = serverImpl | ||
284 | self.observer_only = observer_only | ||
285 | self.featureset = featureset | ||
286 | |||
287 | def connect(self, token = None): | ||
288 | if token is None: | ||
289 | if self.observer_only: | ||
290 | token = "observer" | ||
291 | else: | ||
292 | token = self.connection.addClient() | ||
293 | |||
294 | if token is None: | ||
295 | return None | ||
296 | |||
297 | self.transport.set_connection_token(token) | ||
298 | |||
299 | self.events = uievent.BBUIEventQueue(self.connection, self.clientinfo) | ||
300 | for event in bb.event.ui_queue: | ||
301 | self.events.queue_event(event) | ||
302 | |||
303 | _, error = self.connection.runCommand(["setFeatures", self.featureset]) | ||
304 | if error: | ||
305 | # no need to log it here, the error shall be sent to the client | ||
306 | raise BaseException(error) | ||
307 | |||
308 | return self | ||
309 | |||
310 | def removeClient(self): | ||
311 | if not self.observer_only: | ||
312 | self.connection.removeClient() | ||
313 | |||
314 | def terminate(self): | ||
315 | # Don't wait for server indefinitely | ||
316 | import socket | ||
317 | socket.setdefaulttimeout(2) | ||
318 | try: | ||
319 | self.events.system_quit() | ||
320 | except: | ||
321 | pass | ||
322 | try: | ||
323 | self.connection.removeClient() | ||
324 | except: | ||
325 | pass | ||
326 | |||
327 | class BitBakeServer(BitBakeBaseServer): | ||
328 | def initServer(self, interface = ("localhost", 0)): | ||
329 | self.interface = interface | ||
330 | self.serverImpl = XMLRPCServer(interface) | ||
331 | |||
332 | def detach(self): | ||
333 | daemonize.createDaemon(self.serverImpl.serve_forever, "bitbake-cookerdaemon.log") | ||
334 | del self.cooker | ||
335 | |||
336 | def establishConnection(self, featureset): | ||
337 | self.connection = BitBakeXMLRPCServerConnection(self.serverImpl, self.interface, False, featureset) | ||
338 | return self.connection.connect() | ||
339 | |||
340 | def set_connection_token(self, token): | ||
341 | self.connection.transport.set_connection_token(token) | ||
342 | |||
343 | class BitBakeXMLRPCClient(BitBakeBaseServer): | ||
344 | |||
345 | def __init__(self, observer_only = False, token = None): | ||
346 | self.token = token | ||
347 | |||
348 | self.observer_only = observer_only | ||
349 | # if we need extra caches, just tell the server to load them all | ||
350 | pass | ||
351 | |||
352 | def saveConnectionDetails(self, remote): | ||
353 | self.remote = remote | ||
354 | |||
355 | def establishConnection(self, featureset): | ||
356 | # The format of "remote" must be "server:port" | ||
357 | try: | ||
358 | [host, port] = self.remote.split(":") | ||
359 | port = int(port) | ||
360 | except Exception as e: | ||
361 | bb.warn("Failed to read remote definition (%s)" % str(e)) | ||
362 | raise e | ||
363 | |||
364 | # We need our IP for the server connection. We get the IP | ||
365 | # by trying to connect with the server | ||
366 | try: | ||
367 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
368 | s.connect((host, port)) | ||
369 | ip = s.getsockname()[0] | ||
370 | s.close() | ||
371 | except Exception as e: | ||
372 | bb.warn("Could not create socket for %s:%s (%s)" % (host, port, str(e))) | ||
373 | raise e | ||
374 | try: | ||
375 | self.serverImpl = XMLRPCProxyServer(host, port) | ||
376 | self.connection = BitBakeXMLRPCServerConnection(self.serverImpl, (ip, 0), self.observer_only, featureset) | ||
377 | return self.connection.connect(self.token) | ||
378 | except Exception as e: | ||
379 | bb.warn("Could not connect to server at %s:%s (%s)" % (host, port, str(e))) | ||
380 | raise e | ||
381 | |||
382 | def endSession(self): | ||
383 | self.connection.removeClient() | ||