EtherHub.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003 Andras Varga; CTIE, Monash University, Australia
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00018 #include "EtherHub.h"
00019 #include "EtherFrame_m.h"  // for EtherAutoconfig only
00020 
00021 
00022 Define_Module(EtherHub);
00023 
00024 
00025 static cEnvir& operator<< (cEnvir& out, cMessage *msg)
00026 {
00027     out.printf("(%s)%s",msg->getClassName(),msg->getFullName());
00028     return out;
00029 }
00030 
00031 void EtherHub::initialize()
00032 {
00033     numMessages = 0;
00034     WATCH(numMessages);
00035 
00036     ports = gateSize("ethg");
00037 
00038     // autoconfig: tell everyone that full duplex is not possible over shared media
00039     EV << "Autoconfig: advertising that we only support half-duplex operation\n";
00040     for (int i=0; i<ports; i++)
00041     {
00042         EtherAutoconfig *autoconf = new EtherAutoconfig("autoconf-halfduplex");
00043         autoconf->setHalfDuplex(true);
00044         send(autoconf,"ethg$o",i);
00045     }
00046 }
00047 
00048 void EtherHub::handleMessage(cMessage *msg)
00049 {
00050     // Handle frame sent down from the network entity: send out on every other port
00051     int arrivalPort = msg->getArrivalGate()->getIndex();
00052     EV << "Frame " << msg << " arrived on port " << arrivalPort << ", broadcasting on all other ports\n";
00053 
00054     numMessages++;
00055 
00056     if (ports<=1)
00057     {
00058         delete msg;
00059         return;
00060     }
00061     for (int i=0; i<ports; i++)
00062     {
00063         if (i!=arrivalPort)
00064         {
00065             bool isLast = (arrivalPort==ports-1) ? (i==ports-2) : (i==ports-1);
00066             cMessage *msg2 = isLast ? msg : (cMessage*) msg->dup();
00067             send(msg2,"ethg$o",i);
00068         }
00069     }
00070 }
00071 
00072 void EtherHub::finish ()
00073 {
00074     simtime_t t = simTime();
00075     recordScalar("simulated time", t);
00076     recordScalar("messages handled", numMessages);
00077     if (t>0)
00078         recordScalar("messages/sec", numMessages/t);
00079 }
00080