IBR-DTNSuite
0.12
Main Page
Namespaces
Classes
Files
File List
File Members
StreamConnection.cpp
Go to the documentation of this file.
1
/*
2
* StreamConnection.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/StreamConnection.h
"
23
#include "
ibrdtn/data/Exceptions.h
"
24
#include "
ibrdtn/streams/StreamContactHeader.h
"
25
#include "
ibrdtn/streams/StreamDataSegment.h
"
26
#include "
ibrdtn/data/Exceptions.h
"
27
#include <
ibrcommon/TimeMeasurement.h
>
28
#include <
ibrcommon/Logger.h
>
29
30
using namespace
dtn::data;
31
32
namespace
dtn
33
{
34
namespace
streams
35
{
36
StreamConnection::StreamConnection(
StreamConnection::Callback
&cb, iostream &stream,
const
dtn::data::Length
buffer_size)
37
: std::iostream(&_buf), _callback(cb), _buf(*this, stream, buffer_size), _shutdown_reason(CONNECTION_SHUTDOWN_NOTSET)
38
{
39
}
40
41
StreamConnection::~StreamConnection
()
42
{
43
}
44
45
void
StreamConnection::handshake
(
const
dtn::data::EID
&eid,
const
dtn::data::Timeout
&timeout,
const
dtn::data::Bitset<StreamContactHeader::HEADER_BITS>
&flags)
46
{
47
// create a new header
48
dtn::streams::StreamContactHeader
header(eid);
49
50
// set timeout
51
header.
_keepalive
=
static_cast<
uint16_t
>
(timeout);
52
53
// set flags
54
header.
_flags
= flags;
55
56
// do the handshake
57
_peer = _buf.handshake(header);
58
59
// signal the complete handshake
60
_callback.
eventConnectionUp
(_peer);
61
}
62
63
void
StreamConnection::reject
()
64
{
65
_buf.reject();
66
}
67
68
void
StreamConnection::keepalive
()
69
{
70
_buf.keepalive();
71
}
72
73
void
StreamConnection::shutdown
(
ConnectionShutdownCases
csc)
74
{
75
if
(csc ==
CONNECTION_SHUTDOWN_SIMPLE_SHUTDOWN
)
76
{
77
// wait for the last ACKs
78
_buf.wait();
79
}
80
81
// skip if another shutdown is in progress
82
{
83
ibrcommon::MutexLock
l(_shutdown_reason_lock);
84
if
(_shutdown_reason !=
CONNECTION_SHUTDOWN_NOTSET
)
85
{
86
_buf.abort();
87
return
;
88
}
89
_shutdown_reason = csc;
90
}
91
92
try
{
93
switch
(csc)
94
{
95
case
CONNECTION_SHUTDOWN_IDLE
:
96
_buf.shutdown(
StreamDataSegment::MSG_SHUTDOWN_IDLE_TIMEOUT
);
97
_buf.abort();
98
_callback.
eventTimeout
();
99
break
;
100
case
CONNECTION_SHUTDOWN_ERROR
:
101
_buf.abort();
102
_callback.
eventError
();
103
break
;
104
case
CONNECTION_SHUTDOWN_SIMPLE_SHUTDOWN
:
105
_buf.shutdown(
StreamDataSegment::MSG_SHUTDOWN_NONE
);
106
_callback.
eventShutdown
(csc);
107
break
;
108
case
CONNECTION_SHUTDOWN_NODE_TIMEOUT
:
109
_buf.abort();
110
_callback.
eventTimeout
();
111
break
;
112
case
CONNECTION_SHUTDOWN_PEER_SHUTDOWN
:
113
_buf.shutdown(
StreamDataSegment::MSG_SHUTDOWN_NONE
);
114
_buf.abort();
115
_callback.
eventShutdown
(csc);
116
break
;
117
case
CONNECTION_SHUTDOWN_NOTSET
:
118
_buf.abort();
119
_callback.
eventShutdown
(csc);
120
break
;
121
}
122
}
catch
(
const
StreamConnection::StreamErrorException
&) {
123
_callback.
eventError
();
124
}
125
126
_buf.close();
127
_callback.
eventConnectionDown
();
128
}
129
130
void
StreamConnection::eventShutdown(
StreamConnection::ConnectionShutdownCases
csc)
131
{
132
_callback.
eventShutdown
(csc);
133
}
134
135
void
StreamConnection::eventBundleAck(
const
dtn::data::Length
&ack)
136
{
137
_callback.
eventBundleAck
(ack);
138
}
139
140
void
StreamConnection::eventBundleRefused()
141
{
142
IBRCOMMON_LOGGER_DEBUG_TAG
(
"StreamConnection"
, 20) <<
"bundle has been refused"
<<
IBRCOMMON_LOGGER_ENDL
;
143
_callback.
eventBundleRefused
();
144
}
145
146
void
StreamConnection::eventBundleForwarded()
147
{
148
IBRCOMMON_LOGGER_DEBUG_TAG
(
"StreamConnection"
, 20) <<
"bundle has been forwarded"
<<
IBRCOMMON_LOGGER_ENDL
;
149
_callback.
eventBundleForwarded
();
150
}
151
152
void
StreamConnection::connectionTimeout()
153
{
154
// call superclass
155
_callback.
eventTimeout
();
156
}
157
158
void
StreamConnection::enableIdleTimeout
(
const
dtn::data::Timeout
&seconds)
159
{
160
_buf.enableIdleTimeout(seconds);
161
}
162
}
163
}
ibrdtn
ibrdtn
streams
StreamConnection.cpp
Generated on Thu Mar 27 2014 09:26:21 for IBR-DTNSuite by
1.8.4