Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

gradient.hh

Go to the documentation of this file.
00001 //
00002 // gradient.hh    : Gradient Filter Include File
00003 // author         : Fabio Silva and Chalermek Intanagonwiwat
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // $Id: gradient.hh,v 1.6 2002/11/26 22:45:38 haldar 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 //
00022 
00023 #ifndef _GRADIENT_HH_
00024 #define _GRADIENT_HH_
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #include "config.h"
00028 #endif // HAVE_CONFIG_H
00029 
00030 #include <algorithm>
00031 #include "diffapp.hh"
00032 
00033 #ifdef NS_DIFFUSION
00034 #include <tcl.h>
00035 #include "diffagent.h"
00036 #else
00037 #include "main/hashutils.hh"
00038 #endif // NS_DIFFUSION
00039 
00040 #define GRADIENT_FILTER_PRIORITY 4
00041 #define OLD_MESSAGE -1
00042 #define NEW_MESSAGE 1
00043 
00044 extern NRSimpleAttributeFactory<void *> ReinforcementAttr;
00045 
00046 typedef list<Tcl_HashEntry *> HashList;
00047 
00048 class ReinforcementBlob {
00049 public:
00050   ReinforcementBlob(int32_t rdm_id, int32_t pkt_num) :
00051     rdm_id_(rdm_id), pkt_num_(pkt_num) {};
00052 
00053   int32_t rdm_id_;
00054   int32_t pkt_num_;
00055 };
00056 
00057 class HashEntry {
00058 public:
00059   HashEntry(int32_t last_hop) : last_hop_(last_hop) {};
00060 
00061   int32_t last_hop_;
00062 };
00063 
00064 class GradientEntry {
00065 public:
00066   GradientEntry(int32_t node_addr) : node_addr_(node_addr)
00067   {
00068     GetTime(&tv_);
00069     reinforced_ = false;
00070   };
00071 
00072   int32_t node_addr_;
00073   struct timeval tv_;
00074   bool reinforced_;
00075 };
00076 
00077 class AgentEntry {
00078 public:
00079   AgentEntry(u_int16_t port) : port_(port)
00080   {
00081     GetTime(&tv_);
00082   };
00083 
00084   u_int16_t port_;
00085   struct timeval tv_;
00086 };
00087 
00088 class DataNeighborEntry {
00089 public:
00090   DataNeighborEntry(int32_t neighbor_id, int data_flag) :
00091     neighbor_id_(neighbor_id), data_flag_(data_flag)
00092   {
00093     GetTime(&tv_);
00094   };
00095 
00096   int32_t neighbor_id_;
00097   struct timeval tv_;
00098   int data_flag_;
00099 };
00100 
00101 class AttributeEntry {
00102 public:
00103   AttributeEntry(NRAttrVec *attrs) : attrs_(attrs) {
00104     GetTime(&tv_);
00105   };
00106 
00107   ~AttributeEntry() {
00108     ClearAttrs(attrs_);
00109     delete attrs_;
00110   };
00111 
00112   struct timeval tv_;
00113   NRAttrVec *attrs_;
00114 };
00115 
00116 typedef list<AttributeEntry *> AttributeList;
00117 typedef list<AgentEntry *> AgentList;
00118 typedef list<GradientEntry *> GradientList;
00119 typedef list<DataNeighborEntry *> DataNeighborList;
00120 
00121 class RoutingEntry {
00122 public:
00123   RoutingEntry() {
00124     GetTime(&tv_);
00125   };
00126 
00127   ~RoutingEntry() {
00128     DataNeighborList::iterator data_neighbor_itr;
00129     AttributeList::iterator attr_itr;
00130     GradientList::iterator grad_itr;
00131     AgentList::iterator agents_itr;
00132 
00133     // Clear Attributes
00134     ClearAttrs(attrs_);
00135     delete attrs_;
00136 
00137     // Clear the attribute list
00138     for (attr_itr = attr_list_.begin(); attr_itr != attr_list_.end(); attr_itr++){
00139       delete (*attr_itr);
00140     }
00141     attr_list_.clear();
00142 
00143     // Clear the gradient list
00144     for (grad_itr = gradients_.begin(); grad_itr != gradients_.end(); grad_itr++){
00145       delete (*grad_itr);
00146     }
00147     gradients_.clear();
00148 
00149     // Clear the local agent's list
00150     for (agents_itr = agents_.begin(); agents_itr != agents_.end(); agents_itr++){
00151       delete (*agents_itr);
00152     }
00153     agents_.clear();
00154 
00155     // Clear the data neighbor's list
00156     for (data_neighbor_itr = data_neighbors_.begin(); data_neighbor_itr != data_neighbors_.end(); data_neighbor_itr++){
00157       delete (*data_neighbor_itr);
00158     }
00159     data_neighbors_.clear();
00160   };
00161 
00162   struct timeval tv_;
00163   NRAttrVec *attrs_;
00164   AgentList agents_;
00165   GradientList gradients_;
00166   AttributeList attr_list_;
00167   DataNeighborList data_neighbors_;
00168 };
00169 
00170 typedef list<RoutingEntry *> RoutingTable;
00171 class GradientFilter;
00172 
00173 class GradientFilterReceive : public FilterCallback {
00174 public:
00175   GradientFilterReceive(GradientFilter *app) : app_(app) {};
00176   void recv(Message *msg, handle h);
00177 
00178   GradientFilter *app_;
00179 };
00180 
00181 class DataForwardingHistory {
00182 public:
00183   DataForwardingHistory()
00184   {
00185     data_reinforced_ = false;
00186   };
00187 
00188   ~DataForwardingHistory()
00189   {
00190     node_list_.clear();
00191     agent_list_.clear();
00192   };
00193 
00194   bool alreadyForwardedToNetwork(int32_t node_id)
00195   {
00196     list<int32_t>::iterator list_itr;
00197 
00198     list_itr = find(node_list_.begin(), node_list_.end(), node_id);
00199     if (list_itr == node_list_.end())
00200       return false;
00201     return true;
00202   };
00203 
00204   bool alreadyForwardedToLibrary(u_int16_t agent_id)
00205   {
00206     list<u_int16_t>::iterator list_itr;
00207 
00208     list_itr = find(agent_list_.begin(), agent_list_.end(), agent_id);
00209     if (list_itr == agent_list_.end())
00210       return false;
00211     return true;
00212   };
00213 
00214   bool alreadyReinforced()
00215   {
00216     return data_reinforced_;
00217   };
00218 
00219   void sendingReinforcement()
00220   {
00221     data_reinforced_ = true;
00222   };
00223 
00224   void forwardingToNetwork(int32_t node_id)
00225   {
00226     node_list_.push_back(node_id);
00227   };
00228 
00229   void forwardingToLibrary(u_int16_t agent_id)
00230   {
00231     agent_list_.push_back(agent_id);
00232   };
00233 
00234 private:
00235   list<int32_t> node_list_;
00236   list<u_int16_t> agent_list_;
00237   bool data_reinforced_;
00238 };
00239 
00240 class GradientFilter : public DiffApp {
00241 public:
00242 #ifdef NS_DIFFUSION
00243   GradientFilter(const char *dr);
00244   int command(int argc, const char*const* argv);
00245   void run() {}
00246 #else
00247   GradientFilter(int argc, char **argv);
00248   void run();
00249 #endif // NS_DIFFUSION
00250 
00251   virtual ~GradientFilter()
00252   {
00253     // Nothing to do
00254   };
00255 
00256   void recv(Message *msg, handle h);
00257 
00258   // Timers
00259   void messageTimeout(Message *msg);
00260   void interestTimeout(Message *msg);
00261   void gradientTimeout();
00262   void reinforcementTimeout();
00263   int subscriptionTimeout(NRAttrVec *attrs);
00264 
00265 protected:
00266 
00267   // General Variables
00268   handle filter_handle_;
00269   int pkt_count_;
00270   int random_id_;
00271 
00272   // Hashing structures
00273   HashList hash_list_;
00274   Tcl_HashTable htable_;
00275 
00276   // Receive Callback for the filter
00277   GradientFilterReceive *filter_callback_;
00278 
00279   // List of all known datatypes
00280   RoutingTable routing_list_;
00281 
00282   // Setup the filter
00283   handle setupFilter();
00284 
00285   // Matching functions
00286   RoutingEntry * findRoutingEntry(NRAttrVec *attrs);
00287   void deleteRoutingEntry(RoutingEntry *routing_entry);
00288   RoutingEntry * matchRoutingEntry(NRAttrVec *attrs, RoutingTable::iterator start, RoutingTable::iterator *place);
00289   AttributeEntry * findMatchingSubscription(RoutingEntry *routing_entry, NRAttrVec *attrs);
00290 
00291   // Data structure management
00292   void updateGradient(RoutingEntry *routing_entry, int32_t last_hop, bool reinforced);
00293   void updateAgent(RoutingEntry *routing_entry, u_int16_t source_port);
00294   GradientEntry * findReinforcedGradients(GradientList *agents, GradientList::iterator start, GradientList::iterator *place);
00295   GradientEntry * findReinforcedGradient(int32_t node_addr, RoutingEntry *routing_entry);
00296   void deleteGradient(RoutingEntry *routing_entry, GradientEntry *gradient_entry);
00297   void setReinforcementFlags(RoutingEntry *routing_entry, int32_t last_hop, int new_message);
00298 
00299   // Message forwarding functions
00300   void sendInterest(NRAttrVec *attrs, RoutingEntry *routing_entry);
00301   void sendDisinterest(NRAttrVec *attrs, RoutingEntry *routing_entry);
00302   void sendPositiveReinforcement(NRAttrVec *reinf_attrs, int32_t data_rdm_id,
00303                                  int32_t data_pkt_num, int32_t destination);
00304   void forwardData(Message *msg, RoutingEntry *routing_entry,
00305                    DataForwardingHistory *forwarding_history);
00306   void forwardExploratoryData(Message *msg, RoutingEntry *routing_entry,
00307                               DataForwardingHistory *forwarding_history);
00308   void forwardPushExploratoryData(Message *msg,
00309                                   DataForwardingHistory *forwarding_history);
00310 
00311   // Message Processing functions
00312   void processOldMessage(Message *msg);
00313   void processNewMessage(Message *msg);
00314 
00315   // Hashing functions
00316   HashEntry * getHash(unsigned int pkt_num, unsigned int rdm_id);
00317   void putHash(HashEntry *new_hash_entry, unsigned int pkt_num, unsigned int rdm_id);
00318 };
00319 
00320 class GradientExpirationCheckTimer : public TimerCallback {
00321 public:
00322   GradientExpirationCheckTimer(GradientFilter *agent) : agent_(agent) {};
00323   ~GradientExpirationCheckTimer() {};
00324   int expire();
00325 
00326   GradientFilter *agent_;
00327 };
00328 
00329 class ReinforcementCheckTimer : public TimerCallback {
00330 public:
00331   ReinforcementCheckTimer(GradientFilter *agent) : agent_(agent) {};
00332   ~ReinforcementCheckTimer() {};
00333   int expire();
00334 
00335   GradientFilter *agent_;
00336 };
00337 
00338 class MessageSendTimer : public TimerCallback {
00339 public:
00340   MessageSendTimer(GradientFilter *agent, Message *msg) :
00341     agent_(agent), msg_(msg) {};
00342   ~MessageSendTimer()
00343   {
00344     delete msg_;
00345   };
00346   int expire();
00347 
00348   GradientFilter *agent_;
00349   Message *msg_;
00350 };
00351 
00352 class InterestForwardTimer : public TimerCallback {
00353 public:
00354   InterestForwardTimer(GradientFilter *agent, Message *msg) :
00355     agent_(agent), msg_(msg) {};
00356   ~InterestForwardTimer()
00357   {
00358     delete msg_;
00359   };
00360   int expire();
00361 
00362   GradientFilter *agent_;
00363   Message *msg_;
00364 };
00365 
00366 class SubscriptionExpirationTimer : public TimerCallback {
00367 public:
00368   SubscriptionExpirationTimer(GradientFilter *agent, NRAttrVec *attrs) :
00369     agent_(agent), attrs_(attrs) {};
00370   ~SubscriptionExpirationTimer()
00371   {
00372     ClearAttrs(attrs_);
00373     delete attrs_;
00374   };
00375   int expire();
00376 
00377   GradientFilter *agent_;
00378   NRAttrVec *attrs_;
00379 };
00380 
00381 #endif // !_GRADIENT_HH_

Generated on Tue Apr 20 12:14:18 2004 for NS2.26SourcesOriginal by doxygen 1.3.3