00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _PROPHET_FWD_STRATEGY_H_
00018 #define _PROPHET_FWD_STRATEGY_H_
00019
00020 #include <algorithm>
00021 #include "Bundle.h"
00022 #include "Table.h"
00023
00024 namespace prophet
00025 {
00026
00027
00028 class FwdStrategyComp;
00029
00030 struct FwdStrategy
00031 {
00036 typedef enum {
00037 INVALID_FS = 0,
00038 GRTR,
00039 GTMX,
00040 GRTR_PLUS,
00041 GTMX_PLUS,
00042 GRTR_SORT,
00043 GRTR_MAX
00044 } fwd_strategy_t;
00045
00049 static const char*
00050 fs_to_str(fwd_strategy_t fs)
00051 {
00052 switch(fs) {
00053 #define CASE(_f_s) case _f_s: return # _f_s
00054 CASE(GRTR);
00055 CASE(GTMX);
00056 CASE(GRTR_PLUS);
00057 CASE(GTMX_PLUS);
00058 CASE(GRTR_SORT);
00059 CASE(GRTR_MAX);
00060 #undef CASE
00061 default: return "Unknown forwarding strategy";
00062 }
00063 }
00064
00069 inline static FwdStrategyComp* strategy(
00070 FwdStrategy::fwd_strategy_t fs,
00071 const Table* local_nodes = NULL,
00072 const Table* remote_nodes = NULL);
00073
00074 };
00075
00082 class FwdStrategyComp :
00083 public std::binary_function<const Bundle*,const Bundle*,bool>
00084 {
00085 public:
00089 virtual ~FwdStrategyComp() {}
00090
00094 virtual bool operator() (const Bundle* a, const Bundle* b) const
00095 {
00096 return *b < *a;
00097 }
00098
00100 FwdStrategy::fwd_strategy_t fwd_strategy() const
00101 {
00102 return strategy_;
00103 }
00104 const char* fwd_strategy_str() const
00105 {
00106 return FwdStrategy::fs_to_str(strategy_);
00107 }
00109
00110 protected:
00111 friend class FwdStrategy;
00112
00116 FwdStrategyComp(FwdStrategy::fwd_strategy_t fs = FwdStrategy::INVALID_FS)
00117 : strategy_(fs) {}
00118
00119 FwdStrategy::fwd_strategy_t strategy_;
00120 };
00121
00129 class FwdStrategyCompGRTRSORT : public FwdStrategyComp
00130 {
00131 public:
00135 virtual ~FwdStrategyCompGRTRSORT() {}
00136
00137 virtual bool operator() (const Bundle* a, const Bundle* b) const
00138 {
00139 if (local_ == NULL || remote_ == NULL) return false;
00140 double pa = remote_->p_value(a) - local_->p_value(a);
00141 double pb = remote_->p_value(b) - local_->p_value(b);
00142 return pa < pb;
00143 }
00144
00146 const Table* local_nodes() const { return local_; }
00147 const Table* remote_nodes() const { return remote_; }
00149
00150 protected:
00151 friend class FwdStrategy;
00152
00156 FwdStrategyCompGRTRSORT(FwdStrategy::fwd_strategy_t fs,
00157 const Table* local, const Table* remote)
00158 : FwdStrategyComp(fs), local_(local), remote_(remote) {}
00159
00160 const Table* local_;
00161 const Table* remote_;
00162
00163 };
00164
00165 class FwdStrategyCompGRTRMAX : public FwdStrategyComp
00166 {
00167 public:
00171 virtual ~FwdStrategyCompGRTRMAX() {}
00172
00173 virtual bool operator() (const Bundle* a, const Bundle* b) const
00174 {
00175 if (remote_ == NULL) return false;
00176 return (remote_->p_value(a) < remote_->p_value(b));
00177 }
00178
00180 const Table* remote_nodes() const { return remote_; }
00182
00183 protected:
00184 friend class FwdStrategy;
00185
00189 FwdStrategyCompGRTRMAX(FwdStrategy::fwd_strategy_t fs,const Table* remote)
00190 : FwdStrategyComp(fs),
00191 remote_(remote) {}
00192
00193 const Table* remote_;
00194 };
00195
00204 struct BundleOfferComp :
00205 public std::binary_function<const Bundle*,const Bundle*,bool>
00206 {
00207 BundleOfferComp(const FwdStrategyComp* comp)
00208 : comp_(comp) {}
00209
00210 bool operator() (const Bundle* a, const Bundle* b) const
00211 {
00212 return comp_->operator()(a,b);
00213 }
00214
00215 const FwdStrategyComp* comp_;
00216 };
00217
00218 FwdStrategyComp*
00219 FwdStrategy::strategy(FwdStrategy::fwd_strategy_t fs,
00220 const Table* local,
00221 const Table* remote)
00222 {
00223 FwdStrategyComp* f = NULL;
00224 switch (fs)
00225 {
00226 case FwdStrategy::GRTR:
00227 case FwdStrategy::GTMX:
00228 case FwdStrategy::GRTR_PLUS:
00229 case FwdStrategy::GTMX_PLUS:
00230
00231 f = new FwdStrategyComp(fs);
00232 break;
00233 case FwdStrategy::GRTR_SORT:
00234 {
00235 if (local == NULL || remote == NULL) return NULL;
00236 f = new FwdStrategyCompGRTRSORT(fs,local,remote);
00237 break;
00238 }
00239 case FwdStrategy::GRTR_MAX:
00240 {
00241 if (remote == NULL) return NULL;
00242 f = new FwdStrategyCompGRTRMAX(fs,remote);
00243 break;
00244 }
00245 case FwdStrategy::INVALID_FS:
00246 default:
00247 break;
00248 }
00249 return f;
00250 }
00251
00252 };
00253
00254 #endif // _PROPHET_FWD_STRATEGY_H_