IBR-DTNSuite  0.8
daemon/src/net/DiscoveryAgent.cpp
Go to the documentation of this file.
00001 /*
00002  * DiscoveryAgent.cpp
00003  *
00004  *  Created on: 14.09.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "Configuration.h"
00009 #include "net/DiscoveryAgent.h"
00010 #include "net/DiscoveryService.h"
00011 #include "net/DiscoveryAnnouncement.h"
00012 #include "core/TimeEvent.h"
00013 #include "core/NodeEvent.h"
00014 #include <ibrdtn/utils/Utils.h>
00015 #include <ibrdtn/utils/Clock.h>
00016 #include <ibrcommon/Logger.h>
00017 
00018 using namespace dtn::core;
00019 
00020 namespace dtn
00021 {
00022         namespace net
00023         {
00024                 DiscoveryAgent::DiscoveryAgent(const dtn::daemon::Configuration::Discovery &config)
00025                  : _config(config), _sn(0), _last_announce_sent(0)
00026                 {
00027                 }
00028 
00029                 DiscoveryAgent::~DiscoveryAgent()
00030                 {
00031                 }
00032 
00033                 void DiscoveryAgent::addService(std::string name, std::string parameters)
00034                 {
00035                         DiscoveryService service(name, parameters);
00036                         _services.push_back(service);
00037                 }
00038 
00039                 void DiscoveryAgent::addService(DiscoveryServiceProvider *provider)
00040                 {
00041                         DiscoveryService service(provider);
00042                         _services.push_back(service);
00043                 }
00044 
00045                 void DiscoveryAgent::received(const DiscoveryAnnouncement &announcement, size_t timeout)
00046                 {
00047                         // convert the announcement into NodeEvents
00048                         Node n(announcement.getEID());
00049 
00050                         const std::list<DiscoveryService> services = announcement.getServices();
00051                         for (std::list<DiscoveryService>::const_iterator iter = services.begin(); iter != services.end(); iter++)
00052                         {
00053                                 size_t to_value = (timeout == 0) ? _config.timeout() : timeout;
00054 
00055                                 const DiscoveryService &s = (*iter);
00056 
00057                                 if (s.getName() == "tcpcl")
00058                                 {
00059                                         n.add(Node::URI(Node::NODE_DISCOVERED, Node::CONN_TCPIP, s.getParameters(), to_value, 20));
00060                                 }
00061                                 else if (s.getName() == "udpcl")
00062                                 {
00063                                         n.add(Node::URI(Node::NODE_DISCOVERED, Node::CONN_UDPIP, s.getParameters(), to_value, 20));
00064                                 }
00065                                 else if (s.getName() == "lowpancl")
00066                                 {
00067                                         n.add(Node::URI(Node::NODE_DISCOVERED, Node::CONN_LOWPAN, s.getParameters(), to_value, 20));
00068                                 }
00069                                 else
00070                                 {
00071                                         n.add(Node::Attribute(Node::NODE_DISCOVERED, s.getName(), s.getParameters(), to_value, 20));
00072                                 }
00073                         }
00074 
00075                         // create and raise a new event
00076                         dtn::core::NodeEvent::raise(n, NODE_INFO_UPDATED);
00077 
00078                         // if continuous announcements are disabled, then reply to this message
00079                         if (!_config.announce())
00080                         {
00081                                 // first check if another announcement was sent during the same seconds
00082                                 if (_last_announce_sent != dtn::utils::Clock::getTime())
00083                                 {
00084                                         IBRCOMMON_LOGGER_DEBUG(25) << "reply with discovery announcement" << IBRCOMMON_LOGGER_ENDL;
00085 
00086                                         sendAnnoucement(_sn, _services);
00087 
00088                                         // increment sequencenumber
00089                                         _sn++;
00090 
00091                                         // save the time of the last sent announcement
00092                                         _last_announce_sent = dtn::utils::Clock::getTime();
00093                                 }
00094                         }
00095                 }
00096 
00097                 void DiscoveryAgent::timeout()
00098                 {
00099                         // check if announcements are enabled
00100                         if (_config.announce())
00101                         {
00102                                 IBRCOMMON_LOGGER_DEBUG(25) << "send discovery announcement" << IBRCOMMON_LOGGER_ENDL;
00103 
00104                                 sendAnnoucement(_sn, _services);
00105 
00106                                 // increment sequencenumber
00107                                 _sn++;
00108                         }
00109                 }
00110         }
00111 }