root/ossiedev/branches/0.7.x/tools/wavedash/src/WavedashUtils.py @ 9211

Revision 9211, 15.9 KB (checked in by mcarrick, 5 years ago)

merging deepan's branch: adding wavedash tool, updating query / configure methods for components

Line 
1## Copyright 2005, 2006, 2007, 2008 Virginia Polytechnic Institute and State University
2##
3## This file is part of the OSSIE Waveform Application Visualization Environment
4##
5## WaveDash is free software; you can redistribute it and/or modify
6## it under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 2 of the License, or
8## (at your option) any later version.
9##
10## WaveDash is distributed in the hope that it will be useful, but WITHOUT ANY
11## WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13## GNU General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with OSSIE WaveDash; if not, write to the Free Software
17## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19from ossie.cf import CF
20from omniORB import CORBA
21import sys
22import CosNaming
23import WaveDev.wavedev.importResource as importResource
24import wx
25import xml.dom.minidom
26from xml.dom.minidom import Node
27
28[NON_FATAL, FATAL, INFO] = [1,2,3]
29[SUCCESS, FAILURE] = [True, False]
30
31def showMessage(msg, sev):
32    if (sev == FATAL or sev == NON_FATAL ):
33        dlg = wx.MessageDialog(None, msg, 'Error', wx.OK | wx.ICON_ERROR)
34    elif sev == INFO:
35        dlg = wx.MessageDialog(None, msg, 'Info', wx.OK | wx.ICON_INFORMATION)
36       
37    try:
38       dlg.ShowModal()
39    finally:
40       dlg.Destroy()
41    if sev == FATAL:
42        sys.exit()
43    return
44
45class ConfigureWidget(wx.Dialog):
46    """ This utility class launches a dialog box to configure the min and max values of
47    a slider or spinctrl"""
48    def __init__(self, parent, title):
49        wx.Dialog.__init__(self, parent, -1, title, size = (250,200))
50        self.minValue = 0
51        self.maxValue = 10000
52       
53        panel = wx.Panel(self, -1)
54        vbox = wx.BoxSizer(wx.VERTICAL)
55        #wx.StaticBox(panel, -1, 'Colors', (5, 5), (240, 150))
56        minHBox = wx.BoxSizer(wx.HORIZONTAL)
57        minLabel = wx.StaticText(self, -1, 'Min Value', name = 'minValue')
58        self.minText = wx.TextCtrl(self, -1, str(self.minValue))
59        minHBox.Add(minLabel, 1, wx.ALIGN_LEFT | wx.TOP | wx.BOTTOM, 5)
60        minHBox.Add(self.minText, 1, wx.ALIGN_RIGHT | wx.TOP | wx.BOTTOM, 5)
61       
62        maxHBox = wx.BoxSizer(wx.HORIZONTAL)
63        maxLabel = wx.StaticText(self, -1, 'Max Value', name = 'maxValue')
64        self.maxText = wx.TextCtrl(self, -1, str(self.maxValue))
65        maxHBox.Add(maxLabel, 1, wx.ALIGN_LEFT | wx.TOP | wx.BOTTOM, 5)
66        maxHBox.Add(self.maxText, 1, wx.ALIGN_RIGHT | wx.TOP | wx.BOTTOM, 5)
67
68        btnHBox = wx.BoxSizer(wx.HORIZONTAL)
69        OkBtn = wx.Button(self, -1, 'Ok')
70        CancelBtn = wx.Button(self, -1, 'Cancel')
71        self.Bind(wx.EVT_BUTTON, self.OnOk, OkBtn)
72        self.Bind(wx.EVT_BUTTON, self.OnCancel, CancelBtn)
73       
74        btnHBox.Add(OkBtn, 1,  wx.ALIGN_LEFT | wx.TOP | wx.BOTTOM, 10)
75        btnHBox.AddSpacer((10,10))
76        btnHBox.Add(CancelBtn, 1, wx.ALIGN_RIGHT | wx.TOP | wx.BOTTOM, 10)
77       
78        vbox.Add(minHBox, 1, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 5)
79        vbox.Add(maxHBox, 1,  wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 5)
80        vbox.Add(btnHBox, 1, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM , 5)
81       
82        self.SetSizer(vbox)
83       
84   
85    def OnOk(self, event):
86        try:
87            self.minValue = int(self.minText.GetValue())
88            self.maxValue = int(self.maxText.GetValue())
89            self.Destroy()
90        except(ValueError):
91            msg = 'Invalid values. Please enter only numeric(integer) values'
92            showMessage(msg, NON_FATAL)
93   
94    def OnCancel(self, event):
95        self.Destroy()
96   
97    def getMin(self):
98        #return int(self.minText.GetValue())
99        return self.minValue
100   
101    def getMax(self):
102        #return int(self.maxText.GetValue())
103        return self.maxValue
104   
105    def setMin(self, newVal):
106        self.minText.SetValue(str(newVal))
107   
108    def setMax(self, newVal):
109        self.maxText.SetValue(str(newVal))
110       
111
112class WaveAppStatusBar(wx.StatusBar):
113    def __init__(self, parent):
114        wx.StatusBar.__init__(self, parent)
115        self.SetFieldsCount(2)
116        #self.SetStatusText("Welcome to OSSIE WaveApp", 0)
117        self.SetStatusWidths([-5,-1])
118             
119        startIconPath = "../resources/start.png"
120        stopIconPath = "../resources/stop.png"
121        self.startIcon = wx.Image(startIconPath, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
122        self.stopIcon = wx.Image(stopIconPath, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
123       
124        self.startBtn = wx.BitmapButton(self, -1, self.startIcon)
125        self.uninstall = wx.Button(self, -1, 'U')
126       
127        self.startBtn.SetSize((23,23))
128        self.uninstall.SetSize((23,23))
129       
130        self.Bind(wx.EVT_SIZE, self.OnResize)
131        self.Bind(wx.EVT_BUTTON, self.OnStart, self.startBtn)
132        self.running = True
133        self.placeButtons()
134   
135    def placeButtons(self):
136        rect = self.GetFieldRect(1)
137        print rect.x, rect.y
138        self.startBtn.SetPosition((rect.x+5, rect.y))
139        stSize = self.startBtn.GetSize()
140        self.uninstall.SetPosition((rect.x + stSize[0]+5, rect.y))
141   
142    def OnStart(self, event):
143        if (self.running):
144            self.startBtn.SetBitmapLabel(self.stopIcon)
145            self.running = False
146        else:
147            self.startBtn.SetBitmapLabel(self.startIcon)
148            self.running = True
149    def OnResize(self, event):
150        self.placeButtons()
151
152
153
154class WaveAppCORBA:
155    def __init__(self):
156        self.rootContext = None
157        self.availableWforms = {}
158        #self.init_CORBA()   
159   
160    def init_CORBA(self):
161        self.domMgr = None
162        orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
163        self.obj = orb.resolve_initial_references("NameService")
164        if self.obj is None:
165            showMessage('Could not resolve initial references' , FATAL)
166            return
167        try:
168            self.rootContext = self.obj._narrow(CosNaming.NamingContext)
169        except(Exception), val:
170            print val.__class__, "---", val
171            showMessage('Could not initialize CORBA.\nFailed to get naming context', FATAL)
172            return
173        if self.rootContext is None:
174            showMessage('Could not initialize CORBA.\nFailed to get root context', FATAL)
175            return
176        name = [CosNaming.NameComponent("DomainName1",""), CosNaming.NameComponent("DomainManager","")]
177        try:
178            self.obj = self.rootContext.resolve(name)
179        except:
180            showMessage('Could not find Domain Manager', FATAL)
181            return
182        self.domMgr = self.obj._narrow(CF.DomainManager)
183        if self.domMgr is None:
184            showMessage('Could not resolve Domain Manager', FATAL)
185       
186    def getApplications(self):
187        dom_obj = self.rootContext.resolve([CosNaming.NameComponent("DomainName1","")])
188        dom_context = dom_obj._narrow(CosNaming.NamingContext)
189       
190        if dom_context is None:
191            print 'dom_context not found'
192            return
193        self.availableWforms.clear()
194       
195        try:
196            appSeq = self.domMgr._get_applications()
197        except(CORBA.TRANSIENT), val:
198            print val.__class__
199            showMessage(str(val), FATAL)
200            sys.exit(-1)
201        members = dom_context.list(100)
202        for m in members[0]:
203            wformName = str(m.binding_name[0].id)
204            wformObj = dom_context.resolve([CosNaming.NameComponent(wformName,"")])
205            wformContext = wformObj._narrow(CosNaming.NamingContext)
206            if wformContext is None:
207                print wformName
208                continue
209           
210                       
211            foundApp = False
212            for app in appSeq:
213                appName = app._get_name()
214                if appName in wformName:
215                    if self.availableWforms.has_key(appName):
216                        if wformName not in self.availableWforms[appName]:
217                            self.availableWforms[appName].append(wformName)
218                    else:
219                        self.availableWforms[appName] = [wformName]
220        #returns a dictionary of applications and theri corresponding installed waveforms
221        return self.availableWforms
222   
223    def installWaveform(self, wformSAD, wformDAS, start):
224       
225        sadxml = importResource.stripDoctype(wformSAD)
226        doc_sad = xml.dom.minidom.parse(wformSAD)
227        app_name = doc_sad.getElementsByTagName("softwareassembly")[0].getAttribute("name")
228        _appFacProps = []
229        devMgrSeq = self.domMgr._get_deviceManagers()
230        available_dev_seq = []
231        for devmgr in range(len(devMgrSeq)):
232            devMgr = devMgrSeq[devmgr]
233            curr_devSeq = devMgr._get_registeredDevices()
234            for dev in range(len(curr_devSeq)):
235                curr_dev = curr_devSeq[dev]
236                available_dev_seq.append(curr_dev._get_identifier())
237                #print curr_dev._get_identifier()
238
239        clean_SAD = wformSAD.split("/sdr")
240        rel_wformSAD = clean_SAD[1]
241        self.domMgr.installApplication(rel_wformSAD)
242
243       
244        # Parse the device assignment sequence, ensure
245        doc_das = xml.dom.minidom.parse(wformDAS)
246        deviceassignmenttypeNodeList = doc_das.getElementsByTagName("deviceassignmenttype")
247
248        for deviceassignmenttypeNode in deviceassignmenttypeNodeList:
249            # look for assigndeviceid nodes
250            assigndeviceidNodeList = deviceassignmenttypeNode.getElementsByTagName("assigndeviceid")
251            if len(assigndeviceidNodeList) == 0:
252                ts = "Could not find \"assigndeviceid\" tag\nAborting install"
253                showMessage(ts, NON_FATAL)
254                #errorMsg(self, ts)
255                return
256
257            # get assigndeviceid tag value (DCE:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
258            assigndeviceid = assigndeviceidNodeList[0].firstChild.data
259
260            # ensure assigndeviceid is in list of available devices
261            if assigndeviceid not in available_dev_seq:
262                ts = "Could not find the required device: " + str(assigndeviceid)
263                ts += "\nAborting install"
264                showMessage(ts, NON_FATAL)
265                #errorMsg(self, ts)
266                return
267               
268        _devSeq = self.BuildDevSeq(wformDAS)
269        _applicationFactories = self.domMgr._get_applicationFactories()
270
271        # attempt to match up the waveform application name to
272        # a application factory of the same name
273        app_factory_num = -1
274        for app_num in range(len(_applicationFactories)):
275            if _applicationFactories[app_num]._get_name()==app_name:
276                app_factory_num = app_num
277                break
278   
279        if app_factory_num == -1:
280            showMessage ("Application factory not found", NON_FATAL)
281           
282
283        # use the application factor I found above to create an instance
284        # of an application
285        try:
286            app = _applicationFactories[app_factory_num].create(_applicationFactories[app_factory_num]._get_name(),_appFacProps,_devSeq)
287        except:
288            showMessage("Unable to create application\nMake sure that all appropriate nodes are installed", NON_FATAL)
289            return(None)
290       
291        if start:
292            # start the application
293            app.start()
294       
295        naming_context_list = app._get_componentNamingContexts()
296        naming_context = naming_context_list[0].elementId.split("/")
297        application_name = app._get_name()
298        newInstance = naming_context[1]
299        return newInstance
300   
301    def uninstallWaveform(self, wformName):
302        appRef = self.getAppRef(wformName)
303        appRef.releaseObject()
304        return True
305   
306    def startWaveform(self, wformName):
307        appRef = self.getAppRef(wformName)
308        appRef.start()
309        return True
310   
311    def stopWaveform(self, wformName):
312        appRef = self.getAppRef(wformName)
313        appRef.stop()
314        return True
315   
316    def BuildDevSeq(self, dasXML):
317        doc_das = xml.dom.minidom.parse(dasXML)
318
319        # create node list of "deviceassignmenttype"
320        deviceassignmenttypeNodeList = doc_das.getElementsByTagName("deviceassignmenttype")
321
322        ds = []
323        for n in deviceassignmenttypeNodeList:
324            componentid = n.getElementsByTagName("componentid")[0].firstChild.data
325            assigndeviceid = n.getElementsByTagName("assigndeviceid")[0].firstChild.data
326            ds.append(CF.DeviceAssignmentType(str(componentid),str(assigndeviceid)))
327
328        return ds
329   
330    def getAppRef(self, selWform):
331        dom_obj = self.rootContext.resolve([CosNaming.NameComponent("DomainName1","")])
332        dom_context = dom_obj._narrow(CosNaming.NamingContext)
333       
334        if dom_context is None:
335            showMessage('Could not reference for DomainName1', NON_FATAL)
336            return
337        appSeq = self.domMgr._get_applications()
338        members = dom_context.list(100)
339        for m in members[0]:
340            wformName = str(m.binding_name[0].id)
341           
342            wformObj = dom_context.resolve([CosNaming.NameComponent(wformName,"")])
343            wformContext = wformObj._narrow(CosNaming.NamingContext)
344            if wformContext is None:
345                #print wformName
346                continue
347           
348            wformName = wformName[wformName.index("::") + 2:] #strip off OSSIE:: from wform naem           
349            foundApp = False
350            wformApp = None
351            for app in appSeq:
352                compNameCon = app._get_componentNamingContexts()
353                for compElementType in compNameCon:
354                    if (wformName in compElementType.elementId) and (wformName == selWform):
355                        wformApp = app
356                        break
357           
358            if wformApp is not None:
359                break
360        return wformApp
361   
362    def query(self, wformName, compName, prpList):
363        wformName = "OSSIE::" + wformName
364       
365        try:
366            prp = [CosNaming.NameComponent("DomainName1", ''),
367                   CosNaming.NameComponent(wformName,''),
368                   CosNaming.NameComponent(compName,'')]
369   
370            prpRsrcRef = self.rootContext.resolve(prp)
371            if prpRsrcRef is None:
372                showMessage(("Unable to find rootContext for %s/%s" % (wformName,compName)), NON_FATAL)
373                return None
374       
375            prpRsrcHandle = prpRsrcRef._narrow(CF.Resource)
376            prpSetHandle = prpRsrcRef._narrow(CF.PropertySet)
377       
378            if prpSetHandle is None:
379                showMessage(("Unable to get PropertySet reference for %s/%s" % (wformName, compName)), NON_FATAL)
380   
381            if len(prpList) == 0:
382                prpList = prpSetHandle.query(prpList)
383                       
384        except:
385            errorMsg = sys.exc_info()[1]
386            showMessage(str(errorMsg), NON_FATAL)
387            return None
388       
389        return prpList
390   
391    def configure(self, wformName, compName, prpList):
392        wformName = "OSSIE::" + wformName
393        try:
394            prpRef = [CosNaming.NameComponent("DomainName1", ''),
395                      CosNaming.NameComponent(wformName,''),
396                      CosNaming.NameComponent(compName,'')]
397       
398            prpRsrcRef = self.rootContext.resolve(prpRef)
399            if prpRsrcRef is None:
400                showMessage(("Unable to find rootContext for %s/%s" % (wformName,compName)), NON_FATAL)
401                return False
402       
403            prpRsrcHandle = prpRsrcRef._narrow(CF.Resource)
404            prpSetHandle = prpRsrcRef._narrow(CF.PropertySet)
405            if prpSetHandle is None:
406                showMessage(("Unable to get PropertySet reference for %s/%s" % (wformName, compName)), NON_FATAL)
407                return False
408            prpSetHandle.configure(prpList)
409       
410            return True
411        except:
412            errorMsg = str(sys.exc_info()[1])
413            showMessage(errorMsg, NON_FATAL)
414            return False
Note: See TracBrowser for help on using the browser.