IBR-DTNSuite  0.10
StreamContactHeader.cpp
Go to the documentation of this file.
1 /*
2  * StreamContactHeader.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 
23 #include "ibrdtn/data/Bundle.h"
24 #include "ibrdtn/data/Exceptions.h"
26 #include <typeinfo>
27 #include <netinet/in.h>
28 
29 using namespace dtn::data;
30 
31 namespace dtn
32 {
33  namespace streams
34  {
35  StreamContactHeader::StreamContactHeader()
36  : _flags(REQUEST_ACKNOWLEDGMENTS), _keepalive(0)
37  {
38  }
39 
41  : _localeid(localeid), _flags(REQUEST_ACKNOWLEDGMENTS), _keepalive(0)
42  {
43  }
44 
46  {
47  }
48 
50  {
51  _localeid = other._localeid;
52  _flags = other._flags;
53  _keepalive = other._keepalive;
54  return *this;
55  }
56 
58  {
59  return _localeid;
60  }
61 
62  std::ostream &operator<<(std::ostream &stream, const StreamContactHeader &h)
63  {
64  //BundleStreamWriter writer(stream);
65  stream << "dtn!" << TCPCL_VERSION << h._flags.get<char>();
66 
67  // convert to network byte order
68  uint16_t ka = htons(h._keepalive);
69  stream.write( (char*)&ka, 2 );
70 
72  stream << eid;
73 
74  return stream;
75  }
76 
77  std::istream &operator>>(std::istream &stream, StreamContactHeader &h)
78  {
79  char magic[5];
80 
81  // wait for magic
82  stream.read(magic, 4); magic[4] = '\0';
83  string str_magic(magic);
84 
85  if (str_magic != "dtn!")
86  {
87  throw InvalidProtocolException("not talking dtn");
88  }
89 
90  // version
91  char version; stream.get(version);
92  if (version != TCPCL_VERSION)
93  {
94  throw InvalidProtocolException("invalid bundle protocol version");
95  }
96 
97  // flags
98  char tmp;
99  stream.get(tmp);
100  h._flags = tmp;
101 
102  uint16_t ka = 0;
103  stream.read((char*)&ka, 2);
104 
105  // convert from network byte order
106  h._keepalive = ntohs(ka);
107 
109  stream >> eid;
110  h._localeid = EID(eid);
111 
112  return stream;
113  }
114  }
115 }