| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2004, 2007 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 | ****************************************************************************/ |
|---|
| 22 | #include <iostream> |
|---|
| 23 | #include <string> |
|---|
| 24 | |
|---|
| 25 | #ifdef HAVE_UNISTD_H |
|---|
| 26 | #include <unistd.h> |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | #ifndef HAVE_UNISTD_H |
|---|
| 30 | #include <process.h> |
|---|
| 31 | #endif |
|---|
| 32 | |
|---|
| 33 | #include "ossie/debug.h" |
|---|
| 34 | #include "ossie/Application_impl.h" |
|---|
| 35 | #include "ossie/ossieSupport.h" |
|---|
| 36 | #include "ossie/portability.h" |
|---|
| 37 | #include "ossie/SADParser.h" |
|---|
| 38 | |
|---|
| 39 | Application_impl::Application_impl (const char *_id, |
|---|
| 40 | const char *_name, |
|---|
| 41 | const char *_profile, |
|---|
| 42 | DEV_SEQ * _devSeq, |
|---|
| 43 | ELEM_SEQ * _implSeq, |
|---|
| 44 | ELEM_SEQ * _ncSeq, |
|---|
| 45 | PROC_ID_SEQ * _pidSeq, |
|---|
| 46 | std::string _waveformName, |
|---|
| 47 | std::vector <ossieSupport::ConnectionInfo *> _connectionData, |
|---|
| 48 | std::vector <ossieSupport::ComponentInfo *> _components, |
|---|
| 49 | CF::Resource_ptr _controller, |
|---|
| 50 | CF::DomainManager::ApplicationSequence *_appseq, |
|---|
| 51 | SADParser *sadParser, CosNaming::NamingContext_ptr wnc) : Resource_impl(_id) |
|---|
| 52 | { |
|---|
| 53 | |
|---|
| 54 | appName = _name; |
|---|
| 55 | sadProfile = _profile; |
|---|
| 56 | _sadParser = sadParser; |
|---|
| 57 | connectionData = _connectionData; |
|---|
| 58 | components = _components; |
|---|
| 59 | orb = new ossieSupport::ORB(); |
|---|
| 60 | waveformName = _waveformName; |
|---|
| 61 | waveformContext = wnc; |
|---|
| 62 | |
|---|
| 63 | appseq = _appseq; |
|---|
| 64 | |
|---|
| 65 | if (_devSeq != NULL) |
|---|
| 66 | { |
|---|
| 67 | this->appComponentDevices = new DEV_SEQ (); |
|---|
| 68 | this->appComponentDevices->length (_devSeq->length ()); |
|---|
| 69 | |
|---|
| 70 | for (unsigned i = 0; i < _devSeq->length (); i++) |
|---|
| 71 | (*appComponentDevices)[i] = (*_devSeq)[i]; |
|---|
| 72 | } |
|---|
| 73 | else |
|---|
| 74 | this->appComponentDevices = new DEV_SEQ (0); |
|---|
| 75 | |
|---|
| 76 | if (_implSeq != NULL) |
|---|
| 77 | { |
|---|
| 78 | this->appComponentImplementations = new ELEM_SEQ (); |
|---|
| 79 | this->appComponentImplementations->length (_implSeq->length ()); |
|---|
| 80 | |
|---|
| 81 | for (unsigned int i = 0; i < _implSeq->length (); i++) |
|---|
| 82 | (*appComponentImplementations)[i] = (*_implSeq)[i]; |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | this->appComponentImplementations = new ELEM_SEQ (0); |
|---|
| 86 | |
|---|
| 87 | if (_ncSeq != NULL) |
|---|
| 88 | { |
|---|
| 89 | this->appComponentNamingContexts = new ELEM_SEQ (); |
|---|
| 90 | this->appComponentNamingContexts->length (_ncSeq->length ()); |
|---|
| 91 | |
|---|
| 92 | for (unsigned int i = 0; i < _ncSeq->length (); i++) |
|---|
| 93 | (*appComponentNamingContexts)[i] = (*_ncSeq)[i]; |
|---|
| 94 | } |
|---|
| 95 | else |
|---|
| 96 | this->appComponentNamingContexts = new ELEM_SEQ (0); |
|---|
| 97 | |
|---|
| 98 | if (_pidSeq != NULL) |
|---|
| 99 | { |
|---|
| 100 | this->appComponentProcessIds = new PROC_ID_SEQ (); |
|---|
| 101 | this->appComponentProcessIds->length (_pidSeq->length ()); |
|---|
| 102 | |
|---|
| 103 | for (unsigned int i = 0; i < _pidSeq->length (); i++) |
|---|
| 104 | (*appComponentProcessIds)[i] = (*_pidSeq)[i]; |
|---|
| 105 | } |
|---|
| 106 | else |
|---|
| 107 | this->appComponentProcessIds = new PROC_ID_SEQ (0); |
|---|
| 108 | |
|---|
| 109 | this->assemblyController = _controller; |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | Application_impl::~Application_impl () |
|---|
| 113 | { |
|---|
| 114 | |
|---|
| 115 | delete this->appComponentDevices; |
|---|
| 116 | this->appComponentDevices = NULL; |
|---|
| 117 | |
|---|
| 118 | delete this->appComponentImplementations; |
|---|
| 119 | this->appComponentImplementations = NULL; |
|---|
| 120 | |
|---|
| 121 | delete this->appComponentNamingContexts; |
|---|
| 122 | this->appComponentNamingContexts = NULL; |
|---|
| 123 | |
|---|
| 124 | delete this->appComponentProcessIds; |
|---|
| 125 | this->appComponentProcessIds = NULL; |
|---|
| 126 | }; |
|---|
| 127 | |
|---|
| 128 | void |
|---|
| 129 | Application_impl::start () |
|---|
| 130 | throw (CORBA::SystemException, CF::Resource::StartError) |
|---|
| 131 | { |
|---|
| 132 | assemblyController->start (); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | void |
|---|
| 137 | Application_impl::stop () |
|---|
| 138 | throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 139 | { |
|---|
| 140 | assemblyController->stop (); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | void |
|---|
| 145 | Application_impl::initialize () |
|---|
| 146 | throw (CORBA::SystemException, CF::LifeCycle::InitializeError) |
|---|
| 147 | { |
|---|
| 148 | assemblyController->initialize (); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | CORBA::Object_ptr |
|---|
| 153 | Application_impl::getPort (const char* _id) |
|---|
| 154 | throw (CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 155 | { |
|---|
| 156 | return assemblyController->getPort (_id); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | void |
|---|
| 161 | Application_impl::runTest (CORBA::ULong _testId, CF::Properties& _props) |
|---|
| 162 | throw (CORBA::SystemException, CF::UnknownProperties, CF::TestableObject::UnknownTest) |
|---|
| 163 | |
|---|
| 164 | { |
|---|
| 165 | assemblyController->runTest (_testId, _props); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | /* |
|---|
| 169 | #ifdef MS_dotNET |
|---|
| 170 | ///\bug It is necessary to have this class because under VC7.1, self-destruct before the CORBA call is complete cannot be implemented |
|---|
| 171 | |
|---|
| 172 | class ApplicationKiller : public ACE_Task_Base |
|---|
| 173 | { |
|---|
| 174 | protected: |
|---|
| 175 | Application_impl *local_pApp; |
|---|
| 176 | |
|---|
| 177 | public: |
|---|
| 178 | virtual int svc (void) |
|---|
| 179 | { |
|---|
| 180 | ossieSupport::sleep(1); |
|---|
| 181 | delete local_pApp; |
|---|
| 182 | return 0; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | void get_address(Application_impl *pApp) |
|---|
| 186 | { |
|---|
| 187 | local_pApp = pApp; |
|---|
| 188 | } |
|---|
| 189 | }; |
|---|
| 190 | #endif |
|---|
| 191 | */ |
|---|
| 192 | |
|---|
| 193 | void |
|---|
| 194 | Application_impl::releaseObject () |
|---|
| 195 | throw (CORBA::SystemException, CF::LifeCycle::ReleaseError) |
|---|
| 196 | { |
|---|
| 197 | DEBUG(3, Application, "Entering releaseObject"); |
|---|
| 198 | |
|---|
| 199 | // Remove reference for this application from the ApplicationSequence in DomainManager |
|---|
| 200 | int old_length = appseq->length(); |
|---|
| 201 | int index_found_app = -1; |
|---|
| 202 | for (int i=0; i<old_length; i++) { |
|---|
| 203 | if (!strcmp((*(*appseq)[i]->componentNamingContexts())[0].elementId, (*appComponentNamingContexts)[0].elementId)) { |
|---|
| 204 | index_found_app = i; |
|---|
| 205 | break; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | if (index_found_app == -1) { |
|---|
| 209 | std::cout << "The DomainManager's application list is corrupt" << std::endl; |
|---|
| 210 | } else { |
|---|
| 211 | for (int i=index_found_app; i<old_length-1; i++) { |
|---|
| 212 | (*appseq)[i] = (*appseq)[i+1]; |
|---|
| 213 | } |
|---|
| 214 | appseq->length(old_length-1); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | std::vector <CF::Device_ptr> registeredDevices; |
|---|
| 218 | |
|---|
| 219 | assemblyController = CF::Resource::_nil (); |
|---|
| 220 | |
|---|
| 221 | // break all connections in the application |
|---|
| 222 | for (unsigned int i = 0; i < connectionData.size(); i++) { |
|---|
| 223 | CF::Port_var port = connectionData[i]->getPort(); |
|---|
| 224 | DEBUG(5, Application, "Disconnecting port: " << connectionData[i]->getID()); |
|---|
| 225 | port->disconnectPort(connectionData[i]->getID()); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | DEBUG(4, Application, "app->releaseObject finished disconnecting ports") |
|---|
| 229 | |
|---|
| 230 | // release all resources |
|---|
| 231 | //Before releasing the components, all executed processes should be terminated, |
|---|
| 232 | //all software components unloaded, and all capacities deallocated |
|---|
| 233 | //search thru all waveform components |
|---|
| 234 | //if there is a match with the deviceAssignmentSequence, then get a reference to the device |
|---|
| 235 | //unload and deallocate |
|---|
| 236 | |
|---|
| 237 | CF::DomainManager_ptr dmnMgr; |
|---|
| 238 | CORBA::Object_ptr obj = orb->get_object_from_name("DomainName1/DomainManager"); |
|---|
| 239 | dmnMgr = CF::DomainManager::_narrow (obj); |
|---|
| 240 | |
|---|
| 241 | CF::DomainManager::DeviceManagerSequence* devMgrs; |
|---|
| 242 | devMgrs = dmnMgr->deviceManagers (); |
|---|
| 243 | |
|---|
| 244 | for (unsigned int i=0; i<devMgrs->length(); i++) { |
|---|
| 245 | CF::DeviceSequence* devices = (*devMgrs)[i]->registeredDevices(); |
|---|
| 246 | for (unsigned int j=0; j<devices->length(); j++) { |
|---|
| 247 | registeredDevices.push_back((*devices)[j]); |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | // OLD COMMENT |
|---|
| 252 | //search thru all waveform components |
|---|
| 253 | //if there is a match with the deviceAssignmentSequence, then get a reference to the device |
|---|
| 254 | //unload and deallocate |
|---|
| 255 | |
|---|
| 256 | for (unsigned int i(0); i < components.size(); ++i) { |
|---|
| 257 | ossieSupport::ComponentInfo *component = components[i]; |
|---|
| 258 | |
|---|
| 259 | if (component->getDeployedOnResourceFactory()) { |
|---|
| 260 | ///\todo Write code for resource factory case |
|---|
| 261 | DEBUG(4, Application, "Releasing component deployed on resource factory."); |
|---|
| 262 | |
|---|
| 263 | } else if (component->getDeployedOnLoadableDevice()) { |
|---|
| 264 | ///\todo Write code for loadable device case |
|---|
| 265 | DEBUG(4, Application, "Releasing component deployed on loadable device."); |
|---|
| 266 | |
|---|
| 267 | } else if (component->getDeployedOnExecutableDevice()) { |
|---|
| 268 | DEBUG(4, Application, "Releasing component deployed on executable device."); |
|---|
| 269 | |
|---|
| 270 | CF::Resource_ptr rsc = components[i]->getResourcePtr(); |
|---|
| 271 | DEBUG(4, Application, "about to release Object"); |
|---|
| 272 | |
|---|
| 273 | rsc->releaseObject (); |
|---|
| 274 | |
|---|
| 275 | CF::ExecutableDevice_var execDev = CF::ExecutableDevice::_narrow(component->getDevicePtr()); |
|---|
| 276 | |
|---|
| 277 | DEBUG(4, Application, "Unloading: " << component->getLocalFileName()); |
|---|
| 278 | execDev->unload(component->getLocalFileName()); |
|---|
| 279 | |
|---|
| 280 | DEBUG(4, Application, "Terminating PID: " << component->getPID()); |
|---|
| 281 | execDev->terminate (component->getPID()); |
|---|
| 282 | execDev->deallocateCapacity(component->getAllocationCapacities()); |
|---|
| 283 | |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | DEBUG(4, App, "Finished releasing " << component->getUsageName()); |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | DEBUG(4, Application, "End of component list"); |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | std::string tc("DomainName1"); |
|---|
| 293 | CosNaming::Name_var rootNc = orb->string_to_CosName(tc.c_str()); |
|---|
| 294 | CORBA::Object_var rootObj = orb->inc->resolve(rootNc); |
|---|
| 295 | CosNaming::NamingContext_var rootContext = CosNaming::NamingContext::_narrow(rootObj); |
|---|
| 296 | CosNaming::Name waveformCosName; |
|---|
| 297 | waveformCosName.length(1); |
|---|
| 298 | waveformCosName[0].id = CORBA::string_dup(waveformName.c_str()); |
|---|
| 299 | |
|---|
| 300 | DEBUG(4, Application, "Unbinding: " << waveformName); |
|---|
| 301 | |
|---|
| 302 | rootContext->unbind(waveformCosName); |
|---|
| 303 | |
|---|
| 304 | DEBUG(4, Application, "Destroying waveform naming context. "); |
|---|
| 305 | |
|---|
| 306 | waveformContext->destroy(); |
|---|
| 307 | |
|---|
| 308 | /*#ifdef MS_dotNET |
|---|
| 309 | |
|---|
| 310 | /// \bug this alternate structure is necessary to implement functionality that looks like a self-destruct |
|---|
| 311 | |
|---|
| 312 | ApplicationKiller * handler = new ApplicationKiller; |
|---|
| 313 | |
|---|
| 314 | handler->get_address(this); |
|---|
| 315 | |
|---|
| 316 | int result = handler->activate(); |
|---|
| 317 | |
|---|
| 318 | #else*/ |
|---|
| 319 | |
|---|
| 320 | // this is what the self-destruct should really look like |
|---|
| 321 | ///\bug releaseObject code looks like example of what not to do pp492 |
|---|
| 322 | // delete this; |
|---|
| 323 | //#endif |
|---|
| 324 | |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | char *Application_impl::name () |
|---|
| 329 | throw (CORBA::SystemException) |
|---|
| 330 | { |
|---|
| 331 | return CORBA::string_dup(appName.c_str()); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | char *Application_impl::profile () |
|---|
| 336 | throw (CORBA::SystemException) |
|---|
| 337 | { |
|---|
| 338 | return CORBA::string_dup(sadProfile.c_str()); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | PROC_ID_SEQ * |
|---|
| 343 | Application_impl::componentProcessIds () |
|---|
| 344 | throw (CORBA::SystemException) |
|---|
| 345 | { |
|---|
| 346 | CF::Application::ComponentProcessIdSequence_var result = new CF::Application::ComponentProcessIdSequence(*appComponentProcessIds); |
|---|
| 347 | return result._retn(); |
|---|
| 348 | //return this->appComponentProcessIds; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | ELEM_SEQ * |
|---|
| 353 | Application_impl::componentNamingContexts () |
|---|
| 354 | throw (CORBA::SystemException) |
|---|
| 355 | { |
|---|
| 356 | CF::Application::ComponentElementSequence_var result = new ELEM_SEQ(*appComponentNamingContexts); |
|---|
| 357 | return result._retn(); |
|---|
| 358 | //return this->appComponentNamingContexts; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | ELEM_SEQ * |
|---|
| 363 | Application_impl::componentImplementations () |
|---|
| 364 | throw (CORBA::SystemException) |
|---|
| 365 | { |
|---|
| 366 | CF::Application::ComponentElementSequence_var result = new ELEM_SEQ(*appComponentImplementations); |
|---|
| 367 | return result._retn(); |
|---|
| 368 | //return this->appComponentImplementations; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | CF::DeviceAssignmentSequence * |
|---|
| 373 | Application_impl::componentDevices ()throw (CORBA::SystemException) |
|---|
| 374 | { |
|---|
| 375 | CF::DeviceAssignmentSequence_var result = new CF::DeviceAssignmentSequence(*appComponentDevices); |
|---|
| 376 | return result._retn(); |
|---|
| 377 | //return this->appComponentDevices; |
|---|
| 378 | } |
|---|