SUMO - Simulation of Urban MObility
OutputDevice_Network.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // An output device for TCP/IP Network connections
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2006-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ==========================================================================
25 // included modules
26 // ==========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif // #ifdef _MSC_VER
32 
33 #ifdef WIN32
34 #include <windows.h>
35 #else
36 #include <unistd.h>
37 #endif
38 #include <vector>
39 #include "OutputDevice_Network.h"
40 #include "foreign/tcpip/socket.h"
41 #include "utils/common/ToString.h"
42 
43 
44 // ==========================================================================
45 // method definitions
46 // ==========================================================================
48  const int port) {
49  mySocket = new tcpip::Socket(host, port);
50 #ifdef _MSC_VER
51 #pragma warning(push)
52 #pragma warning(disable: 4127) // do not warn about constant conditional expression
53 #endif
54  for (int wait = 1000; true; wait += 1000) {
55 #ifdef _MSC_VER
56 #pragma warning(pop)
57 #endif
58  try {
59  mySocket->connect();
60  break;
61  } catch (tcpip::SocketException& e) {
62  if (wait == 9000) {
63  throw IOError(toString(e.what()) + " (host: " + host + ", port: " + toString(port) + ")");
64  }
65 #ifdef WIN32
66  Sleep(wait);
67 #else
68  usleep(wait * 1000);
69 #endif
70  }
71  }
72 }
73 
74 
76  mySocket->close();
77  delete mySocket;
78 }
79 
80 
81 std::ostream&
83  return myMessage;
84 }
85 
86 
87 void
89  std::string toSend = myMessage.str();
90  std::vector<unsigned char> msg;
91  msg.insert(msg.end(), toSend.begin(), toSend.end());
92  try {
93  mySocket->send(msg);
94  } catch (tcpip::SocketException& e) {
95  throw IOError(toString(e.what()));
96  }
97  myMessage.str("");
98 }
99 
100 
101 /****************************************************************************/
tcpip::Socket * mySocket
the socket to transfer the data
void connect()
Connects to host_:port_.
Definition: socket.cpp:324
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:56
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:367
virtual void postWriteHook()
Sends the data which was written to the string stream over the socket.
void close()
Definition: socket.cpp:349