Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

EtherHub.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003 CTIE, Monash University
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #ifdef _MSC_VER
00020 #pragma warning(disable:4786)
00021 #endif
00022 
00023 #include <omnetpp.h>
00024 #include "EtherFrame_m.h"  // for EtherAutoconfig only
00025 #include "utils.h"
00026 
00027 
00028 
00033 class EtherHub : public cSimpleModule
00034 {
00035     Module_Class_Members(EtherHub,cSimpleModule,0);
00036 
00037     virtual void initialize();
00038     virtual void handleMessage(cMessage*);
00039     virtual void finish();
00040 
00041   private:
00042     int ports;          // number of ports
00043     long numMessages;   // number of messages handled
00044 
00045 };
00046 
00047 Define_Module(EtherHub);
00048 
00049 static cEnvir& operator<< (cEnvir& ev, cMessage *msg)
00050 {
00051     ev.printf("(%s)%s",msg->className(),msg->fullName());
00052     return ev;
00053 }
00054 
00055 void EtherHub::initialize()
00056 {
00057     numMessages = 0;
00058     WATCH(numMessages);
00059 
00060     ports = gate("in",0)->size();
00061     if (gate("out",0)->size()!=ports)
00062         error("the sizes of the in[] and out[] gate vectors must be the same");
00063 
00064 
00065     // autoconfig: tell everyone that full duplex is not possible over shared media
00066     EV << "Autoconfig: advertising that we only support half-duplex operation\n";
00067     for (int i=0; i<ports; i++)
00068     {
00069         EtherAutoconfig *autoconf = new EtherAutoconfig("autoconf-halfduplex");
00070         autoconf->setHalfDuplex(true);
00071         send(autoconf,"out",i);
00072     }
00073 }
00074 
00075 void EtherHub::handleMessage(cMessage *msg)
00076 {
00077     // Handle frame sent down from the network entity: send out on every other port
00078     int arrivalPort = msg->arrivalGate()->index();
00079     EV << "Frame " << msg << " arrived on port " << arrivalPort << ", broadcasting on all other ports\n";
00080 
00081     numMessages++;
00082 
00083     if (ports<=1)
00084     {
00085         delete msg;
00086         return;
00087     }
00088     for (int i=0; i<ports; i++)
00089     {
00090         if (i!=arrivalPort)
00091         {
00092             bool isLast = (arrivalPort==ports-1) ? (i==ports-2) : (i==ports-1);
00093             cMessage *msg2 = isLast ? msg : (cMessage*) msg->dup();
00094             send(msg2,"out",i);
00095         }
00096     }
00097 }
00098 
00099 void EtherHub::finish ()
00100 {
00101     if (par("writeScalars").boolValue())
00102     {
00103         double t = simTime();
00104         recordScalar("simulated time", t);
00105         recordScalar("messages handled", numMessages);
00106         if (t>0)
00107             recordScalar("messages/sec", numMessages/t);
00108     }
00109 }
00110 

Generated on Sat May 15 20:30:42 2004 for Ethernet by doxygen1.2.17