IBR-DTNSuite
0.12
Main Page
Namespaces
Classes
Files
File List
File Members
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
22
#include "
ibrdtn/streams/StreamDataSegment.h
"
23
#include "
ibrdtn/data/Number.h
"
24
25
namespace
dtn
26
{
27
namespace
streams
28
{
29
StreamDataSegment::StreamDataSegment
(
SegmentType
type,
const
dtn::data::Number
&size)
30
: _value(size), _type(type), _reason(MSG_SHUTDOWN_IDLE_TIMEOUT), _flags(0)
31
{
32
}
33
34
StreamDataSegment::StreamDataSegment
(
SegmentType
type)
35
: _value(0), _type(type), _reason(MSG_SHUTDOWN_IDLE_TIMEOUT), _flags(0)
36
{
37
}
38
39
StreamDataSegment::StreamDataSegment
(
ShutdownReason
reason,
const
dtn::data::Number
&reconnect)
40
: _value(reconnect), _type(MSG_SHUTDOWN), _reason(reason), _flags(3)
41
{
42
}
43
44
StreamDataSegment::~StreamDataSegment
()
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
{
58
case
StreamDataSegment::MSG_DATA_SEGMENT
:
59
// write the length + data
60
stream << seg.
_value
;
61
break
;
62
63
case
StreamDataSegment::MSG_ACK_SEGMENT
:
64
// write the acknowledged length
65
stream << seg.
_value
;
66
break
;
67
68
case
StreamDataSegment::MSG_REFUSE_BUNDLE
:
69
break
;
70
71
case
StreamDataSegment::MSG_KEEPALIVE
:
72
break
;
73
74
case
StreamDataSegment::MSG_SHUTDOWN
:
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
{
94
case
StreamDataSegment::MSG_DATA_SEGMENT
:
95
// read the length
96
stream >> seg.
_value
;
97
break
;
98
99
case
StreamDataSegment::MSG_ACK_SEGMENT
:
100
// read the acknowledged length
101
stream >> seg.
_value
;
102
break
;
103
104
case
StreamDataSegment::MSG_REFUSE_BUNDLE
:
105
break
;
106
107
case
StreamDataSegment::MSG_KEEPALIVE
:
108
break
;
109
110
case
StreamDataSegment::MSG_SHUTDOWN
:
111
// read the reason (char) + reconnect time (SDNV)
112
char
reason;
113
stream.get(reason);
114
seg.
_reason
=
StreamDataSegment::ShutdownReason
(reason);
115
116
stream >> seg.
_value
;
117
break
;
118
}
119
120
return
stream;
121
}
122
}
123
}
ibrdtn
ibrdtn
streams
StreamDataSegment.cpp
Generated on Thu Mar 27 2014 09:26:21 for IBR-DTNSuite by
1.8.4