34 #include <netinet/in.h>
42 : _version(version), _flags(BEACON_NO_FLAGS), _canonical_eid(eid), _sn(0)
55 return (_flags & DiscoveryBeacon::BEACON_SHORT);
58 return !(_flags & DiscoveryBeacon::BEACON_CONTAINS_EID);
71 return _canonical_eid;
91 for (service_list::const_iterator iter = _services.begin(); iter != _services.end(); ++iter)
93 if ((*iter).getName() == name)
104 for (service_list::iterator iter = _services.begin(); iter != _services.end(); ++iter)
106 if ((*iter).getName() == name)
117 _services.push_back(service);
129 switch (announcement._version)
133 if (services.empty())
135 const unsigned char flags = DiscoveryBeacon::BEACON_SHORT | announcement._flags;
147 for (dtn::net::DiscoveryBeacon::service_list::const_iterator iter = services.begin(); iter != services.end(); ++iter)
149 beacon_len += (*iter).getLength();
154 for (dtn::net::DiscoveryBeacon::service_list::const_iterator iter = services.begin(); iter != services.end(); ++iter)
164 unsigned char flags = 0;
170 flags |= DiscoveryBeacon::BEACON_CONTAINS_EID;
173 if (!services.empty())
175 flags |= DiscoveryBeacon::BEACON_SERVICE_BLOCK;
181 const uint16_t sn = htons(announcement._sn);
182 stream.write( (
const char*)&sn, 2 );
184 if ( flags & DiscoveryBeacon::BEACON_CONTAINS_EID )
190 if ( flags & DiscoveryBeacon::BEACON_SERVICE_BLOCK )
194 for (dtn::net::DiscoveryBeacon::service_list::const_iterator iter = services.begin(); iter != services.end(); ++iter)
207 uint8_t interval = 10;
209 uint16_t inet_port = htons(4556);
210 std::string eid = announcement._canonical_eid.
getString();
211 uint16_t eid_len = htons((uint16_t)eid.length());
212 unsigned int add_zeros = (4 - (eid.length() % 4)) % 4;
213 uint16_t length = htons(static_cast<uint16_t>(12 + eid.length() + add_zeros));
216 stream << (
unsigned char)cl_type;
217 stream.write((
char*)&interval, 1);
218 stream.write((
char*)&length, 2);
229 stream.write(&zero, 1);
230 stream.write(&zero, 1);
231 stream.write(&zero, 1);
232 stream.write(&zero, 1);
234 stream.write((
char*)&inet_port, 2);
235 stream.write((
char*)&eid_len, 2);
238 for (
unsigned int i = 0; i < add_zeros; ++i)
240 stream.write((
char*)&zero, 1);
252 unsigned char version = 0;
263 version = (
unsigned char)stream.get();
275 stream.
get((
char&)announcement._flags);
278 if (DiscoveryBeacon::BEACON_SHORT == announcement._flags)
284 stream >> beacon_len;
285 int remain = beacon_len.
get<
int>();
289 remain -=
static_cast<int>(eid.getLength());
291 announcement._canonical_eid =
dtn::data::EID((
const std::string&)eid);
304 services.push_back(service);
305 remain -=
static_cast<int>(service.getLength());
314 stream.get((
char&)announcement._flags);
319 stream.read((
char*)&sn, 2);
322 uint16_t sequencenumber = ntohs(sn);
326 if (announcement._flags & DiscoveryBeacon::BEACON_CONTAINS_EID)
331 announcement._canonical_eid =
dtn::data::EID((
const std::string&)eid);
336 if (announcement._flags & DiscoveryBeacon::BEACON_SERVICE_BLOCK)
343 stream >> num_services;
350 for (
unsigned int i = 0; num_services > i; ++i)
355 services.push_back(service);
361 if (announcement._flags & DiscoveryBeacon::BEACON_BLOOMFILTER)
380 stream.read((
char*)&cl_type, 1);
381 stream.read((
char*)&interval, 1);
382 stream.read((
char*)&length, 2);
383 stream.read((
char*)&inet_addr, 4);
384 stream.read((
char*)&inet_port, 2);
385 stream.read((
char*)&eid_len, 2);
387 std::vector<char> eid(eid_len);
388 stream.read(&eid[0], eid.size());
391 announcement._canonical_eid =
dtn::data::EID(std::string(eid.begin(), eid.end()));
411 ss <<
"ANNOUNCE: " << _canonical_eid.
getString();