Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer |
| 3 | # Copyright (C) 2006 - 2007 Richard Purdie |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
| 8 | """ |
| 9 | Use this class to fork off a thread to recieve event callbacks from the bitbake |
| 10 | server and queue them for the UI to process. This process must be used to avoid |
| 11 | client/server deadlocks. |
| 12 | """ |
| 13 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 14 | import socket, threading, pickle, collections |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | from xmlrpc.server import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
| 17 | class BBUIEventQueue: |
| 18 | def __init__(self, BBServer, clientinfo=("localhost, 0")): |
| 19 | |
| 20 | self.eventQueue = [] |
| 21 | self.eventQueueLock = threading.Lock() |
| 22 | self.eventQueueNotify = threading.Event() |
| 23 | |
| 24 | self.BBServer = BBServer |
| 25 | self.clientinfo = clientinfo |
| 26 | |
| 27 | server = UIXMLRPCServer(self.clientinfo) |
| 28 | self.host, self.port = server.socket.getsockname() |
| 29 | |
| 30 | server.register_function( self.system_quit, "event.quit" ) |
| 31 | server.register_function( self.send_event, "event.sendpickle" ) |
| 32 | server.socket.settimeout(1) |
| 33 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 34 | self.EventHandle = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | |
| 36 | # the event handler registration may fail here due to cooker being in invalid state |
| 37 | # this is a transient situation, and we should retry a couple of times before |
| 38 | # giving up |
| 39 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 40 | for count_tries in range(5): |
| 41 | ret = self.BBServer.registerEventHandler(self.host, self.port) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 43 | if isinstance(ret, collections.Iterable): |
| 44 | self.EventHandle, error = ret |
| 45 | else: |
| 46 | self.EventHandle = ret |
| 47 | error = "" |
| 48 | |
| 49 | if self.EventHandle != None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | break |
| 51 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 52 | errmsg = "Could not register UI event handler. Error: %s, host %s, "\ |
| 53 | "port %d" % (error, self.host, self.port) |
| 54 | bb.warn("%s, retry" % errmsg) |
| 55 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | import time |
| 57 | time.sleep(1) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 58 | else: |
| 59 | raise Exception(errmsg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
| 61 | self.server = server |
| 62 | |
| 63 | self.t = threading.Thread() |
| 64 | self.t.setDaemon(True) |
| 65 | self.t.run = self.startCallbackHandler |
| 66 | self.t.start() |
| 67 | |
| 68 | def getEvent(self): |
| 69 | |
| 70 | self.eventQueueLock.acquire() |
| 71 | |
| 72 | if len(self.eventQueue) == 0: |
| 73 | self.eventQueueLock.release() |
| 74 | return None |
| 75 | |
| 76 | item = self.eventQueue.pop(0) |
| 77 | |
| 78 | if len(self.eventQueue) == 0: |
| 79 | self.eventQueueNotify.clear() |
| 80 | |
| 81 | self.eventQueueLock.release() |
| 82 | return item |
| 83 | |
| 84 | def waitEvent(self, delay): |
| 85 | self.eventQueueNotify.wait(delay) |
| 86 | return self.getEvent() |
| 87 | |
| 88 | def queue_event(self, event): |
| 89 | self.eventQueueLock.acquire() |
| 90 | self.eventQueue.append(event) |
| 91 | self.eventQueueNotify.set() |
| 92 | self.eventQueueLock.release() |
| 93 | |
| 94 | def send_event(self, event): |
| 95 | self.queue_event(pickle.loads(event)) |
| 96 | |
| 97 | def startCallbackHandler(self): |
| 98 | |
| 99 | self.server.timeout = 1 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | bb.utils.set_process_name("UIEventQueue") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | while not self.server.quit: |
| 102 | try: |
| 103 | self.server.handle_request() |
| 104 | except Exception as e: |
| 105 | import traceback |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | logger.error("BBUIEventQueue.startCallbackHandler: Exception while trying to handle request: %s\n%s" % (e, traceback.format_exc())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | |
| 108 | self.server.server_close() |
| 109 | |
| 110 | def system_quit( self ): |
| 111 | """ |
| 112 | Shut down the callback thread |
| 113 | """ |
| 114 | try: |
| 115 | self.BBServer.unregisterEventHandler(self.EventHandle) |
| 116 | except: |
| 117 | pass |
| 118 | self.server.quit = True |
| 119 | |
| 120 | class UIXMLRPCServer (SimpleXMLRPCServer): |
| 121 | |
| 122 | def __init__( self, interface ): |
| 123 | self.quit = False |
| 124 | SimpleXMLRPCServer.__init__( self, |
| 125 | interface, |
| 126 | requestHandler=SimpleXMLRPCRequestHandler, |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | logRequests=False, allow_none=True, use_builtin_types=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | |
| 129 | def get_request(self): |
| 130 | while not self.quit: |
| 131 | try: |
| 132 | sock, addr = self.socket.accept() |
| 133 | sock.settimeout(1) |
| 134 | return (sock, addr) |
| 135 | except socket.timeout: |
| 136 | pass |
| 137 | return (None, None) |
| 138 | |
| 139 | def close_request(self, request): |
| 140 | if request is None: |
| 141 | return |
| 142 | SimpleXMLRPCServer.close_request(self, request) |
| 143 | |
| 144 | def process_request(self, request, client_address): |
| 145 | if request is None: |
| 146 | return |
| 147 | SimpleXMLRPCServer.process_request(self, request, client_address) |
| 148 | |