# DataDistribution.cc

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // CERTI - HLA RunTime Infrastructure
    00003 // Copyright (C) 2003-2005  ONERA
    00004 //
    00005 // This file is part of CERTI
    00006 //
    00007 // CERTI is free software ; you can redistribute it and/or modify
    00008 // it under the terms of the GNU General Public License as published by
    00009 // the Free Software Foundation ; either version 2 of the License, or
    00010 // (at your option) any later version.
    00011 //
    00012 // CERTI is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY ; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    00015 // GNU General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU General Public License
    00018 // along with this program ; if not, write to the Free Software
    00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    00020 //
    00021 // $Id: DataDistribution.cc,v 3.27 2008/11/08 11:36:05 erk Exp $
    00022 // ----------------------------------------------------------------------------
    00023
    00024 #include <config.h>
    00025 #include "DataDistribution.hh"
    00026
    00027 #include "Interaction.hh"
    00028 #include "InteractionSet.hh"
    00029 #include "Object.hh"
    00030 #include "ObjectAttribute.hh"
    00031 #include "ObjectClass.hh"
    00032 #include "ObjectClassSet.hh"
    00033 #include "ObjectClassAttribute.hh"
    00034 #include "RoutingSpace.hh"
    00035 #include "FedRegion.hh"
    00036 #include "NM_Classes.hh"
    00037
    00038 #include <cassert>
    00039 #include <memory>
    00040
    00041 using std::string ;
    00042 using std::endl ;
    00043
    00044 namespace certi {
    00045 namespace rtia {
    00046
    00047 static pdCDebug D("RTIA_DDM", __FILE__);
    00048
    00049 // ----------------------------------------------------------------------------
    00050 DataDistribution::DataDistribution(RootObject *root_object,
    00051                                    FederationManagement *fed_management,
    00052                                    Communications *communications)
    00053     : rootObject(root_object),
    00054       fm(fed_management),
    00055       comm(communications) { }
    00056
    00057 // ----------------------------------------------------------------------------
    00058 SpaceHandle
    00059 DataDistribution::getRoutingSpaceHandle(std::string name) const
    00060 {
    00061     return rootObject->getRoutingSpaceHandle(name);
    00062 }
    00063
    00064 // ----------------------------------------------------------------------------
    00065 // getRoutingSpaceName
    00066 //
    00067 string
    00068 DataDistribution::getRoutingSpaceName(SpaceHandle handle) const
    00069 {
    00070     return rootObject->getRoutingSpaceName(handle);
    00071 }
    00072
    00073 // ----------------------------------------------------------------------------
    00074 // getDimensionHandle
    00075 //
    00076 DimensionHandle
    00077 DataDistribution::getDimensionHandle(std::string dimension, SpaceHandle space) const
    00078     throw (SpaceNotDefined, NameNotFound)
    00079 {
    00080     return rootObject->getRoutingSpace(space).getDimensionHandle(dimension);
    00081 }
    00082
    00083 // ----------------------------------------------------------------------------
    00084 // getDimensionName
    00085 //
    00086 string
    00087 DataDistribution::getDimensionName(DimensionHandle dimension,
    00088                    SpaceHandle space) const
    00089     throw (SpaceNotDefined, DimensionNotDefined)
    00090 {
    00091     return rootObject->getRoutingSpace(space).getDimensionName(dimension);
    00092 }
    00093
    00094 // ----------------------------------------------------------------------------
    00095 // getAttributeSpaceHandle
    00096 //
    00097 SpaceHandle
    00098 DataDistribution::getAttributeSpace(AttributeHandle attribute,
    00099                                     ObjectClassHandle object_class) const
    00100     throw (ObjectClassNotDefined, AttributeNotDefined)
    00101 {
    00102 std::cout<<"DataDistribution::getAttributeSpace"<<std::endl;
    00103     return rootObject->ObjectClasses->getObjectFromHandle(object_class)->
    00104         getAttribute(attribute)->getSpace();
    00105 }
    00106
    00107 // ----------------------------------------------------------------------------
    00108 // getInteractionSpaceHandle
    00109 //
    00110 SpaceHandle
    00111 DataDistribution::getInteractionSpace(InteractionClassHandle interaction) const
    00112     throw (InteractionClassNotDefined)
    00113 {
    00114     return rootObject->Interactions->getObjectFromHandle(interaction)->getSpace();
    00115 }
    00116
    00117 // ----------------------------------------------------------------------------
    00118 // createRegion
    00119 //
    00120 long
    00121 DataDistribution::createRegion(SpaceHandle space,
    00122                                unsigned long nb_extents,
    00123                                TypeException &e)
    00124     throw (SpaceNotDefined)
    00125 {
    00126     D[pdDebug] << "Start creating region in space " << space << "..." << endl ;
    00127     NM_DDM_Create_Region req;
    00128
    00129     req.federation = fm->_numero_federation ;
    00130     req.federate = fm->federate ;
    00131     req.space = space ;
    00132     req.nbExtents = nb_extents ;
    00133
    00134     comm->sendMessage(&req);
    00135     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_CREATE_REGION, req.federate));
    00136     e = rep->exception ;
    00137
    00138     if (e == e_NO_EXCEPTION) {
    00139     D[pdDebug] << "Create region " << rep->region << endl ;
    00140         RTIRegion *region = new RTIRegion(rep->region,
    00141                       rootObject->getRoutingSpace(space),
    00142                       nb_extents);
    00143
    00144     assert(region->getNumberOfExtents() == nb_extents);
    00145         rootObject->addRegion(region);
    00146         return rep->region;
    00147     }
    00148     else {
    00149         return 0 ;
    00150     }
    00151 } /* end of createRegion */
    00152
    00153 // ----------------------------------------------------------------------------
    00154 // modifyRegion
    00155 //
    00156 void
    00157 DataDistribution::modifyRegion(RegionHandle handle,
    00158                    const std::vector<Extent> &extents,
    00159                    TypeException &e)
    00160 {
    00161     D[pdDebug] << "Modify region " << handle << "..." << endl ;
    00162
    00163     // check region
    00164     RTIRegion *region = rootObject->getRegion(handle);
    00165
    00166     // Request to RTIG
    00167     NM_DDM_Modify_Region req;
    00168
    00169     req.federation = fm->_numero_federation ;
    00170     req.federate = fm->federate ;
    00171     req.region = handle ;
    00172     req.setExtents(extents);
    00173
    00174     comm->sendMessage(&req);
    00175     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_MODIFY_REGION, req.federate));
    00176     e = rep->exception ;
    00177
    00178     if (e == e_NO_EXCEPTION) {
    00179     region->replaceExtents(extents);
    00180     D[pdDebug] << "Modified region " << handle << endl ;
    00181     }
    00182 } /* end of modifyRegion */
    00183
    00184 // ----------------------------------------------------------------------------
    00185 // deleteRegion
    00186 //
    00187 void
    00188 DataDistribution::deleteRegion(long handle, TypeException &e)
    00189     throw (RegionNotKnown, RegionInUse)
    00190 {
    00191     D[pdDebug] << "Delete region " << handle << "..." << endl ;
    00192
    00193     // check region
    00194     rootObject->getRegion(handle);
    00195
    00196     // Request to RTIG
    00197     NM_DDM_Delete_Region req;
    00198
    00199     req.federation = fm->_numero_federation ;
    00200     req.federate = fm->federate ;
    00201     req.region = handle ;
    00202
    00203     comm->sendMessage(&req);
    00204     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_DELETE_REGION, req.federate));
    00205     e = rep->exception ;
    00206
    00207     if (e == e_NO_EXCEPTION) {
    00208         rootObject->deleteRegion(handle);
    00209         D[pdDebug] << "Deleted region " << handle << endl ;
    00210     }
    00211 } /* end of deleteRegion */
    00212
    00213 // ----------------------------------------------------------------------------
    00214 void
    00215 DataDistribution::associateRegion(ObjectHandle object,
    00216                   RegionHandle region,
    00217                   std::vector <AttributeHandle> &attr,
    00218                   int nb,
    00219                   TypeException &e)
    00220     throw (RegionNotKnown)
    00221 {
    00222     D[pdDebug] << "Associate Region " << region << std::endl ;
    00223
    00224     RTIRegion *r = rootObject->getRegion(region);
    00225
    00226     D[pdDebug] << "- unassociate object " << object << std::endl ;
    00227     rootObject->getObject(object)->unassociate(r);
    00228     for (int i = 0 ; i < nb ; ++i) {
    00229     D[pdDebug] << "- associate attribute " << attr[i] << std::endl ;
    00230     rootObject->getObjectAttribute(object, attr[i])->associate(r);
    00231     }
    00232
    00233     NM_DDM_Associate_Region req;
    00234
    00235     req.federation = fm->_numero_federation ;
    00236     req.federate = fm->federate ;
    00237     req.object = object ;
    00238     req.region = region ;
    00239
    00240     req.setAHS(attr, nb);
    00241
    00242     comm->sendMessage(&req);
    00243     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_ASSOCIATE_REGION,req.federate));
    00244
    00245     e = rep->exception ;
    00246
    00247 } /* end of associateRegion */
    00248
    00249 // ----------------------------------------------------------------------------
    00250 ObjectHandle
    00251 DataDistribution::registerObject(ObjectClassHandle class_handle,
    00252                  const std::string name,
    00253                  const std::vector <AttributeHandle> &attrs,
    00254                  int nb,
    00255                  const std::vector<RegionHandle> regions,
    00256                  TypeException &e)
    00257 {
    00258     D[pdDebug] << "Register object of class " << class_handle << " with "
    00259            << regions.size() << " region(s)." << std::endl ;
    00260
    00261     NM_DDM_Register_Object req;
    00262
    00263     req.federation = fm->_numero_federation ;
    00264     req.federate = fm->federate ;
    00265     req.objectClass = class_handle ;
    00266     req.setTag(name.c_str());
    00267     req.setAHS(attrs, nb);
    00268     req.setRegions(regions);
    00269
    00270     comm->sendMessage(&req);
    00271     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_REGISTER_OBJECT,req.federate));
    00272
    00273     e = rep->exception ;
    00274
    00275     if (e == e_NO_EXCEPTION) {
    00276         rootObject->registerObjectInstance(fm->federate, class_handle, rep->object,
    00277                                            rep->getLabel().c_str());
    00278     for (int i = 0 ; i < nb ; ++i) {
    00279         D[pdDebug] << "Register attribute [" << i << "] Attr: " << attrs[i]
    00280                << " Region: " << regions[i] << std::endl ;
    00281
    00282         ObjectAttribute *attribute = rootObject->getObjectAttribute(rep->object, attrs[i]);
    00283         RTIRegion *region = rootObject->getRegion(regions[i]);
    00284         attribute->associate(region);
    00285     }
    00286         return rep->object;
    00287     }
    00288     else return 0 ;
    00289 } /* end of registerObject */
    00290
    00291 // ----------------------------------------------------------------------------
    00292 void
    00293 DataDistribution::unassociateRegion(ObjectHandle object,
    00294                     RegionHandle region,
    00295                     TypeException &e)
    00296     throw (ObjectNotKnown, InvalidRegionContext, RegionNotKnown)
    00297 {
    00298     D[pdDebug] << "Unassociate Region " << region << std::endl ;
    00299
    00300     RTIRegion *r = rootObject->getRegion(region);
    00301
    00302     rootObject->getObject(object)->unassociate(r);
    00303
    00304     NM_DDM_Unassociate_Region req;
    00305
    00306     req.federation = fm->_numero_federation ;
    00307     req.federate = fm->federate ;
    00308     req.object = object ;
    00309     req.region = region ;
    00310
    00311     comm->sendMessage(&req);
    00312     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_UNASSOCIATE_REGION,
    00313               req.federate));
    00314
    00315    e = rep->exception ;
    00316 } /* end of unassociateRegion */
    00317
    00318 // ----------------------------------------------------------------------------
    00319 void
    00320 DataDistribution::subscribe(ObjectClassHandle obj_class,
    00321                 RegionHandle region,
    00322                 std::vector <AttributeHandle> &attr,
    00323                 int nb,
    00324                 TypeException &e)
    00325     throw (RegionNotKnown)
    00326 {
    00327     D[pdDebug] << "Subscribe attributes with region " << region << endl ;
    00328     rootObject->getRegion(region);
    00329
    00330     NM_DDM_Subscribe_Attributes req;
    00331
    00332     req.federation = fm->_numero_federation ;
    00333     req.federate = fm->federate ;
    00334     req.objectClass = obj_class ;
    00335     req.region = region ;
    00336     req.setAHS(attr, nb);
    00337
    00338     comm->sendMessage(&req);
    00339     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_SUBSCRIBE_ATTRIBUTES,
    00340               req.federate));
    00341
    00342     e = rep->exception ;
    00343 } /* end of subscribe */
    00344
    00345 // ----------------------------------------------------------------------------
    00346 void
    00347 DataDistribution::unsubscribeAttributes(ObjectClassHandle obj_class,
    00348                     RegionHandle region,
    00349                     TypeException &e)
    00350     throw (RegionNotKnown)
    00351 {
    00352     D[pdDebug] << "Unsubscribe class " << obj_class
    00353            << " with region " << region << endl ;
    00354     rootObject->getRegion(region);
    00355
    00356     NM_DDM_Unsubscribe_Attributes req;
    00357
    00358     req.federation = fm->_numero_federation ;
    00359     req.federate = fm->federate ;
    00360     req.objectClass = obj_class ;
    00361     req.region = region ;
    00362
    00363     comm->sendMessage(&req);
    00364     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_UNSUBSCRIBE_ATTRIBUTES,
    00365               req.federate));
    00366
    00367     e = rep->exception ;
    00368 } /* end of unsubscribeAttributes */
    00369
    00370 // ----------------------------------------------------------------------------
    00371 void
    00372 DataDistribution::subscribeInteraction(InteractionClassHandle int_class,
    00373                        RegionHandle region,
    00374                        TypeException &e)
    00375     throw (RegionNotKnown)
    00376 {
    00377     D[pdDebug] << "Subscribe interaction with region " << region << endl ;
    00378     rootObject->getRegion(region);
    00379
    00380     NM_DDM_Subscribe_Interaction req;
    00381
    00382     req.interactionClass = int_class ;
    00383     req.region = region ;
    00384
    00385     comm->sendMessage(&req);
    00386     std::auto_ptr<NetworkMessage> rep(comm->waitMessage(NetworkMessage::DDM_SUBSCRIBE_INTERACTION,
    00387               req.federate));
    00388
    00389     e = rep->exception ;
    00390 } /* end of subscribeInteraction */
    00391
    00392 // ----------------------------------------------------------------------------
    00393 void
    00394 DataDistribution::unsubscribeInteraction(InteractionClassHandle int_class,
    00395                      RegionHandle region,
    00396                      TypeException &e)
    00397     throw (RegionNotKnown)
    00398 {
    00399     D[pdDebug] << "Unsubscribe interaction with region " << region << endl ;
    00400     rootObject->getRegion(region);
    00401
    00402     NM_DDM_Unsubscribe_Interaction req;
    00403
    00404     req.interactionClass = int_class ;
    00405     req.region = region ;
    00406
    00407     comm->sendMessage(&req);
    00408     std::auto_ptr<NetworkMessage>  rep(comm->waitMessage(NetworkMessage::DDM_UNSUBSCRIBE_INTERACTION,
    00409               req.federate));
    00410
    00411     e = rep->exception ;
    00412 } /* end of unsubscribeInteraction */
    00413
    00414 }} // namespace certi::rtia
    00415
    00416 // $Id: DataDistribution.cc,v 3.27 2008/11/08 11:36:05 erk Exp $