User Manual, Developers Guide and API Documentation

Email.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <APPLICATIONS/session/client/Email.hpp>
00029 
00030 using namespace applications::session::client;
00031 
00032 STATIC_FACTORY_REGISTER_WITH_CREATOR(applications::session::client::Email,
00033                      applications::session::Session,
00034                      "client.Email", wns::PyConfigViewCreator);
00035 
00036 Email::Email(const wns::pyconfig::View& _pyco) :
00037   Session(_pyco),
00038   numberOfEmailsDistribution(NULL),
00039   emailSizeChoiceDistribution(NULL),
00040   largeEmailSizeDistribution(NULL),
00041   smallEmailSizeDistribution(NULL),
00042   emailWritingTimeDistribution(NULL),
00043   emailReadingTimeDistribution(NULL),
00044   lastEmailSent(false)
00045 {
00046   wns::pyconfig::View nOEConfig(_pyco, "numberOfEmails");
00047   std::string nOEName = nOEConfig.get<std::string>("__plugin__");
00048   numberOfEmailsDistribution = wns::distribution::DistributionFactory::creator(nOEName)->create(nOEConfig);
00049 
00050   wns::pyconfig::View eSCConfig(_pyco, "emailSizeChoice");
00051   std::string eSCName = eSCConfig.get<std::string>("__plugin__");
00052   emailSizeChoiceDistribution = wns::distribution::DistributionFactory::creator(eSCName)->create(eSCConfig);
00053 
00054   wns::pyconfig::View lESConfig(_pyco, "largeEmailSize");
00055   std::string lESName = lESConfig.get<std::string>("__plugin__");
00056   largeEmailSizeDistribution = wns::distribution::DistributionFactory::creator(lESName)->create(lESConfig);
00057 
00058   wns::pyconfig::View sESConfig(_pyco, "smallEmailSize");
00059   std::string sESName = sESConfig.get<std::string>("__plugin__");
00060   smallEmailSizeDistribution = wns::distribution::DistributionFactory::creator(sESName)->create(sESConfig);
00061 
00062   wns::pyconfig::View eWTConfig(_pyco, "emailWritingTime");
00063   std::string eWTName = eWTConfig.get<std::string>("__plugin__");
00064   emailWritingTimeDistribution = wns::distribution::DistributionFactory::creator(eWTName)->create(eWTConfig);
00065 
00066   wns::pyconfig::View eRTConfig(_pyco, "emailReadingTime");
00067   std::string eRTName = eRTConfig.get<std::string>("__plugin__");
00068   emailReadingTimeDistribution = wns::distribution::DistributionFactory::creator(eRTName)->create(eRTConfig);
00069 
00070   numberOfEmails = (*numberOfEmailsDistribution)();
00071 
00072   settlingTime = _pyco.get<wns::simulator::Time>("settlingTime");
00073 
00074   MESSAGE_BEGIN(NORMAL, logger, m, "APPL: Number of emails client wants to send is ");
00075   m << numberOfEmails << ".";
00076   MESSAGE_END();
00077 
00078   emailState1 = waitforpasswordok; //requestpop3ok; if a session has to be started with POP3!
00079   emailState2 = readlastemail;
00080 
00081   sessionDelay = (*sessionDelayDistribution)();
00082   MESSAGE_SINGLE(NORMAL, logger, "APPL: Delay before session starts: " << sessionDelay << ".\n");
00083   setTimeout(connectiontimeout, sessionDelay);
00084   setTimeout(probetimeout, windowSize);
00085 
00086   state = running;
00087 
00088   /* only for probing */
00089   sessionType = email;
00090 }
00091 
00092 Email::~Email()
00093 {
00094   if(numberOfEmailsDistribution != NULL)
00095     delete numberOfEmailsDistribution;
00096   numberOfEmailsDistribution = NULL;
00097 
00098   if(emailSizeChoiceDistribution != NULL)
00099     delete emailSizeChoiceDistribution;
00100   emailSizeChoiceDistribution = NULL;
00101 
00102   if(smallEmailSizeDistribution !=NULL)
00103     delete smallEmailSizeDistribution;
00104   smallEmailSizeDistribution = NULL;
00105 
00106   if(largeEmailSizeDistribution != NULL)
00107     delete largeEmailSizeDistribution;
00108   largeEmailSizeDistribution = NULL;
00109 
00110   if(emailWritingTimeDistribution != NULL)
00111     delete emailWritingTimeDistribution;
00112   emailWritingTimeDistribution = NULL;
00113 
00114   if(emailReadingTimeDistribution != NULL)
00115     delete emailReadingTimeDistribution;
00116   emailReadingTimeDistribution = NULL;
00117 }
00118 
00119 void
00120 Email::onData(const wns::osi::PDUPtr& _pdu)
00121 {
00122   assureType(_pdu.getPtr(), applications::session::PDU*);
00123 
00124   receivedPacketNumber = static_cast<applications::session::PDU*>(_pdu.getPtr())->getPacketNumber();
00125 
00126   MESSAGE_SINGLE(NORMAL, logger, "APPL: receivedPacketNumber = " << receivedPacketNumber << ".");
00127 
00128   applications::session::Session::incomingProbesCalculation(_pdu);
00129 
00130   lastPacket = static_cast<applications::session::PDU*>(_pdu.getPtr())->getLastPacket();
00131 
00132   if((lastPacket == true) && (state != sessionended))
00133     {
00134       state = running;
00135 
00136       /* Received all emails. Start with sending new emails after read last one. */
00137       onTimeout(sendtimeout);
00138     }
00139   else if(lastPacket == false && state != sessionended)
00140     {
00141       state = running;
00142 
00143       onTimeout(statetimeout);
00144     }
00145 }
00146 
00147 void
00148 Email::onTimeout(const Timeout& _t)
00149 {
00150   if(_t == statetimeout)
00151     {
00152       if(emailState1 == receiving)
00153     {
00154       /* Received email and now reading it.*/
00155       wns::simulator::Time readingTime = (*emailReadingTimeDistribution)();
00156 
00157       MESSAGE_SINGLE(NORMAL, logger, "APPL: Received email. Readingtime = " << readingTime << ".");
00158 
00159       if(lastPacket == false)
00160         {
00161           setTimeout(statetimeout, readingTime);
00162 
00163           emailState1 = reading;
00164           state = running;
00165         }
00166     }
00167       else if(emailState1 == reading)
00168     {
00169       /* Sending request for next email.*/
00170       packetSize = 176;
00171       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00172       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00173       applicationPDU->setRequest(true);
00174 
00175       ++packetNumber;
00176       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00177       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00178 
00179       wns::osi::PDUPtr pdu(applicationPDU);
00180       applications::session::Session::outgoingProbesCalculation(pdu);
00181 
00182       connection->sendData(pdu);
00183 
00184       emailState1 = receiving;
00185       state = idle;
00186     }
00187       else if(emailState1 == requestpop3ok)
00188     {
00189       /* Start with sending POP3 request.*/
00190       MESSAGE_SINGLE(NORMAL, logger, "APPL: Start sending POP3 request!");
00191 
00192       packetSize = 2800;
00193       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00194       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00195       applicationPDU->setRequest(true);
00196 
00197       ++packetNumber;
00198       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00199       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00200 
00201       wns::osi::PDUPtr pdu(applicationPDU);
00202       applications::session::Session::outgoingProbesCalculation(pdu);
00203 
00204       connection->sendData(pdu);
00205 
00206       emailState1 = waitforpop3ok;
00207       state = idle;
00208     }
00209       else if(emailState1 == waitforpop3ok)
00210     {
00211       MESSAGE_SINGLE(NORMAL, logger, "APPL: Received POP3 OK! Now requesting username OK.");
00212 
00213       /* Received POP3 OK. Sending request for username OK.*/
00214       packetSize = 232;
00215       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00216       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00217       applicationPDU->setRequest(true);
00218 
00219       ++packetNumber;
00220       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00221       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00222 
00223       wns::osi::PDUPtr pdu(applicationPDU);
00224       applications::session::Session::outgoingProbesCalculation(pdu);
00225 
00226       connection->sendData(pdu);
00227 
00228       emailState1 = waitforusernameok;
00229       state = idle;
00230     }
00231       else if(emailState1 == waitforusernameok)
00232     {
00233       MESSAGE_SINGLE(NORMAL, logger, "APPL: Received username OK! Now requesting Password OK.");
00234 
00235       /* Received username OK. Sending request for password OK.*/
00236       packetSize = 240;
00237       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00238       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00239       applicationPDU->setRequest(true);
00240 
00241       ++packetNumber;
00242       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00243       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00244 
00245       wns::osi::PDUPtr pdu(applicationPDU);
00246       applications::session::Session::outgoingProbesCalculation(pdu);
00247 
00248       connection->sendData(pdu);
00249 
00250       emailState1 = waitforpasswordok;
00251       state = idle;
00252     }
00253       else if(emailState1 == waitforpasswordok)
00254     {
00255       MESSAGE_SINGLE(NORMAL, logger,"APPL: Requesting first Email.");
00256 
00257       /* Received passwordok OK. Sending request for the first email.*/
00258       packetSize = 176;
00259       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00260       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00261       applicationPDU->setRequest(true);
00262 
00263       ++packetNumber;
00264       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00265       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00266 
00267       wns::osi::PDUPtr pdu(applicationPDU);
00268       applications::session::Session::outgoingProbesCalculation(pdu);
00269 
00270       connection->sendData(pdu);
00271 
00272       emailState1 = receiving;
00273       state = idle;
00274     }
00275     }
00276   else if(_t == sendtimeout)
00277     {
00278       if(emailState2 == waitforsmtpok || emailState2 == sending)
00279     {
00280       /* Starting to write email. */
00281       wns::simulator::Time writingTime = (*emailWritingTimeDistribution)();
00282 
00283       MESSAGE_SINGLE(NORMAL, logger, "APPL: Writing email. Writingtime = " << writingTime << ".");
00284 
00285       setTimeout(sendtimeout, writingTime);
00286 
00287       emailState2 = writing;
00288       state = running;
00289     }
00290       else if(emailState2 == writing)
00291     {
00292       /* Wrote email and now send it. */
00293       double emailSizeChoice = (*emailSizeChoiceDistribution)();
00294       if(emailSizeChoice <= 0.8)
00295         {
00296           double distributedValue = (*smallEmailSizeDistribution)();
00297           /* The distributed value is in Kbyte, so it has to be converted to bit! */
00298           packetSize = distributedValue * 8.0 * 1024.0;
00299         }
00300       else
00301         {
00302           double distributedValue = (*largeEmailSizeDistribution)();
00303           /* The distributed value is in Kbyte, so it has to be converted to bit! */
00304           packetSize = distributedValue * 8.0 * 1024.0;
00305         }
00306 
00307       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00308       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00309 
00310       ++packetNumber;
00311       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00312       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00313 
00314       if(numberOfEmails == 1)
00315         {
00316           /* sending last packet */
00317           applicationPDU->setLastPacket(true);
00318           wns::osi::PDUPtr pdu(applicationPDU);
00319           applications::session::Session::outgoingProbesCalculation(pdu);
00320 
00321           connection->sendData(pdu);
00322 
00323           lastEmailSent = true;
00324 
00325           /* Wait till session ends. */
00326           if(lastPacket == true)
00327         {
00328           MESSAGE_SINGLE(NORMAL, logger, "APPL: All emails were sent and received. Now wait till session ends!");
00329         }
00330         }
00331       else
00332         {
00333           numberOfEmails = numberOfEmails - 1;
00334 
00335           wns::osi::PDUPtr pdu(applicationPDU);
00336           applications::session::Session::outgoingProbesCalculation(pdu);
00337 
00338           connection->sendData(pdu);
00339 
00340           emailState2 = sending;
00341 
00342           /* Start writing next email! */
00343           onTimeout(sendtimeout);
00344         }
00345     }
00346       else if(emailState2 == requestsmtpok)
00347     {
00348       MESSAGE_SINGLE(NORMAL, logger, "APPL: Sending SMTP OK request!");
00349 
00350       packetSize = 648;
00351       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00352       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00353       applicationPDU->setRequest(true);
00354 
00355       packetNumber = 1;
00356       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00357       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00358 
00359       wns::osi::PDUPtr pdu(applicationPDU);
00360       applications::session::Session::outgoingProbesCalculation(pdu);
00361 
00362       connection->sendData(pdu);
00363 
00364       emailState2 = waitforsmtpok;
00365       state = idle;
00366     }
00367       else if(emailState2 == readlastemail)
00368     {
00369       /* Reading last received email.*/
00370       wns::simulator::Time readingTime = (*emailReadingTimeDistribution)();
00371 
00372       MESSAGE_SINGLE(NORMAL, logger, "APPL: Received last email. Readingtime = " << readingTime << ".");
00373 
00374       setTimeout(sendtimeout, readingTime);
00375 
00376       emailState2 = sending; //requestsmtpok;
00377     }
00378     }
00379   else if(_t == probetimeout)
00380     {
00381       applications::session::Session::onTimeout(_t);
00382     }
00383   else if(_t == connectiontimeout)
00384     {
00385       packetFrom = "client.Email";
00386 
00387       /* Open connection */
00388       binding->initBinding();
00389     }
00390   else
00391     {
00392       assure(false, "APPL: Unknown timout type =" << _t);
00393     }
00394 }
00395 

Generated on Sun May 27 03:32:15 2012 for openWNS by  doxygen 1.5.5