• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/diffusion3/filters/diffusion/two_phase_pull.hh

00001 //
00002 // two_phase_pull.hh  : Two-Phase Pull/One-Phase Push Include File
00003 // author             : Fabio Silva and Chalermek Intanagonwiwat
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // $Id: two_phase_pull.hh,v 1.4 2005/09/13 04:53:47 tomh Exp $
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License,
00010 // version 2, as published by the Free Software Foundation.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License along
00018 // with this program; if not, write to the Free Software Foundation, Inc.,
00019 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020 //
00021 // Linking this file statically or dynamically with other modules is making
00022 // a combined work based on this file.  Thus, the terms and conditions of
00023 // the GNU General Public License cover the whole combination.
00024 //
00025 // In addition, as a special exception, the copyright holders of this file
00026 // give you permission to combine this file with free software programs or
00027 // libraries that are released under the GNU LGPL and with code included in
00028 // the standard release of ns-2 under the Apache 2.0 license or under
00029 // otherwise-compatible licenses with advertising requirements (or modified
00030 // versions of such code, with unchanged license).  You may copy and
00031 // distribute such a system following the terms of the GNU GPL for this
00032 // file and the licenses of the other code concerned, provided that you
00033 // include the source code of that other code when and as the GNU GPL
00034 // requires distribution of source code.
00035 //
00036 // Note that people who make modified versions of this file are not
00037 // obligated to grant this special exception for their modified versions;
00038 // it is their choice whether to do so.  The GNU General Public License
00039 // gives permission to release a modified version without this exception;
00040 // this exception also makes it possible to release a modified version
00041 // which carries forward this exception.
00042 
00043 #ifndef _GRADIENT_HH_
00044 #define _GRADIENT_HH_
00045 
00046 #ifdef HAVE_CONFIG_H
00047 #include "config.h"
00048 #endif // HAVE_CONFIG_H
00049 
00050 #include <algorithm>
00051 #include "diffapp.hh"
00052 
00053 #ifdef NS_DIFFUSION
00054 #include <tcl.h>
00055 #include "diffagent.h"
00056 #else
00057 #include "main/hashutils.hh"
00058 #endif // NS_DIFFUSION
00059 
00060 #define GRADIENT_FILTER_PRIORITY 75
00061 #define OLD_MESSAGE -1
00062 #define NEW_MESSAGE 1
00063 
00064 typedef list<Tcl_HashEntry *> HashList;
00065 
00066 class ReinforcementBlob {
00067 public:
00068   ReinforcementBlob(int32_t rdm_id, int32_t pkt_num) :
00069     rdm_id_(rdm_id), pkt_num_(pkt_num) {};
00070 
00071   int32_t rdm_id_;
00072   int32_t pkt_num_;
00073 };
00074 
00075 class HashEntry {
00076 public:
00077   HashEntry(int32_t last_hop) : last_hop_(last_hop) {};
00078 
00079   int32_t last_hop_;
00080 };
00081 
00082 class GradientEntry {
00083 public:
00084   GradientEntry(int32_t node_addr) : node_addr_(node_addr)
00085   {
00086     GetTime(&tv_);
00087     reinforced_ = false;
00088   };
00089 
00090   int32_t node_addr_;
00091   struct timeval tv_;
00092   bool reinforced_;
00093 };
00094 
00095 class AgentEntry {
00096 public:
00097   AgentEntry(u_int16_t port) : port_(port)
00098   {
00099     GetTime(&tv_);
00100   };
00101 
00102   u_int16_t port_;
00103   struct timeval tv_;
00104 };
00105 
00106 class DataNeighborEntry {
00107 public:
00108   DataNeighborEntry(int32_t neighbor_id, int data_flag) :
00109     neighbor_id_(neighbor_id), data_flag_(data_flag)
00110   {
00111     GetTime(&tv_);
00112   };
00113 
00114   int32_t neighbor_id_;
00115   struct timeval tv_;
00116   int data_flag_;
00117 };
00118 
00119 class AttributeEntry {
00120 public:
00121   AttributeEntry(NRAttrVec *attrs) : attrs_(attrs) {
00122     GetTime(&tv_);
00123   };
00124 
00125   ~AttributeEntry() {
00126     ClearAttrs(attrs_);
00127     delete attrs_;
00128   };
00129 
00130   struct timeval tv_;
00131   NRAttrVec *attrs_;
00132 };
00133 
00134 typedef list<AttributeEntry *> AttributeList;
00135 typedef list<AgentEntry *> AgentList;
00136 typedef list<GradientEntry *> GradientList;
00137 typedef list<DataNeighborEntry *> DataNeighborList;
00138 
00139 class TppRoutingEntry {
00140 public:
00141   TppRoutingEntry() {
00142     GetTime(&tv_);
00143   };
00144 
00145   ~TppRoutingEntry() {
00146     DataNeighborList::iterator data_neighbor_itr;
00147     AttributeList::iterator attr_itr;
00148     GradientList::iterator grad_itr;
00149     AgentList::iterator agents_itr;
00150 
00151     // Clear Attributes
00152     ClearAttrs(attrs_);
00153     delete attrs_;
00154 
00155     // Clear the attribute list
00156     for (attr_itr = attr_list_.begin(); attr_itr != attr_list_.end(); attr_itr++){
00157       delete (*attr_itr);
00158     }
00159     attr_list_.clear();
00160 
00161     // Clear the gradient list
00162     for (grad_itr = gradients_.begin(); grad_itr != gradients_.end(); grad_itr++){
00163       delete (*grad_itr);
00164     }
00165     gradients_.clear();
00166 
00167     // Clear the local agent's list
00168     for (agents_itr = agents_.begin(); agents_itr != agents_.end(); agents_itr++){
00169       delete (*agents_itr);
00170     }
00171     agents_.clear();
00172 
00173     // Clear the data neighbor's list
00174     for (data_neighbor_itr = data_neighbors_.begin(); data_neighbor_itr != data_neighbors_.end(); data_neighbor_itr++){
00175       delete (*data_neighbor_itr);
00176     }
00177     data_neighbors_.clear();
00178   };
00179 
00180   struct timeval tv_;
00181   NRAttrVec *attrs_;
00182   AgentList agents_;
00183   GradientList gradients_;
00184   AttributeList attr_list_;
00185   DataNeighborList data_neighbors_;
00186 };
00187 
00188 typedef list<TppRoutingEntry *> RoutingTable;
00189 class GradientFilter;
00190 
00191 class GradientFilterReceive : public FilterCallback {
00192 public:
00193   GradientFilterReceive(GradientFilter *app) : app_(app) {};
00194   void recv(Message *msg, handle h);
00195 
00196   GradientFilter *app_;
00197 };
00198 
00199 class DataForwardingHistory {
00200 public:
00201   DataForwardingHistory()
00202   {
00203     data_reinforced_ = false;
00204   };
00205 
00206   ~DataForwardingHistory()
00207   {
00208     node_list_.clear();
00209     agent_list_.clear();
00210   };
00211 
00212   bool alreadyForwardedToNetwork(int32_t node_id)
00213   {
00214     list<int32_t>::iterator list_itr;
00215 
00216     list_itr = find(node_list_.begin(), node_list_.end(), node_id);
00217     if (list_itr == node_list_.end())
00218       return false;
00219     return true;
00220   };
00221 
00222   bool alreadyForwardedToLibrary(u_int16_t agent_id)
00223   {
00224     list<u_int16_t>::iterator list_itr;
00225 
00226     list_itr = find(agent_list_.begin(), agent_list_.end(), agent_id);
00227     if (list_itr == agent_list_.end())
00228       return false;
00229     return true;
00230   };
00231 
00232   bool alreadyReinforced()
00233   {
00234     return data_reinforced_;
00235   };
00236 
00237   void sendingReinforcement()
00238   {
00239     data_reinforced_ = true;
00240   };
00241 
00242   void forwardingToNetwork(int32_t node_id)
00243   {
00244     node_list_.push_back(node_id);
00245   };
00246 
00247   void forwardingToLibrary(u_int16_t agent_id)
00248   {
00249     agent_list_.push_back(agent_id);
00250   };
00251 
00252 private:
00253   list<int32_t> node_list_;
00254   list<u_int16_t> agent_list_;
00255   bool data_reinforced_;
00256 };
00257 
00258 class GradientFilter : public DiffApp {
00259 public:
00260 #ifdef NS_DIFFUSION
00261   GradientFilter(const char *dr);
00262   int command(int argc, const char*const* argv);
00263   void run() {}
00264 #else
00265   GradientFilter(int argc, char **argv);
00266   void run();
00267 #endif // NS_DIFFUSION
00268 
00269   virtual ~GradientFilter()
00270   {
00271     // Nothing to do
00272   };
00273 
00274   void recv(Message *msg, handle h);
00275 
00276   // Timers
00277   void messageTimeout(Message *msg);
00278   void interestTimeout(Message *msg);
00279   void gradientTimeout();
00280   void reinforcementTimeout();
00281   int subscriptionTimeout(NRAttrVec *attrs);
00282 
00283 protected:
00284 
00285   // General Variables
00286   handle filter_handle_;
00287   int pkt_count_;
00288   int random_id_;
00289 
00290   // Hashing structures
00291   HashList hash_list_;
00292   Tcl_HashTable htable_;
00293 
00294   // Receive Callback for the filter
00295   GradientFilterReceive *filter_callback_;
00296 
00297   // List of all known datatypes
00298   RoutingTable routing_list_;
00299 
00300   // Setup the filter
00301   handle setupFilter();
00302 
00303   // Matching functions
00304   TppRoutingEntry * findRoutingEntry(NRAttrVec *attrs);
00305   void deleteRoutingEntry(TppRoutingEntry *routing_entry);
00306   TppRoutingEntry * matchRoutingEntry(NRAttrVec *attrs, RoutingTable::iterator start, RoutingTable::iterator *place);
00307   AttributeEntry * findMatchingSubscription(TppRoutingEntry *routing_entry, NRAttrVec *attrs);
00308 
00309   // Data structure management
00310   void updateGradient(TppRoutingEntry *routing_entry, int32_t last_hop, bool reinforced);
00311   void updateAgent(TppRoutingEntry *routing_entry, u_int16_t source_port);
00312   GradientEntry * findReinforcedGradients(GradientList *agents, GradientList::iterator start, GradientList::iterator *place);
00313   GradientEntry * findReinforcedGradient(int32_t node_addr, TppRoutingEntry *routing_entry);
00314   void deleteGradient(TppRoutingEntry *routing_entry, GradientEntry *gradient_entry);
00315   void setReinforcementFlags(TppRoutingEntry *routing_entry, int32_t last_hop, int new_message);
00316 
00317   // Message forwarding functions
00318   void sendInterest(NRAttrVec *attrs, TppRoutingEntry *routing_entry);
00319   void sendDisinterest(NRAttrVec *attrs, TppRoutingEntry *routing_entry);
00320   void sendPositiveReinforcement(NRAttrVec *reinf_attrs, int32_t data_rdm_id,
00321                                  int32_t data_pkt_num, int32_t destination);
00322   void forwardData(Message *msg, TppRoutingEntry *routing_entry,
00323                    DataForwardingHistory *forwarding_history);
00324   void forwardExploratoryData(Message *msg, TppRoutingEntry *routing_entry,
00325                               DataForwardingHistory *forwarding_history);
00326   void forwardPushExploratoryData(Message *msg,
00327                                   DataForwardingHistory *forwarding_history);
00328 
00329   // Message Processing functions
00330   void processOldMessage(Message *msg);
00331   void processNewMessage(Message *msg);
00332 
00333   // Hashing functions
00334   HashEntry * getHash(unsigned int pkt_num, unsigned int rdm_id);
00335   void putHash(HashEntry *new_hash_entry, unsigned int pkt_num, unsigned int rdm_id);
00336 };
00337 
00338 class TppGradientExpirationCheckTimer : public TimerCallback {
00339 public:
00340   TppGradientExpirationCheckTimer(GradientFilter *agent) : agent_(agent) {};
00341   ~TppGradientExpirationCheckTimer() {};
00342   int expire();
00343 
00344   GradientFilter *agent_;
00345 };
00346 
00347 class TppReinforcementCheckTimer : public TimerCallback {
00348 public:
00349   TppReinforcementCheckTimer(GradientFilter *agent) : agent_(agent) {};
00350   ~TppReinforcementCheckTimer() {};
00351   int expire();
00352 
00353   GradientFilter *agent_;
00354 };
00355 
00356 class TppMessageSendTimer : public TimerCallback {
00357 public:
00358   TppMessageSendTimer(GradientFilter *agent, Message *msg) :
00359     agent_(agent), msg_(msg) {};
00360   ~TppMessageSendTimer()
00361   {
00362     delete msg_;
00363   };
00364   int expire();
00365 
00366   GradientFilter *agent_;
00367   Message *msg_;
00368 };
00369 
00370 class TppInterestForwardTimer : public TimerCallback {
00371 public:
00372   TppInterestForwardTimer(GradientFilter *agent, Message *msg) :
00373     agent_(agent), msg_(msg) {};
00374   ~TppInterestForwardTimer()
00375   {
00376     delete msg_;
00377   };
00378   int expire();
00379 
00380   GradientFilter *agent_;
00381   Message *msg_;
00382 };
00383 
00384 class TppSubscriptionExpirationTimer : public TimerCallback {
00385 public:
00386   TppSubscriptionExpirationTimer(GradientFilter *agent, NRAttrVec *attrs) :
00387     agent_(agent), attrs_(attrs) {};
00388   ~TppSubscriptionExpirationTimer()
00389   {
00390     ClearAttrs(attrs_);
00391     delete attrs_;
00392   };
00393   int expire();
00394 
00395   GradientFilter *agent_;
00396   NRAttrVec *attrs_;
00397 };
00398 
00399 #endif // !_GRADIENT_HH_

Generated on Tue Aug 10 2010 16:16:06 for ns-2.33 by  doxygen 1.7.1