00001 // 00002 // Copyright (C) 2004, 2008 Andras Varga 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 00019 #include <omnetpp.h> 00020 #include "AbstractQueue.h" 00021 00022 00023 AbstractQueue::AbstractQueue() 00024 { 00025 msgServiced = NULL; 00026 endServiceMsg = NULL; 00027 } 00028 00029 AbstractQueue::~AbstractQueue() 00030 { 00031 delete msgServiced; 00032 cancelAndDelete(endServiceMsg); 00033 } 00034 00035 void AbstractQueue::initialize() 00036 { 00037 msgServiced = NULL; 00038 endServiceMsg = new cMessage("end-service"); 00039 queue.setName("queue"); 00040 } 00041 00042 void AbstractQueue::handleMessage(cMessage *msg) 00043 { 00044 if (msg==endServiceMsg) 00045 { 00046 doEndService(); 00047 } 00048 else if (!msgServiced) 00049 { 00050 cPacket *msg2 = arrivalWhenIdle( PK(msg) ); 00051 if (msg2) 00052 { 00053 msgServiced = msg2; 00054 doStartService(); 00055 } 00056 00057 } 00058 else 00059 { 00060 arrival( PK(msg) ); 00061 } 00062 } 00063 00064 void AbstractQueue::doStartService() 00065 { 00066 simtime_t serviceTime = startService( msgServiced ); 00067 if (serviceTime != 0) 00068 scheduleAt( simTime()+serviceTime, endServiceMsg ); 00069 else 00070 doEndService(); 00071 } 00072 00073 void AbstractQueue::doEndService() 00074 { 00075 endService( msgServiced ); 00076 if (queue.empty()) 00077 { 00078 msgServiced = NULL; 00079 } 00080 else 00081 { 00082 msgServiced = queue.pop(); 00083 doStartService(); 00084 } 00085 } 00086