00001 /* 00002 * BaseRouter.cpp 00003 * 00004 * Created on: 15.02.2010 00005 * Author: morgenro 00006 */ 00007 00008 #include "routing/BaseRouter.h" 00009 #include "core/BundleCore.h" 00010 00011 #include "net/TransferAbortedEvent.h" 00012 #include "net/TransferCompletedEvent.h" 00013 #include "net/BundleReceivedEvent.h" 00014 #include "routing/QueueBundleEvent.h" 00015 #include "routing/RequeueBundleEvent.h" 00016 #include "core/BundleStorage.h" 00017 #include "core/BundleGeneratedEvent.h" 00018 #include "core/BundleExpiredEvent.h" 00019 #include "core/BundleEvent.h" 00020 #include "core/NodeEvent.h" 00021 #include "core/TimeEvent.h" 00022 00023 #include <ibrcommon/Logger.h> 00024 #include <ibrcommon/thread/MutexLock.h> 00025 00026 namespace dtn 00027 { 00028 namespace routing 00029 { 00030 BaseRouter *BaseRouter::Extension::_router = NULL; 00031 00035 BaseRouter::ThreadedExtension::ThreadedExtension() 00036 { } 00037 00038 BaseRouter::ThreadedExtension::~ThreadedExtension() 00039 { 00040 join(); 00041 } 00042 00046 BaseRouter::Extension::Extension() 00047 { } 00048 00049 BaseRouter::Extension::~Extension() 00050 { } 00051 00052 BaseRouter* BaseRouter::Extension::getRouter() 00053 { 00054 return _router; 00055 } 00056 00060 BaseRouter::Endpoint::Endpoint() 00061 { } 00062 00063 BaseRouter::Endpoint::~Endpoint() 00064 { } 00065 00069 BaseRouter::VirtualEndpoint::VirtualEndpoint(dtn::data::EID name) 00070 : _client(NULL), _name(name) 00071 { } 00072 00073 BaseRouter::VirtualEndpoint::~VirtualEndpoint() 00074 { } 00075 00079 BaseRouter::BaseRouter(dtn::core::BundleStorage &storage) 00080 : _storage(storage) 00081 { 00082 // register myself for all extensions 00083 Extension::_router = this; 00084 } 00085 00086 BaseRouter::~BaseRouter() 00087 { 00088 } 00089 00094 void BaseRouter::addExtension(BaseRouter::Extension *extension) 00095 { 00096 _extensions.push_back(extension); 00097 } 00098 00099 void BaseRouter::componentUp() 00100 { 00101 bindEvent(dtn::net::TransferAbortedEvent::className); 00102 bindEvent(dtn::net::TransferCompletedEvent::className); 00103 bindEvent(dtn::net::BundleReceivedEvent::className); 00104 bindEvent(dtn::routing::QueueBundleEvent::className); 00105 bindEvent(dtn::routing::RequeueBundleEvent::className); 00106 bindEvent(dtn::core::NodeEvent::className); 00107 bindEvent(dtn::core::BundleExpiredEvent::className); 00108 bindEvent(dtn::core::TimeEvent::className); 00109 bindEvent(dtn::core::BundleGeneratedEvent::className); 00110 00111 for (std::list<BaseRouter::Extension*>::iterator iter = _extensions.begin(); iter != _extensions.end(); iter++) 00112 { 00113 ThreadedExtension *thread = dynamic_cast<ThreadedExtension*>(*iter); 00114 00115 if (thread != NULL) 00116 { 00117 // run the thread 00118 thread->start(); 00119 } 00120 } 00121 } 00122 00123 void BaseRouter::componentDown() 00124 { 00125 unbindEvent(dtn::net::TransferAbortedEvent::className); 00126 unbindEvent(dtn::net::TransferCompletedEvent::className); 00127 unbindEvent(dtn::net::BundleReceivedEvent::className); 00128 unbindEvent(dtn::routing::QueueBundleEvent::className); 00129 unbindEvent(dtn::routing::RequeueBundleEvent::className); 00130 unbindEvent(dtn::core::NodeEvent::className); 00131 unbindEvent(dtn::core::BundleExpiredEvent::className); 00132 unbindEvent(dtn::core::TimeEvent::className); 00133 unbindEvent(dtn::core::BundleGeneratedEvent::className); 00134 00135 // delete all extensions 00136 for (std::list<BaseRouter::Extension*>::iterator iter = _extensions.begin(); iter != _extensions.end(); iter++) 00137 { 00138 delete (*iter); 00139 } 00140 } 00141 00148 void BaseRouter::transferTo(const dtn::data::EID &destination, const dtn::data::BundleID &id) 00149 { 00150 // get the bundle out of the storage 00151 dtn::data::Bundle b = _storage.get(id); 00152 00153 // send the bundle 00154 transferTo(destination, b); 00155 } 00156 00157 void BaseRouter::transferTo(const dtn::data::EID &destination, dtn::data::Bundle &bundle) 00158 { 00159 // send the bundle 00160 dtn::core::BundleCore::getInstance().transferTo(destination, bundle); 00161 } 00162 00166 void BaseRouter::raiseEvent(const dtn::core::Event *evt) 00167 { 00168 try { 00169 const dtn::core::TimeEvent &time = dynamic_cast<const dtn::core::TimeEvent&>(*evt); 00170 ibrcommon::MutexLock l(_known_bundles_lock); 00171 _known_bundles.expire(time.getTimestamp()); 00172 00173 } catch (std::bad_cast) { 00174 } 00175 00176 try { 00177 const dtn::net::BundleReceivedEvent &received = dynamic_cast<const dtn::net::BundleReceivedEvent&>(*evt); 00178 00179 // Store incoming bundles into the storage 00180 try { 00181 // store the bundle into a storage module 00182 dtn::core::BundleCore::getInstance().getStorage().store(received.bundle); 00183 00184 // set the bundle as known 00185 if (!isKnown(received.bundle)) 00186 { 00187 ibrcommon::MutexLock l(_known_bundles_lock); 00188 _known_bundles.add(received.bundle); 00189 00190 // raise the queued event to notify all receivers about the new bundle 00191 QueueBundleEvent::raise(received.bundle); 00192 } 00193 00194 // finally create a bundle received event 00195 dtn::core::BundleEvent::raise(received.bundle, dtn::core::BUNDLE_RECEIVED); 00196 00197 } catch (ibrcommon::IOException ex) { 00198 IBRCOMMON_LOGGER(notice) << "Unable to store bundle " << received.bundle.toString() << IBRCOMMON_LOGGER_ENDL; 00199 } catch (dtn::core::BundleStorage::StorageSizeExeededException ex) { 00200 IBRCOMMON_LOGGER(notice) << "No space left for bundle " << received.bundle.toString() << IBRCOMMON_LOGGER_ENDL; 00201 } 00202 00203 return; 00204 } catch (std::bad_cast) { 00205 } 00206 00207 try { 00208 const dtn::core::BundleGeneratedEvent &generated = dynamic_cast<const dtn::core::BundleGeneratedEvent&>(*evt); 00209 00210 // set the bundle as known 00211 { 00212 ibrcommon::MutexLock l(_known_bundles_lock); 00213 _known_bundles.add(generated.bundle); 00214 } 00215 00216 // Store incoming bundles into the storage 00217 try { 00218 // store the bundle into a storage module 00219 dtn::core::BundleCore::getInstance().getStorage().store(generated.bundle); 00220 00221 // raise the queued event to notify all receivers about the new bundle 00222 QueueBundleEvent::raise(generated.bundle); 00223 00224 } catch (ibrcommon::IOException ex) { 00225 IBRCOMMON_LOGGER(notice) << "Unable to store bundle " << generated.bundle.toString() << IBRCOMMON_LOGGER_ENDL; 00226 } catch (dtn::core::BundleStorage::StorageSizeExeededException ex) { 00227 IBRCOMMON_LOGGER(notice) << "No space left for bundle " << generated.bundle.toString() << IBRCOMMON_LOGGER_ENDL; 00228 } 00229 00230 return; 00231 } catch (std::bad_cast) { 00232 } 00233 00234 try { 00235 const QueueBundleEvent &queued = dynamic_cast<const QueueBundleEvent&>(*evt); 00236 00237 const dtn::data::EID &dest = queued.bundle.destination; 00238 00239 if ( (dest.getNodeEID() == BundleCore::local.getNodeEID()) && dest.hasApplication() ) 00240 { 00241 // if the bundle is for a local application, do not forward it to routing modules 00242 return; 00243 } 00244 } catch (std::bad_cast) { 00245 } 00246 00247 // notify all underlying extensions 00248 for (std::list<BaseRouter::Extension*>::iterator iter = _extensions.begin(); iter != _extensions.end(); iter++) 00249 { 00250 (*iter)->notify(evt); 00251 } 00252 } 00253 00259 dtn::data::Bundle BaseRouter::getBundle(const dtn::data::BundleID &id) 00260 { 00261 return _storage.get(id); 00262 } 00263 00264 dtn::core::BundleStorage& BaseRouter::getStorage() 00265 { 00266 return _storage; 00267 } 00268 00269 // set the bundle as known 00270 bool BaseRouter::isKnown(const dtn::data::BundleID &id) 00271 { 00272 ibrcommon::MutexLock l(_known_bundles_lock); 00273 return _known_bundles.contains(id); 00274 } 00275 00276 const SummaryVector BaseRouter::getSummaryVector() 00277 { 00278 ibrcommon::MutexLock l(_known_bundles_lock); 00279 return _known_bundles.getSummaryVector(); 00280 } 00281 } 00282 }
1.6.3