00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef _MSC_VER
00020 #pragma warning(disable:4786)
00021 #endif
00022
00023 #include <omnetpp.h>
00024 #include "EtherFrame_m.h"
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;
00043 long numMessages;
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
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
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