IBR-DTNSuite  0.10
StreamDataSegment.cpp
Go to the documentation of this file.
1 /*
2  * StreamDataSegment.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/Number.h"
24 
25 namespace dtn
26 {
27  namespace streams
28  {
30  : _value(size), _type(type), _reason(MSG_SHUTDOWN_IDLE_TIMEOUT), _flags(0)
31  {
32  }
33 
35  : _value(0), _type(type), _reason(MSG_SHUTDOWN_IDLE_TIMEOUT), _flags(0)
36  {
37  }
38 
40  : _value(reconnect), _type(MSG_SHUTDOWN), _reason(reason), _flags(3)
41  {
42  }
43 
45  {
46  }
47 
48  std::ostream &operator<<(std::ostream &stream, const StreamDataSegment &seg)
49  {
50  char header = seg._flags;
51  header |= static_cast<char>((seg._type & 0x0F) << 4);
52 
53  // write the header (1-byte)
54  stream.put(header);
55 
56  switch (seg._type)
57  {
59  // write the length + data
60  stream << seg._value;
61  break;
62 
64  // write the acknowledged length
65  stream << seg._value;
66  break;
67 
69  break;
70 
72  break;
73 
75  // write the reason (char) + reconnect time (SDNV)
76  stream.put((char)seg._reason);
77  stream << seg._value;
78  break;
79  }
80 
81  return stream;
82  }
83 
84  std::istream &operator>>(std::istream &stream, StreamDataSegment &seg)
85  {
86  char header = 0;
87  stream.get(header);
88 
89  seg._type = StreamDataSegment::SegmentType( (header & 0xF0) >> 4 );
90  seg._flags = (header & 0x0F);
91 
92  switch (seg._type)
93  {
95  // read the length
96  stream >> seg._value;
97  break;
98 
100  // read the acknowledged length
101  stream >> seg._value;
102  break;
103 
105  break;
106 
108  break;
109 
111  // read the reason (char) + reconnect time (SDNV)
112  char reason;
113  stream.get(reason);
115 
116  stream >> seg._value;
117  break;
118  }
119 
120  return stream;
121  }
122  }
123 }