IBR-DTNSuite  0.8
ibrdtn/ibrdtn/streams/StreamContactHeader.cpp
Go to the documentation of this file.
00001 /*
00002  * StreamContactHeader.cpp
00003  *
00004  *  Created on: 30.06.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/streams/StreamContactHeader.h"
00009 #include "ibrdtn/data/Bundle.h"
00010 #include "ibrdtn/data/Exceptions.h"
00011 #include "ibrdtn/data/BundleString.h"
00012 #include <netinet/in.h>
00013 #include <typeinfo>
00014 
00015 using namespace dtn::data;
00016 
00017 namespace dtn
00018 {
00019         namespace streams
00020         {
00021                 StreamContactHeader::StreamContactHeader()
00022                  : _flags(1), _keepalive(0)
00023                 {
00024                 }
00025 
00026                 StreamContactHeader::StreamContactHeader(EID localeid)
00027                  : _localeid(localeid), _flags(REQUEST_ACKNOWLEDGMENTS), _keepalive(0)
00028                 {
00029                 }
00030 
00031                 StreamContactHeader::~StreamContactHeader()
00032                 {
00033                 }
00034 
00035                 StreamContactHeader& StreamContactHeader::operator=(const StreamContactHeader &other)
00036                 {
00037                         _localeid = other._localeid;
00038                         _flags = other._flags;
00039                         _keepalive = other._keepalive;
00040                         return *this;
00041                 }
00042 
00043                 const EID StreamContactHeader::getEID() const
00044                 {
00045                         return _localeid;
00046                 }
00047 
00048                 std::ostream &operator<<(std::ostream &stream, const StreamContactHeader &h)
00049                 {
00050                         //BundleStreamWriter writer(stream);
00051                         stream << "dtn!" << TCPCL_VERSION << h._flags;
00052 
00053                         // convert to network byte order
00054                         u_int16_t ka = htons(h._keepalive);
00055                         stream.write( (char*)&ka, 2 );
00056 
00057                         dtn::data::BundleString eid(h._localeid.getString());
00058                         stream << eid;
00059 
00060                         return stream;
00061                 }
00062 
00063                 std::istream &operator>>(std::istream &stream, StreamContactHeader &h)
00064                 {
00065                         char magic[5];
00066 
00067                         // wait for magic
00068                         stream.read(magic, 4); magic[4] = '\0';
00069                         string str_magic(magic);
00070 
00071                         if (str_magic != "dtn!")
00072                         {
00073                                 throw InvalidProtocolException("not talking dtn");
00074                         }
00075 
00076                         // version
00077                         char version; stream.get(version);
00078                         if (version != TCPCL_VERSION)
00079                         {
00080                                 throw InvalidProtocolException("invalid bundle protocol version");
00081                         }
00082 
00083                         // flags
00084                         stream.get(h._flags);
00085 
00086                         u_int16_t ka = 0;
00087                         stream.read((char*)&ka, 2);
00088 
00089                         // convert from network byte order
00090                         h._keepalive = ntohs(ka);
00091 
00092                         dtn::data::BundleString eid;
00093                         stream >> eid;
00094                         h._localeid = EID(eid);
00095 
00096                         return stream;
00097                 }
00098         }
00099 }