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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // An output device for TCP/IP Network connections
20 /****************************************************************************/
21 
22 
23 // ==========================================================================
24 // included modules
25 // ==========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif // #ifdef _MSC_VER
31 
32 #ifdef WIN32
33 #define NOMINMAX
34 #include <windows.h>
35 #undef NOMINMAX
36 #else
37 #include <unistd.h>
38 #endif
39 #include <vector>
40 #include "OutputDevice_Network.h"
41 #include "foreign/tcpip/socket.h"
42 #include "utils/common/ToString.h"
43 
44 
45 // ==========================================================================
46 // method definitions
47 // ==========================================================================
49  const int port) {
50  mySocket = new tcpip::Socket(host, port);
51 #ifdef _MSC_VER
52 #pragma warning(push)
53 #pragma warning(disable: 4127) // do not warn about constant conditional expression
54 #endif
55  for (int wait = 1000; true; wait += 1000) {
56 #ifdef _MSC_VER
57 #pragma warning(pop)
58 #endif
59  try {
60  mySocket->connect();
61  break;
62  } catch (tcpip::SocketException& e) {
63  if (wait == 9000) {
64  throw IOError(toString(e.what()) + " (host: " + host + ", port: " + toString(port) + ")");
65  }
66 #ifdef WIN32
67  Sleep(wait);
68 #else
69  usleep(wait * 1000);
70 #endif
71  }
72  }
73 }
74 
75 
77  mySocket->close();
78  delete mySocket;
79 }
80 
81 
82 std::ostream&
84  return myMessage;
85 }
86 
87 
88 void
90  std::string toSend = myMessage.str();
91  std::vector<unsigned char> msg;
92  msg.insert(msg.end(), toSend.begin(), toSend.end());
93  try {
94  mySocket->send(msg);
95  } catch (tcpip::SocketException& e) {
96  throw IOError(toString(e.what()));
97  }
98  myMessage.str("");
99 }
100 
101 
102 /****************************************************************************/
tcpip::Socket * mySocket
the socket to transfer the data
void connect()
Connects to host_:port_.
Definition: socket.cpp:331
OutputDevice_Network(const std::string &host, const int port)
Constructor.
std::ostream & getOStream()
Returns the associated ostream.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
std::ostringstream myMessage
packet buffer
virtual const char * what() const
Definition: socket.h:70
~OutputDevice_Network()
Destructor.
void send(const std::vector< unsigned char > &buffer)
Definition: socket.cpp:374
virtual void postWriteHook()
Sends the data which was written to the string stream over the socket.
void close()
Definition: socket.cpp:356