OutputDevice_Network.cpp

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

Generated on Wed May 5 00:06:35 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6