ThresholdDecider.h

00001 #ifndef THRESHOLDDECIDER_H_
00002 #define THRESHOLDDECIDER_H_
00003 
00004 #include <string>
00005 #include <cmath>
00006 #include <map>
00007 
00008 #include <Decider.h>
00009 
00024 class ThresholdDecider:public Decider {
00025 protected:
00026   int myIndex;
00027 
00028   double threshold;
00029 
00032   std::map<Signal*, int> currentSignals;
00033 
00034   enum {
00035     FIRST,
00036     HEADER_OVER,
00037     SIGNAL_OVER
00038   };
00039 
00040 protected:
00041 
00045   simtime_t handleNewSignal(Signal* s){
00046     //set the state the signal will be in the next time we get it
00047     currentSignals[s] = HEADER_OVER;
00048 
00049     log("First processing of this signal. Scheduling it to end of header to decide if Signal should be received.");
00050     //we say that the length of the header is at 10% of the length of the signal
00051     return s->getSignalStart() + 0.10 * s->getSignalLength();
00052   }
00053 
00057   simtime_t handleHeaderOver(std::map<Signal*, int>::iterator& it){
00058     log("Second receive of a signal from Phy - Deciding if packet should be received - Let's try to receive it.");
00059     //we don't really do something after the header, so we only update the next state
00060     it->second = SIGNAL_OVER;
00061     return it->first->getSignalStart() + it->first->getSignalLength();
00062   }
00063 
00067   simtime_t handleSignalOver(std::map<Signal*, int>::iterator& it, AirFrame* frame){
00068     log("Last receive of signal from Phy - Deciding if the packet could be received correctly...");
00069 
00070     //get the receiving power from the signal (calculated at each call of
00071     //this method
00072     //Mapping* receivingPower = it->first->getReceivingPower();
00073 
00074 
00075     //------print the mappings----------------------
00076     ev << "Sending power mapping: " << endl;
00077     printMapping(it->first->getTransmissionPower());
00078 
00079     ConstMapping* receivingPower;
00080 
00081     simtime_t delay = it->first->getPropagationDelay();
00082     if(delay == 0)
00083       receivingPower = it->first->getTransmissionPower()->constClone();
00084     else
00085       receivingPower = new ConstDelayedMapping(it->first->getTransmissionPower(), delay);
00086 
00087     std::list<ConstMapping*> attList = it->first->getAttenuation();
00088     int count = 1;
00089     for(std::list<ConstMapping*>::const_iterator aIt = attList.begin();
00090       aIt != attList.end(); ++aIt){
00091 
00092       ev << endl;
00093       ev << " multiplied with Attenuation " << count;
00094       switch(count){
00095       case 1:
00096         ev << "(Radiostate)";
00097         break;
00098       case 2:
00099         ev << "(Pathloss)";
00100         break;
00101       case 3:
00102         ev << "(Random time and freq attenuation)";
00103         break;
00104       case 4:
00105         ev << "(Random freq only attenuation)";
00106         break;
00107       }
00108       ev << ":" << endl;
00109       printMapping(*aIt);
00110       ++count;
00111 
00112       ConstMapping* tmp = MappingUtils::multiply(*receivingPower, **aIt, 0.0);
00113       delete receivingPower;
00114       receivingPower = tmp;
00115 
00116       ev << endl;
00117       ev << " result:" << endl;
00118       printMapping(receivingPower);
00119     }
00120 
00121     ev << endl;
00122     ev << "Receiving power calculated.\n";
00123     ev << "Signal receiving power. (Should be same as above)\n";
00124     printMapping(it->first->getReceivingPower());
00125 
00126     ev << endl;
00127     ev << "Threshold is " << toDecibel(threshold) << "dbm" << endl;
00128     //----------------------------------------------
00129 
00130     bool toWeak = false;
00131 
00132     //iterate over receiving power mapping and chekc if every value is bigger
00133     //then the threshold
00134     ConstMappingIterator* mIt = receivingPower->createConstIterator();
00135 
00136     while(mIt->inRange()){
00137       if(mIt->getValue() < threshold){
00138         // print receiving power if too weak
00139         ev << "receiving power: " << mIt->getValue() << endl;
00140         toWeak = true;
00141         break;
00142       }
00143 
00144       if(!mIt->hasNext())
00145         break;
00146 
00147       mIt->next();
00148     }
00149 
00150     delete mIt;
00151     delete receivingPower;
00152 
00153     if(toWeak){
00154       log("...signal is to weak -> discard.");
00155     } else {
00156       log("...strong enough -> forwarding it to Mac layer.");
00157       phy->sendUp(frame, new DeciderResult(true));
00158     }
00159 
00160     currentSignals.erase(it);
00161     return -1;
00162   }
00163 
00164 
00165   //----------Utility methods----------------------------
00166   void log(std::string msg) {
00167     ev << "[Host " << myIndex << "] - PhyLayer(Decider): " << msg << endl;
00168   }
00169 
00170   double toDecibel(double v){
00171     return 10.0 * log10(v);
00172   }
00173 
00174   template<class T>
00175   std::string toString(T v, unsigned int length){
00176     char* tmp = new char[255];
00177     sprintf(tmp, "%.2f", v);
00178 
00179     std::string result(tmp);
00180     delete[] tmp;
00181     while(result.length() < length)
00182       result += " ";
00183     return result;
00184   }
00185 
00186   std::string toString(simtime_t v, unsigned int length){
00187     return toString(SIMTIME_DBL(v), length);
00188   }
00189 
00193   void printMapping(ConstMapping* m){
00194     Dimension frequency("frequency");
00195     //const DimensionSet& dims = m->getDimensionSet();
00196 
00197     std::set<simtime_t> timeEntries;
00198     std::set<double> freqEntries;
00199 
00200     std::map<double, std::set<simtime_t> > entries;
00201 
00202     ConstMappingIterator* it = m->createConstIterator();
00203 
00204     while(it->inRange()){
00205       entries[it->getPosition().getArgValue(frequency)].insert(it->getPosition().getTime());
00206       timeEntries.insert(it->getPosition().getTime());
00207       freqEntries.insert(it->getPosition().getArgValue(frequency));
00208 
00209       if(!it->hasNext())
00210         break;
00211 
00212       it->next();
00213     }
00214 
00215     delete it;
00216 
00217     ev << "--------+---------------------------------------------------------" << endl;
00218     ev << "GHz \\ms | ";
00219     for(std::set<simtime_t>::const_iterator tIt = timeEntries.begin();
00220       tIt != timeEntries.end(); ++tIt){
00221       ev << toString(*tIt * 1000, 6) << " ";
00222     }
00223     ev << endl;
00224     ev << "--------+---------------------------------------------------------" << endl;
00225     if(freqEntries.begin() == freqEntries.end()) {
00226       ev << "        | Defines no own key entries." << endl;
00227       ev << "        | That does NOT mean it doesn't define any attenuation." << endl;
00228     }
00229     else {
00230       Argument pos;
00231       for(std::set<double>::const_iterator fIt = freqEntries.begin();
00232         fIt != freqEntries.end(); ++fIt){
00233         ev << toString((*fIt)/1e9, 5) << "   | ";
00234         pos.setArgValue(frequency, *fIt);
00235 
00236         std::map<double, std::set<simtime_t> >::iterator tmpIt = entries.find(*fIt);
00237 
00238         for(std::set<simtime_t>::const_iterator tIt = timeEntries.begin();
00239           tIt != timeEntries.end(); ++tIt){
00240 
00241           if(tmpIt != entries.end() && tmpIt->second.find(*tIt) != tmpIt->second.end()){
00242             pos.setTime(*tIt);
00243             ev << toString(toDecibel(m->getValue(pos)), 6) << " ";
00244           } else {
00245             ev << "       ";
00246           }
00247         }
00248         ev << endl;
00249       }
00250     }
00251     ev << "--------+---------------------------------------------------------" << endl;
00252   }
00253 
00254 public:
00255   ThresholdDecider(DeciderToPhyInterface* phy, int myIndex, double threshold):
00256     Decider(phy), myIndex(myIndex), threshold(threshold) {}
00257 
00262   virtual simtime_t processSignal(AirFrame* frame) {
00263     Signal* s = &frame->getSignal();
00264 
00265     //Check if we already know this signal...
00266     std::map<Signal*, int>::iterator it = currentSignals.find(s);
00267 
00268     //if not, we handle it as a new signal
00269     if(it == currentSignals.end()) {
00270       return handleNewSignal(s);
00271 
00272     //otherwise handle it depending on the state it currently is in
00273     } else {
00274       switch(it->second) {
00275       case HEADER_OVER:
00276         return handleHeaderOver(it);
00277 
00278       case SIGNAL_OVER:
00279         return handleSignalOver(it, frame);
00280 
00281       default:
00282         break;
00283       }
00284     }
00285 
00286     //we should never get here!
00287     assert(false);
00288     return 0;
00289   }
00290 
00291   virtual ChannelState getChannelState() {
00292     return ChannelState(false, 0);
00293   }
00294   virtual simtime_t handleChannelSenseRequest(ChannelSenseRequest*) {
00295     return -1;
00296   }
00297 };
00298 
00299 #endif /*TESTDECIDER_H_*/