<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>DTN Reference Implementation: Simulator.cc Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.8 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>Simulator.cc</h1><a href="Simulator_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * Copyright 2004-2006 Intel Corporation</span> <a name="l00003"></a>00003 <span class="comment"> * </span> <a name="l00004"></a>00004 <span class="comment"> * Licensed under the Apache License, Version 2.0 (the "License");</span> <a name="l00005"></a>00005 <span class="comment"> * you may not use this file except in compliance with the License.</span> <a name="l00006"></a>00006 <span class="comment"> * You may obtain a copy of the License at</span> <a name="l00007"></a>00007 <span class="comment"> * </span> <a name="l00008"></a>00008 <span class="comment"> * http://www.apache.org/licenses/LICENSE-2.0</span> <a name="l00009"></a>00009 <span class="comment"> * </span> <a name="l00010"></a>00010 <span class="comment"> * Unless required by applicable law or agreed to in writing, software</span> <a name="l00011"></a>00011 <span class="comment"> * distributed under the License is distributed on an "AS IS" BASIS,</span> <a name="l00012"></a>00012 <span class="comment"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> <a name="l00013"></a>00013 <span class="comment"> * See the License for the specific language governing permissions and</span> <a name="l00014"></a>00014 <span class="comment"> * limitations under the License.</span> <a name="l00015"></a>00015 <span class="comment"> */</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span> <a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor"># include <<a class="code" href="dtn-config_8h.html">dtn-config.h</a>></span> <a name="l00019"></a>00019 <span class="preprocessor">#endif</span> <a name="l00020"></a>00020 <span class="preprocessor"></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <oasys/tclcmd/TclCommand.h></span> <a name="l00022"></a>00022 <a name="l00023"></a>00023 <span class="preprocessor">#include "<a class="code" href="Simulator_8h.html">Simulator.h</a>"</span> <a name="l00024"></a>00024 <span class="preprocessor">#include "<a class="code" href="sim_2Node_8h.html">Node.h</a>"</span> <a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="Topology_8h.html">Topology.h</a>"</span> <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="SimLog_8h.html">SimLog.h</a>"</span> <a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="BundleTimestamp_8h.html">bundling/BundleTimestamp.h</a>"</span> <a name="l00028"></a>00028 <a name="l00029"></a>00029 <span class="keyword">using namespace </span>dtn; <a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="keyword">namespace </span>dtnsim { <a name="l00032"></a>00032 <a name="l00033"></a>00033 <span class="keyword">template</span><> <a name="l00034"></a>00034 Simulator* oasys::Singleton<Simulator, false>::instance_ = NULL; <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00037"></a>00037 <a name="l00038"></a>00038 <span class="keywordtype">double</span> <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">Simulator::time_</a> = 0; <a name="l00039"></a>00039 <span class="keywordtype">bool</span> <a class="code" href="classdtnsim_1_1Simulator.html#2066f289b67cff4cd17baf93a8a12902">Simulator::interrupted_</a> = <span class="keyword">false</span>; <a name="l00040"></a>00040 <span class="keywordtype">double</span> <a class="code" href="classdtnsim_1_1Simulator.html#c3e483526794d4ccd4a3e9e78bc95b2f" title="time to end the simulation">Simulator::runtill_</a> = -1; <a name="l00041"></a>00041 <a name="l00042"></a>00042 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00043"></a><a class="code" href="classdtnsim_1_1Simulator.html#5d17ae3f95414a283a1595fdd2cefb3e">00043</a> <a class="code" href="classdtnsim_1_1Simulator.html#5d17ae3f95414a283a1595fdd2cefb3e" title="Constructor.">Simulator::Simulator</a>() <a name="l00044"></a>00044 : <a class="code" href="classoasys_1_1Logger.html">Logger</a>(<span class="stringliteral">"Simulator"</span>, <span class="stringliteral">"/dtnsim"</span>), <a name="l00045"></a>00045 eventq_(), <a name="l00046"></a>00046 exit_event_(NULL) <a name="l00047"></a>00047 { <a name="l00048"></a>00048 } <a name="l00049"></a>00049 <a name="l00050"></a>00050 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00051"></a>00051 <span class="keywordtype">void</span> <a name="l00052"></a><a class="code" href="classdtnsim_1_1Simulator.html#ebd17c1beaacc7f1f6e11e963d09e0ec">00052</a> <a class="code" href="classdtnsim_1_1Simulator.html#ebd17c1beaacc7f1f6e11e963d09e0ec" title="Add an event to the main event queue.">Simulator::post</a>(<a class="code" href="classdtnsim_1_1SimEvent.html">SimEvent</a>* e) <a name="l00053"></a>00053 { <a name="l00054"></a>00054 instance_->eventq_.push(e); <a name="l00055"></a>00055 } <a name="l00056"></a>00056 <a name="l00057"></a>00057 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00058"></a>00058 <span class="keywordtype">void</span> <a name="l00059"></a><a class="code" href="classdtnsim_1_1Simulator.html#7acdfd3573abad2d862944e92bfca16d">00059</a> <a class="code" href="classdtnsim_1_1Simulator.html#7acdfd3573abad2d862944e92bfca16d" title="Stops simulation and exits the loop.">Simulator::exit</a>() <a name="l00060"></a>00060 {<a class="code" href="classdtnsim_1_1Simulator.html#7acdfd3573abad2d862944e92bfca16d" title="Stops simulation and exits the loop."></a> <a name="l00061"></a>00061 <a class="code" href="classdtnsim_1_1Simulator.html#7acdfd3573abad2d862944e92bfca16d" title="Stops simulation and exits the loop."> ::exit</a>(0); <a name="l00062"></a>00062 } <a name="l00063"></a>00063 <a name="l00064"></a>00064 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00065"></a>00065 <span class="keywordtype">int</span> <a name="l00066"></a><a class="code" href="classdtnsim_1_1Simulator.html#f52eb7a2e5c0ee7343fcbd50dc2faef7">00066</a> <a class="code" href="classdtnsim_1_1Simulator.html#f52eb7a2e5c0ee7343fcbd50dc2faef7" title="Handle all bundle events at nodes returning the amount of time (in ms) until the...">Simulator::run_node_events</a>() <a name="l00067"></a>00067 { <a name="l00068"></a>00068 <span class="keywordtype">bool</span> done; <a name="l00069"></a>00069 <span class="keywordtype">int</span> next_timer; <a name="l00070"></a>00070 <span class="keywordflow">do</span> { <a name="l00071"></a>00071 done = <span class="keyword">true</span>; <a name="l00072"></a>00072 next_timer = -1; <a name="l00073"></a>00073 <a name="l00074"></a>00074 Topology::NodeTable::iterator iter; <a name="l00075"></a>00075 <span class="keywordflow">for</span> (iter = <a class="code" href="classdtnsim_1_1Topology.html#fa8695d124c438bc35f43ef9c4767bbd">Topology::node_table</a>()->begin(); <a name="l00076"></a>00076 iter != <a class="code" href="classdtnsim_1_1Topology.html#fa8695d124c438bc35f43ef9c4767bbd">Topology::node_table</a>()->end(); <a name="l00077"></a>00077 ++iter) <a name="l00078"></a>00078 { <a name="l00079"></a>00079 <a class="code" href="classdtnsim_1_1Simulator.html#327cd35d3972508033443ffeaf9eb90d">check_interrupt</a>(); <a name="l00080"></a>00080 <a name="l00081"></a>00081 <a class="code" href="classdtnsim_1_1Node.html" title="Class representing a node in the simulator (i.e.">Node</a>* node = iter->second; <a name="l00082"></a>00082 node-><a class="code" href="classdtnsim_1_1Node.html#5f8ad70a765bbb1b8d8e8d07ef502036" title="Set the node as the &quot;active&quot; node in the simulation.">set_active</a>(); <a name="l00083"></a>00083 <a name="l00084"></a>00084 <span class="keywordtype">int</span> next = oasys::TimerSystem::instance()->run_expired_timers(); <a name="l00085"></a>00085 <span class="keywordflow">if</span> (next != -1) { <a name="l00086"></a>00086 <span class="keywordflow">if</span> (next_timer == -1) { <a name="l00087"></a>00087 next_timer = next; <a name="l00088"></a>00088 } <span class="keywordflow">else</span> { <a name="l00089"></a>00089 next_timer = std::min(next_timer, next); <a name="l00090"></a>00090 } <a name="l00091"></a>00091 } <a name="l00092"></a>00092 <a name="l00093"></a>00093 log_debug(<span class="stringliteral">"processing all bundle events for node %s"</span>, node-><a class="code" href="classdtnsim_1_1Node.html#cac8ab04c1d7117e0dea6b3ad74f70de" title="Accessor for name.">name</a>()); <a name="l00094"></a>00094 <span class="keywordflow">if</span> (node-><a class="code" href="classdtnsim_1_1Node.html#ec11501277a101b0e58382de65dad675" title="Drain and process a bundle event from the queue, if one exists.">process_one_bundle_event</a>()) { <a name="l00095"></a>00095 done = <span class="keyword">false</span>; <a name="l00096"></a>00096 <span class="keywordflow">while</span> (node-><a class="code" href="classdtnsim_1_1Node.html#ec11501277a101b0e58382de65dad675" title="Drain and process a bundle event from the queue, if one exists.">process_one_bundle_event</a>()) { <a name="l00097"></a>00097 <a class="code" href="classdtnsim_1_1Simulator.html#327cd35d3972508033443ffeaf9eb90d">check_interrupt</a>(); <a name="l00098"></a>00098 } <a name="l00099"></a>00099 } <a name="l00100"></a>00100 } <a name="l00101"></a>00101 } <span class="keywordflow">while</span> (!done); <a name="l00102"></a>00102 <a name="l00103"></a>00103 <span class="keywordflow">return</span> next_timer; <a name="l00104"></a>00104 } <a name="l00105"></a>00105 <a name="l00106"></a>00106 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00107"></a>00107 <span class="keywordtype">void</span> <a name="l00108"></a><a class="code" href="classdtnsim_1_1Simulator.html#045c83976fbabbc3494142e02e0820b1">00108</a> <a class="code" href="classdtnsim_1_1Simulator.html#045c83976fbabbc3494142e02e0820b1">Simulator::log_inqueue_stats</a>() <a name="l00109"></a>00109 { <a name="l00110"></a>00110 Topology::NodeTable::iterator node_iter; <a name="l00111"></a>00111 <span class="keywordflow">for</span> (node_iter = <a class="code" href="classdtnsim_1_1Topology.html#fa8695d124c438bc35f43ef9c4767bbd">Topology::node_table</a>()->begin(); <a name="l00112"></a>00112 node_iter != <a class="code" href="classdtnsim_1_1Topology.html#fa8695d124c438bc35f43ef9c4767bbd">Topology::node_table</a>()->end(); <a name="l00113"></a>00113 ++node_iter) <a name="l00114"></a>00114 { <a name="l00115"></a>00115 <a class="code" href="classdtnsim_1_1Node.html" title="Class representing a node in the simulator (i.e.">Node</a>* node = node_iter->second; <a name="l00116"></a>00116 <a name="l00117"></a>00117 oasys::ScopeLock l(node-><a class="code" href="classdtn_1_1BundleDaemon.html#99030fd37a2c2b727b5e6c64962d6709" title="Accessor for the pending bundles list.">pending_bundles</a>()-><a class="code" href="classdtn_1_1BundleList.html#0d0c9f789f0f7f7d1cda3b4ecf32d2bc" title="Return the internal lock on this list.">lock</a>(), <span class="stringliteral">"log_inqueue_stats"</span>); <a name="l00118"></a>00118 BundleList::iterator bundle_iter; <a name="l00119"></a>00119 <span class="keywordflow">for</span> (bundle_iter = node-><a class="code" href="classdtn_1_1BundleDaemon.html#99030fd37a2c2b727b5e6c64962d6709" title="Accessor for the pending bundles list.">pending_bundles</a>()-><a class="code" href="classdtn_1_1BundleList.html#24cd371acd62f2f9725ff3f92b55badf" title="Iterator used to iterate through the list.">begin</a>(); <a name="l00120"></a>00120 bundle_iter != node-><a class="code" href="classdtn_1_1BundleDaemon.html#99030fd37a2c2b727b5e6c64962d6709" title="Accessor for the pending bundles list.">pending_bundles</a>()-><a class="code" href="classdtn_1_1BundleList.html#19f95941fe436d25ba49ae0b2d8bf2c1" title="Iterator used to mark the end of the list.">end</a>(); <a name="l00121"></a>00121 ++bundle_iter) <a name="l00122"></a>00122 { <a name="l00123"></a>00123 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle = *bundle_iter; <a name="l00124"></a>00124 SimLog::instance()->log_inqueue(node, bundle); <a name="l00125"></a>00125 } <a name="l00126"></a>00126 } <a name="l00127"></a>00127 } <a name="l00128"></a>00128 <a name="l00129"></a>00129 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00130"></a>00130 <span class="keywordtype">void</span> <a name="l00131"></a><a class="code" href="classdtnsim_1_1Simulator.html#02abc66ca653adfcc92d8aec68b183da">00131</a> <a class="code" href="classdtnsim_1_1Simulator.html#02abc66ca653adfcc92d8aec68b183da" title="Main run loop.">Simulator::run</a>() <a name="l00132"></a>00132 { <a name="l00133"></a>00133 oasys::Log* <a class="code" href="namespacedtn.html#68bf4270dd0a7e5e8390869b83889922">log</a> = oasys::Log::instance(); <a name="l00134"></a>00134 log->set_prefix(<span class="stringliteral">"--"</span>); <a name="l00135"></a>00135 <a name="l00136"></a>00136 log_debug(<span class="stringliteral">"Configuring all nodes"</span>); <a name="l00137"></a>00137 Topology::NodeTable::iterator iter; <a name="l00138"></a>00138 <span class="keywordflow">for</span> (iter = <a class="code" href="classdtnsim_1_1Topology.html#fa8695d124c438bc35f43ef9c4767bbd">Topology::node_table</a>()->begin(); <a name="l00139"></a>00139 iter != <a class="code" href="classdtnsim_1_1Topology.html#fa8695d124c438bc35f43ef9c4767bbd">Topology::node_table</a>()->end(); <a name="l00140"></a>00140 ++iter) <a name="l00141"></a>00141 { <a name="l00142"></a>00142 iter->second->configure(); <a name="l00143"></a>00143 } <a name="l00144"></a>00144 <a name="l00145"></a>00145 log_debug(<span class="stringliteral">"Setting up interrupt handler"</span>); <a name="l00146"></a>00146 signal(SIGINT, <a class="code" href="classdtnsim_1_1Simulator.html#079d73f7048d4371501d93e064bcf138">handle_interrupt</a>); <a name="l00147"></a>00147 <a name="l00148"></a>00148 log_debug(<span class="stringliteral">"Starting Simulator event loop..."</span>); <a name="l00149"></a>00149 <a name="l00150"></a>00150 <span class="keywordflow">while</span> (1) { <a name="l00151"></a>00151 <a class="code" href="classdtnsim_1_1Simulator.html#327cd35d3972508033443ffeaf9eb90d">check_interrupt</a>(); <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="keywordtype">int</span> next_timer_ms = <a class="code" href="classdtnsim_1_1Simulator.html#f52eb7a2e5c0ee7343fcbd50dc2faef7" title="Handle all bundle events at nodes returning the amount of time (in ms) until the...">run_node_events</a>(); <a name="l00154"></a>00154 <span class="keywordtype">double</span> next_timer = (next_timer_ms == -1) ? INT_MAX : <a name="l00155"></a>00155 <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a> + (((<span class="keywordtype">double</span>)next_timer_ms) / 1000); <a name="l00156"></a>00156 <span class="keywordtype">double</span> next_event = INT_MAX; <a name="l00157"></a>00157 log->set_prefix(<span class="stringliteral">"--"</span>); <a name="l00158"></a>00158 <a name="l00159"></a>00159 <a class="code" href="classdtnsim_1_1SimEvent.html">SimEvent</a>* e = NULL; <a name="l00160"></a>00160 <span class="keywordflow">if</span> (! <a class="code" href="classdtnsim_1_1Simulator.html#6aa1c59f30f934aa3201768eeab02b50">eventq_</a>.empty()) { <a name="l00161"></a>00161 e = <a class="code" href="classdtnsim_1_1Simulator.html#6aa1c59f30f934aa3201768eeab02b50">eventq_</a>.top(); <a name="l00162"></a>00162 next_event = e-><a class="code" href="classdtnsim_1_1SimEvent.html#7fac75dd67b59a8d40673700f1357dc8">time</a>(); <a name="l00163"></a>00163 } <a name="l00164"></a>00164 <a name="l00165"></a>00165 <span class="keywordflow">if</span> ((next_timer_ms == -1) && (e == NULL)) { <a name="l00166"></a>00166 <span class="keywordflow">break</span>; <a name="l00167"></a>00167 } <a name="l00168"></a>00168 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (next_timer < next_event) { <a name="l00169"></a>00169 <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a> = next_timer; <a name="l00170"></a>00170 log_debug(<span class="stringliteral">"advancing time by %u ms to %f for next timer"</span>, <a name="l00171"></a>00171 next_timer_ms, <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a>); <a name="l00172"></a>00172 } <a name="l00173"></a>00173 <span class="keywordflow">else</span> { <a name="l00174"></a>00174 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(e != NULL); <a name="l00175"></a>00175 <a class="code" href="classdtnsim_1_1Simulator.html#6aa1c59f30f934aa3201768eeab02b50">eventq_</a>.pop(); <a name="l00176"></a>00176 <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a> = e-><a class="code" href="classdtnsim_1_1SimEvent.html#7fac75dd67b59a8d40673700f1357dc8">time</a>(); <a name="l00177"></a>00177 <a name="l00178"></a>00178 <span class="keywordflow">if</span> (e-><a class="code" href="classdtnsim_1_1SimEvent.html#ac66a522af3b0b5f83fa9e5a2bf86755">is_valid</a>()) { <a name="l00179"></a>00179 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(e-><a class="code" href="classdtnsim_1_1SimEvent.html#3c499eef78bee8a1d3fbc5cf8df86ae4">handler</a>() != NULL); <a name="l00180"></a>00180 <span class="comment">/* Process the event */</span> <a name="l00181"></a>00181 log_debug(<span class="stringliteral">"Event:%p type %s at time %f"</span>, <a name="l00182"></a>00182 e, e-><a class="code" href="classdtnsim_1_1SimEvent.html#f4af98be7e81c4d574ac1c3f40304fcb">type_str</a>(), <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a>); <a name="l00183"></a>00183 e-><a class="code" href="classdtnsim_1_1SimEvent.html#3c499eef78bee8a1d3fbc5cf8df86ae4">handler</a>()-><a class="code" href="classdtnsim_1_1SimEventHandler.html#f29a84edd81f5cc40648bd5f85fd2618">process</a>(e); <a name="l00184"></a>00184 } <a name="l00185"></a>00185 } <a name="l00186"></a>00186 <a name="l00187"></a>00187 <span class="keywordflow">if</span> ((<a class="code" href="classdtnsim_1_1Simulator.html#c3e483526794d4ccd4a3e9e78bc95b2f" title="time to end the simulation">Simulator::runtill_</a> != -1) && <a name="l00188"></a>00188 (<a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a> > <a class="code" href="classdtnsim_1_1Simulator.html#c3e483526794d4ccd4a3e9e78bc95b2f" title="time to end the simulation">Simulator::runtill_</a>)) { <a name="l00189"></a>00189 log_info(<span class="stringliteral">"Exiting simulation. "</span> <a name="l00190"></a>00190 <span class="stringliteral">"Current time (%f) > Max time (%f)"</span>, <a name="l00191"></a>00191 <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a>, <a class="code" href="classdtnsim_1_1Simulator.html#c3e483526794d4ccd4a3e9e78bc95b2f" title="time to end the simulation">Simulator::runtill_</a>); <a name="l00192"></a>00192 <span class="keywordflow">goto</span> done; <a name="l00193"></a>00193 } <a name="l00194"></a>00194 } <a name="l00195"></a>00195 <a name="l00196"></a>00196 log_info(<span class="stringliteral">"Simulator loop done -- no pending events or timers (time is %f)"</span>, <a name="l00197"></a>00197 <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a>); <a name="l00198"></a>00198 <a name="l00199"></a>00199 <span class="keywordflow">if</span> (<a class="code" href="classdtnsim_1_1Simulator.html#8aaf01a69ac5f46847e396af564a0f45">exit_event_</a>) { <a name="l00200"></a>00200 <a class="code" href="classdtnsim_1_1Simulator.html#773551d739b93f1dc1ae063c623c7c29">run_at_event</a>(<a class="code" href="classdtnsim_1_1Simulator.html#8aaf01a69ac5f46847e396af564a0f45">exit_event_</a>); <a name="l00201"></a>00201 } <a name="l00202"></a>00202 <a name="l00203"></a>00203 done: <a name="l00204"></a>00204 <a class="code" href="classdtnsim_1_1Simulator.html#045c83976fbabbc3494142e02e0820b1">log_inqueue_stats</a>(); <a name="l00205"></a>00205 SimLog::instance()->flush(); <a name="l00206"></a>00206 } <a name="l00207"></a>00207 <a name="l00208"></a>00208 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00209"></a>00209 <span class="keywordtype">void</span> <a name="l00210"></a><a class="code" href="classdtnsim_1_1Simulator.html#e253c215d84b63068747ac94adb7b778">00210</a> <a class="code" href="classdtnsim_1_1Simulator.html#e253c215d84b63068747ac94adb7b778" title="Pause execution of the simulator, running a console loop until it exits.">Simulator::pause</a>() <a name="l00211"></a>00211 { <a name="l00212"></a>00212 oasys::StaticStringBuffer<128> cmd; <a name="l00213"></a>00213 cmd.appendf(<span class="stringliteral">"puts \"Simulator paused at time %f...\""</span>, <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a>); <a name="l00214"></a>00214 oasys::TclCommandInterp::instance()->exec_command(cmd.c_str()); <a name="l00215"></a>00215 <a name="l00216"></a>00216 <a class="code" href="classdtnsim_1_1Simulator.html#9a749bd8ace235e9e8cafb5a4d8fc9b1" title="Run the command loop.">run_console</a>(<span class="keyword">false</span>); <a name="l00217"></a>00217 } <a name="l00218"></a>00218 <a name="l00219"></a>00219 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00220"></a>00220 <span class="keywordtype">void</span> <a name="l00221"></a><a class="code" href="classdtnsim_1_1Simulator.html#9a749bd8ace235e9e8cafb5a4d8fc9b1">00221</a> <a class="code" href="classdtnsim_1_1Simulator.html#9a749bd8ace235e9e8cafb5a4d8fc9b1" title="Run the command loop.">Simulator::run_console</a>(<span class="keywordtype">bool</span> complete) <a name="l00222"></a>00222 { <a name="l00223"></a>00223 <a class="code" href="classdtnsim_1_1Node.html" title="Class representing a node in the simulator (i.e.">Node</a>* cur_active = <a class="code" href="classdtnsim_1_1Node.html#0092d41cdd29927ede0833a8164885e9" title="Return the current active node.">Node::active_node</a>(); <a name="l00224"></a>00224 <a class="code" href="classdtnsim_1_1Simulator.html#2066f289b67cff4cd17baf93a8a12902">interrupted_</a> = <span class="keyword">true</span>; <a name="l00225"></a>00225 <a name="l00226"></a>00226 <span class="keywordflow">if</span> (complete) { <a name="l00227"></a>00227 oasys::TclCommandInterp::instance()->command_loop(<span class="stringliteral">"dtnsim% "</span>); <a name="l00228"></a>00228 } <span class="keywordflow">else</span> { <a name="l00229"></a>00229 <span class="comment">// we can't re-enter tclreadline more than once so if it's not</span> <a name="l00230"></a>00230 <span class="comment">// complete we need to use the simple command loop</span> <a name="l00231"></a>00231 oasys::TclCommandInterp::instance()->exec_command( <a name="l00232"></a>00232 <span class="stringliteral">"simple_command_loop \"dtnsim% \""</span>); <a name="l00233"></a>00233 } <a name="l00234"></a>00234 <a name="l00235"></a>00235 <a class="code" href="classdtnsim_1_1Simulator.html#2066f289b67cff4cd17baf93a8a12902">interrupted_</a> = <span class="keyword">false</span>; <a name="l00236"></a>00236 cur_active-><a class="code" href="classdtnsim_1_1Node.html#5f8ad70a765bbb1b8d8e8d07ef502036" title="Set the node as the &quot;active&quot; node in the simulation.">set_active</a>(); <a name="l00237"></a>00237 } <a name="l00238"></a>00238 <a name="l00239"></a>00239 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00240"></a>00240 <span class="keywordtype">void</span> <a name="l00241"></a><a class="code" href="classdtnsim_1_1Simulator.html#079d73f7048d4371501d93e064bcf138">00241</a> <a class="code" href="classdtnsim_1_1Simulator.html#079d73f7048d4371501d93e064bcf138">Simulator::handle_interrupt</a>(<span class="keywordtype">int</span> sig) <a name="l00242"></a>00242 { <a name="l00243"></a>00243 (void)sig; <a name="l00244"></a>00244 <a name="l00245"></a>00245 <span class="keywordflow">if</span> (<a class="code" href="classdtnsim_1_1Simulator.html#2066f289b67cff4cd17baf93a8a12902">interrupted_</a>) { <a name="l00246"></a>00246 instance()->exit(); <a name="l00247"></a>00247 } <span class="keywordflow">else</span> { <a name="l00248"></a>00248 <a class="code" href="classdtnsim_1_1Simulator.html#2066f289b67cff4cd17baf93a8a12902">interrupted_</a> = <span class="keyword">true</span>; <a name="l00249"></a>00249 } <a name="l00250"></a>00250 } <a name="l00251"></a>00251 <a name="l00252"></a>00252 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00253"></a>00253 <span class="keywordtype">void</span> <a name="l00254"></a><a class="code" href="classdtnsim_1_1Simulator.html#327cd35d3972508033443ffeaf9eb90d">00254</a> <a class="code" href="classdtnsim_1_1Simulator.html#327cd35d3972508033443ffeaf9eb90d">Simulator::check_interrupt</a>() <a name="l00255"></a>00255 { <a name="l00256"></a>00256 <span class="keywordflow">if</span> (<a class="code" href="classdtnsim_1_1Simulator.html#2066f289b67cff4cd17baf93a8a12902">interrupted_</a>) { <a name="l00257"></a>00257 oasys::StaticStringBuffer<128> cmd; <a name="l00258"></a>00258 cmd.appendf(<span class="stringliteral">"puts \"Simulator interrupted at time %f...\""</span>, <a class="code" href="classdtnsim_1_1Simulator.html#726f668284f415ba7160cc8ebcc9a124" title="current time (static to avoid object)">time_</a>); <a name="l00259"></a>00259 oasys::TclCommandInterp::instance()->exec_command(cmd.c_str()); <a name="l00260"></a>00260 <a class="code" href="classdtnsim_1_1Simulator.html#9a749bd8ace235e9e8cafb5a4d8fc9b1" title="Run the command loop.">run_console</a>(<span class="keyword">false</span>); <a name="l00261"></a>00261 } <a name="l00262"></a>00262 } <a name="l00263"></a>00263 <a name="l00264"></a>00264 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00268"></a>00268 <span class="comment"></span><span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">int</span> <a name="l00269"></a><a class="code" href="namespacedtnsim.html#372b0bd474085f20b38efbbade7ce72a">00269</a> <a class="code" href="namespacedtnsim.html#372b0bd474085f20b38efbbade7ce72a" title="Override gettimeofday to return the simulator time.">gettimeofday</a>(<span class="keyword">struct</span> timeval *tv, <span class="keyword">struct</span> timezone *tz) <a name="l00270"></a>00270 { <a name="l00271"></a>00271 (void)tz; <a name="l00272"></a>00272 <span class="keywordtype">double</span> now = <a class="code" href="classdtnsim_1_1Simulator.html#e428d32758e9f94c855b43527d87bd84" title="Return the current simulator time.">Simulator::time</a>(); <a name="l00273"></a>00273 DOUBLE_TO_TIMEVAL(now, *tv); <a name="l00274"></a>00274 <a name="l00275"></a>00275 <span class="comment">// Sometimes converting a double like 100.2 into an timeval</span> <a name="l00276"></a>00276 <span class="comment">// results in a value like of 100.199999 so we fudge it a bit here</span> <a name="l00277"></a>00277 <span class="comment">// to make the output look prettier</span> <a name="l00278"></a>00278 <span class="keywordflow">if</span> ((tv->tv_usec % 1000) == 999) { <a name="l00279"></a>00279 tv->tv_usec += 1; <a name="l00280"></a>00280 } <a name="l00281"></a>00281 <span class="keywordflow">return</span> 0; <a name="l00282"></a>00282 } <a name="l00283"></a>00283 <a name="l00284"></a>00284 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00285"></a>00285 <span class="keywordtype">void</span> <a name="l00286"></a><a class="code" href="classdtnsim_1_1Simulator.html#5e2a212a321fd812422ad8d824308fb8">00286</a> <a class="code" href="classdtnsim_1_1Simulator.html#5e2a212a321fd812422ad8d824308fb8" title="Virtual from SimEventHandler.">Simulator::process</a>(<a class="code" href="classdtnsim_1_1SimEvent.html">SimEvent</a> *e) <a name="l00287"></a>00287 { <a name="l00288"></a>00288 <span class="keywordflow">switch</span> (e-><a class="code" href="classdtnsim_1_1SimEvent.html#bce459b06f5ab4673ed662e7d4aec1ad">type</a>()) { <a name="l00289"></a>00289 <span class="keywordflow">case</span> <a class="code" href="namespacedtnsim.html#61b536ebb250b12bfe00104b1f33ef4d918aad3b730a21decb92ddebfa0c8690" title="Generic event for delayed tcl commands.">SIM_AT_EVENT</a>: { <a name="l00290"></a>00290 <a class="code" href="classdtnsim_1_1Simulator.html#773551d739b93f1dc1ae063c623c7c29">run_at_event</a>((<a class="code" href="classdtnsim_1_1SimAtEvent.html">SimAtEvent</a>*)e); <a name="l00291"></a>00291 <span class="keywordflow">break</span>; <a name="l00292"></a>00292 } <a name="l00293"></a>00293 <span class="keywordflow">default</span>: <a name="l00294"></a>00294 NOTREACHED; <a name="l00295"></a>00295 } <a name="l00296"></a>00296 } <a name="l00297"></a>00297 <a name="l00298"></a>00298 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00299"></a>00299 <span class="keywordtype">void</span> <a name="l00300"></a><a class="code" href="classdtnsim_1_1Simulator.html#e37776c67ac6018b431382d0f02f5cad">00300</a> <a class="code" href="classdtnsim_1_1Simulator.html#e37776c67ac6018b431382d0f02f5cad" title="Register a command to run at exit.">Simulator::set_exit_event</a>(<a class="code" href="classdtnsim_1_1SimAtEvent.html">SimAtEvent</a>* evt) <a name="l00301"></a>00301 { <a name="l00302"></a>00302 ASSERTF(<a class="code" href="classdtnsim_1_1Simulator.html#8aaf01a69ac5f46847e396af564a0f45">exit_event_</a> == NULL, <span class="stringliteral">"cannot set multiple exit events"</span>); <a name="l00303"></a>00303 <a class="code" href="classdtnsim_1_1Simulator.html#8aaf01a69ac5f46847e396af564a0f45">exit_event_</a> = evt; <a name="l00304"></a>00304 } <a name="l00305"></a>00305 <a name="l00306"></a>00306 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00307"></a>00307 <span class="keywordtype">void</span> <a name="l00308"></a><a class="code" href="classdtnsim_1_1Simulator.html#773551d739b93f1dc1ae063c623c7c29">00308</a> <a class="code" href="classdtnsim_1_1Simulator.html#773551d739b93f1dc1ae063c623c7c29">Simulator::run_at_event</a>(<a class="code" href="classdtnsim_1_1SimAtEvent.html">SimAtEvent</a>* evt) <a name="l00309"></a>00309 { <a name="l00310"></a>00310 <span class="keywordtype">int</span> err = oasys::TclCommandInterp::instance()-> <a name="l00311"></a>00311 exec_command(evt-><a class="code" href="classdtnsim_1_1SimAtEvent.html#edbb12680a4509ce6e8b7b84c8612c76">objc_</a>, evt-><a class="code" href="classdtnsim_1_1SimAtEvent.html#fc655f7381c0fc2de8999f7de177a948">objv_</a>); <a name="l00312"></a>00312 <span class="keywordflow">if</span> (err != 0) { <a name="l00313"></a>00313 oasys::StringBuffer cmd; <a name="l00314"></a>00314 cmd.appendf(<span class="stringliteral">"puts \"ERROR in at command, pausing simulation\""</span>); <a name="l00315"></a>00315 oasys::TclCommandInterp::instance()->exec_command(cmd.c_str()); <a name="l00316"></a>00316 <a class="code" href="classdtnsim_1_1Simulator.html#e253c215d84b63068747ac94adb7b778" title="Pause execution of the simulator, running a console loop until it exits.">pause</a>(); <a name="l00317"></a>00317 } <a name="l00318"></a>00318 } <a name="l00319"></a>00319 <a name="l00320"></a>00320 } <span class="comment">// namespace dtnsim</span> </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Fri Jan 30 09:26:58 2009 for DTN Reference Implementation by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.8 </small></address> </body> </html>