From e411a2738ded599de6dfa2510a996561438f217d Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Fri, 28 Mar 2014 04:50:46 -0700 Subject: [PATCH] Convert scanning_thread Queues to a Pipe Signed-off-by: Sean Robinson --- wifiradar/connections.py | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/wifiradar/connections.py b/wifiradar/connections.py index 28a20d5..83a55c9 100644 --- a/wifiradar/connections.py +++ b/wifiradar/connections.py @@ -66,21 +66,14 @@ def get_enc_mode(use_wpa, key): else: return 'wep' -def scanning_thread(config_manager, apQueue, commandQueue, exit_event): - """ Scan for a limited time and return AP names and bssid found. - Access points we find will be put on the outgoing Queue, apQueue. +def scanning_thread(config_manager, msg_pipe): + """ Scan for access point information and send a profile for each + on 'msg_pipe'. Parameters: 'config_manager' -- ConfigManager - Configuration Manager - - 'apQueue' -- Queue - Queue on which to put AP profiles - - 'commandQueue' -- Queue - Queue from which to read commands - - Returns: - - nothing + 'msg_pipe' -- Pipe - bi-directional pipe """ # Setup our essid pattern matcher essid_pattern = re.compile("ESSID\s*(:|=)\s*\"([^\"]+)\"", re.I | re.M | re.S) @@ -95,14 +88,22 @@ def scanning_thread(config_manager, apQueue, commandQueue, exit_event): command = "scan" while True: # check for exit call before trying to process - if exit_event.isSet(): - return - try: - command = commandQueue.get_nowait() - logger.info("received command: %s" % (command, )) - command_read = True - except queue.Empty: - command_read = False + if msg_pipe.poll(1): + try: + msg = msg_pipe.recv() + except (EOFError, IOError) as e: + # This is bad, really bad. + logger.critical('read on closed ' + + 'Pipe ({}), failing...'.format(rfd)) + raise misc.PipeError(e) + else: + if msg.topic == 'EXIT': + msg_pipe.close() + break + elif msg.topic == 'SCAN-START': + command = 'scan' + elif msg.topic == 'SCAN-STOP': + command = 'pause' try: device = config_manager.get_network_device() except misc.NoDeviceError as e: @@ -148,13 +149,7 @@ def scanning_thread(config_manager, apQueue, commandQueue, exit_event): for bssid in access_points: access_points[bssid]['available'] = (access_points[bssid]['signal'] > 0) # Put all, now or previously, sensed access_points into apQueue - try: - apQueue.put_nowait(access_points[bssid]) - except queue.Full: - pass - if command_read: - commandQueue.task_done() - sleep(1) + msg_pipe.send(Message('ACCESSPOINT', access_points[bssid])) class ConnectionManager(object): -- 2.11.4.GIT