00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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,
00054 Type_Vital = 1,
00055 Type_Casual = 2,
00056 Type_Count = 3
00057 };
00058
00059 void Execute( nSendBuffer& buffer, nBandwidthControl& control ) { this->DoExecute( buffer, control ); }
00060 void Priorize() { if ( this->priorizer_ ) this->DoPriorize(); }
00061
00062 int EstimateSize ( void ) const { return this->DoEstimateSize(); }
00063
00064 nType Type ( void ) const { return type_; }
00065 REAL Priority ( void ) const { return priority_; }
00066
00067 void SetType ( nType t );
00068 void AddPriority ( REAL add ) { priority_ += add; this->Priorize(); }
00069 void Timestep ( REAL dt ) { waiting_ += dt; this->Priorize(); }
00070
00071 protected:
00072 virtual void DoExecute( nSendBuffer& buffer, nBandwidthControl& control ) = 0;
00073 virtual int DoEstimateSize() const = 0;
00074 virtual void DoPriorize();
00075
00076 virtual tHeapBase *Heap() const;
00077
00078 virtual ~nBandwidthTask();
00079 nBandwidthTask( nType type );
00080 private:
00081 nType type_;
00082 REAL priority_;
00083 REAL waiting_;
00084 nBandwidthTaskPriorizer* priorizer_;
00085 };
00086
00087
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 );
00109 nBandwidthTask* PeekNext( nType type );
00110 virtual ~nBandwidthTaskPriorizer(){}
00111 protected:
00112 tJUST_CONTROLLED_PTR<nBandwidthTask> Next( nType type );
00113 private:
00114 virtual void OnChange(){};
00115 nTaskHeap tasks_[ nBandwidthTask::Type_Count ];
00116 };
00117
00118
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 );
00126 void Timestep( REAL dt );
00127 bool UseBandwidth( REAL dt ){ return DoUseBandwidth( dt ); }
00128 protected:
00129 nBandwidthArbitrator();
00130 ~nBandwidthArbitrator();
00131 private:
00132 nType FirstType() const;
00133 virtual void OnChange();
00134 virtual bool DoUseBandwidth( REAL dt ) = 0;
00135 virtual tHeapBase* Heap() const;
00136
00137 virtual REAL TimeScale(){ return 0.1f; }
00138 virtual REAL PacketOverhead(){ return 60.0f; }
00139
00140 nBandwidthSceduler* sceduler_;
00141 };
00142
00143
00144 class nBandwidthSceduler
00145 {
00146 friend class nBandwidthArbitrator;
00147
00148 public:
00149 ~nBandwidthSceduler();
00150
00151 void UseBandwidth( REAL dt );
00152 void AddArbitrator ( nBandwidthArbitrator& arbitrator );
00153 void RemoveArbitrator ( nBandwidthArbitrator& arbitrator );
00154 private:
00155 tList< nBandwidthArbitrator, false, true > arbitratorList_;
00156 tHeap< nBandwidthArbitrator > arbitratorHeap_;
00157 };
00158
00159 class nNetObject;
00160
00161
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;
00171 private:
00172 tJUST_CONTROLLED_PTR< nNetObject > object_;
00173 };
00174
00175
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 );
00185 };
00186
00187
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 );
00197 };
00198
00199 #endif
00200