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
/****************************************************************************/
17
// An output device for TCP/IP Network connections
18
/****************************************************************************/
19
20
21
// ==========================================================================
22
// included modules
23
// ==========================================================================
24
#include <
config.h
>
// #ifdef _MSC_VER
25
26
#ifdef WIN32
27
#define NOMINMAX
28
#include <windows.h>
29
#undef NOMINMAX
30
#else
31
#include <unistd.h>
32
#endif
33
#include <vector>
34
#include "
OutputDevice_Network.h
"
35
#include "
foreign/tcpip/socket.h
"
36
#include "
utils/common/ToString.h
"
37
38
39
// ==========================================================================
40
// method definitions
41
// ==========================================================================
42
OutputDevice_Network::OutputDevice_Network
(
const
std::string& host,
43
const
int
port) {
44
mySocket
=
new
tcpip::Socket
(host, port);
45
#ifdef _MSC_VER
46
#pragma warning(push)
47
#pragma warning(disable: 4127) // do not warn about constant conditional expression
48
#endif
49
for
(
int
wait = 1000;
true
; wait += 1000) {
50
#ifdef _MSC_VER
51
#pragma warning(pop)
52
#endif
53
try
{
54
mySocket
->
connect
();
55
break
;
56
}
catch
(
tcpip::SocketException
& e) {
57
if
(wait == 9000) {
58
throw
IOError
(
toString
(e.what()) +
" (host: "
+ host +
", port: "
+
toString
(port) +
")"
);
59
}
60
#ifdef WIN32
61
Sleep(wait);
62
#else
63
usleep(wait * 1000);
64
#endif
65
}
66
}
67
myFilename
= host +
":"
+
toString
(port);
68
}
69
70
71
OutputDevice_Network::~OutputDevice_Network
() {
72
mySocket
->
close
();
73
delete
mySocket
;
74
}
75
76
77
std::ostream&
78
OutputDevice_Network::getOStream
() {
79
return
myMessage
;
80
}
81
82
83
void
84
OutputDevice_Network::postWriteHook
() {
85
std::string toSend =
myMessage
.str();
86
std::vector<unsigned char> msg;
87
msg.insert(msg.end(), toSend.begin(), toSend.end());
88
try
{
89
mySocket
->
send
(msg);
90
}
catch
(
tcpip::SocketException
& e) {
91
throw
IOError
(
toString
(e.what()));
92
}
93
myMessage
.str(
""
);
94
}
95
96
97
/****************************************************************************/
ToString.h
tcpip::Socket
Definition:
socket.h:59
tcpip::Socket::close
void close()
Definition:
socket.cpp:387
tcpip::Socket::connect
void connect()
Connects to host_:port_.
Definition:
socket.cpp:363
OutputDevice_Network::~OutputDevice_Network
~OutputDevice_Network()
Destructor.
Definition:
OutputDevice_Network.cpp:71
OutputDevice::myFilename
std::string myFilename
Definition:
OutputDevice.h:360
OutputDevice_Network::mySocket
tcpip::Socket * mySocket
the socket to transfer the data
Definition:
OutputDevice_Network.h:94
tcpip::Socket::send
void send(const std::vector< unsigned char > &buffer)
Definition:
socket.cpp:405
OutputDevice_Network::myMessage
std::ostringstream myMessage
packet buffer
Definition:
OutputDevice_Network.h:91
tcpip::SocketException
Definition:
socket.h:53
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition:
ToString.h:48
OutputDevice_Network::OutputDevice_Network
OutputDevice_Network(const std::string &host, const int port)
Constructor.
Definition:
OutputDevice_Network.cpp:42
IOError
Definition:
UtilExceptions.h:147
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:84
OutputDevice_Network::getOStream
std::ostream & getOStream()
Returns the associated ostream.
Definition:
OutputDevice_Network.cpp:78
src
utils
iodevices
OutputDevice_Network.cpp
Generated on Wed Jan 15 2020 15:04:13 for Eclipse SUMO - Simulation of Urban MObility by
1.8.16