IBR-DTNSuite  0.10
DiscoveryAgent.cpp
Go to the documentation of this file.
1 /*
2  * DiscoveryAgent.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "Configuration.h"
23 #include "net/DiscoveryAgent.h"
24 #include "net/DiscoveryService.h"
26 #include "core/BundleCore.h"
27 #include "core/TimeEvent.h"
28 #include "core/NodeEvent.h"
29 #include <ibrdtn/utils/Utils.h>
30 #include <ibrdtn/utils/Clock.h>
31 #include <ibrcommon/Logger.h>
32 
33 using namespace dtn::core;
34 
35 namespace dtn
36 {
37  namespace net
38  {
39  DiscoveryAgent::DiscoveryAgent(const dtn::daemon::Configuration::Discovery &config)
40  : _config(config), _sn(0), _last_announce_sent(0)
41  {
42  }
43 
45  {
46  }
47 
49  {
50  _provider.push_back(provider);
51  }
52 
53  void DiscoveryAgent::received(const dtn::data::EID &source, const std::list<DiscoveryService> &services, const dtn::data::Number &timeout)
54  {
55  // convert the announcement into NodeEvents
56  Node n(source);
57 
58  for (std::list<DiscoveryService>::const_iterator iter = services.begin(); iter != services.end(); ++iter)
59  {
60  const dtn::data::Number to_value = (timeout == 0) ? _config.timeout() : timeout;
61 
62  const DiscoveryService &s = (*iter);
63 
64  if (s.getName() == "tcpcl")
65  {
66  n.add(Node::URI(Node::NODE_DISCOVERED, Node::CONN_TCPIP, s.getParameters(), to_value, 20));
67  }
68  else if (s.getName() == "udpcl")
69  {
70  n.add(Node::URI(Node::NODE_DISCOVERED, Node::CONN_UDPIP, s.getParameters(), to_value, 20));
71  }
72  else if (s.getName() == "lowpancl")
73  {
74  n.add(Node::URI(Node::NODE_DISCOVERED, Node::CONN_LOWPAN, s.getParameters(), to_value, 20));
75  }
76  else
77  {
78  n.add(Node::Attribute(Node::NODE_DISCOVERED, s.getName(), s.getParameters(), to_value, 20));
79  }
80  }
81 
82  // announce NodeInfo to ConnectionManager
84 
85  // if continuous announcements are disabled, then reply to this message
86  if (!_config.announce())
87  {
88  // first check if another announcement was sent during the same seconds
89  if (_last_announce_sent != dtn::utils::Clock::getTime())
90  {
91  IBRCOMMON_LOGGER_DEBUG_TAG("DiscoveryAgent", 55) << "reply with discovery announcement" << IBRCOMMON_LOGGER_ENDL;
92 
93  sendAnnoucement(_sn, _provider);
94 
95  // increment sequencenumber
96  _sn++;
97 
98  // save the time of the last sent announcement
99  _last_announce_sent = dtn::utils::Clock::getTime();
100  }
101  }
102  }
103 
105  {
106  // check if announcements are enabled
107  if (_config.announce())
108  {
109  IBRCOMMON_LOGGER_DEBUG_TAG("DiscoveryAgent", 55) << "send discovery announcement" << IBRCOMMON_LOGGER_ENDL;
110 
111  sendAnnoucement(_sn, _provider);
112 
113  // increment sequencenumber
114  _sn++;
115  }
116  }
117  }
118 }