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-2015 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 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // #ifdef CHECK_MEMORY_LEAKS
46 
47 
48 // ==========================================================================
49 // method definitions
50 // ==========================================================================
52  const int port) {
53  mySocket = new tcpip::Socket(host, port);
54  for (int wait = 1000; true; wait += 1000) {
55  try {
56  mySocket->connect();
57  break;
58  } catch (tcpip::SocketException& e) {
59  if (wait == 9000) {
60  throw IOError(toString(e.what()) + " (host: " + host + ", port: " + toString(port) + ")");
61  }
62 #ifdef WIN32
63  Sleep(wait);
64 #else
65  usleep(wait * 1000);
66 #endif
67  }
68  }
69 }
70 
71 
73  mySocket->close();
74  delete mySocket;
75 }
76 
77 
78 std::ostream&
80  return myMessage;
81 }
82 
83 
84 void
86  std::string toSend = myMessage.str();
87  std::vector<unsigned char> msg;
88  msg.insert(msg.end(), toSend.begin(), toSend.end());
89  try {
90  mySocket->send(msg);
91  } catch (tcpip::SocketException& e) {
92  throw IOError(toString(e.what()));
93  }
94  myMessage.str("");
95 }
96 
97 
98 /****************************************************************************/
tcpip::Socket * mySocket
the socket to transfer the data
void connect()
Connects to host_:port_.
Definition: socket.cpp:314
OutputDevice_Network(const std::string &host, const int port)
Constructor.
std::ostream & getOStream()
Returns the associated ostream.
std::ostringstream myMessage
packet buffer
~OutputDevice_Network()
Destructor.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
virtual const char * what() const
Definition: socket.h:70
void send(const std::vector< unsigned char > &buffer)
Definition: socket.cpp:363
virtual void postWriteHook()
Sends the data which was written to the string stream over the socket.
void close()
Definition: socket.cpp:345