src/network/nPriorizing.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #ifndef ArmageTron_nPRIORIZING_H
00029 #define ArmageTron_nPRIORIZING_H
00030 
00031 #include "defs.h"
00032 #include "tHeap.h"
00033 
00034 class nBandwidthSceduler;
00035 class nBandwidthArbitrator;
00036 class nSendBuffer;
00037 class nBandwidthControl;
00038 class nBandwidthTask;
00039 class nBandwidthTaskPriorizer;
00040 
00041 tDECLARE_REFOBJ( nBandwidthTask )
00042 tDECLARE_REFOBJ( nBandwidthArbitrator )
00043 
00044 // small task that will eat away some bandwidth
00045 class nBandwidthTask: public tHeapElement, public tReferencable< nBandwidthTask >
00046 {
00047     friend class tReferencable< nBandwidthTask >;
00048     friend class nBandwidthTaskPriorizer;
00049 
00050 public:
00051     enum nType
00052     {
00053         Type_System = 0,                                                        // extremely important task for the integrety of the connection
00054         Type_Vital  = 1,                                                        // task important for someone's survival in the game
00055         Type_Casual = 2,                                                        // not so important task
00056         Type_Count  = 3
00057     };
00058 
00059     void Execute( nSendBuffer& buffer, nBandwidthControl& control )             {       this->DoExecute( buffer, control );     }                               // executes the small task
00060     void Priorize()                                                                                                             {       if ( this->priorizer_ ) this->DoPriorize(); }           // rethinks priority
00061 
00062     int         EstimateSize    ( void          ) const {       return this->DoEstimateSize(); }        // estimate bandwidth usage
00063 
00064     nType       Type                    ( void          ) const {       return type_;           }                               // returns the type of task
00065     REAL        Priority                ( void          ) const {       return priority_;       }                               // returns the priority ( event gets important after 1/priority_ seconds )
00066 
00067     void        SetType                 ( nType t       );                                                                                              // sets the task type
00068     void        AddPriority             ( REAL add      )               { priority_ += add; this->Priorize(); } // adds priority
00069 void    Timestep                ( REAL dt       )               { waiting_ += dt;       this->Priorize(); }     // adds time
00070 
00071 protected:
00072     virtual void DoExecute( nSendBuffer& buffer, nBandwidthControl& control ) = 0;                              // executes whatever it has to do
00073     virtual int  DoEstimateSize() const = 0;                                                                                                    // estimate bandwidth usage
00074     virtual void DoPriorize();                                                                                                                                  // rethinks priority
00075 
00076     virtual tHeapBase *Heap() const;                                                                                                                    // in wich heap are we?
00077 
00078     virtual ~nBandwidthTask();
00079     nBandwidthTask( nType type );
00080 private:
00081     nType type_;                                                                        // type of this task
00082     REAL priority_;                                                                     // something terrible happens if this task is not executed within 1/priority_ seconds
00083     REAL waiting_;                                                                      // time this task is already waiting ( plus small offset )
00084     nBandwidthTaskPriorizer* priorizer_;                        // the arbitrator taking care of this task
00085 };
00086 
00087 // bandwidth priorizer: selects bandwidth taks
00088 class nBandwidthTaskPriorizer
00089 {
00090 public:
00091     typedef tHeap< nBandwidthTask > nTaskHeap;
00092     typedef nBandwidthTask::nType nType;
00093 
00094     nTaskHeap& Tasks( nType t )
00095     {
00096         tASSERT( 0 <= t && t < nBandwidthTask::Type_Count );
00097 
00098         return tasks_[ t ];
00099     }
00100 
00101     const nTaskHeap& Tasks( nType t ) const
00102     {
00103         tASSERT( 0 <= t && t < nBandwidthTask::Type_Count );
00104 
00105         return tasks_[ t ];
00106     }
00107 
00108     void Insert( nBandwidthTask* task );                                                        // inserts a task into the queue
00109     nBandwidthTask* PeekNext( nType type );                                                     // returns the top priority task
00110     virtual ~nBandwidthTaskPriorizer(){}
00111 protected:
00112     tJUST_CONTROLLED_PTR<nBandwidthTask> Next( nType type );            // removes and returns the top priority task
00113 private:
00114     virtual void OnChange(){};                                                                          // called on every change of data
00115     nTaskHeap tasks_[ nBandwidthTask::Type_Count ];
00116 };
00117 
00118 // bandwidth arbitrator: executes bandwidth tasks
00119 class nBandwidthArbitrator: public nBandwidthTaskPriorizer, public tHeapElement, public tListMember, public tReferencable< nBandwidthArbitrator >
00120 {
00121     friend class nBandwidthSceduler;
00122     friend class tReferencable< nBandwidthArbitrator >;
00123 
00124 public:
00125     bool Fill( nSendBuffer& buffer, nBandwidthControl& control );       // fills the send buffer with top priority messages
00126     void Timestep( REAL dt );                                                                           // advances timers of all tasks
00127     bool UseBandwidth( REAL dt ){ return DoUseBandwidth( dt ); }        // consumes some bandwidth
00128 protected:
00129     nBandwidthArbitrator();
00130     ~nBandwidthArbitrator();
00131 private:
00132     nType FirstType() const;                                                                            // determines the type of the message to send next
00133     virtual void OnChange();                                                                            // called on every change of data
00134     virtual bool DoUseBandwidth( REAL dt ) = 0;
00135     virtual tHeapBase* Heap() const;
00136 
00137     virtual REAL TimeScale(){ return 0.1f; }                                            // higher values let really urgent messages be sent even if the bandwidth control objectd
00138     virtual REAL PacketOverhead(){ return 60.0f; }                                      // overhead in bytes per sent packet. Determines average package size
00139 
00140     nBandwidthSceduler* sceduler_;
00141 };
00142 
00143 // sceduler: distributes bandwidth around all arbitrators
00144 class nBandwidthSceduler
00145 {
00146     friend class nBandwidthArbitrator;
00147 
00148 public:
00149     ~nBandwidthSceduler();
00150 
00151     void UseBandwidth( REAL dt );                                                                               // consumes bandwidth
00152     void AddArbitrator          ( nBandwidthArbitrator& arbitrator );           // adds an arbitrator
00153     void RemoveArbitrator       ( nBandwidthArbitrator& arbitrator );           // removes an arbitrator
00154 private:
00155     tList< nBandwidthArbitrator, false, true >          arbitratorList_;        // arbitrators managed by the sceduler
00156     tHeap< nBandwidthArbitrator >                                       arbitratorHeap_;        // arbitrators managed by the sceduler organized in priority heap
00157 };
00158 
00159 class nNetObject;
00160 
00161 // object syncing or creating bandwidth task
00162 class nBandwidthTaskObject: public nBandwidthTask
00163 {
00164 public:
00165     nBandwidthTaskObject( nType type, nNetObject& object );
00166     virtual ~nBandwidthTaskObject();
00167 
00168     nNetObject& Object() const { return *object_; }
00169 protected:
00170     virtual int  DoEstimateSize() const;                                                                                                        // estimate bandwidth usage
00171 private:
00172     tJUST_CONTROLLED_PTR< nNetObject > object_;
00173 };
00174 
00175 // object syncing bandwidth task
00176 class nBandwidthTaskSync: public nBandwidthTaskObject
00177 {
00178 public:
00179     nBandwidthTaskSync( nType type, nNetObject& object )
00180             : nBandwidthTaskObject( type, object )
00181     {}
00182 
00183 protected:
00184     virtual void DoExecute( nSendBuffer& buffer, nBandwidthControl& control );                          // executes whatever it has to do
00185 };
00186 
00187 // object syncing bandwidth task
00188 class nBandwidthTaskCreate: public nBandwidthTaskObject
00189 {
00190 public:
00191     nBandwidthTaskCreate( nType type, nNetObject& object )
00192             : nBandwidthTaskObject( type, object )
00193     {}
00194 
00195 protected:
00196     virtual void DoExecute( nSendBuffer& buffer, nBandwidthControl& control );                          // executes whatever it has to do
00197 };
00198 
00199 #endif
00200 

Generated on Sat Mar 15 22:55:51 2008 for Armagetron Advanced by  doxygen 1.5.4