Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

cabal_waiter.cpp

00001 /*
00002    Cabal - cabal_waiter
00003    Short description
00004 
00005    $Id: cabal_waiter.cpp,v 1.1 2004/03/29 14:17:28 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      : lun mar 29 2004 $TIME$
00008    Author     : Giancarlo Niccolai <gc@niccolai.ws>, (C) 2004
00009 
00010    Last modified because:
00011 
00012 */
00013 
00014 /**************************************************************************
00015 *   This program is free software; you can redistribute it and/or modify  *
00016 *   it under the terms of the GNU Library General Public License as       *
00017 *   published by the Free Software Foundation; either version 2.1 of the  *
00018 *   License, or (at your option) any later version.                       *
00019 ***************************************************************************/
00020 
00021 #include <cabal_waiter.h>
00022 #include <cabal_os.h>
00023 namespace Cabal
00024 {
00025 
00026 void Waiter::usedBandwidth( const long int bytes )
00027 {
00028    // avoid recording is this line is free
00029    if ( m_bandwidth == 0 ) return;
00030    
00031    // calculates bytes left from last time.
00032    MSECS curTime = Net::timeOfDay();
00033    
00034    MSECS timeLapse;
00035    if ( m_lastSentTime < 0 ) 
00036       timeLapse = 0;
00037    else
00038       timeLapse = curTime < m_lastSentTime ? 
00039          CABAL_DAY_LENGTH - m_lastSentTime + curTime : 
00040          curTime - m_lastSentTime;
00041    
00042    // how much time has the previous data taken to go out?
00043    MSECS usedTime = m_lastSentSize * 1000 / m_bandwidth;
00044    if ( usedTime > timeLapse ) { 
00045       // still some data left;
00046       m_lastSentSize = (m_bandwidth / ( usedTime - timeLapse ) ) + bytes + 1; // round up.
00047    }
00048    else
00049       m_lastSentSize = bytes;                        
00050    
00051    // stop time here
00052    m_lastSentTime = curTime;   
00053 
00054 }
00055 
00056 MSECS Waiter::minimalWait()
00057 {
00058    // free speed?
00059    if ( m_bandwidth == 0 ) return 0;
00060    
00061    // calculates bytes left from last time.
00062    MSECS curTime = Net::timeOfDay();
00063    MSECS timeLapse ;
00064    if ( m_lastSentTime < 0 ) 
00065       timeLapse = 0;
00066    else
00067       timeLapse = curTime < m_lastSentTime ? 
00068          CABAL_DAY_LENGTH - m_lastSentTime + curTime : 
00069          curTime - m_lastSentTime;
00070    
00071    // how many bytes may have done through in the elapsed time?
00072    long int bytesGone = m_bandwidth * timeLapse / 1000;
00073    
00074    // can we immediately proceed?
00075    if ( m_lastSentSize <= bytesGone ) { 
00076       return 0;
00077    }
00078    
00079    // how much time will it take to send the remaining bytes?
00080    return static_cast<MSECS>( (m_lastSentSize - bytesGone) * 1000 / m_bandwidth );
00081 }
00082 
00083 }
00084 
00085 /* end of cabal_waiter */

Generated on Sat Apr 10 17:41:49 2004 for Cabal by doxygen 1.3.5