<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="description" content="OMNeT++ 4 中文版Tictoc指南,来自奥普网络http://www.oopsweb.net"/>
<meta name="keywords" content="OMNeT++ 4,OMNeT++,Tictoc,网络模拟,无线传感器网络,通信网络"/>
<title>Tictoc Tutorial: txc9.cc</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.5 -->
<div class="navigation" id="top">
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="dirs.html"><span>Directories</span></a></li>
</ul>
</div>
</div>
<div class="contents">
<h1><a class="anchor" name="txc9.cc"><a class="el" href="txc9_8cc.html">txc9.cc</a> </a></h1><div class="fragment"><pre class="fragment"><span class="comment">//</span>
<span class="comment">// This file is part of an OMNeT++/OMNEST simulation example.</span>
<span class="comment">//</span>
<span class="comment">// Copyright (C) 2003-2008 Andras Varga</span>
<span class="comment">//</span>
<span class="comment">// This file is distributed WITHOUT ANY WARRANTY. See the file</span>
<span class="comment">// `license' for details on this and other legal matters.</span>
<span class="comment">//</span>
<span class="preprocessor">#include <stdio.h></span>
<span class="preprocessor">#include <string.h></span>
<span class="preprocessor">#include <omnetpp.h></span>
<span class="keyword">class </span><a class="code" href="classTic9.html">Tic9</a> : <span class="keyword">public</span> <a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcSimpleModule.html">cSimpleModule</a>
{
<span class="keyword">private</span>:
simtime_t timeout; <span class="comment">// timeout</span>
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcMessage.html">cMessage</a> *timeoutEvent; <span class="comment">// holds pointer to the timeout self-message</span>
<span class="keywordtype">int</span> seq; <span class="comment">// message sequence number</span>
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcMessage.html">cMessage</a> *message; <span class="comment">// message that has to be re-sent on timeout</span>
<span class="keyword">public</span>:
<a class="code" href="classTic9.html">Tic9</a>();
<span class="keyword">virtual</span> ~<a class="code" href="classTic9.html">Tic9</a>();
<span class="keyword">protected</span>:
<span class="keyword">virtual</span> <a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcMessage.html">cMessage</a> *generateNewMessage();
<span class="keyword">virtual</span> <span class="keywordtype">void</span> sendCopyOf(<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcMessage.html">cMessage</a> *msg);
<span class="keyword">virtual</span> <span class="keywordtype">void</span> initialize();
<span class="keyword">virtual</span> <span class="keywordtype">void</span> handleMessage(<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcMessage.html">cMessage</a> *msg);
};
<a class="code" href="txc1_8cc.html#5ac407c43df69e3adc074c5c56d511ff">Define_Module</a>(<a class="code" href="classTic9.html">Tic9</a>);
<a class="code" href="classTic9.html#053c33016a59b3c3af383ad1d22496b9">Tic9::Tic9</a>()
{
<a class="code" href="classTic9.html#694851df464e6712932962fa6afe8e8d">timeoutEvent</a> = <a class="code" href="classTic9.html#559f8331047cee02dbff9e03b2a83eb3">message</a> = NULL;
}
<a class="code" href="classTic9.html#bb09707e14c2c78ede4373338a1d458e">Tic9::~Tic9</a>()
{
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcSimpleModule.html#59d46fbc9a77cb3e8164b0f624a51699">cancelAndDelete</a>(<a class="code" href="classTic9.html#694851df464e6712932962fa6afe8e8d">timeoutEvent</a>);
<span class="keyword">delete</span> <a class="code" href="classTic9.html#559f8331047cee02dbff9e03b2a83eb3">message</a>;
}
<span class="keywordtype">void</span> <a class="code" href="classTic9.html#3b100ef7831fd5ec050019d1bcb8804f">Tic9::initialize</a>()
{
<span class="comment">// Initialize variables.</span>
<a class="code" href="classTic9.html#8cfab4487172256992228c7e5c58eadc">seq</a> = 0;
<a class="code" href="classTic9.html#af910949436727dd40604c4d2d0e0851">timeout</a> = 1.0;
<a class="code" href="classTic9.html#694851df464e6712932962fa6afe8e8d">timeoutEvent</a> = <span class="keyword">new</span> <a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcOwnedObject.html#f46e516dfdf6a913fe913c4c2e496cf2">cMessage</a>(<span class="stringliteral">"timeoutEvent"</span>);
<span class="comment">// Generate and send initial message.</span>
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//group__Envir.html#g650ef3eff8a2900bef69dae29c05d2dd">EV</a> << <span class="stringliteral">"Sending initial message\n"</span>;
<a class="code" href="classTic9.html#559f8331047cee02dbff9e03b2a83eb3">message</a> = <a class="code" href="classTic9.html#729ebf1c6017dbe15c1b277ff32d9e9f">generateNewMessage</a>();
<a class="code" href="classTic9.html#3f5dff4f46521d7f01a1fea3194dce8a">sendCopyOf</a>(<a class="code" href="classTic9.html#559f8331047cee02dbff9e03b2a83eb3">message</a>);
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcSimpleModule.html#29a64f1f0c560ac75494940d5c7a048d">scheduleAt</a>(simTime()+<a class="code" href="classTic9.html#af910949436727dd40604c4d2d0e0851">timeout</a>, <a class="code" href="classTic9.html#694851df464e6712932962fa6afe8e8d">timeoutEvent</a>);
}
<span class="keywordtype">void</span> <a class="code" href="classTic9.html#8a20492db273120e0e2c7239b5445d15">Tic9::handleMessage</a>(<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcMessage.html">cMessage</a> *msg)
{
<span class="keywordflow">if</span> (msg==<a class="code" href="classTic9.html#694851df464e6712932962fa6afe8e8d">timeoutEvent</a>)
{
<span class="comment">// If we receive the timeout event, that means the packet hasn't</span>
<span class="comment">// arrived in time and we have to re-send it.</span>
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//group__Envir.html#g650ef3eff8a2900bef69dae29c05d2dd">EV</a> << <span class="stringliteral">"Timeout expired, resending message and restarting timer\n"</span>;
<a class="code" href="classTic9.html#3f5dff4f46521d7f01a1fea3194dce8a">sendCopyOf</a>(<a class="code" href="classTic9.html#559f8331047cee02dbff9e03b2a83eb3">message</a>);
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcSimpleModule.html#29a64f1f0c560ac75494940d5c7a048d">scheduleAt</a>(simTime()+<a class="code" href="classTic9.html#af910949436727dd40604c4d2d0e0851">timeout</a>, <a class="code" href="classTic9.html#694851df464e6712932962fa6afe8e8d">timeoutEvent</a>);
}
<span class="keywordflow">else</span> <span class="comment">// message arrived</span>
{
<span class="comment">// Acknowledgement received!</span>
<a class="codeRef" doxygen="opptags.xml:../api//" href="../api//group__Envir.html#g650ef3eff8a2900bef69dae29c05d2dd">EV</a> << <span class="stringliteral">"Received: "</span> << msg-><a class="codeRef" doxygen="opptags.xml:../api//" href="../api//classcNamedObject.html#5b06a5fdb473bb997c939ba55435c041">getName</a>() << <spa