| 1 | #! /usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | ''' |
|---|
| 4 | /**************************************************************************** |
|---|
| 5 | |
|---|
| 6 | Copyright 2007 Virginia Polytechnic Institute and State University |
|---|
| 7 | |
|---|
| 8 | This file is part of the OSSIE USRP_Commander_GUI. |
|---|
| 9 | |
|---|
| 10 | OSSIE USRP_Commander_GUI is free software; you can redistribute it and/or modify |
|---|
| 11 | it under the terms of the GNU General Public License as published by |
|---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | (at your option) any later version. |
|---|
| 14 | |
|---|
| 15 | OSSIE USRP_Commander_GUI is distributed in the hope that it will be useful, |
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | GNU General Public License for more details. |
|---|
| 19 | |
|---|
| 20 | You should have received a copy of the GNU General Public License |
|---|
| 21 | along with OSSIE USRP_Commander_GUI; if not, write to the Free Software |
|---|
| 22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | |
|---|
| 24 | ****************************************************************************/ |
|---|
| 25 | |
|---|
| 26 | ''' |
|---|
| 27 | |
|---|
| 28 | #!/usr/bin/env python |
|---|
| 29 | import threading |
|---|
| 30 | |
|---|
| 31 | class txWorkClass: |
|---|
| 32 | """This class provides a place for the main processing of the |
|---|
| 33 | component to reside.""" |
|---|
| 34 | |
|---|
| 35 | def __init__(self, rc2007_gui_ref): |
|---|
| 36 | '''Initialization. Sets a reference to parent. |
|---|
| 37 | Initializes the buffer. Starts the Process data |
|---|
| 38 | thread, which waits for data to be added to the buffer''' |
|---|
| 39 | |
|---|
| 40 | self.rc2007_gui_ref = rc2007_gui_ref |
|---|
| 41 | |
|---|
| 42 | self.voice_data_queue = [] |
|---|
| 43 | self.voice_data_queue_lock = threading.Lock() |
|---|
| 44 | self.voice_data_signal = threading.Event() |
|---|
| 45 | |
|---|
| 46 | self.is_running = True |
|---|
| 47 | |
|---|
| 48 | self.process_thread = threading.Thread(target=self.Process) |
|---|
| 49 | self.process_thread.start() |
|---|
| 50 | |
|---|
| 51 | def __del__(self): |
|---|
| 52 | '''Destructor''' |
|---|
| 53 | pass |
|---|
| 54 | |
|---|
| 55 | def AddVoiceData(self, data): |
|---|
| 56 | '''Generally called by parent. Adds data to a buffer. |
|---|
| 57 | The Process() method will wait for this buffer to be set. |
|---|
| 58 | ''' |
|---|
| 59 | self.voice_data_queue_lock.acquire() |
|---|
| 60 | self.voice_data_queue.insert(0, data) |
|---|
| 61 | self.voice_data_queue_lock.release() |
|---|
| 62 | self.voice_data_signal.set() |
|---|
| 63 | |
|---|
| 64 | def SendTextData(self,text): |
|---|
| 65 | # TODO: pack/depack |
|---|
| 66 | # TODO: add metadata |
|---|
| 67 | # TODO: text should be data in send_data method |
|---|
| 68 | self.rc2007_gui_ref.to_radio_port_servant.send_data(text) |
|---|
| 69 | |
|---|
| 70 | def Release(self): |
|---|
| 71 | self.is_running = False |
|---|
| 72 | self.voice_data_signal.set() |
|---|
| 73 | |
|---|
| 74 | def Process(self): |
|---|
| 75 | while self.is_running: |
|---|
| 76 | self.voice_data_signal.wait() # wait for data to be aded to the |
|---|
| 77 | # buffer in self.AddVoiceData() |
|---|
| 78 | while len(self.voice_data_queue) > 0: |
|---|
| 79 | # get the data from the buffer: |
|---|
| 80 | self.voice_data_queue_lock.acquire() |
|---|
| 81 | new_data = self.voice_data_queue.pop() |
|---|
| 82 | self.voice_data_queue_lock.release() |
|---|
| 83 | |
|---|
| 84 | # forwarding voice data: |
|---|
| 85 | if self.rc2007_gui_ref.talk_flag: |
|---|
| 86 | if self.rc2007_gui_ref.to_radio_port_active: |
|---|
| 87 | # TODO: send voice metadata |
|---|
| 88 | self.rc2007_gui_ref.to_radio_port_servant.send_data(data) |
|---|
| 89 | |
|---|
| 90 | self.voice_data_signal.clear() # done reading the buffer |
|---|
| 91 | |
|---|
| 92 | class rxWorkClass: |
|---|
| 93 | """This class provides a place for the main processing of the |
|---|
| 94 | component to reside.""" |
|---|
| 95 | |
|---|
| 96 | def __init__(self, rc2007_gui_ref): |
|---|
| 97 | '''Initialization. Sets a reference to parent. |
|---|
| 98 | Initializes the buffer. Starts the Process data |
|---|
| 99 | thread, which waits for data to be added to the buffer''' |
|---|
| 100 | |
|---|
| 101 | self.rc2007_gui_ref = rc2007_gui_ref |
|---|
| 102 | |
|---|
| 103 | self.voice_data_queue = [] |
|---|
| 104 | self.voice_data_queue_lock = threading.Lock() |
|---|
| 105 | self.voice_data_signal = threading.Event() |
|---|
| 106 | |
|---|
| 107 | self.is_running = True |
|---|
| 108 | |
|---|
| 109 | self.process_thread = threading.Thread(target=self.Process) |
|---|
| 110 | self.process_thread.start() |
|---|
| 111 | |
|---|
| 112 | def __del__(self): |
|---|
| 113 | '''Destructor''' |
|---|
| 114 | pass |
|---|
| 115 | |
|---|
| 116 | def AddMetaData(self, data): |
|---|
| 117 | '''Generally called by parent. Adds data to a buffer. |
|---|
| 118 | The Process() method will wait for this buffer to be set. |
|---|
| 119 | ''' |
|---|
| 120 | self.data_queue_lock.acquire() |
|---|
| 121 | self.data_queue.insert(0, data) |
|---|
| 122 | self.data_queue_lock.release() |
|---|
| 123 | self.data_signal.set() |
|---|
| 124 | |
|---|
| 125 | def forwardVoiceData(self, data) |
|---|
| 126 | self.rc2007_gui_ref.to_CVSD_port_servant.send_data(data) |
|---|
| 127 | |
|---|
| 128 | def Release(self): |
|---|
| 129 | self.is_running = False |
|---|
| 130 | self.data_signal.set() |
|---|
| 131 | |
|---|
| 132 | def Process(self): |
|---|
| 133 | while self.is_running: |
|---|
| 134 | self.data_signal.wait() # wait for data to be aded to the |
|---|
| 135 | # buffer in self.AddVoiceData() |
|---|
| 136 | while len(self.voice_data_queue) > 0: |
|---|
| 137 | # get the data from the buffer: |
|---|
| 138 | self.data_queue_lock.acquire() |
|---|
| 139 | new_data = self.data_queue.pop() |
|---|
| 140 | self.data_queue_lock.release() |
|---|
| 141 | |
|---|
| 142 | voice = False |
|---|
| 143 | text = False |
|---|
| 144 | # TODO: look at the metadata and decide if it's voice or text |
|---|
| 145 | # forwarding voice data: |
|---|
| 146 | if voice: |
|---|
| 147 | if self.rc2007_gui_ref.to_CVSD_port_active: |
|---|
| 148 | # TODO: remove metadata? |
|---|
| 149 | self.rc2007_gui_ref.to_CVSD_port_servant.send_data(data) |
|---|
| 150 | if text: |
|---|
| 151 | # TODO: update the display |
|---|
| 152 | pass |
|---|
| 153 | |
|---|
| 154 | self.data_signal.clear() # done reading the buffer |
|---|
| 155 | |
|---|