Eclipse SUMO - Simulation of Urban MObility
OutputDevice_Network.cpp
Go to the documentation of this file.
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3
// Copyright (C) 2006-2019 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials
5
// are made available under the terms of the Eclipse Public License v2.0
6
// which accompanies this distribution, and is available at
7
// http://www.eclipse.org/legal/epl-v20.html
8
// SPDX-License-Identifier: EPL-2.0
9
/****************************************************************************/
16
// An output device for TCP/IP Network connections
17
/****************************************************************************/
18
19
20
// ==========================================================================
21
// included modules
22
// ==========================================================================
23
#include <
config.h
>
24
25
#include <thread>
26
#include <chrono>
27
#include <vector>
28
#include "
OutputDevice_Network.h
"
29
#include "
foreign/tcpip/socket.h
"
30
#include "
utils/common/ToString.h
"
31
32
33
// ==========================================================================
34
// method definitions
35
// ==========================================================================
36
OutputDevice_Network::OutputDevice_Network
(
const
std::string& host,
37
const
int
port) {
38
mySocket
=
new
tcpip::Socket
(host, port);
39
#ifdef _MSC_VER
40
#pragma warning(push)
41
#pragma warning(disable: 4127) // do not warn about constant conditional expression
42
#endif
43
for
(
int
wait = 1000;
true
; wait += 1000) {
44
#ifdef _MSC_VER
45
#pragma warning(pop)
46
#endif
47
try
{
48
mySocket
->
connect
();
49
break
;
50
}
catch
(
tcpip::SocketException
& e) {
51
if
(wait == 9000) {
52
throw
IOError
(
toString
(e.what()) +
" (host: "
+ host +
", port: "
+
toString
(port) +
")"
);
53
}
54
std::this_thread::sleep_for(std::chrono::milliseconds(wait));
55
}
56
}
57
myFilename
= host +
":"
+
toString
(port);
58
}
59
60
61
OutputDevice_Network::~OutputDevice_Network
() {
62
mySocket
->
close
();
63
delete
mySocket
;
64
}
65
66
67
std::ostream&
68
OutputDevice_Network::getOStream
() {
69
return
myMessage
;
70
}
71
72
73
void
74
OutputDevice_Network::postWriteHook
() {
75
std::string toSend =
myMessage
.str();
76
std::vector<unsigned char> msg;
77
msg.insert(msg.end(), toSend.begin(), toSend.end());
78
try
{
79
mySocket
->
send
(msg);
80
}
catch
(
tcpip::SocketException
& e) {
81
throw
IOError
(
toString
(e.what()));
82
}
83
myMessage
.str(
""
);
84
}
85
86
87
/****************************************************************************/
ToString.h
tcpip::Socket
Definition:
socket.h:61
tcpip::Socket::close
void close()
Definition:
socket.cpp:382
tcpip::Socket::connect
void connect()
Connects to host_:port_.
Definition:
socket.cpp:358
OutputDevice_Network::~OutputDevice_Network
~OutputDevice_Network()
Destructor.
Definition:
OutputDevice_Network.cpp:61
OutputDevice::myFilename
std::string myFilename
Definition:
OutputDevice.h:359
OutputDevice_Network::mySocket
tcpip::Socket * mySocket
the socket to transfer the data
Definition:
OutputDevice_Network.h:93
tcpip::Socket::send
void send(const std::vector< unsigned char > &buffer)
Definition:
socket.cpp:400
OutputDevice_Network::myMessage
std::ostringstream myMessage
packet buffer
Definition:
OutputDevice_Network.h:90
tcpip::SocketException
Definition:
socket.h:55
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition:
ToString.h:47
OutputDevice_Network::OutputDevice_Network
OutputDevice_Network(const std::string &host, const int port)
Constructor.
Definition:
OutputDevice_Network.cpp:36
IOError
Definition:
UtilExceptions.h:161
socket.h
OutputDevice_Network.h
config.h
OutputDevice_Network::postWriteHook
virtual void postWriteHook()
Sends the data which was written to the string stream over the socket.
Definition:
OutputDevice_Network.cpp:74
OutputDevice_Network::getOStream
std::ostream & getOStream()
Returns the associated ostream.
Definition:
OutputDevice_Network.cpp:68
src
utils
iodevices
OutputDevice_Network.cpp
Generated on Sun Dec 13 2020 08:44:58 for Eclipse SUMO - Simulation of Urban MObility by
1.8.20