00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00099
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
00112 if (exists(num_fed))
00113 throw RTIinternalError("LBTS: Federate already present.");
00114
00115
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
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 }
00162
00163