SUMO - Simulation of Urban MObility
OutputDevice_Network.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // An output device for TCP/IP Network connections
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ==========================================================================
00025 // included modules
00026 // ==========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif // #ifdef _MSC_VER
00032 
00033 #include <vector>
00034 #include "OutputDevice_Network.h"
00035 #include "foreign/tcpip/socket.h"
00036 #include "utils/common/ToString.h"
00037 
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // #ifdef CHECK_MEMORY_LEAKS
00041 
00042 
00043 // ==========================================================================
00044 // method definitions
00045 // ==========================================================================
00046 OutputDevice_Network::OutputDevice_Network(const std::string& host,
00047         const int port) {
00048     mySocket = new tcpip::Socket(host, port);
00049     try {
00050         mySocket->connect();
00051     } catch (tcpip::SocketException& e) {
00052         throw IOError(toString(e.what()) + " (host: " + host + ", port: " + toString(port) + ")");
00053     }
00054 }
00055 
00056 
00057 OutputDevice_Network::~OutputDevice_Network() {
00058     mySocket->close();
00059     delete mySocket;
00060 }
00061 
00062 
00063 std::ostream&
00064 OutputDevice_Network::getOStream() {
00065     return myMessage;
00066 }
00067 
00068 
00069 void
00070 OutputDevice_Network::postWriteHook() {
00071     std::string toSend = myMessage.str();
00072     std::vector<unsigned char> msg;
00073     msg.insert(msg.end(), toSend.begin(), toSend.end());
00074     mySocket->send(msg);
00075     myMessage.str("");
00076 }
00077 
00078 
00079 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines