Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "EtherHub.h"
00019 #include "EtherFrame_m.h"
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
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
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