LBTS.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // CERTI - HLA RunTime Infrastructure
00003 // Copyright (C) 2002, 2003, 2004  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 // $Id: LBTS.cc,v 3.13 2009/04/02 19:58:07 erk Exp $
00023 // ----------------------------------------------------------------------------
00024 
00025 
00026 
00027 #include "LBTS.hh"
00028 #include "Exception.hh"
00029 #include "PrettyDebug.hh"
00030 
00031 #ifndef _WIN32
00032     #include <sys/types.h>
00033     #include <sys/stat.h>
00034     #include <unistd.h>
00035     #include <stdlib.h>
00036 #endif
00037 #include <float.h>
00038 #include <limits>
00039 
00040 using std::vector ;  
00041 
00042 namespace certi {
00043 
00044 static pdCDebug D("LBTS", __FILE__);
00045 
00046 // ----------------------------------------------------------------------------
00050 LBTS::LBTS()
00051     : _LBTS(std::numeric_limits<double>::infinity()),
00052       MyFederateNumber(0)
00053 {
00054 }
00055 
00056 // ----------------------------------------------------------------------------
00057 LBTS::~LBTS()
00058 {
00059 }
00060 
00061 // ----------------------------------------------------------------------------
00063 void
00064 LBTS::compute()
00065 {
00066     FederationTime hl ;
00067 
00068     // LBTS = + l'infini
00069     _LBTS = std::numeric_limits<double>::infinity();
00070 
00071     ClockSet::iterator i ;
00072     for (i = clocks.begin(); i != clocks.end(); ++i) {
00073         if (i->first != MyFederateNumber) {
00074             hl = i->second ;
00075             if (hl < _LBTS)
00076                 _LBTS = hl ;
00077         }
00078     }
00079 }
00080 
00081 // ----------------------------------------------------------------------------
00084 bool
00085 LBTS::exists(FederateHandle federate) const
00086 {
00087     return clocks.find(federate) != clocks.end();
00088 }
00089 
00090 // ----------------------------------------------------------------------------
00094 void
00095 LBTS::get(std::vector<FederateClock> &v) const
00096 {
00097     v.reserve(v.size() + clocks.size());
00098     // append clocks to v
00099     // note, the ClockSet::value_type and FederateClock differ in const-ness
00100     for(ClockSet::const_iterator pos = clocks.begin();
00101         pos != clocks.end(); pos++)
00102         v.push_back(FederateClock(pos->first, pos->second));
00103 }
00104 
00105 // ----------------------------------------------------------------------------
00108 void
00109 LBTS::insert(FederateHandle num_fed, FederationTime time)
00110 {
00111     // TODO: is this check really required ?
00112     if (exists(num_fed))
00113         throw RTIinternalError("LBTS: Federate already present.");
00114 
00115     // BUG: We should verify that clock time is correct.
00116     clocks[num_fed] = time ;
00117     compute();
00118 }
00119 
00120 // ----------------------------------------------------------------------------
00122 void
00123 LBTS::update(FederateHandle num_fed, FederationTime time)
00124 {
00125     D.Out(pdDebug, "LBTS.update: Updating federate %d(%f).", num_fed, time.getTime());
00126 
00127     ClockSet::iterator it = clocks.find(num_fed);
00128 
00129     if (it == clocks.end())
00130         throw RTIinternalError("LBTS: Federate not found.");
00131 
00132     // Coherence test.
00133     if (it->second > time)
00134         D.Out(pdDebug,
00135               "LBTS.update: federate %u, new time lower than oldest one.",
00136               num_fed);
00137     else {
00138         D.Out(pdDebug, "before LBTS.update: federate %u, old time %f.",
00139               it->first, it->second.getTime());
00140         it->second = time ;
00141         D.Out(pdDebug, "after LBTS.update: federate %u, new time %f.",
00142               it->first, it->second.getTime());
00143         compute();
00144     }
00145 }
00146 
00147 // ----------------------------------------------------------------------------
00149 void
00150 LBTS::remove(FederateHandle num_fed)
00151 {
00152     ClockSet::iterator it = clocks.find(num_fed);
00153 
00154     if (it == clocks.end())
00155         throw RTIinternalError("LBTS: Federate not found.");
00156 
00157     clocks.erase(it);
00158     compute();
00159 }
00160 
00161 } // namespace certi
00162 
00163 // $Id: LBTS.cc,v 3.13 2009/04/02 19:58:07 erk Exp $

Generated on Thu Apr 30 15:53:49 2009 for CERTIDeveloperDocumentation by doxygen 1.5.5