forked from RedhawkSDR/rest-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain.py
More file actions
222 lines (176 loc) · 6.76 KB
/
domain.py
File metadata and controls
222 lines (176 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#
# This file is protected by Copyright. Please refer to the COPYRIGHT file
# distributed with this source distribution.
#
# This file is part of REDHAWK rest-python.
#
# REDHAWK rest-python is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# REDHAWK rest-python is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
"""
REDHAWK Helper class used by the Server Handlers
"""
import logging
from ossie.utils import redhawk
from ossie.utils.redhawk.channels import ODMListener
import traceback
def scan_domains():
return redhawk.scan()
class ResourceNotFound(Exception):
def __init__(self, resource='resource', name='Unknown'):
self.name = name
self.resource = resource
def __str__(self):
return "Unable to find %s '%s'" % (self.resource, self.name)
class WaveformLaunchError(Exception):
def __init__(self, name='Unknown', msg=''):
self.name = name
self.msg = msg
def __str__(self):
return "Not able to launch waveform '%s'. %s" % (self.name, self.msg)
class ApplicationReleaseError(Exception):
def __init__(self, name='Unknown', msg=''):
self.name = name
self.msg = msg
def __str__(self):
return "Not able to release waveform '%s'. %s" % (self.name, self.msg)
class Domain:
domMgr_ptr = None
odmListener = None
eventHandlers = []
name = None
def __init__(self, domainname):
logging.trace("Estasblishing domain %s", domainname, exc_info=True)
self.name = domainname
try:
self._establish_domain()
except StandardError, e:
logging.warn("Unable to find domain %s", e, exc_info=1)
raise ResourceNotFound("domain", domainname)
def _odm_response(self, event):
for eventH in self.eventHandlers:
eventH.event_queue.put(event)
def _connect_odm_listener(self):
self.odmListener = ODMListener()
self.odmListener.connect(self.domMgr_ptr)
self.odmListener.deviceManagerAdded.addListener(self._odm_response)
self.odmListener.deviceManagerRemoved.addListener(self._odm_response)
self.odmListener.applicationAdded.addListener(self._odm_response)
self.odmListener.applicationRemoved.addListener(self._odm_response)
def _establish_domain(self):
redhawk.setTrackApps(False)
self.domMgr_ptr = redhawk.attach(str(self.name))
self.domMgr_ptr.__odmListener = None
self._connect_odm_listener()
def properties(self):
props = self.domMgr_ptr.query([]) # TODO: self.domMgr_ptr._properties
return props
def get_domain_info(self):
if self.domMgr_ptr:
return self.domMgr_ptr
raise ResourceNotFound('domain', self.name)
def find_app(self, app_id=None):
_dom = self.get_domain_info()
apps = _dom.apps
if not app_id:
return apps
for app in apps:
if app._get_identifier() == app_id:
return app
raise ResourceNotFound('application', app_id)
def find_component(self, app_id, comp_id=None):
app = self.find_app(app_id)
if not comp_id:
return app.comps
for comp in app.comps:
if comp._id == comp_id:
return comp
raise ResourceNotFound('component', comp_id)
def find_device_manager(self, device_manager_id=None):
_dom = self.get_domain_info()
if not device_manager_id:
return _dom.devMgrs
for dev_mgr in _dom.devMgrs:
if dev_mgr.id == device_manager_id:
return dev_mgr
raise ResourceNotFound('device manager', device_manager_id)
def find_device(self, device_manager_id, device_id=None):
dev_mgr = self.find_device_manager(device_manager_id)
if not device_id:
return dev_mgr.devs
for dev in dev_mgr.devs:
if dev._id == device_id:
return dev
raise ResourceNotFound('device', device_id)
def find_service(self, device_manager_id, service_id=None):
dev_mgr = self.find_device_manager(device_manager_id)
if not service_id:
return dev_mgr.services
for svc in dev_mgr.services:
if svc.id == service_id:
return svc
raise ResourceNotFound('service', service_id)
def apps(self):
apps_dict = []
apps = self.find_app()
for app in apps:
apps_dict.append({'name': app.name, 'id': app._get_identifier()})
return apps_dict
def components(self, app_id):
comps_dict = []
comps = self.find_component(app_id)
for comp in comps:
comps_dict.append({'name': comp.name, 'id': comp._get_identifier()})
return comps_dict
def launch(self, app_name):
_dom = self.get_domain_info()
try:
app = _dom.createApplication(app_name)
return app._get_identifier()
except Exception, e:
raise WaveformLaunchError(app_name, str(e))
def release(self, app_id):
app = self.find_app(app_id)
try:
app.releaseObject()
return app_id
except Exception, e:
raise ApplicationReleaseError(app_id, str(e))
def available_apps(self):
_dom = self.get_domain_info()
sads_full_path = _dom.catalogSads()
sads = _dom._sads
sad_ret = []
for idx in range(len(sads)):
sad_ret.append({'name': sads[idx], 'sad': sads_full_path[idx]})
return sad_ret
def device_managers(self):
if self.domMgr_ptr is None:
raise ResourceNotFound('domain', self.name)
dev_mgrs = self.domMgr_ptr.devMgrs
dev_mgrs_dict = []
for dev_mgr in dev_mgrs:
dev_mgrs_dict.append({'name': dev_mgr.name, 'id': dev_mgr.id})
return dev_mgrs_dict
def devices(self, dev_mgr_id):
devs = self.find_device(dev_mgr_id)
ret_dict = []
for dev in devs:
ret_dict.append({'name': dev.name, 'id': dev._id})
return ret_dict
def services(self, dev_mgr_id):
svcs = self.find_service(dev_mgr_id)
ret_dict = []
for svc in svcs:
ret_dict.append({'name': svc._instanceName, 'id': svc._refid})
return ret_dict