EtherAppSrv.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 <stdio.h>
00019 #include <string.h>
00020 #include "EtherAppSrv.h"
00021 #include "Ieee802Ctrl_m.h"
00022 #include "EtherApp_m.h"
00023 
00024 Define_Module (EtherAppSrv);
00025 
00026 void EtherAppSrv::initialize()
00027 {
00028     localSAP = ETHERAPP_SRV_SAP;
00029     remoteSAP = ETHERAPP_CLI_SAP;
00030 
00031     // statistics
00032     packetsSent = packetsReceived = 0;
00033     eedVector.setName("end-to-end delay");
00034     eedStats.setName("end-to-end delay");
00035     WATCH(packetsSent);
00036     WATCH(packetsReceived);
00037 
00038     bool registerSAP = par("registerSAP");
00039     if (registerSAP)
00040         registerDSAP(localSAP);
00041 }
00042 
00043 void EtherAppSrv::handleMessage(cMessage *msg)
00044 {
00045     EV << "Received packet `" << msg->getName() << "'\n";
00046 
00047     packetsReceived++;
00048     simtime_t lastEED = simTime() - msg->getCreationTime();
00049     eedVector.record(lastEED);
00050     eedStats.collect(lastEED);
00051 
00052     EtherAppReq *req = check_and_cast<EtherAppReq *>(msg);
00053     Ieee802Ctrl *ctrl = check_and_cast<Ieee802Ctrl *>(req->removeControlInfo());
00054     MACAddress srcAddr = ctrl->getSrc();
00055     long requestId = req->getRequestId();
00056     long replyBytes = req->getResponseBytes();
00057     char msgname[30];
00058     strcpy(msgname,msg->getName());
00059 
00060     delete msg;
00061     delete ctrl;
00062 
00063     // send back packets asked by EtherAppCli side
00064     int k = 0;
00065     strcat(msgname,"-resp-");
00066     char *s = msgname+strlen(msgname);
00067     while (replyBytes>0)
00068     {
00069         int l = replyBytes>MAX_REPLY_CHUNK_SIZE ? MAX_REPLY_CHUNK_SIZE : replyBytes;
00070         replyBytes -= l;
00071 
00072         sprintf(s,"%d",k);
00073 
00074         EV << "Generating packet `" << msgname << "'\n";
00075 
00076         EtherAppResp *datapacket = new EtherAppResp(msgname, IEEE802CTRL_DATA);
00077         datapacket->setRequestId(requestId);
00078         datapacket->setByteLength(l);
00079         sendPacket(datapacket, srcAddr);
00080         packetsSent++;
00081 
00082         k++;
00083     }
00084 
00085 }
00086 
00087 void EtherAppSrv::sendPacket(cMessage *datapacket, const MACAddress& destAddr)
00088 {
00089     Ieee802Ctrl *etherctrl = new Ieee802Ctrl();
00090     etherctrl->setSsap(localSAP);
00091     etherctrl->setDsap(remoteSAP);
00092     etherctrl->setDest(destAddr);
00093     datapacket->setControlInfo(etherctrl);
00094     send(datapacket, "out");
00095 }
00096 
00097 void EtherAppSrv::registerDSAP(int dsap)
00098 {
00099     EV << getFullPath() << " registering DSAP " << dsap << "\n";
00100 
00101     Ieee802Ctrl *etherctrl = new Ieee802Ctrl();
00102     etherctrl->setDsap(dsap);
00103     cMessage *msg = new cMessage("register_DSAP", IEEE802CTRL_REGISTER_DSAP);
00104     msg->setControlInfo(etherctrl);
00105 
00106     send(msg, "out");
00107 }
00108 
00109 void EtherAppSrv::finish()
00110 {
00111     recordScalar("packets sent", packetsSent);
00112     recordScalar("packets rcvd", packetsReceived);
00113     recordScalar("end-to-end delay mean", eedStats.getMean());
00114     recordScalar("end-to-end delay stddev", eedStats.getStddev());
00115     recordScalar("end-to-end delay min", eedStats.getMin());
00116     recordScalar("end-to-end delay max", eedStats.getMax());
00117 }
00118 
00119