"omnet++" TICTOC example Two

Source: Internet
Author: User

This example describes generating messages from a node and sending them randomly. The destination node is reached to show the message reached, ending the simulation. Ned is as follows

//distributed in the hope that it'll be useful,//But without any WARRANTY; without even the implied WARRANTY of//merchantability or FITNESS for A particular PURPOSE. See the//GNU Lesser general public License for more details.// //You should has received a copy of the GNU Lesser general public License PackageZhao;simple txc{Parameters:@display("I=block/routing");//Display string specifies the icon for the Txc2 module for the routerGates:inputinch[]; Output out[];//Declare the input gate and output Gate of the Txc module as Vector}network tictoc{Types:channel C extends Ned. delaychannel{delay= -MS;}//Defines a Ned,delaychannel type C with a delay time of 100mssubmodules:tic[6]:TXC;//Specify a simple module that makes up this model for 6 tic modulesconnections:tic[0]. out++-->c-->tic[1].inch++; tic[0].inch++<--c<--tic[1]. out++; tic[1]. out++-->c-->tic[2].inch++; tic[1].inch++<--c<--tic[2]. out++; tic[1]. out++-->c-->tic[4].inch++; tic[1].inch++<--c<--tic[4]. out++; tic[3]. out++-->c-->tic[4].inch++; tic[3].inch++<--c<--tic[4]. out++; tic[4]. out++-->c-->tic[5].inch++; tic[4].inch++<--c<--tic[5]. out++;}


CC files are as follows

#include <stdio.h>#include <string.h>#include <omnetpp.h>classTXC: Publiccsimplemodule{//define member functionsprotected:Virtual voidForwardmessage (Cmessage *msg);Virtual voidInitialize ();Virtual voidHandlemessage (Cmessage *msg);;D Efine_module (TXC);//Macro Register TXC modulevoidTxc::initialize () {//Initialization Parameters    if(GetIndex () = =0)    {//Start the process to dispatch the initial message as a self-message        Charmsgname[ -];sprintf(Msgname,"tic-%d", GetIndex ()); Cmessage *msg=NewCmessage (Msgname);//Generate message MsgnameScheduleat (0.0, msg);//The MSG event is dispatched at 0.0 hours}}voidTxc::handlemessage (Cmessage *msg) {//If the message reaches the module with index number 3, delete this message and the emulation stops, otherwise the module sends this message to the other module.     if(GetIndex () = =3) {ev<<"Message"<<msg<<"arrived!"<<endl;//Show message arrives}Else{forwardmessage (msg);//Send Message}}voidTxc::forwardmessage (Cmessage *msg) {//Find out the size of each module's output gate and send a message randomly through an output gate .    intN=gatesize ("Out");//Get the number of gate vectors    intK=intuniform (0, N-1);//Randomly produce an integer that matches the uniform distribution of "0,n-1"ev<<"Forwarding message"<<msg<<"on port out["<<k<<"]"<<endl; Send (MSG,"Out", k);//Out output message from the K gate}

"omnet++" TICTOC example Two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.