#include <OutputDevice_Network.h>

The implementation uses a portable socket implementation from the Shawn project (shawn.sf.net) located in src/foreign/tcpip/socket.h. It uses an internal storage for the messages, which is sent via the socket when "postWriteHook" is called.
Definition at line 54 of file OutputDevice_Network.h.
Public Member Functions | |
| OutputDevice_Network (const std::string &host, const int port) throw (IOError) | |
| Constructor. | |
| ~OutputDevice_Network () throw () | |
| Destructor. | |
OutputDevice member methods | |
| void | close () throw () |
| Closes the device and removes it from the dictionary. | |
| bool | closeTag (bool abbreviated=false) throw () |
| Closes the most recently opened tag. | |
| OutputDevice & | indent () throw () |
| Adds indentation. | |
| void | inform (const std::string &msg) |
| Retrieves a message to this device. | |
| virtual bool | ok () throw () |
| returns the information whether one can write into the device | |
| OutputDevice & | openTag (const std::string &xmlElement) throw () |
| Opens an XML tag. | |
| template<class T> | |
| OutputDevice & | operator<< (const T &t) |
| Abstract output operator. | |
| void | setPrecision (unsigned int precision=OUTPUT_ACCURACY) throw () |
| Sets the precison or resets it to default. | |
| bool | writeXMLHeader (const std::string &rootElement, const bool writeConfig=true, const std::string &attrs="", const std::string &comment="") throw () |
| Writes an XML header with optional configuration. | |
Static Public Member Functions | |
static access methods to OutputDevices | |
| static void | closeAll () throw () |
| static bool | createDeviceByOption (const std::string &optionName, const std::string &rootElement="") throw (IOError) |
| Creates the device using the output definition stored in the named option. | |
| static OutputDevice & | getDevice (const std::string &name, const std::string &base="") throw (IOError) |
| Returns the described OutputDevice. | |
| static OutputDevice & | getDeviceByOption (const std::string &name) throw (IOError, InvalidArgument) |
| Returns the device described by the option. | |
Protected Member Functions | |
Methods that override/implement OutputDevice-methods | |
| std::ostream & | getOStream () throw () |
| Returns the associated ostream. | |
| virtual void | postWriteHook () throw () |
| Sends the data which was written to the string stream over the socket. | |
Private Attributes | |
| std::ostringstream | myMessage |
| packet buffer | |
| tcpip::Socket * | mySocket |
| the socket to transfer the data | |
| OutputDevice_Network::OutputDevice_Network | ( | const std::string & | host, | |
| const int | port | |||
| ) | throw (IOError) |
Constructor.
| [in] | host | The host to connect |
| [in] | port | The port to connect |
| IOError | If the connection could not been established |
Definition at line 43 of file OutputDevice_Network.cpp.
References toString(), and tcpip::SocketException::what().
00044 { 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 }
| OutputDevice_Network::~OutputDevice_Network | ( | ) | throw () |
Destructor.
Definition at line 54 of file OutputDevice_Network.cpp.
References tcpip::Socket::close(), and mySocket.
| void OutputDevice::close | ( | ) | throw () [inherited] |
Closes the device and removes it from the dictionary.
Definition at line 133 of file OutputDevice.cpp.
References OutputDevice::closeTag(), and OutputDevice::myOutputDevices.
Referenced by RONet::closeOutput(), GUIDialog_ViewSettings::onCmdExportSetting(), GUIParameterTracker::onCmdSave(), GUIDialog_Breakpoints::onCmdSave(), RODFDetectorCon::save(), PCPolyContainer::save(), GUISelectedStorage::save(), GUISelectedStorage::SingleTypeSelections::save(), RODFDetectorCon::saveAsPOIs(), GUIDialog_ViewSettings::saveDecals(), NBNodeCont::savePlain(), NBEdgeCont::savePlain(), RODFDetectorCon::saveRoutes(), RODFDetector::writeEmitterDefinition(), RODFDetectorCon::writeEmitterPOIs(), RODFDetectorCon::writeEmitters(), RODFDetectorCon::writeEndRerouterDetectors(), RODFDetector::writeSingleSpeedTrigger(), RODFDetectorCon::writeSpeedTrigger(), and RODFDetectorCon::writeValidationDetectors().
00133 { 00134 while (closeTag()); 00135 for (DeviceMap::iterator i=myOutputDevices.begin(); i!=myOutputDevices.end(); ++i) { 00136 if (i->second == this) { 00137 myOutputDevices.erase(i); 00138 break; 00139 } 00140 } 00141 delete this; 00142 }
| void OutputDevice::closeAll | ( | ) | throw () [static, inherited] |
Closes all registered devices
Definition at line 115 of file OutputDevice.cpp.
References OutputDevice::myOutputDevices.
Referenced by GUIRunThread::deleteSim(), and main().
00115 { 00116 while (myOutputDevices.size()!=0) { 00117 myOutputDevices.begin()->second->close(); 00118 } 00119 myOutputDevices.clear(); 00120 }
Closes the most recently opened tag.
The topmost xml-element from the stack is written into the stream as a closing element ("</" + element + ">") and is then removed from the stack. If abbreviated closing is requested, only "/>" is the output.
| [in] | name | whether abbreviated closing is performed |
Definition at line 188 of file OutputDevice.cpp.
References OutputDevice::getOStream(), OutputDevice::indent(), OutputDevice::myXMLStack, and OutputDevice::postWriteHook().
Referenced by OutputDevice::close(), MSPersonControl::erase(), ROVehicle::saveAllAsXML(), MSVehicleControl::scheduleVehicleRemoval(), RORouteDef_Complete::writeXMLDefinition(), RORouteDef_Alternatives::writeXMLDefinition(), and RORoute::writeXMLDefinition().
00188 { 00189 if (!myXMLStack.empty()) { 00190 if (abbreviated) { 00191 getOStream() << "/>" << std::endl; 00192 } else { 00193 std::string indent(4*(myXMLStack.size()-1), ' '); 00194 getOStream() << indent << "</" << myXMLStack.back() << ">" << std::endl; 00195 } 00196 myXMLStack.pop_back(); 00197 postWriteHook(); 00198 return true; 00199 } 00200 return false; 00201 }
| bool OutputDevice::createDeviceByOption | ( | const std::string & | optionName, | |
| const std::string & | rootElement = "" | |||
| ) | throw (IOError) [static, inherited] |
Creates the device using the output definition stored in the named option.
Creates and returns the device named by the option. Asks whether the option and retrieves the name from the option if so. Optionally the XML header gets written as well. Returns whether a device was created (option was set).
Please note, that we do not have to consider the "application base" herein, because this call is only used to get file names of files referenced within XML-declarations of structures which paths already is aware of the cwd.
| [in] | optionName | The name of the option to use for retrieving the output definition |
| [in] | rootElement | The root element to use (XML-output) |
| IOError | If the output could not be built for any reason (error message is supplied) |
Definition at line 91 of file OutputDevice.cpp.
References OutputDevice::getDevice(), OptionsCont::getOptions(), and OutputDevice::writeXMLHeader().
Referenced by MSFrame::buildStreams(), NBNetBuilder::compute(), NBNodeCont::computeNodeShapes(), and main().
00092 { 00093 if (!OptionsCont::getOptions().isSet(optionName)) { 00094 return false; 00095 } 00096 OutputDevice& dev = OutputDevice::getDevice(OptionsCont::getOptions().getString(optionName)); 00097 if (rootElement != "") { 00098 dev.writeXMLHeader(rootElement); 00099 } 00100 return true; 00101 }
| OutputDevice & OutputDevice::getDevice | ( | const std::string & | name, | |
| const std::string & | base = "" | |||
| ) | throw (IOError) [static, inherited] |
Returns the described OutputDevice.
Creates and returns the named device. "stdout" and "-" refer to standard out, "hostname:port" initiates socket connection. Otherwise a filename is assumed and the second parameter may be used to give a base directory. If there already is a device with the same name this one is returned.
| [in] | name | The description of the output name/port/whatever |
| [in] | base | The base path the application is run within |
| IOError | If the output could not be built for any reason (error message is supplied) |
Definition at line 60 of file OutputDevice.cpp.
References TplConvert< E >::_2int(), FileHelpers::checkForRelativity(), OutputDevice::getOStream(), FileHelpers::isSocket(), and OutputDevice::setPrecision().
Referenced by NLHandler::addE1Detector(), NLHandler::addE2Detector(), NLHandler::addEdgeLaneMeanData(), NLHandler::addRouteProbeDetector(), NLHandler::addVTypeProbeDetector(), NLHandler::beginE3Detector(), NBNetBuilder::buildLoaded(), NLDiscreteEventBuilder::buildSaveTLStateCommand(), NLDiscreteEventBuilder::buildSaveTLSwitchesCommand(), NLDiscreteEventBuilder::buildSaveTLSwitchStatesCommand(), OutputDevice::createDeviceByOption(), OutputDevice::getDeviceByOption(), MsgHandler::initOutputOptions(), GUIDialog_ViewSettings::onCmdExportSetting(), GUIParameterTracker::onCmdSave(), GUIDialog_EditViewport::onCmdSave(), GUIDialog_Breakpoints::onCmdSave(), RONet::openOutput(), RODFDetectorCon::save(), PCPolyContainer::save(), GUISelectedStorage::save(), GUISelectedStorage::SingleTypeSelections::save(), RODFDetectorCon::saveAsPOIs(), GUIDialog_ViewSettings::saveDecals(), NBNodeCont::savePlain(), NBEdgeCont::savePlain(), RODFDetectorCon::saveRoutes(), RODFDetector::writeEmitterDefinition(), RODFDetectorCon::writeEmitterPOIs(), RODFDetectorCon::writeEmitters(), RODFDetectorCon::writeEndRerouterDetectors(), RODFDetector::writeSingleSpeedTrigger(), RODFDetectorCon::writeSpeedTrigger(), and RODFDetectorCon::writeValidationDetectors().
00061 { 00062 // check whether the device has already been aqcuired 00063 if (myOutputDevices.find(name)!=myOutputDevices.end()) { 00064 return *myOutputDevices[name]; 00065 } 00066 // build the device 00067 OutputDevice *dev = 0; 00068 // check whether the device shall print to stdout 00069 if (name=="stdout" || name=="-") { 00070 dev = new OutputDevice_COUT(); 00071 } else if (FileHelpers::isSocket(name)) { 00072 try { 00073 int port = TplConvert<char>::_2int(name.substr(name.find(":")+1).c_str()); 00074 dev = new OutputDevice_Network(name.substr(0, name.find(":")), port); 00075 } catch (NumberFormatException &) { 00076 throw IOError("Given port number '" + name.substr(name.find(":")+1) + "' is not numeric."); 00077 } catch (EmptyData &) { 00078 throw IOError("No port number given."); 00079 } 00080 } else { 00081 dev = new OutputDevice_File(FileHelpers::checkForRelativity(name, base)); 00082 } 00083 dev->setPrecision(); 00084 dev->getOStream() << std::setiosflags(std::ios::fixed); 00085 myOutputDevices[name] = dev; 00086 return *dev; 00087 }
| OutputDevice & OutputDevice::getDeviceByOption | ( | const std::string & | name | ) | throw (IOError, InvalidArgument) [static, inherited] |
Returns the device described by the option.
Returns the device named by the option. If the option is unknown, unset or the device was not created before, InvalidArgument is thrown.
Please note, that we do not have to consider the "application base" herein.
| [in] | name | The name of the option to use for retrieving the output definition |
| IOError | If the output could not be built for any reason (error message is supplied) | |
| InvalidArgument | If the option with the given name does not exist |
Definition at line 105 of file OutputDevice.cpp.
References OutputDevice::getDevice(), OptionsCont::getOptions(), and OptionsCont::getString().
Referenced by NBNodeShapeComputer::compute(), NBNodeShapeComputer::computeNodeShapeByCrosses(), MSPersonControl::erase(), main(), MSVehicleControl::scheduleVehicleRemoval(), NBNode::sortNodesEdges(), and MSNet::writeOutput().
00105 { 00106 std::string devName = OptionsCont::getOptions().getString(optionName); 00107 if (myOutputDevices.find(devName)==myOutputDevices.end()) { 00108 throw InvalidArgument("Device '" + devName + "' has not been created."); 00109 } 00110 return OutputDevice::getDevice(devName); 00111 }
| std::ostream & OutputDevice_Network::getOStream | ( | ) | throw () [protected, virtual] |
Returns the associated ostream.
The stream is an ostringstream, actually, into which the message is written. It is sent when postWriteHook is called.
Implements OutputDevice.
Definition at line 61 of file OutputDevice_Network.cpp.
References myMessage.
00061 { 00062 return myMessage; 00063 }
| OutputDevice & OutputDevice::indent | ( | ) | throw () [inherited] |
Adds indentation.
An intendation, depending on the current xml-element-stack size, is written.
Definition at line 171 of file OutputDevice.cpp.
References OutputDevice::getOStream(), OutputDevice::myXMLStack, and OutputDevice::postWriteHook().
Referenced by OutputDevice::closeTag().
00171 { 00172 getOStream() << std::string(4*myXMLStack.size(), ' '); 00173 postWriteHook(); 00174 return *this; 00175 }
| void OutputDevice::inform | ( | const std::string & | msg | ) | [inherited] |
Retrieves a message to this device.
Implementation of the MessageRetriever interface. Writes the given message to the output device.
| [in] | msg | The msg to write to the device |
Definition at line 209 of file OutputDevice.cpp.
References OutputDevice::getOStream(), and OutputDevice::postWriteHook().
00209 { 00210 getOStream() << msg << '\n'; 00211 postWriteHook(); 00212 }
| bool OutputDevice::ok | ( | ) | throw () [virtual, inherited] |
returns the information whether one can write into the device
Definition at line 128 of file OutputDevice.cpp.
References OutputDevice::getOStream().
00128 { 00129 return getOStream().good(); 00130 }
| OutputDevice & OutputDevice::openTag | ( | const std::string & | xmlElement | ) | throw () [inherited] |
Opens an XML tag.
An intendation, depending on the current xml-element-stack size, is written followed by the given xmlelement ("<" + xmlElement) The xmlelement is added to the stack, then.
| [in] | xmlElement | Name of element to open |
Definition at line 179 of file OutputDevice.cpp.
References OutputDevice::getOStream(), OutputDevice::myXMLStack, and OutputDevice::postWriteHook().
Referenced by MSPersonControl::erase(), MSVehicleControl::scheduleVehicleRemoval(), RORouteDef_Complete::writeXMLDefinition(), RORouteDef_Alternatives::writeXMLDefinition(), RORoute::writeXMLDefinition(), OutputDevice::writeXMLHeader(), and MSVehicle::writeXMLRoute().
00179 { 00180 getOStream() << std::string(4*myXMLStack.size(), ' ') << "<" << xmlElement; 00181 postWriteHook(); 00182 myXMLStack.push_back(xmlElement); 00183 return *this; 00184 }
| OutputDevice& OutputDevice::operator<< | ( | const T & | t | ) | [inline, inherited] |
Abstract output operator.
Definition at line 222 of file OutputDevice.h.
References OutputDevice::getOStream(), and OutputDevice::postWriteHook().
00222 { 00223 getOStream() << t; 00224 postWriteHook(); 00225 return *this; 00226 }
| void OutputDevice_Network::postWriteHook | ( | ) | throw () [protected, virtual] |
Sends the data which was written to the string stream over the socket.
Converts the stored message into a vector of chars and sends them via to the socket implementation. Resets the message, afterwards.
Reimplemented from OutputDevice.
Definition at line 67 of file OutputDevice_Network.cpp.
References myMessage, mySocket, and tcpip::Socket::send().
00067 { 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 }
| void OutputDevice::setPrecision | ( | unsigned int | precision = OUTPUT_ACCURACY |
) | throw () [inherited] |
Sets the precison or resets it to default.
| [in] | precision | The accuracy (number of digits behind '.') to set |
Definition at line 146 of file OutputDevice.cpp.
References OutputDevice::getOStream().
Referenced by MSVTypeProbe::execute(), OutputDevice::getDevice(), NBNodeCont::savePlain(), RORouteDef_Alternatives::writeXMLDefinition(), and RORoute::writeXMLDefinition().
00146 { 00147 getOStream() << std::setprecision(precision); 00148 }
| bool OutputDevice::writeXMLHeader | ( | const std::string & | rootElement, | |
| const bool | writeConfig = true, |
|||
| const std::string & | attrs = "", |
|||
| const std::string & | comment = "" | |||
| ) | throw () [inherited] |
Writes an XML header with optional configuration.
If something has been written (myXMLStack is not empty), nothing is written and false returned.
| [in] | rootElement | The root element to use |
| [in] | writeConfig | Whether the configuration used while this file was generated shall be saved |
| [in] | attrs | Additional attributes to save within the rootElement |
| [in] | comment | Additional comment (saved in front the rootElement) |
Definition at line 152 of file OutputDevice.cpp.
References OptionsCont::getOptions(), OutputDevice::getOStream(), OutputDevice::myXMLStack, OutputDevice::openTag(), and OptionsCont::writeXMLHeader().
Referenced by OutputDevice::createDeviceByOption(), RONet::openOutput(), RODFDetectorCon::save(), PCPolyContainer::save(), RODFDetectorCon::saveAsPOIs(), NBNodeCont::savePlain(), NBEdgeCont::savePlain(), RODFDetectorCon::saveRoutes(), RODFDetector::writeEmitterDefinition(), RODFDetectorCon::writeEmitterPOIs(), RODFDetectorCon::writeEmitters(), RODFDetectorCon::writeEndRerouterDetectors(), RODFDetector::writeSingleSpeedTrigger(), RODFDetectorCon::writeSpeedTrigger(), and RODFDetectorCon::writeValidationDetectors().
00153 { 00154 if (myXMLStack.empty()) { 00155 OptionsCont::getOptions().writeXMLHeader(getOStream(), writeConfig); 00156 if (comment != "") { 00157 getOStream() << comment << "\n"; 00158 } 00159 openTag(rootElement); 00160 if (attrs != "") { 00161 getOStream() << " " << attrs; 00162 } 00163 getOStream() << ">\n"; 00164 return true; 00165 } 00166 return false; 00167 }
std::ostringstream OutputDevice_Network::myMessage [private] |
packet buffer
Definition at line 95 of file OutputDevice_Network.h.
Referenced by getOStream(), and postWriteHook().
tcpip::Socket* OutputDevice_Network::mySocket [private] |
the socket to transfer the data
Definition at line 98 of file OutputDevice_Network.h.
Referenced by postWriteHook(), and ~OutputDevice_Network().
1.5.6