00001
00002 #include "wireless_channel_manager.hpp"
00003 #include "wireless_channel.hpp"
00004 #include "physical_layer.hpp"
00005 #include "event.hpp"
00006 #include "simulator.hpp"
00007
00009
00011
00017 class SignalEndEvent : public Event {
00018 public:
00019
00021 SignalEndEvent(ConstWirelessChannelManagerPtr channelManager,
00022 PhysicalLayerPtr receiver, WirelessCommSignalPtr signal)
00023 : Event(), m_channelManager(channelManager),
00024 m_receiver(receiver), m_signal(signal)
00025 {
00026 assert(m_channelManager.get() != 0);
00027 assert(m_receiver != 0);
00028 assert(m_signal.get() != 0);
00029 }
00030
00031 void execute()
00032 {
00033 m_channelManager->passSignalToReceiver(m_receiver, m_signal);
00034 }
00035
00036 private:
00037 ConstWirelessChannelManagerPtr m_channelManager;
00038 PhysicalLayerPtr m_receiver;
00039 WirelessCommSignalPtr m_signal;
00040 };
00041
00044 typedef boost::shared_ptr<SignalEndEvent> SignalEndEventPtr;
00045
00046
00048
00050
00051 WirelessChannelManager::WirelessChannelManager()
00052 {
00053
00054 }
00055
00056 void WirelessChannelManager::recvSignal(PhysicalLayerPtr sender,
00057 WirelessCommSignalPtr signal)
00058 {
00059 assert(sender.get() != 0);
00060 assert(signal.get() != 0);
00061
00062 typedef SenderChannel::const_iterator senderIterator;
00063 pair<senderIterator,senderIterator> iteratorRange =
00064 m_senders.equal_range(sender);
00065
00066 for(senderIterator i = iteratorRange.first;
00067 i != iteratorRange.second; ++i) {
00068 WirelessChannelPtr channel = i->second;
00069 sendSignalOnChannel(sender, signal, channel);
00070 }
00071 }
00072
00073 t_uint WirelessChannelManager::getChannelId(
00074 WirelessChannelPtr channel) const
00075 {
00076 t_uint channelId = 0;
00077 bool wasFound = false;
00078 ChannelIdMap::const_iterator p;
00079 for(p = m_channels.begin(); p != m_channels.end(); ++p) {
00080 if(p->second == channel) {
00081
00082 assert(!wasFound);
00083 wasFound = true;
00084 channelId = p->first;
00085 }
00086 }
00087 assert(wasFound);
00088
00089 return channelId;
00090 }
00091
00092 void WirelessChannelManager::sendSignalOnChannel(
00093 ConstPhysicalLayerPtr sender, WirelessCommSignalPtr signal,
00094 WirelessChannelPtr channel)
00095 {
00096 assert(sender != 0);
00097 assert(signal.get() != 0);
00098 assert(channel.get() != 0);
00099
00100 typedef ChannelObserver::const_iterator observerIterator;
00101 pair<observerIterator,observerIterator> iteratorRange =
00102 m_listeners.equal_range(channel);
00103
00104 SimTime signalEndTime = signal->getDuration();
00105
00106
00107 signal->setChannelId(getChannelId(channel));
00108
00109 for(observerIterator i = iteratorRange.first;
00110 i != iteratorRange.second; ++i) {
00111
00112 PhysicalLayerPtr listener = i->second;
00113
00114
00115
00116 if(listener != sender) {
00117
00118
00119 double signalStrength =
00120 channel->getRecvdStrength(*signal, *listener);
00121
00122 if(m_DEBUG_SIGNAL_STRENGTH) {
00123 ostringstream debugStream;
00124 debugStream << "listener: " << listener->getNodeId() <<
00125 ", ss: " << signalStrength << " RXThresh: " <<
00126 listener->getRxThreshold();
00127 LogStreamManager::instance()->logDebugItem(debugStream.str());
00128 }
00129
00130
00131
00132
00133 if(listener->captureSignal(signalStrength)) {
00134 listener->setPendingSignal(signal);
00135 }
00136
00137
00138
00139 listener->addSignal(signal, signalStrength);
00140
00141
00142
00143
00144
00145
00146
00147
00148 if(signal != listener->getPendingSignal() &&
00149 listener->pendingSignalIsWeak()) {
00150 listener->resetPendingSignal();
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 if(listener->getPendingSignal().get() != 0 &&
00164 !listener->getPendingSignalError()) {
00165 listener->setPendingSignalError(
00166 channel->signalHasError(
00167 listener->getPendingSignalSinr(),
00168 *listener->getPendingSignal()
00169 )
00170 );
00171 }
00172
00173
00174 SignalEndEventPtr signalEnd(
00175 new SignalEndEvent(shared_from_this(), listener, signal));
00176 SimTime recvTime = signalEndTime +
00177 channel->propagationDelay(*sender, *listener);
00178 Simulator::instance()->scheduleEvent(signalEnd, recvTime);
00179
00180 }
00181 }
00182 }
00183
00184 void WirelessChannelManager::passSignalToReceiver(
00185 PhysicalLayerPtr receiver, WirelessCommSignalPtr signal) const
00186 {
00187 assert(receiver != 0);
00188
00189
00190 if(signal == receiver->getPendingSignal()) {
00191 WirelessCommSignalPtr signalDeepCopy =
00192 WirelessCommSignal::create(*signal);
00193 double recvdSignalStrength = receiver->getPendingSignalStrength();
00194 receiver->recvPendingSignal(signalDeepCopy, recvdSignalStrength);
00195 receiver->resetPendingSignal();
00196 }
00197
00198
00199 receiver->removeSignal(signal);
00200
00201 }
00202
00203 bool WirelessChannelManager::attachAsSender(PhysicalLayerPtr physicalLayer,
00204 t_uint channelId)
00205 {
00206 assert(physicalLayer != 0);
00207 ChannelIdMap::iterator channelIterator = m_channels.find(channelId);
00208
00209 bool wasSuccessful = false;
00210 bool channelFound = (channelIterator != m_channels.end());
00211 if(channelFound) {
00212 WirelessChannelPtr channel = channelIterator->second;
00213 assert(channel.get() != 0);
00214 m_senders.insert(make_pair(physicalLayer, channel));
00215 wasSuccessful = true;
00216 }
00217
00218 return wasSuccessful;
00219 }
00220
00221 bool WirelessChannelManager::detachAsSender(PhysicalLayerPtr physicalLayer,
00222 t_uint channelId)
00223 {
00224 assert(physicalLayer != 0);
00225 ChannelIdMap::iterator channelIterator = m_channels.find(channelId);
00226 bool channelFound = (channelIterator != m_channels.end());
00227
00228 bool wasSuccessful = false;
00229
00230 if(channelFound) {
00231 WirelessChannelPtr channel = channelIterator->second;
00232 typedef SenderChannel::iterator senderIterator;
00233 pair<senderIterator,senderIterator> iteratorRange =
00234 m_senders.equal_range(physicalLayer);
00235
00236 for(senderIterator i = iteratorRange.first;
00237 i != iteratorRange.second; ++i) {
00238 if(i->second == channel) {
00239 m_senders.erase(i);
00240 wasSuccessful = true;
00241 break;
00242 }
00243 }
00244 }
00245
00246 return wasSuccessful;
00247 }
00248
00249 bool WirelessChannelManager::attachAsListener(PhysicalLayerPtr physicalLayer,
00250 t_uint channelId)
00251 {
00252 assert(physicalLayer != 0);
00253 ChannelIdMap::iterator channelIterator = m_channels.find(channelId);
00254
00255 bool wasSuccessful = false;
00256 bool channelFound = (channelIterator != m_channels.end());
00257 if(channelFound) {
00258 WirelessChannelPtr channel = channelIterator->second;
00259 assert(channel.get() != 0);
00260 m_listeners.insert(make_pair(channel, physicalLayer));
00261 wasSuccessful = true;
00262 }
00263
00264 return wasSuccessful;
00265 }
00266
00267 bool WirelessChannelManager::detachAsListener(PhysicalLayerPtr physicalLayer,
00268 t_uint channelId)
00269 {
00270 assert(physicalLayer != 0);
00271 ChannelIdMap::iterator channelIterator = m_channels.find(channelId);
00272 bool channelFound = (channelIterator != m_channels.end());
00273
00274 bool wasSuccessful = false;
00275
00276 if(channelFound) {
00277 WirelessChannelPtr channel = channelIterator->second;
00278 typedef ChannelObserver::iterator observerIterator;
00279 pair<observerIterator,observerIterator> iteratorRange =
00280 m_listeners.equal_range(channel);
00281
00282 for(observerIterator i = iteratorRange.first;
00283 i != iteratorRange.second; ++i) {
00284 if(i->second == physicalLayer) {
00285 m_listeners.erase(i);
00286 wasSuccessful = true;
00287 break;
00288 }
00289 }
00290 }
00291
00292 return wasSuccessful;
00293 }
00294
00295 void WirelessChannelManager::addChannel(t_uint channelId,
00296 WirelessChannelPtr channel)
00297 {
00298 assert(channel.get() != 0);
00299 m_channels[channelId] = channel;
00300 }
00301
00302 bool WirelessChannelManager::removeChannel(t_uint channelId)
00303 {
00304 int numChannelsRemoved = m_channels.erase(channelId);
00305 bool wasSuccessful = (numChannelsRemoved > 0);
00306 return wasSuccessful;
00307 }
00308
00309