summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/ui/uievent.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/ui/uievent.py')
-rw-r--r--bitbake/lib/bb/ui/uievent.py133
1 files changed, 133 insertions, 0 deletions
diff --git a/bitbake/lib/bb/ui/uievent.py b/bitbake/lib/bb/ui/uievent.py
new file mode 100644
index 0000000..98658f6
--- /dev/null
+++ b/bitbake/lib/bb/ui/uievent.py
@@ -0,0 +1,133 @@
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3#
4# Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer
5# Copyright (C) 2006 - 2007 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"""
22Use this class to fork off a thread to recieve event callbacks from the bitbake
23server and queue them for the UI to process. This process must be used to avoid
24client/server deadlocks.
25"""
26
27import socket, threading, pickle
28from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
29
30class BBUIEventQueue:
31 def __init__(self, BBServer, clientinfo=("localhost, 0")):
32
33 self.eventQueue = []
34 self.eventQueueLock = threading.Lock()
35 self.eventQueueNotify = threading.Event()
36
37 self.BBServer = BBServer
38 self.clientinfo = clientinfo
39
40 server = UIXMLRPCServer(self.clientinfo)
41 self.host, self.port = server.socket.getsockname()
42
43 server.register_function( self.system_quit, "event.quit" )
44 server.register_function( self.send_event, "event.sendpickle" )
45 server.socket.settimeout(1)
46
47 self.EventHandle = self.BBServer.registerEventHandler(self.host, self.port)
48
49 if (self.EventHandle == None):
50 bb.fatal("Could not register UI event handler")
51
52 self.server = server
53
54 self.t = threading.Thread()
55 self.t.setDaemon(True)
56 self.t.run = self.startCallbackHandler
57 self.t.start()
58
59 def getEvent(self):
60
61 self.eventQueueLock.acquire()
62
63 if len(self.eventQueue) == 0:
64 self.eventQueueLock.release()
65 return None
66
67 item = self.eventQueue.pop(0)
68
69 if len(self.eventQueue) == 0:
70 self.eventQueueNotify.clear()
71
72 self.eventQueueLock.release()
73 return item
74
75 def waitEvent(self, delay):
76 self.eventQueueNotify.wait(delay)
77 return self.getEvent()
78
79 def queue_event(self, event):
80 self.eventQueueLock.acquire()
81 self.eventQueue.append(event)
82 self.eventQueueNotify.set()
83 self.eventQueueLock.release()
84
85 def send_event(self, event):
86 self.queue_event(pickle.loads(event))
87
88 def startCallbackHandler(self):
89
90 self.server.timeout = 1
91 while not self.server.quit:
92 self.server.handle_request()
93 self.server.server_close()
94
95 def system_quit( self ):
96 """
97 Shut down the callback thread
98 """
99 try:
100 self.BBServer.unregisterEventHandler(self.EventHandle)
101 except:
102 pass
103 self.server.quit = True
104
105class UIXMLRPCServer (SimpleXMLRPCServer):
106
107 def __init__( self, interface ):
108 self.quit = False
109 SimpleXMLRPCServer.__init__( self,
110 interface,
111 requestHandler=SimpleXMLRPCRequestHandler,
112 logRequests=False, allow_none=True)
113
114 def get_request(self):
115 while not self.quit:
116 try:
117 sock, addr = self.socket.accept()
118 sock.settimeout(1)
119 return (sock, addr)
120 except socket.timeout:
121 pass
122 return (None, None)
123
124 def close_request(self, request):
125 if request is None:
126 return
127 SimpleXMLRPCServer.close_request(self, request)
128
129 def process_request(self, request, client_address):
130 if request is None:
131 return
132 SimpleXMLRPCServer.process_request(self, request, client_address)
133