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