SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
12 // Copyright (C) 2001-2013 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 #include <vector>
34 #include "OutputDevice_Network.h"
35 #include "foreign/tcpip/socket.h"
36 #include "utils/common/ToString.h"
37 
38 #ifdef CHECK_MEMORY_LEAKS
39 #include <foreign/nvwa/debug_new.h>
40 #endif // #ifdef CHECK_MEMORY_LEAKS
41 
42 
43 // ==========================================================================
44 // method definitions
45 // ==========================================================================
47  const int port) {
48  mySocket = new tcpip::Socket(host, port);
49  try {
50  mySocket->connect();
51  } catch (tcpip::SocketException& e) {
52  throw IOError(toString(e.what()) + " (host: " + host + ", port: " + toString(port) + ")");
53  }
54 }
55 
56 
58  mySocket->close();
59  delete mySocket;
60 }
61 
62 
63 std::ostream&
65  return myMessage;
66 }
67 
68 
69 void
71  std::string toSend = myMessage.str();
72  std::vector<unsigned char> msg;
73  msg.insert(msg.end(), toSend.begin(), toSend.end());
74  mySocket->send(msg);
75  myMessage.str("");
76 }
77 
78 
79 /****************************************************************************/
tcpip::Socket * mySocket
the socket to transfer the data
void connect()
Connects to host_:port_.
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:51
virtual const char * what() const
Definition: socket.h:70
void send(const std::vector< unsigned char > &buffer)
virtual void postWriteHook()
Sends the data which was written to the string stream over the socket.
void close()
Definition: socket.cpp:347