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 #define NOMINMAX
35 #include <windows.h>
36 #undef NOMINMAX
37 #else
38 #include <unistd.h>
39 #endif
40 #include <vector>
41 #include "OutputDevice_Network.h"
42 #include "foreign/tcpip/socket.h"
43 #include "utils/common/ToString.h"
44 
45 
46 // ==========================================================================
47 // method definitions
48 // ==========================================================================
50  const int port) {
51  mySocket = new tcpip::Socket(host, port);
52 #ifdef _MSC_VER
53 #pragma warning(push)
54 #pragma warning(disable: 4127) // do not warn about constant conditional expression
55 #endif
56  for (int wait = 1000; true; wait += 1000) {
57 #ifdef _MSC_VER
58 #pragma warning(pop)
59 #endif
60  try {
61  mySocket->connect();
62  break;
63  } catch (tcpip::SocketException& e) {
64  if (wait == 9000) {
65  throw IOError(toString(e.what()) + " (host: " + host + ", port: " + toString(port) + ")");
66  }
67 #ifdef WIN32
68  Sleep(wait);
69 #else
70  usleep(wait * 1000);
71 #endif
72  }
73  }
74 }
75 
76 
78  mySocket->close();
79  delete mySocket;
80 }
81 
82 
83 std::ostream&
85  return myMessage;
86 }
87 
88 
89 void
91  std::string toSend = myMessage.str();
92  std::vector<unsigned char> msg;
93  msg.insert(msg.end(), toSend.begin(), toSend.end());
94  try {
95  mySocket->send(msg);
96  } catch (tcpip::SocketException& e) {
97  throw IOError(toString(e.what()));
98  }
99  myMessage.str("");
100 }
101 
102 
103 /****************************************************************************/
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:56
std::ostringstream myMessage
packet buffer
~OutputDevice_Network()
Destructor.
virtual const char * what() const
Definition: socket.h:70
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