📄 objectclass.cc
字号:
// Display Instances cout << " " << objectSet.size() << " Instances(s):" << endl ; list<Object *>::const_iterator o ; for (o = objectSet.begin(); o != objectSet.end(); o++) { (*o)->display(); }}// ----------------------------------------------------------------------------//! getAttributeHandle.AttributeHandleObjectClass::getAttributeHandle(const char *the_name) const throw (NameNotFound, RTIinternalError){ list<ObjectClassAttribute *>::const_iterator a ; for (a = attributeSet.begin(); a != attributeSet.end(); a++) { if (strcmp((*a)->getName(), the_name) == 0) return (*a)->getHandle(); } D.Out(pdExcept, "ObjectClass %u: Attribute \"%s\" not defined.", handle, the_name); throw NameNotFound();}// ----------------------------------------------------------------------------//! getAttributeName.const char *ObjectClass::getAttributeName(AttributeHandle the_handle) const throw (AttributeNotDefined, RTIinternalError){ return getAttributeWithHandle(the_handle)->getName();}// ----------------------------------------------------------------------------//! getAttributeWithHandle (private module).ObjectClassAttribute *ObjectClass::getAttributeWithHandle(AttributeHandle the_handle) const throw (AttributeNotDefined){ list<ObjectClassAttribute *>::const_iterator a ; for (a = attributeSet.begin(); a != attributeSet.end(); a++) { if ((*a)->getHandle() == the_handle) return (*a); } D.Out(pdDebug, "ObjectClass %d: Attribute %d not defined.", handle, the_handle); throw AttributeNotDefined();}// ----------------------------------------------------------------------------//! getInstanceWithId (private module).Object *ObjectClass::getInstanceWithID(ObjectHandle the_id) const throw (ObjectNotKnown){ list<Object *>::const_iterator o ; for (o = objectSet.begin(); o != objectSet.end(); o++) { if ((*o)->getHandle() == the_id) return (*o); } throw ObjectNotKnown();}// ----------------------------------------------------------------------------//! Return true if the Federate is publishing any attribute of this class.boolObjectClass::isFederatePublisher(FederateHandle the_federate) const{ D.Out(pdRegister, "attributeSet.size() = %d.", attributeSet.size()); list<ObjectClassAttribute *>::const_iterator a ; for (a = attributeSet.begin(); a != attributeSet.end(); a++) { if ((*a)->isPublishing(the_federate) == RTI_TRUE) return true ; } return false ;}// ----------------------------------------------------------------------------//! Return true if the Federate has subscribe to any attribute.boolObjectClass::isFederateSubscriber(FederateHandle the_federate) const{ list<ObjectClassAttribute *>::const_iterator a ; for (a = attributeSet.begin(); a != attributeSet.end(); a++) { if ((*a)->hasSubscribed(the_federate) == RTI_TRUE) return true ; } return false ;}// ----------------------------------------------------------------------------/*! Return RTI_TRUE if the object instance designated by 'theID' is present in that class, else return RTI_FALSE.*/boolObjectClass::isInstanceInClass(ObjectHandle theID){ try { getInstanceWithID(theID); } catch (ObjectNotKnown &e) { return false ; } return true ;}// ----------------------------------------------------------------------------//! killFederate.ObjectClassBroadcastList *ObjectClass::killFederate(FederateHandle the_federate) throw (){ D.Out(pdRegister, "Object Class %d: Killing Federate %d.", handle, the_federate); try { // Does federate is publishing something ? (not important) if (isFederatePublisher(the_federate)) { publish(the_federate, NULL, 0, RTI_FALSE); } // Does federate subscribed something ? // Problem, no removeObject or discover must not be sent to it. if (isFederateSubscriber(the_federate)) { subscribe(the_federate, NULL, 0, RTI_FALSE); } } catch (SecurityError &e) {} // Does federate owns instances ? list<Object *>::iterator o ; for (o = objectSet.begin(); o != objectSet.end(); o++) { if ((*o)->getOwner() == the_federate) { // Return non-NULL to indicate that : // 1- A RemoveObject message should be broadcasted through parent // class // 2- The federate may own another instance, and this function // must be called again. // BUG: String \/ return deleteInstance(the_federate, (*o)->getHandle(), "Killed"); } } D.Out(pdRegister, "Object Class %d:Federate %d killed.", handle, the_federate); // Return NULL if the Federate did not own any instance. return NULL ;}// ----------------------------------------------------------------------------//! publish.voidObjectClass::publish(FederateHandle theFederateHandle, AttributeHandle *theAttributeList, UShort theListSize, bool PubOrUnpub) throw (AttributeNotDefined, RTIinternalError, SecurityError){ D.Out(pdInit, "Beginning of Publish for the federate %d", theFederateHandle); // Do all attribute handles exist ? It may throw AttributeNotDefined. for (UShort index = 0 ; index < theListSize ; index++) getAttributeWithHandle(theAttributeList[index]); // Check Security Levels checkFederateAccess(theFederateHandle, "Publish"); // Reset all previous publish information. D.Out(pdInit, "ObjectClass %d: Reset publish info of Federate %d.", handle, theFederateHandle); list<ObjectClassAttribute *>::const_iterator a ; for (a = attributeSet.begin(); a != attributeSet.end(); a++) { if ((*a)->isPublishing(theFederateHandle)) (*a)->unpublish(theFederateHandle); } // Publish attributes one by one. ObjectClassAttribute * attribute ; for (UShort i = 0 ; i < theListSize ; i++) { D.Out(pdInit, "ObjectClass %d: Federate %d publishes attribute %d.", handle, theFederateHandle, theAttributeList[i]); attribute = getAttributeWithHandle(theAttributeList[i]); if (PubOrUnpub) attribute->publish(theFederateHandle); else attribute->unpublish(theFederateHandle); }}// ----------------------------------------------------------------------------/*! Register a new object instance, and start to broadcast the DiscoverObject Message to class subscribers. Return a Broadcast List of Federates, in order to allow our ObjectClassSet to go on with the message broadcasting, by giving the list to our parent class.*/ObjectClassBroadcastList *ObjectClass::registerObjectInstance(FederateHandle the_federate, Object *the_object, ObjectClassHandle) throw (ObjectClassNotPublished, ObjectAlreadyRegistered, RTIinternalError){ D.Out(pdInit, "ObjectClass."); // Pre-conditions checking if (isInstanceInClass(the_object->getHandle())) { D.Out(pdExcept, "exception : ObjectAlreadyRegistered."); throw ObjectAlreadyRegistered(); } // This condition is only to be checked on the RTIG if ((server != NULL) && (!isFederatePublisher(the_federate))) { D.Out(pdExcept, "exception : ObjectClassNotPublished."); throw ObjectClassNotPublished(); } D.Out(pdInit, "ObjectClass1."); // Ownership management : // Copy instance attributes // Federate only owns attributes it publishes. ObjectAttribute * oa ; list<ObjectClassAttribute *>::reverse_iterator a ; for (a = attributeSet.rbegin(); a != attributeSet.rend(); a++) { if ((*a)->isPublishing(the_federate)) { oa = new ObjectAttribute((*a)->getHandle(), the_federate, *a); } else { oa = new ObjectAttribute((*a)->getHandle(), 0, *a); } // privilegeToDelete is owned by federate even not published. if (!strcmp((*a)->getName(), "privilegeToDelete")) { oa->setOwner(the_federate); } the_object->addAttribute(oa); } objectSet.push_front(the_object); // Prepare and Broadcast message for this class ObjectClassBroadcastList *ocbList = NULL ; if (server != NULL) { D.Out(pdRegister, "Object %u registered in class %u, now broadcasting...", the_object->getHandle(), handle); NetworkMessage *answer = new NetworkMessage ; answer->type = NetworkMessage::DISCOVER_OBJECT ; answer->federation = server->federation(); answer->federate = the_federate ; answer->exception = e_NO_EXCEPTION ; answer->objectClass = handle ; // Class Handle answer->object = the_object->getHandle(); strcpy(answer->label, the_object->getName()); ocbList = new ObjectClassBroadcastList(answer, 0); broadcastClassMessage(ocbList); } else { D.Out(pdRegister, "Object %u registered in class %u, no broadcast to do.", the_object->getHandle(), handle); } // Return the BroadcastList in case it had to be passed to the parent // class. return ocbList ;}// ----------------------------------------------------------------------------//! sendDiscoverMessages.BooleanObjectClass::sendDiscoverMessages(FederateHandle theFederate, ObjectClassHandle theOriginalClass){ // 1- If this class is not the original class, and the Federate is a // subscriber of the class, the Recursive process must be stopped, // because the Federate must have received all previous DiscoverObject // Message for this class and its sub-classes. if ((handle != theOriginalClass) && (isFederateSubscriber(theFederate))) { return RTI_FALSE ; } // 2- If there is no instance of the class, return.(the recursive process // must continue). if (objectSet.empty()) return RTI_TRUE ; // 3- Else prepare the common part of the Message. // Messages are sent on behalf of the original class. NetworkMessage *message = new NetworkMessage ; message->type = NetworkMessage::DISCOVER_OBJECT ; message->federation = server->federation(); message->federate = theFederate ; message->exception = e_NO_EXCEPTION ; message->objectClass = theOriginalClass ; // 4- For each Object instance in the class, send a Discover message. Socket *socket = NULL ; list<Object *>::const_iterator o ; for (o = objectSet.begin(); o != objectSet.end(); o++) { D.Out(pdInit, "Sending DiscoverObj to Federate %d for Object %u in class %u ", theFederate, (*o)->getHandle(), handle, message->label); message->object = (*o)->getHandle(); (*o)->getName(message->label); // Send Message to Federate try { socket = server->getSocketLink(theFederate); message->write(socket); } catch (RTIinternalError &e) { D.Out(pdExcept, "Reference to a killed Federate while sending DO msg."); } catch (NetworkError &e) { D.Out(pdExcept, "Network error while sending DO msg, ignoring."); } } // for each object instance delete message ; // 5- The same method must be called on my sub-classes. return RTI_TRUE ;}// ----------------------------------------------------------------------------//! setName.voidObjectClass::setName(const char *new_name) throw (ValueLengthExceeded, RTIinternalError){ // Check Length if ((new_name == NULL) || (strlen(new_name) > MAX_USER_TAG_LENGTH)) { D.Out(pdExcept, "Object class Name %s too long.", new_name); throw ValueLengthExceeded("Object class name too long."); } // Free previous name if (Name != NULL) free(Name); // Store new name Name = strdup(new_name); if (Name == NULL) throw RTIinternalError("Memory Exhausted.");}// ----------------------------------------------------------------------------//! A class' LevelID can only be increased.voidObjectClass::setLevelId(SecurityLevelID new_levelID)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -