objectclass.cc~
来自「certi-SHM-3.0.tar 不错的开源的分布式方针软件 大家多多支持 他」· CC~ 代码 · 共 1,185 行 · 第 1/3 页
CC~
1,185 行
// ----------------------------------------------------------------------------//! 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 RTI_TRUE if the Federate is publishing any attribute of this class.BooleanObjectClass::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 RTI_TRUE ; } return RTI_FALSE ;}// ----------------------------------------------------------------------------//! Return RTI_TRUE if the Federate has subscribe to any attribute.BooleanObjectClass::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 RTI_TRUE ; } return RTI_FALSE ;}// ----------------------------------------------------------------------------/*! Return RTI_TRUE if the object instance designated by 'theID' is present in that class, else return RTI_FALSE.*/BooleanObjectClass::isInstanceInClass(ObjectHandle theID){ try { getInstanceWithID(theID); } catch (ObjectNotKnown &e) { return RTI_FALSE ; } return RTI_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) == RTI_TRUE) 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) == RTI_TRUE) 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)->Owner == 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) == RTI_TRUE) (*a)->publish(theFederateHandle, RTI_FALSE); } // 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]); attribute->publish(theFederateHandle, PubOrUnpub); }}// ----------------------------------------------------------------------------/*! 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::registerInstance(FederateHandle the_federate_handle, ObjectHandle the_object_handle, const char *the_object_name) throw (ObjectClassNotPublished, ObjectAlreadyRegistered, RTIinternalError){ D.Out(pdInit, "ObjectClass."); // Pre-conditions checking if (isInstanceInClass(the_object_handle) == RTI_TRUE) { D.Out(pdExcept, "exception : ObjectAlreadyRegistered."); throw ObjectAlreadyRegistered(); } // This condition is only to be checked on the RTIG if ((server != NULL) && (isFederatePublisher(the_federate_handle) != RTI_TRUE)) { D.Out(pdExcept, "exception : ObjectClassNotPublished."); throw ObjectClassNotPublished(); } D.Out(pdInit, "ObjectClass1."); // Register new Object. Object *object = new Object(the_federate_handle); object->setHandle(the_object_handle); if (the_object_name != NULL) object->setName(the_object_name); // 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_handle)) oa = new ObjectAttribute((*a)->getHandle(), the_federate_handle); else oa = new ObjectAttribute((*a)->getHandle(), 0); // privilegeToDelete is owned by federate even not published. if (strcmp((*a)->getName(), "privilegeToDelete") == 0) oa->setOwner(the_federate_handle); object->addAttribute(oa); } objectSet.push_front(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_handle, handle); NetworkMessage *answer = new NetworkMessage ; answer->type = m_DISCOVER_OBJECT ; answer->federation = server->federation(); answer->federate = the_federate_handle ; answer->exception = e_NO_EXCEPTION ; answer->objectClass = handle ; // Class Handle answer->object = the_object_handle ; strcpy(answer->label, the_object_name); ocbList = new ObjectClassBroadcastList(answer, 0); broadcastClassMessage(ocbList); } else { D.Out(pdRegister, "Object %u registered in class %u, no broadcast to do.", the_object_handle, 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) == RTI_TRUE)) 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 = m_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){ if (server->dominates(new_levelID, LevelID) == RTI_FALSE) throw SecurityError("Attempt to lower object class level."); LevelID = new_levelID ;}// ----------------------------------------------------------------------------/*! Return RTI_TRUE if theFederate had never subscribed to this class before. In that case, ObjectClassSet will call SendDiscoverMessages on this class and on all child classes to allow them to send Discover Messages for already registered instances.*/BooleanObjectClass::subscribe(FederateHandle theFederate, AttributeHandle *theAttributeList, UShort theListSize, bool SubOrUnsub) throw (AttributeNotDefined, RTIinternalError, SecurityError){ // Check Security Levels checkFederateAccess(theFederate, "Subscribe"); // Do all attribute handles exist ? It may throw AttributeNotDefined. for (UShort index = 0 ; index < theListSize ; index++) getAttributeWithHandle(theAttributeList[index]); // Save the Federate number. if (theFederate > MaxSubscriberHandle) MaxSubscriberHandle = theFederate ; // A new subscribtion invalidates all previous subscription to the same // object class, so we first remove all previous subscription information. D.Out(pdInit, "ObjectClass %d: Resetting previous Sub info of Federate %d.", handle, theFederate); Boolean wasPreviousSubscriber = RTI_FALSE ; list<ObjectClassAttribute *>::iterator a ; for (a = attributeSet.begin(); a != attributeSet.end(); a++) { if ((*a)->hasSubscribed(theFederate) == RTI_TRUE) { (*a)->subscribe(theFederate, RTI_FALSE); wasPreviousSubscriber = RTI_TRUE ; } } // Subscribe to attributes one by one. ObjectClassAttribute *attribute ;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?