root/ossiedev/branches/ssayed/devs/src/system/ossie/framework/DeviceManager_impl.cpp @ 7854

Revision 7854, 21.1 KB (checked in by shereef, 5 years ago)

FileManager::getFSandFSPath is selecting the wrong mount point; I'm in big trouble; getting colder

  • Property svn:eol-style set to native
Line 
1/****************************************************************************
2
3Copyright 2008, Virginia Polytechnic Institute and State University
4
5This file is part of the OSSIE Core Framework.
6
7OSSIE Core Framework is free software; you can redistribute it and/or modify
8it under the terms of the Lesser GNU General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or
10(at your option) any later version.
11
12OSSIE Core Framework is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15Lesser GNU General Public License for more details.
16
17You should have received a copy of the Lesser GNU General Public License
18along with OSSIE Core Framework; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21Nov/10/03       C. Neely        Created
22C. Aguayo
23
24****************************************************************************/
25#include <iostream>
26
27#include <string.h>
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#else  /// \todo change else to ifdef windows var
31#include <process.h>
32#endif
33#ifdef HAVE_STDLIB_H
34#include <stdlib.h>
35#endif
36#include <errno.h>
37
38#include "ossie/debug.h"
39#include "ossie/ossieSupport.h"
40#include "ossie/DeviceManager_impl.h"
41#include "ossie/portability.h"
42
43DeviceManager_impl::~DeviceManager_impl ()
44{
45}
46
47
48DeviceManager_impl::DeviceManager_impl(const char *DCDInput, const char* _rootfs)
49{
50  _fsroot = _rootfs;
51  _deviceConfigurationProfile = DCDInput;
52}
53
54//Parsing constructor
55void DeviceManager_impl::post_constructor (CF::DeviceManager_var my_object_var) throw (CORBA::SystemException)
56
57{
58    orb_obj = new ossieSupport::ORB();
59
60    myObj = my_object_var;
61
62    _registeredDevices.length(0);
63    _registeredServices.length(0);
64
65    fs_servant = new FileSystem_impl(_fsroot.c_str());
66    _fileSys = fs_servant->_this();
67
68    try
69    {
70     _fileSys->exists(_deviceConfigurationProfile.c_str());
71    } catch( CF::InvalidFileName &_ex ) {
72     std::cout << "[DeviceManager::post_ctor] While testing for the existence of DCD: " << _ex.msg << "\n";
73     exit(EXIT_FAILURE);
74    } catch( ... ) {
75     std::cout << "[DeciceManager::post_ctor] While testing for the existence of DCD: Unknown Exception\n";
76     exit(EXIT_FAILURE);
77    }
78
79//Get Device Manager attributes (deviceConfigurationProfile, identifier and label)
80//from DCD file
81    CF::File_var _dcd;
82    try
83    {
84     _dcd = _fileSys->open( _deviceConfigurationProfile.c_str(), true );
85    } catch( CF::InvalidFileName &_ex ) {
86     std::cout << "[DeviceManager::post_ctor] While opening DCD: " << _ex.msg << "\n";
87     exit(EXIT_FAILURE);
88    } catch( CF::FileException &_ex ) {
89     std::cout << "[DeviceManager::post_ctor] While opening DCD: " << _ex.msg << "\n";
90     exit(EXIT_FAILURE);
91    } catch( ... ) {
92     std::cout << "[DeviceManager::post_ctor] While opening DCD: Unknown Exception\n";
93     exit(EXIT_FAILURE);
94    }
95
96    DCDParser _DCDParser ( _dcd );
97    _dcd->close();
98    _identifier = _DCDParser.getID();
99    _label = _DCDParser.getName();
100
101//get DomainManager reference
102    getDomainManagerReference ((char *)_DCDParser.getDomainManagerName ());
103//Register DeviceManager with DomainManager
104    try
105    {
106     _dmnMgr->registerDeviceManager (my_object_var);
107    } catch( CF::InvalidObjectReference &_ex ) {
108     std::cout << "[DeviceManager::post_ctor] While registering DevMgr with DomMgr: " << _ex.msg << "\n";
109     exit(EXIT_FAILURE);
110    } catch( ... ) {
111     std::cout << "[DeviceManager::post_ctor] While registering DevMgr with DomMgr: Unknown Exception\n";
112     exit(EXIT_FAILURE);
113    }
114
115    _adminState = DEVMGR_REGISTERED;
116
117//parse filesystem names
118
119//Parse local componenents from DCD files
120     std::vector <componentPlacement> componentPlacements = _DCDParser.getComponentPlacements ();
121
122     for (unsigned int i = 0; i < componentPlacements.size(); i++)
123     {
124//get spd reference
125//parse spd file
126      CF::File_var _spd;
127
128      try
129      {
130       _spd = _fileSys->open( _DCDParser.getFileNameFromRefId(componentPlacements[i].refId()), true );
131      } catch( CF::InvalidFileName &_ex ) {
132       std::cout << "[DeviceManager::post_ctor] While opening SPD " << i << ": " << _ex.msg << "\n";
133       exit(EXIT_FAILURE);
134      } catch( CF::FileException &_ex ) {
135       std::cout << "[DeviceManager::post_ctor] While opening SPD " << i << ": " << _ex.msg << "\n";
136       exit(EXIT_FAILURE);
137      } catch( ... ) {
138       std::cout << "[DeviceManager::post_ctor] While opening SPD " << i << ": Unknown Exception\n";
139       exit(EXIT_FAILURE);
140      }
141
142      SPDParser _SPDParser ( _spd );
143      _spd->close();
144//get code file name from implementation
145      std::vector < SPDImplementation * >*_implementations = _SPDParser.getImplementations ();
146
147///----------Assuming only one implementation
148//spawn device
149#ifdef HAVE_WORKING_FORK
150      int myPid2;
151
152      DEBUG(2, DevMgr, "Launching Device file " << (*_implementations)[0]->getCodeFile () << " Usage name " << componentPlacements[i].usageName())
153
154      if ((myPid2 = fork()) < 0) std::cout << "Fork Error" << std::endl;
155      if (myPid2 == 0)
156      {
157        // in child
158       if (getenv("VALGRIND")) {
159        std::string logFile = "--log-file=";
160        logFile += (*_implementations)[0]->getCodeFile ();
161        char *val = "/usr/local/bin/valgrind";
162        execl(val, val, logFile.c_str(), (*_implementations)[0]->getCodeFile (), componentPlacements[i].id(), componentPlacements[i].usageName() , _DCDParser.getFileNameFromRefId(componentPlacements[i].refId()), NULL);
163        } else {
164         execl((*_implementations)[0]->getCodeFile (), (*_implementations)[0]->getCodeFile (),componentPlacements[i].id(), componentPlacements[i].usageName() , _DCDParser.getFileNameFromRefId(componentPlacements[i].refId()), NULL);
165        }
166        std::cout << "Device did not execute : " << strerror(errno) << std::endl;
167        exit (EXIT_FAILURE);
168      }
169#endif
170      ossieSupport::nsleep(0, 1000*1000);
171      CORBA::Object_var _obj = CORBA::Object::_nil();
172      char nameStr[255];
173      sprintf( nameStr, "DomainName1/%s", componentPlacements[i].usageName() );      DEBUG(3, DevMgr, "searching for "<< nameStr)
174      do
175      {
176/// \todo sleep prevents system from beating Name Service to death, Fix better
177       ossieSupport::nsleep(0, 50*1000);
178       try
179       {
180        _obj = orb_obj->get_object_from_name(nameStr);
181       } catch (CosNaming::NamingContext::NotFound) {
182        ossieSupport::nsleep(0, 100*1000);
183       } catch( ... ) {
184        std::cout << "[DeviceManager::post_ctor] While obtaining NamingContext: Unknown Exception\n";
185        exit(EXIT_FAILURE);
186       }
187      } while (CORBA::is_nil (_obj));
188      DEBUG(3, DevMgr, "found "<< nameStr)
189
190      CF::Device_var tempDevice = CF::Device::_narrow (_obj);
191      tempDevice->initialize ();
192
193      if( strlen( _SPDParser.getPRFFile() ) > 0 )
194      {
195//Get properties from PRF
196       CF::File_var _prf;
197       try
198       {
199        _prf = _fileSys->open( _SPDParser.getPRFFile(), true );
200       } catch( CF::InvalidFileName &_ex ) {
201        std::cout << "[DeviceManager::post_ctor] While opening PRF " << _SPDParser.getPRFFile() <<  ": " << _ex.msg << "\n";
202        exit(EXIT_FAILURE);
203       } catch( CF::FileException &_ex ) {
204        std::cout << "[DeviceManager::post_ctor] While opening PRF " << _SPDParser.getPRFFile() << ": " << _ex.msg << "\n";
205        exit(EXIT_FAILURE);
206       } catch( ... ) {
207        std::cout << "[DeviceManager::post_ctor] While opening PRF: Unknown Exception\n";
208        exit(EXIT_FAILURE);
209       }
210
211       PRFParser _PRFparser ( _prf );
212       _prf->close();
213       std::vector <PRFProperty *> *prfSimpleProp = _PRFparser.getConfigureProperties ();
214       CF::Properties configCapacities;
215       configCapacities.length (prfSimpleProp->size ());
216       for (unsigned int i = 0; i < prfSimpleProp->size (); i++)
217       {
218        configCapacities[i] = *((*prfSimpleProp)[i]->getDataType ());
219       }
220
221//configure properties
222       DEBUG(3, DevMgr, "Configuring capacities")
223       tempDevice->configure (configCapacities);
224      }
225
226      DEBUG(3, DevMgr, "Registering device")
227      try
228      {
229       registerDevice (CF::Device::_duplicate(tempDevice));
230      } catch( CF::InvalidObjectReference &_ex ) {
231       std::cout << "[DeviceManager::post_ctor] While registering Device: " << _ex.msg << "\n";
232       exit(EXIT_FAILURE);
233      } catch( ... ) {
234       std::cout << "[DeviceManager::post_ctor] While registering Device: Unknown Exception\n";
235       exit(EXIT_FAILURE);
236      }
237
238      DEBUG(3, DevMgr, "Device Registered")
239     }
240//So far, it is assumed that all components are local
241/*//Obtain DEPLY-ON components
242   std::vector<DCDComponentPlacement> DeployOnComponentsVector = _DCDParser.getDeployOnComponents();
243
244   std::vector<char*> DPDList;
245
246   //Get DPD file names from DeployOnComponentsVector
247   for(int i = 0; i < DeployOnComponentsVector.size(); i++) //Makes list of DPD files.
248   DPDList.push_back( DeployOnComponentsVector[i].getDPDFile() );
249 */
250}
251
252
253void
254DeviceManager_impl::init ()
255{
256
257    _adminState = DEVMGR_UNREGISTERED;
258}
259
260
261void
262DeviceManager_impl::getDomainManagerReference (char *domainManagerName)
263{
264    CORBA::Object_var obj = CORBA::Object::_nil();
265
266/// \todo sleep prevents system from beating Name Service to death, Fix better
267    do{
268      obj = orb_obj->get_object_from_name (domainManagerName);
269      usleep(1000);
270    }while(CORBA::is_nil(obj));
271
272    _dmnMgr = CF::DomainManager::_narrow (obj);
273}
274
275
276char *DeviceManager_impl::deviceConfigurationProfile ()
277throw (CORBA::SystemException)
278{
279    return CORBA::string_dup(_deviceConfigurationProfile.c_str());
280}
281
282
283CF::FileSystem_ptr DeviceManager_impl::fileSys ()throw (CORBA::
284SystemException)
285{
286    CF::FileSystem_var result = _fileSys;
287    return result._retn();
288}
289
290
291char *DeviceManager_impl::identifier ()
292throw (CORBA::SystemException)
293{
294    return CORBA::string_dup (_identifier.c_str());
295}
296
297
298char *DeviceManager_impl::label ()
299throw (CORBA::SystemException)
300{
301    return CORBA::string_dup (_label.c_str());
302}
303
304
305CF::DeviceSequence *
306DeviceManager_impl::registeredDevices ()throw (CORBA::SystemException)
307{
308    CF::DeviceSequence_var result = new CF::DeviceSequence(_registeredDevices);
309    return result._retn();
310}
311
312
313CF::DeviceManager::ServiceSequence *
314DeviceManager_impl::registeredServices ()throw (CORBA::SystemException)
315{
316    CF::DeviceManager::ServiceSequence_var result = new CF::DeviceManager::ServiceSequence(_registeredServices);
317    return result._retn();
318}
319
320
321void
322DeviceManager_impl::registerDevice (CF::Device_ptr registeringDevice)
323throw (CORBA::SystemException, CF::InvalidObjectReference)
324{
325    if (CORBA::is_nil (registeringDevice)) {
326        //writeLogRecord(FAILURE_ALARM,invalid reference input parameter.)
327        throw (CF::
328            InvalidObjectReference
329            ("[DeviceManager::registerDevice] Cannot register Device. registeringDevice is a nil reference."));
330    }
331
332    // Register the device with the Device manager, unless it is already
333    // registered
334    if (!deviceIsRegistered (registeringDevice)) {
335        _registeredDevices.length (_registeredDevices.length () + 1);
336        _registeredDevices[_registeredDevices.length () - 1] =
337            registeringDevice;
338    }
339
340    // If this Device Manager is registered with a Domain Manager, register
341    // the new device with the Domain Manager
342    if (_adminState == DEVMGR_REGISTERED) {
343        _dmnMgr->registerDevice (registeringDevice, myObj);
344    }
345
346//The registerDevice operation shall write a FAILURE_ALARM log record to a
347//DomainManagers Log, upon unsuccessful registration of a Device to the DeviceManagers
348//registeredDevices.
349}
350
351
352//This function returns TRUE if the input registeredDevice is contained in the _registeredDevices list attribute
353bool DeviceManager_impl::deviceIsRegistered (CF::Device_ptr registeredDevice)
354{
355//Look for registeredDevice in _registeredDevices
356    for (unsigned int i = 0; i < _registeredDevices.length (); i++)
357    {
358        if (strcmp (_registeredDevices[i]->label (), registeredDevice->label ())
359            == 0)
360        {
361            return true;
362        }
363    }
364    return false;
365}
366
367
368//This function returns TRUE if the input serviceName is contained in the _registeredServices list attribute
369bool DeviceManager_impl::serviceIsRegistered (const char *serviceName)
370{
371//Look for registeredDevice in _registeredDevices
372    for (unsigned int i = 0; i < _registeredServices.length (); i++)
373    {
374        if (strcmp (_registeredServices[i].serviceName, serviceName)  == 0)
375        {
376            return true;
377        }
378    }
379    return false;
380}
381
382
383void
384DeviceManager_impl::unregisterDevice (CF::Device_ptr registeredDevice)
385throw (CORBA::SystemException, CF::InvalidObjectReference)
386{
387    bool deviceFound = false;
388    if (CORBA::is_nil (registeredDevice))         //|| !deviceIsRegistered(registeredDevice) )
389    {
390//The unregisterDevice operation shall write a FAILURE_ALARM log record, when it cannot
391//successfully remove a registeredDevice from the DeviceManagers registeredDevices.
392
393//The unregisterDevice operation shall raise the CF InvalidObjectReference when the input
394//registeredDevice is a nil CORBA object reference or does not exist in the DeviceManagers
395//registeredDevices attribute.
396/*writeLogRecord(FAILURE_ALARM,invalid reference input parameter.); */
397        throw (CF::
398            InvalidObjectReference
399            ("Cannot unregister Device. registeringDevice is a nil reference."));
400
401        return;
402    }
403
404//The unregisterDevice operation shall remove the input registeredDevice from the
405//DeviceManagers registeredDevices attribute.
406
407//Look for registeredDevice in _registeredDevices
408    for (unsigned int i = 0; i < _registeredDevices.length (); i++)
409    {
410        if (strcmp (_registeredDevices[i]->label (), registeredDevice->label ())
411            == 0)
412        {
413//when the appropiater device is found, remove it from the _registeredDevices sequence
414            deviceFound = true;
415            if (_adminState == DEVMGR_REGISTERED)
416            {
417                _dmnMgr->unregisterDevice (CF::Device::_duplicate (registeredDevice));
418                CORBA::release (registeredDevice);
419            }
420            for (unsigned int j = i; j < _registeredDevices.length () - 1; j++)
421            {
422//The unregisterDevice operation shall unregister
423//the input registeredDevice from the DomainManager when the input registeredDevice is
424//registered with the DeviceManager and the DeviceManager is not shutting down.
425                _registeredDevices[j] = _registeredDevices[j + 1];
426            }
427//_registeredDevices[_registeredDevices.length() - 1] = 0;
428            _registeredDevices.length (_registeredDevices.length () - 1);
429//TO DO: Avoid memory leaks by reducing the length of the sequence _registeredDevices
430            break;
431        }
432    }
433    if (!deviceFound)
434    {
435/*writeLogRecord(FAILURE_ALARM,invalid reference input parameter.); */
436
437        throw (CF::
438            InvalidObjectReference
439            ("Cannot unregister Device. registeringDevice was not registered."));
440        return;
441    }
442
443}
444
445
446void
447DeviceManager_impl::shutdown ()
448throw (CORBA::SystemException)
449{
450    _adminState = DEVMGR_SHUTTING_DOWN;
451
452//The shutdown operation shall unregister the DeviceManager from the DomainManager.
453    _dmnMgr->unregisterDeviceManager (this->_this ()); ///\bug This looks wrong.
454
455//The shutdown operation shall perform releaseObject on all of the DeviceManagers registered
456//Devices (DeviceManagers registeredDevices attribute).
457
458    for (int i = _registeredDevices.length () - 1; i >= 0; i--)
459    {
460//Important Note: It is necessary to manage the lenght of the _registeredDevices sequence
461//otherwise, some elements in the sequence will be null.
462        _registeredDevices[i]->label ();          ////////////////////////////////////////////////test
463        CF::Device_var tempDev = CF::Device::_duplicate (_registeredDevices[i]);
464//_registeredDevices[i]->releaseObject();
465        unregisterDevice (_registeredDevices[i]);
466        tempDev->releaseObject ();
467    }
468
469}
470
471
472void
473DeviceManager_impl::registerService (CORBA::Object_ptr registeringService,
474const char *name)
475throw (CORBA::SystemException, CF::InvalidObjectReference)
476{
477//This release does not support services
478    if (CORBA::is_nil (registeringService))
479    {
480/*writeLogRecord(FAILURE_ALARM,invalid reference input parameter.); */
481
482        throw (CF::
483            InvalidObjectReference
484            ("Cannot register Device. registeringDevice is a nil reference."));
485        return;
486    }
487
488//The registerService operation shall add the input registeringService to the DeviceManagers
489//registeredServices attribute when the input registeringService does not already exist in the
490//registeredServices attribute. The registeringService is ignored when duplicated.
491    if (!serviceIsRegistered (name))
492    {
493        _registeredServices.length (_registeredServices.length () + 1);
494        _registeredServices[_registeredServices.length () - 1].serviceObject = registeringService;
495        _registeredServices[_registeredServices.length () - 1].serviceName = name;
496    }
497
498//The registerService operation shall register the registeringService with the DomainManager
499//when the DeviceManager has already registered to the DomainManager and the
500//registeringService has been successfully added to the DeviceManagers registeredServices
501//attribute.
502    if (_adminState == DEVMGR_REGISTERED)
503    {
504        _dmnMgr->registerService (registeringService, this->_this (), name);
505    }
506
507//The registerService operation shall write a FAILURE_ALARM log record, upon unsuccessful
508//registration of a Service to the DeviceManagers registeredServices.
509//The registerService operation shall raise the CF InvalidObjectReference exception when the
510//input registeringService is a nil CORBA object reference.
511
512}
513
514
515void
516DeviceManager_impl::unregisterService (CORBA::Object_ptr registeredService,
517const char *name)
518throw (CORBA::SystemException, CF::InvalidObjectReference)
519{
520    if (CORBA::is_nil (registeredService))
521    {
522/*writeLogRecord(FAILURE_ALARM,invalid reference input parameter.); */
523
524        throw (CF::
525            InvalidObjectReference
526            ("Cannot unregister Service. registeringService is a nil reference."));
527        return;
528    }
529
530//The unregisterService operation shall remove the input registeredService from the
531//DeviceManagers registeredServices attribute. The unregisterService operation shall unregister
532//the input registeredService from the DomainManager when the input registeredService is
533//registered with the DeviceManager and the DeviceManager is not in the shutting down state.
534
535//Look for registeredService in _registeredServices
536    for (unsigned int i = 0; i < _registeredServices.length (); i++)
537    {
538        if (strcmp (_registeredServices[i].serviceName, name) == 0)
539        {
540//when the appropiater device is found, remove it from the _registeredDevices sequence
541            if (_adminState == DEVMGR_REGISTERED)
542            {
543                _dmnMgr->unregisterService (registeredService, name);
544            }
545
546            for (unsigned int j = i; j < _registeredServices.length ()-1; j++)
547            {
548
549                CORBA::release (registeredService);
550                _registeredServices[j] = _registeredServices[j+1];
551            }
552            _registeredServices.length (_registeredServices.length () - 1);
553            return;
554        }
555    }
556
557//If it didn't find registeredDevice, then throw an exception
558/*writeLogRecord(FAILURE_ALARM,invalid reference input parameter.);*/
559    throw (CF::
560        InvalidObjectReference
561        ("Cannot unregister Service. registeringService was not registered."));
562//The unregisterService operation shall write a FAILURE_ALARM log record, when it cannot
563//successfully remove a registeredService from the DeviceManagers registeredServices.
564//The unregisterService operation shall raise the CF InvalidObjectReference when the input
565//registeredService is a nil CORBA object reference or does not exist in the DeviceManagers
566//registeredServices attribute.
567}
568
569
570char *
571DeviceManager_impl::
572getComponentImplementationId (const char *componentInstantiationId)
573throw (CORBA::SystemException)
574{
575//The getComponentImplementationId operation shall return the SPD implementation elements
576//ID attribute that matches the SPD implementation element used to create the component
577//identified by the input componentInstantiationId parameter.
578
579#if 0
580    DCDParser _DCDParser (_deviceConfigurationProfile);
581    std::vector < char *>*LocalComponentsVector =
582        _DCDParser.getLocalComponents ();
583#endif
584
585    std::cout << "If this appears look at DeviceManager_impl.cpp line 572" << std::endl;
586/*for (int i = 0; i<localComponentsVector->size();i++)
587   {
588   //get componentInstatiationId from each loal component
589   std::vector<ComponentInstantiation*> instantiations = LocalComponentsVector[i].getInstantiations();
590   //assuming only one instantiation
591   if( strcmp(componentInstantiationId, instantiations[0]->getID()) == 0)
592   {
593   SPDParser spdParser ( LocalComponentsVector[i].getSPDFile() );
594   std::vector<SPDImplementation*>  implementations = spdParser.getImplementations();
595   return implementations[0]->getID();
596   }
597} */
598    return "";
599
600//The getComponentImplementationId operation shall return an empty string when the input
601//componentInstantiationId parameter does not match the ID attribute of any SPD implementation
602//element used to create the component.
603}
604
Note: See TracBrowser for help on using the browser.