# ObjectSet.cc

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // CERTI - HLA RunTime Infrastructure
    00003 // Copyright (C) 2002-2005  ONERA
    00004 //
    00005 // This file is part of CERTI-libCERTI
    00006 //
    00007 // CERTI-libCERTI is free software ; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Lesser General Public License
    00009 // as published by the Free Software Foundation ; either version 2 of
    00010 // the License, or (at your option) any later version.
    00011 //
    00012 // CERTI-libCERTI is distributed in the hope that it will be useful, but
    00013 // WITHOUT ANY WARRANTY ; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00015 // Lesser General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Lesser General Public
    00018 // License along with this program ; if not, write to the Free Software
    00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    00020 // USA
    00021 //
    00022 // ----------------------------------------------------------------------------
    00023
    00024
    00025
    00026 // Project
    00027 #include "Object.hh"
    00028 #include "ObjectAttribute.hh"
    00029 #include "ObjectSet.hh"
    00030 #include "PrettyDebug.hh"
    00031 #include "NM_Classes.hh"
    00032
    00033 // Standard
    00034 #include <iostream>
    00035 #include <sstream>
    00036
    00037 using std::pair ;
    00038 using std::cout ;
    00039 using std::endl ;
    00040 using std::ostringstream ;
    00041 using std::string ;
    00042
    00043 namespace certi {
    00044
    00045 static pdCDebug D("OBJECTSET", "(ObjectSet) - ");
    00046 static PrettyDebug G("GENDOC",__FILE__);
    00047
    00048 // ----------------------------------------------------------------------------
    00049 ObjectSet::ObjectSet(SecurityServer *the_server)
    00050     : server(the_server)
    00051 {
    00052 }
    00053
    00054 // ----------------------------------------------------------------------------
    00055 ObjectSet::~ObjectSet()
    00056 {
    00057     std::map<ObjectHandle, Object *>::iterator i ;
    00058
    00059     for (i = begin(); i != end(); i++) {
    00060         delete i->second ;
    00061     }
    00062     erase(begin(), end());
    00063 }
    00064
    00065 // ----------------------------------------------------------------------------
    00066 void
    00067 ObjectSet::changeAttributeTransportationType(ObjectHandle,
    00068                                              AttributeHandle *,
    00069                                              UShort,
    00070                                              TransportType)
    00071     throw (ObjectNotKnown,
    00072            AttributeNotDefined,
    00073            AttributeNotOwned,
    00074            RTIinternalError,
    00075            InvalidObjectHandle)
    00076 {
    00077     // Object *object = getObject(the_object);
    00078 }
    00079
    00080 // ----------------------------------------------------------------------------
    00081 void
    00082 ObjectSet::changeAttributeOrderType(ObjectHandle,
    00083                                     AttributeHandle *,
    00084                                     UShort,
    00085                                     TransportType)
    00086     throw (ObjectNotKnown,
    00087            AttributeNotDefined,
    00088            AttributeNotOwned,
    00089            RTIinternalError,
    00090            InvalidObjectHandle)
    00091 {
    00092     // Object *object = getObject(the_object);
    00093 }
    00094
    00095 // ----------------------------------------------------------------------------
    00096 ObjectHandle
    00097 ObjectSet::getObjectInstanceHandle(std::string the_name) const
    00098     throw (ObjectNotKnown, RTIinternalError)
    00099 {
    00100     std::stringstream msg;
    00101
    00102     std::map<ObjectHandle, Object *>::const_iterator i ;
    00103     for (i = begin(); i != end(); i++) {
    00104         if (i->second->getName() == the_name)
    00105             return i->second->getHandle();
    00106     }
    00107
    00108     msg << "No object instance with name <" << the_name;
    00109     throw ObjectNotKnown(msg.str().c_str());
    00110 }
    00111
    00112 // ----------------------------------------------------------------------------
    00113 const char *
    00114 ObjectSet::getObjectInstanceName(ObjectHandle the_object) const
    00115     throw (ObjectNotKnown, RTIinternalError)
    00116 {
    00117     Object *object = getObject(the_object);
    00118
    00119     return object->getName().c_str();
    00120 }
    00121
    00122 // ----------------------------------------------------------------------------
    00123 ObjectClassHandle
    00124 ObjectSet::getObjectClass(ObjectHandle the_object) const
    00125     throw (ObjectNotKnown, FederateNotExecutionMember,
    00126            ConcurrentAccessAttempted, RTIinternalError)
    00127 {
    00128     return getObject(the_object)->getClass();
    00129 }
    00130
    00131 // ----------------------------------------------------------------------------
    00132 Object *
    00133 ObjectSet::registerObjectInstance(FederateHandle the_federate,
    00134                   ObjectClassHandle the_class,
    00135                                   ObjectHandle the_object,
    00136                                   std::string the_name)
    00137     throw (ObjectAlreadyRegistered, ConcurrentAccessAttempted,
    00138            SaveInProgress, RestoreInProgress, RTIinternalError)
    00139 {
    00140     const_iterator i ;
    00141
    00142     i = find(the_object);
    00143
    00144     if (i != end()) {
    00145         throw ObjectAlreadyRegistered("Object already in ObjectSet map.");
    00146     }
    00147
    00148     if (the_name.size() > 0) {
    00149         for (i = begin(); i != end(); i++) {
    00150             if (i->second->getName() == the_name)
    00151                 throw ObjectAlreadyRegistered("Object name already defined.");
    00152         }
    00153     }
    00154
    00155     Object *object = new Object(the_federate);
    00156     object->setHandle(the_object);
    00157     object->setClass(the_class);
    00158
    00159     if (the_name.size() > 0) {
    00160         object->setName(the_name);
    00161     }
    00162     else {
    00163         ostringstream tmp ;
    00164         tmp << "HLAobject_" << the_object ;
    00165         object->setName(tmp.str());
    00166     }
    00167
    00168     pair<ObjectHandle, Object *> tmp(the_object, object);
    00169     insert(tmp);
    00170
    00171     return object ;
    00172 }
    00173
    00174 // ----------------------------------------------------------------------------
    00175 void
    00176 ObjectSet::deleteObjectInstance(FederateHandle,
    00177                                 ObjectHandle the_object,
    00178                                 std::string the_tag)
    00179     throw (ObjectNotKnown,
    00180            DeletePrivilegeNotHeld,
    00181            FederateNotExecutionMember,
    00182            ConcurrentAccessAttempted,
    00183            SaveInProgress,
    00184            RestoreInProgress,
    00185            RTIinternalError)
    00186 {
    00187     Object *object = getObject(the_object);
    00188
    00189     delete object ; // Remove the Object instance.
    00190
    00191     std::map<ObjectHandle, Object *>::erase(the_object);
    00192 }
    00193
    00194 // ----------------------------------------------------------------------------
    00195 void
    00196 ObjectSet::killFederate(FederateHandle the_federate)
    00197     throw (RTIinternalError)
    00198 {
    00199     std::map<ObjectHandle, Object *>::iterator i ;
    00200     for (i = begin(); i != end(); i++) {
    00201         if ((i->second)->getOwner() == the_federate) {
    00202             std::map<ObjectHandle, Object *>::erase(i);
    00203             i = begin();
    00204         }
    00205     }
    00206 }
    00207
    00208 // ----------------------------------------------------------------------------
    00209 bool
    00210 ObjectSet::isAttributeOwnedByFederate(FederateHandle the_federate,
    00211                                       ObjectHandle the_object,
    00212                                       AttributeHandle the_attribute) const
    00213     throw (ObjectNotKnown, AttributeNotDefined, RTIinternalError)
    00214 {
    00215     D.Out(pdDebug, "isAttributeOwnedByFederate called for attribute %u, "
    00216           "objet %u", the_attribute, the_object);
    00217
    00218     Object *object = getObject(the_object);
    00219
    00220     if (server == 0) {
    00221         throw RTIinternalError("isAttributeOwnedByFederate not called by RTIG");
    00222     }
    00223
    00224     return object->isAttributeOwnedByFederate(the_federate, the_attribute);
    00225 }
    00226
    00227 // ----------------------------------------------------------------------------
    00228 void
    00229 ObjectSet::queryAttributeOwnership(FederateHandle the_federate,
    00230                                    ObjectHandle the_object,
    00231                                    AttributeHandle the_attribute) const
    00232     throw (ObjectNotKnown, AttributeNotDefined, RTIinternalError)
    00233 {
    00234     Object *object = getObject(the_object);
    00235
    00236     D.Out(pdDebug, "query attribute ownership for attribute %u and object %u",
    00237           the_attribute, the_object);
    00238
    00239     if (server) {
    00240         ObjectAttribute * oa ;
    00241         oa = object->getAttribute(the_attribute);
    00242
    00243         NetworkMessage *answer;
    00244         if (oa->getOwner()) {
    00245             answer = NM_Factory::create(NetworkMessage::INFORM_ATTRIBUTE_OWNERSHIP);
    00246         } else {
    00247             answer = NM_Factory::create(NetworkMessage::ATTRIBUTE_IS_NOT_OWNED);
    00248         }
    00249
    00250         answer->federation = server->federation();
    00251         answer->exception = e_NO_EXCEPTION ;
    00252         answer->object = the_object ;
    00253         answer->handleArray.resize(1) ;
    00254         answer->handleArray[0] = the_attribute ;
    00255         answer->federate = oa->getOwner();
    00256
    00257         sendToFederate(answer, the_federate);
    00258     }
    00259     else {
    00260         D.Out(pdDebug, "Should only be called by RTIG");
    00261     }
    00262 }
    00263
    00264 // ----------------------------------------------------------------------------
    00265 void ObjectSet::
    00266 negotiatedAttributeOwnershipDivestiture(FederateHandle,
    00267                                         ObjectHandle,
    00268                                         AttributeHandle *,
    00269                                         UShort,
    00270                                         const char *)
    00271     throw (ObjectNotKnown,
    00272            AttributeNotDefined,
    00273            AttributeNotOwned,
    00274            AttributeAlreadyBeingDivested,
    00275            RTIinternalError)
    00276 {
    00277     // Object *object = getObject(the_object);
    00278 }
    00279
    00280 // ----------------------------------------------------------------------------
    00281 void ObjectSet::
    00282 attributeOwnershipAcquisitionIfAvailable(FederateHandle,
    00283                                          ObjectHandle,
    00284                                          AttributeHandle *,
    00285                                          UShort)
    00286     throw (ObjectNotKnown,
    00287            ObjectClassNotPublished,
    00288            AttributeNotDefined,
    00289            AttributeNotPublished,
    00290            FederateOwnsAttributes,
    00291            AttributeAlreadyBeingAcquired,
    00292            RTIinternalError)
    00293 {
    00294     // Object *object = getObject(the_object);
    00295 }
    00296
    00297 // ----------------------------------------------------------------------------
    00298 void ObjectSet::
    00299 unconditionalAttributeOwnershipDivestiture(FederateHandle,
    00300                                            ObjectHandle,
    00301                                            AttributeHandle *,
    00302                                            UShort)
    00303     throw (ObjectNotKnown,
    00304            AttributeNotDefined,
    00305            AttributeNotOwned,
    00306            RTIinternalError)
    00307 {
    00308     // Object *object = getObject(the_object);
    00309 }
    00310
    00311 // ----------------------------------------------------------------------------
    00312 void
    00313 ObjectSet::attributeOwnershipAcquisition(FederateHandle,
    00314                                          ObjectHandle,
    00315                                          AttributeHandle *,
    00316                                          UShort,
    00317                                          const char *)
    00318
    00319     throw (ObjectNotKnown,
    00320            ObjectClassNotPublished,
    00321            AttributeNotDefined,
    00322            AttributeNotPublished,
    00323            FederateOwnsAttributes,
    00324            RTIinternalError)
    00325 {
    00326     // Object *object = getObject(the_object);
    00327 }
    00328
    00329 // ----------------------------------------------------------------------------
    00330 void ObjectSet::
    00331 cancelNegotiatedAttributeOwnershipDivestiture(FederateHandle the_federate,
    00332                                               ObjectHandle the_object,
    00333                                               std::vector <AttributeHandle> &the_attributes,
    00334                                               UShort the_size)
    00335     throw (ObjectNotKnown,
    00336            AttributeNotDefined,
    00337            AttributeNotOwned,
    00338            AttributeDivestitureWasNotRequested,
    00339            RTIinternalError)
    00340 {
    00341     Object *object = getObject(the_object);
    00342
    00343     ObjectAttribute * oa ;
    00344     for (int i = 0 ; i < the_size ; i++) {
    00345         oa = object->getAttribute(the_attributes[i]);
    00346
    00347         // Does federate owns every attributes.
    00348         if (oa->getOwner() != the_federate)
    00349             throw AttributeNotOwned("");
    00350         // Does federate called NegotiatedAttributeOwnershipDivestiture
    00351         if (!oa->beingDivested())
    00352             throw AttributeDivestitureWasNotRequested("");
    00353     }
    00354
    00355     if (server != NULL) {
    00356         for (int i = 0 ; i < the_size ; i++) {
    00357             oa = object->getAttribute(the_attributes[i]);
    00358             oa->setDivesting(false);
    00359         }
    00360     }
    00361     else {
    00362         D.Out(pdExcept, "CancelNegotiatedAttributeOwnershipDivestiture should "
    00363               "not be called on the RTIA.");
    00364         throw RTIinternalError("CancelNegotiatedAttributeOwnershipDivestiture "
    00365                                "called on the RTIA.");
    00366     }
    00367 }
    00368
    00369 // ----------------------------------------------------------------------------
    00370 AttributeHandleSet *
    00371 ObjectSet::attributeOwnershipReleaseResponse(FederateHandle,
    00372                                              ObjectHandle,
    00373                                              std::vector <AttributeHandle> &,
    00374                                              UShort)
    00375     throw (ObjectNotKnown,
    00376            AttributeNotDefined,
    00377            AttributeNotOwned,
    00378            FederateWasNotAskedToReleaseAttribute,
    00379            RTIinternalError)
    00380 {
    00381     // Object *object = getObject(the_object);
    00382
    00383     return 0 ;
    00384 }
    00385
    00386 // ----------------------------------------------------------------------------
    00387 void
    00388 ObjectSet::cancelAttributeOwnershipAcquisition(FederateHandle,
    00389                                                ObjectHandle,
    00390                                                std::vector <AttributeHandle> &,
    00391                                                UShort)
    00392     throw (ObjectNotKnown,
    00393            AttributeNotDefined,
    00394            AttributeAlreadyOwned,
    00395            AttributeAcquisitionWasNotRequested,
    00396            RTIinternalError)
    00397 {
    00398     // Object *object = getObject(the_object);
    00399 }
    00400
    00401 // ----------------------------------------------------------------------------
    00402 Object *
    00403 ObjectSet::getObject(ObjectHandle the_object) const
    00404     throw (ObjectNotKnown)
    00405 {
    00406     std::map<ObjectHandle, Object *>::const_iterator i ;
    00407     i = find(the_object);
    00408
    00409     if (i != end())
    00410         return i->second ;
    00411
    00412     throw ObjectNotKnown("Object not found in map set.");
    00413 }
    00414
    00415 // ----------------------------------------------------------------------------
    00417 void
    00418 ObjectSet::sendToFederate(NetworkMessage *msg,
    00419                           FederateHandle the_federate) const
    00420 {
    00421     // Send the message 'msg' to the Federate which Handle is theFederate.
    00422     Socket *socket = NULL ;
    00423     try {
    00424 #ifdef HLA_USES_UDP
    00425         socket = server->getSocketLink(the_federate, BEST_EFFORT);
    00426 #else
    00427         socket = server->getSocketLink(the_federate);
    00428 #endif
    00429         msg->send(socket,const_cast<MessageBuffer&>(NM_msgBufSend));
    00430     }
    00431     catch (RTIinternalError &e) {
    00432         D.Out(pdExcept,
    00433               "Reference to a killed Federate while broadcasting.");
    00434     }
    00435     catch (NetworkError &e) {
    00436         D.Out(pdExcept, "Network error while broadcasting, ignoring.");
    00437     }
    00438     // BUG: If except = 0, could use Multicast.
    00439 }
    00440 // ----------------------------------------------------------------------------
    00441 FederateHandle
    00442 ObjectSet::requestObjectOwner(FederateHandle the_federate,
    00443                                   ObjectHandle the_object)
    00444     throw (ObjectNotKnown)
    00445 {
    00446     G.Out(pdGendoc,"enter ObjectSet::requestObjectOwner");
    00447     const_iterator i ;
    00448     i = find(the_object);
    00449
    00450     if (i == end())
    00451        {
    00452        // Object not found !
    00453        throw ObjectNotKnown("Object not found in ObjectSet map.");
    00454        }
    00455
    00456     // Object found, return the owner
    00457     G.Out(pdGendoc,"exit  ObjectSet::requestObjectOwner");
    00458     return ( i->second->getOwner()) ;
    00459 }
    00460 } // namespace certi
    00461
    00462 // $Id: ObjectSet.cc,v 3.22 2008/06/10 13:41:47 rousse Exp $