![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/probe/bus/TextProbeBus.hpp> 00029 #include <WNS/simulator/ISimulator.hpp> 00030 #include <WNS/events/scheduler/Interface.hpp> 00031 00032 #include <sstream> 00033 #include <iomanip> 00034 00035 using namespace wns::probe::bus; 00036 00037 STATIC_FACTORY_REGISTER_WITH_CREATOR( 00038 TextProbeBus, 00039 wns::probe::bus::ProbeBus, 00040 "TextProbeBus", 00041 wns::PyConfigViewCreator); 00042 00043 00044 TextProbeBus::TextProbeBus(const wns::pyconfig::View& pyco): 00045 key_(pyco.get<std::string>("key")), 00046 outputPath_(wns::simulator::getConfiguration().get<std::string>("outputDir")), 00047 outfileBase_(pyco.get<std::string>("outputFilename")), 00048 writeHeader_(pyco.get<bool>("writeHeader")), 00049 prependSimTimeFlag_(pyco.get<bool>("prependSimTimeFlag")), 00050 isJSON_(pyco.get<bool>("isJSON")), 00051 simTimePrecision_(pyco.get<int>("simTimePrecision")), 00052 simTimeWidth_(pyco.get<int>("simTimeWidth")), 00053 skipInterval_(pyco.get<int>("skipInterval")), 00054 numCalls_(0), 00055 jsonEntrySeparator_("") 00056 { 00057 } 00058 00059 TextProbeBus::~TextProbeBus() 00060 { 00061 if(isJSON_) 00062 { 00063 std::stringstream filename; 00064 filename << outputPath_ << "/" << outfileBase_ << "_Text.dat"; 00065 00066 // Try to append to existing file 00067 std::ofstream existingFile(filename.str().c_str(), std::ios::app); 00068 if (existingFile.good()) 00069 { 00070 existingFile << "]}" << std::endl; 00071 } 00072 existingFile.close(); 00073 } 00074 } 00075 00076 void 00077 TextProbeBus::output() 00078 { 00079 std::stringstream filename; 00080 filename << outputPath_ << "/" << outfileBase_ << "_Text.dat"; 00081 00082 // Try to append to existing file 00083 std::ofstream existingFile(filename.str().c_str(), std::ios::app); 00084 if (existingFile.good()) 00085 { 00086 printText(existingFile); 00087 existingFile.close(); 00088 return; 00089 } 00090 existingFile.close(); 00091 00092 // If appending did not work, open a new file 00093 std::ofstream newFile(filename.str().c_str(), std::ios::out); 00094 printText(newFile); 00095 newFile.close(); 00096 } 00097 00098 00099 void 00100 TextProbeBus::onMeasurement(const wns::simulator::Time&, const double&, const IContext& reg) 00101 { 00102 putText(reg.getString(key_).c_str()); 00103 } 00104 00105 bool 00106 TextProbeBus::accepts(const wns::simulator::Time&, const IContext& reg) 00107 { 00108 return reg.knows(key_); 00109 } 00110 00111 00112 void 00113 TextProbeBus::printText(std::ostream& theStream) 00114 { 00115 bool output_time = true; 00116 std::string prefix = "#"; 00117 std::string separator(prefix + " -------------------------------------"); 00118 00119 if (writeHeader_) 00120 { 00121 if (!isJSON_) 00122 { 00123 theStream << prefix + " PROBE TEXT RESULT (THIS IS A MAGIC LINE)" 00124 << std::endl 00125 << separator 00126 << std::endl 00127 << prefix + " Text file of data, any style " 00128 << std::endl 00129 << separator 00130 << std::endl 00131 << prefix + " Name: " 00132 << name_ 00133 << std::endl 00134 << prefix + " Description: " 00135 << description_ 00136 << std::endl 00137 << separator 00138 << std::endl; 00139 } 00140 else 00141 { 00142 theStream << "{ \"content\" : [\n" << std::endl; 00143 } 00144 00145 if (!theStream.good()) 00146 { 00147 throw(wns::Exception("Can't dump ProbeText data file")); 00148 } 00149 writeHeader_ = false; 00150 } 00151 00152 while (! messages_.empty()) 00153 { 00154 if (output_time && (!isJSON_)) 00155 { 00156 time_t cur_time = time(NULL); 00157 00158 output_time = false; 00159 std::string time_str = ctime(&cur_time); 00160 theStream << prefix + " " 00161 << time_str; 00162 if (!theStream.good()) 00163 { 00164 throw(wns::Exception("Can't dump ProbeText data file")); 00165 } 00166 } 00167 theStream << jsonEntrySeparator_ << std::endl; 00168 00169 theStream << messages_.front() 00170 << std::endl; 00171 00172 if (isJSON_) 00173 { 00174 jsonEntrySeparator_ = ","; 00175 } 00176 00177 if (!theStream.good()) 00178 { 00179 throw(wns::Exception("Can't dump ProbeText data file")); 00180 } 00181 messages_.pop_front(); 00182 } 00183 } 00184 00185 00186 void 00187 TextProbeBus::putText(std::string message) 00188 { 00189 numCalls_++; 00190 00191 // Ignore this value? 00192 if (skipInterval_ && ((numCalls_ - 1) % skipInterval_)) 00193 { 00194 return; 00195 } 00196 00197 // Store value for later output 00198 std::stringstream str; 00199 if (prependSimTimeFlag_) 00200 { 00201 wns::simulator::Time simTime = wns::simulator::getEventScheduler()->getTime(); 00202 00203 str << "(" 00204 << resetiosflags(std::ios::fixed) 00205 << resetiosflags(std::ios::scientific) 00206 << resetiosflags(std::ios::right) 00207 << setiosflags(std::ios::right) 00208 << setiosflags(std::ios::fixed) 00209 << setiosflags(std::ios::dec) 00210 << std::setprecision(simTimePrecision_) 00211 << std::setw(simTimeWidth_) 00212 << simTime 00213 << ")\t" 00214 << message; 00215 } 00216 else 00217 { 00218 str << message; 00219 } 00220 messages_.push_back(str.str()); 00221 }
1.5.5