00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <math.h>
00022 #include <omnetpp.h>
00023
00024 #include "EtherCtrl_m.h"
00025 #include "EtherApp_m.h"
00026 #include "utils.h"
00027
00028
00029
00033 class EtherAppCli : public cSimpleModule
00034 {
00035
00036 long seqNum;
00037 cPar *reqLength;
00038 cPar *respLength;
00039 cPar *waitTime;
00040
00041 int localSAP;
00042 int remoteSAP;
00043 MACAddress destMACAddress;
00044
00045
00046 long packetsSent;
00047 long packetsReceived;
00048 cOutVector eedVector;
00049 cStdDev eedStats;
00050
00051 public:
00052 Module_Class_Members(EtherAppCli,cSimpleModule,0);
00053
00054 virtual void initialize(int stage);
00055 virtual int numInitStages() const {return 2;}
00056 virtual void handleMessage(cMessage *msg);
00057 virtual void finish();
00058
00059 MACAddress resolveDestMACAddress();
00060
00061 void sendPacket();
00062 void receivePacket(cMessage *msg);
00063 void registerDSAP(int dsap);
00064 };
00065
00066 Define_Module (EtherAppCli);
00067
00068 void EtherAppCli::initialize(int stage)
00069 {
00070
00071
00072 if (stage!=1) return;
00073
00074 reqLength = &par("reqLength");
00075 respLength = &par("respLength");
00076 waitTime = &par("waitTime");
00077
00078 localSAP = ETHERAPP_CLI_SAP;
00079 remoteSAP = ETHERAPP_SRV_SAP;
00080
00081 seqNum = 0;
00082 WATCH(seqNum);
00083
00084
00085 packetsSent = packetsReceived = 0;
00086 eedVector.setName("end-to-end delay");
00087 eedStats.setName("end-to-end delay");
00088 WATCH(packetsSent);
00089 WATCH(packetsReceived);
00090
00091 destMACAddress = resolveDestMACAddress();
00092
00093
00094 if (destMACAddress.isEmpty())
00095 return;
00096
00097 registerDSAP(localSAP);
00098
00099 cMessage *timermsg = new cMessage("generateNextPacket");
00100 double d = waitTime->doubleValue();
00101 scheduleAt(simTime()+d, timermsg);
00102
00103 }
00104
00105
00106 MACAddress EtherAppCli::resolveDestMACAddress()
00107 {
00108 MACAddress destMACAddress;
00109 const char *destAddr = par("destAddress");
00110 const char *destStation = par("destStation");
00111 if (strcmp(destAddr,"") && strcmp(destStation,""))
00112 {
00113 error("only one of the `destAddress' and `destStation' module parameters should be filled in");
00114 }
00115 else if (strcmp(destAddr,""))
00116 {
00117 destMACAddress.setAddress(destAddr);
00118 }
00119 else if (strcmp(destStation,""))
00120 {
00121 std::string destModName = std::string(destStation) + ".mac";
00122 cModule *destMod = simulation.moduleByPath(destModName.c_str());
00123 if (!destMod)
00124 error("module `%s' (MAC submodule of `destStation') not found", destModName.c_str());
00125 destMACAddress.setAddress(destMod->par("address"));
00126 }
00127 return destMACAddress;
00128 }
00129
00130 void EtherAppCli::handleMessage(cMessage *msg)
00131 {
00132 if (msg->isSelfMessage())
00133 {
00134 sendPacket();
00135 double d = waitTime->doubleValue();
00136 scheduleAt(simTime()+d, msg);
00137 }
00138 else
00139 {
00140 receivePacket(msg);
00141 }
00142 }
00143
00144 void EtherAppCli::registerDSAP(int dsap)
00145 {
00146 EV << fullPath() << " registering DSAP " << dsap << "\n";
00147
00148 EtherCtrl *etherctrl = new EtherCtrl();
00149 etherctrl->setDsap(dsap);
00150 cMessage30 *msg = new cMessage30("register_DSAP", ETHCTRL_REGISTER_DSAP);
00151 msg->setControlInfo(etherctrl);
00152
00153 send(msg, "out");
00154 }
00155
00156 void EtherAppCli::sendPacket()
00157 {
00158 seqNum++;
00159
00160 char msgname[30];
00161 sprintf(msgname, "req-%d-%ld", id(), seqNum);
00162 EV << "Generating packet `" << msgname << "'\n";
00163
00164 EtherAppReq *datapacket = new EtherAppReq(msgname, ETHCTRL_DATA);
00165
00166 datapacket->setRequestId(seqNum);
00167
00168 long len = 8*reqLength->longValue();
00169 datapacket->setLength(len);
00170
00171 long respLen = respLength->longValue();
00172 datapacket->setResponseBytes(respLen);
00173
00174 EtherCtrl *etherctrl = new EtherCtrl();
00175 etherctrl->setSsap(localSAP);
00176 etherctrl->setDsap(remoteSAP);
00177 etherctrl->setDest(destMACAddress);
00178 datapacket->setControlInfo(etherctrl);
00179
00180 send(datapacket, "out");
00181 packetsSent++;
00182 }
00183
00184 void EtherAppCli::receivePacket(cMessage *msg)
00185 {
00186 EV << "Received packet `" << msg->name() << "'\n";
00187
00188 packetsReceived++;
00189 simtime_t lastEED = simTime() - msg->creationTime();
00190 eedVector.record(lastEED);
00191 eedStats.collect(lastEED);
00192
00193 delete msg;
00194 }
00195
00196 void EtherAppCli::finish()
00197 {
00198 if (par("writeScalars").boolValue())
00199 {
00200 recordScalar("packets sent", packetsSent);
00201 recordScalar("packets rcvd", packetsReceived);
00202 recordScalar("end-to-end delay mean", eedStats.mean());
00203 recordScalar("end-to-end delay stddev", eedStats.stddev());
00204 recordScalar("end-to-end delay min", eedStats.min());
00205 recordScalar("end-to-end delay max", eedStats.max());
00206 }
00207 }
00208