• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/diffusion3/lib/dr.hh

00001 //
00002 // dr.hh           : Diffusion Routing Class Definitions
00003 // authors         : John Heidemann and Fabio Silva
00004 //
00005 // Copyright (C) 2000-2003 by the University of Southern California
00006 // $Id: dr.hh,v 1.17 2005/09/13 04:53:49 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 // This file defines Diffusion Routing's Publish/Subscribe, Filter and
00044 // Timer APIs. It is included from diffapp.hh (and therefore
00045 // applications and filters should include diffapp.hh instead). For a
00046 // detailed description of the API, please look at the Diffusion
00047 // Routing API document (available at the SCADDS website:
00048 // http://www.isi.edu/scadds).
00049 
00050 #ifndef _DR_HH_
00051 #define _DR_HH_
00052 
00053 #ifdef HAVE_CONFIG_H
00054 #include "config.h"
00055 #endif // HAVE_CONFIG_H
00056 
00057 #include <pthread.h>
00058 #include <string.h>
00059 #include <math.h>
00060 #include <map>
00061 
00062 #include "main/timers.hh"
00063 #include "main/filter.hh"
00064 #include "main/config.hh"
00065 #include "main/iodev.hh"
00066 #include "main/tools.hh"
00067 
00068 #ifdef NS_DIFFUSION
00069 #include "diffagent.h"
00070 #endif // NS_DIFFUSION
00071 
00072 #ifdef UDP
00073 #include "drivers/UDPlocal.hh"
00074 #endif // UDP
00075 
00076 #define WAIT_FOREVER       -1
00077 #define POLLING_INTERVAL   10 // seconds
00078 #define SMALL_TIMEOUT      10 // milliseconds
00079 
00080 typedef long handle;
00081 
00082 class HandleEntry;
00083 class CallbackEntry;
00084 class DiffusionRouting;
00085 
00086 typedef list<HandleEntry *> HandleList;
00087 typedef list<CallbackEntry *> CallbackList;
00088 
00089 class TimerCallbacks {
00090 public:
00091   virtual ~TimerCallbacks () {}
00092   virtual int expire(handle hdl, void *p) = 0;
00093   virtual void del(void *p) = 0;
00094 };
00095 
00096 class InterestCallback : public TimerCallback {
00097 public:
00098   InterestCallback(DiffusionRouting *drt, HandleEntry *handle_entry) :
00099     drt_(drt), handle_entry_(handle_entry) {};
00100   ~InterestCallback() {};
00101   int expire();
00102 
00103   DiffusionRouting *drt_;
00104   HandleEntry *handle_entry_;
00105 };
00106 
00107 class FilterKeepaliveCallback : public TimerCallback {
00108 public:
00109   FilterKeepaliveCallback(DiffusionRouting *drt, FilterEntry *filter_entry) :
00110     drt_(drt), filter_entry_(filter_entry) {};
00111   ~FilterKeepaliveCallback() {};
00112   int expire();
00113 
00114   DiffusionRouting *drt_;
00115   FilterEntry *filter_entry_;
00116 };
00117 
00118 class OldAPITimer : public TimerCallback {
00119 public:
00120   OldAPITimer(TimerCallbacks *cb, void *p) :
00121     cb_(cb), p_(p) {};
00122   ~OldAPITimer() {};
00123   int expire();
00124 
00125   TimerCallbacks *cb_;
00126   void *p_;
00127 };
00128 
00129 // Rmst specific definitions
00130 typedef map<int, void*, less<int> > Int2Frag;
00131 class RecRmst {
00132 public:
00133   RecRmst(int id){rmst_no_ = id;}
00134   ~RecRmst(){
00135     void *tmp_frag_ptr;
00136     Int2Frag::iterator frag_iterator;
00137     for(frag_iterator=frag_map_.begin(); frag_iterator!=frag_map_.end(); ++frag_iterator){
00138       tmp_frag_ptr = (void*)(*frag_iterator).second;
00139       delete((char *)tmp_frag_ptr);
00140     }
00141   }
00142   int rmst_no_;
00143   int max_frag_;
00144   int max_frag_len_;
00145   int mtu_len_;
00146   Int2Frag frag_map_;
00147 };
00148 typedef map<int, RecRmst*, less<int> > Int2RecRmst;
00149 
00150 class DiffusionRouting : public NR {
00151 public:
00152 
00153 #ifdef NS_DIFFUSION
00154   DiffusionRouting(u_int16_t port, DiffAppAgent *da);
00155   int getNodeId();               // node-id
00156   int getAgentId(int id = -1);   // port-id
00157   MobileNode *getNode(MobileNode *mn = 0)
00158   {
00159     if (mn != 0)
00160       node_ = mn;
00161     return node_;
00162   };
00163 #else
00164   DiffusionRouting(u_int16_t port);
00165   void run(bool wait_condition, long max_timeout);
00166 #endif // NS_DIFFUSION
00167 
00168   virtual ~DiffusionRouting();
00169 
00170   // NR Publish/Subscribe API functions
00171 
00172   handle subscribe(NRAttrVec *subscribe_attrs, NR::Callback *cb);
00173 
00174   int unsubscribe(handle subscription_handle);
00175 
00176   handle publish(NRAttrVec *publish_attrs);
00177 
00178   int unpublish(handle publication_handle);
00179 
00180   int send(handle publication_handle, NRAttrVec *send_attrs);
00181 
00182   int sendRmst(handle publication_handle, NRAttrVec *send_attrs, int fragment_size);
00183 
00184   // NR Filter API functions
00185 
00186   handle addFilter(NRAttrVec *filter_attrs, u_int16_t priority,
00187                    FilterCallback *cb);
00188 
00189   int removeFilter(handle filter_handle);
00190 
00191   int sendMessage(Message *msg, handle h, u_int16_t priority = FILTER_KEEP_PRIORITY);
00192 
00193   int addToBlacklist(int32_t node);
00194 
00195   int clearBlacklist();
00196 
00197   // NR Timer API functions
00198   handle addTimer(int timeout, TimerCallback *callback);
00199 
00200   // This is an old API function that will be discontinued in
00201   // diffusion's next major release
00202   handle addTimer(int timeout, void *param, TimerCallbacks *cb);
00203 
00204   bool removeTimer(handle hdl);
00205 
00206   // NR API functions that allow single thread support
00207 
00208   void doIt();
00209 
00210   void doOne(long timeout = WAIT_FOREVER);
00211 
00212   int interestTimeout(HandleEntry *handle_entry);
00213   int filterKeepaliveTimeout(FilterEntry *filter_entry);
00214 
00215 #ifndef NS_DIFFUSION
00216   // Outside NS, all these can be protected members
00217 protected:
00218 #endif // !NS_DIFFUSION
00219   void recvPacket(DiffPacket pkt);
00220   void recvMessage(Message *msg);
00221 #ifdef NS_DIFFUSION
00222   // In NS, the protected members start here
00223 protected:
00224   // Handle to MobileNode
00225   MobileNode *node_;
00226 #endif // NS_DIFFUSION
00227 
00228   void sendMessageToDiffusion(Message *msg);
00229   void sendPacketToDiffusion(DiffPacket pkt, int len, int dst);
00230 
00231   bool processRmst(Message *msg);
00232   void processMessage(Message *msg);
00233   void processControlMessage(Message *msg);
00234 
00235   bool checkSubscription(NRAttrVec *attrs);
00236   bool checkPublication(NRAttrVec *attrs);
00237   bool checkSend(NRAttrVec *attrs);
00238   bool isPushData(NRAttrVec *attrs);
00239 
00240   HandleEntry * removeHandle(handle my_handle, HandleList *hl);
00241   HandleEntry * findHandle(handle my_handle, HandleList *hl);
00242 
00243   FilterEntry * deleteFilter(handle my_handle);
00244   FilterEntry * findFilter(handle my_handle);
00245   bool hasScope(NRAttrVec *attrs);
00246 
00247   // RMST support 
00248   Int2RecRmst rec_rmst_map_;
00249 
00250   // Handle variables
00251   int next_handle_;
00252   HandleList pub_list_;
00253   HandleList sub_list_;
00254   FilterList filter_list_;
00255 
00256   // Threads and Mutexes
00257   pthread_mutex_t *dr_mtx_;
00258 
00259   // Data structures
00260   TimerManager *timers_manager_;
00261 
00262   // Lists
00263   DeviceList in_devices_;
00264   DeviceList local_out_devices_;
00265 
00266   // Node-specific variables
00267   u_int16_t diffusion_port_;
00268   int pkt_count_;
00269   int random_id_;
00270 
00271 #ifdef NS_DIFFUSION
00272   int agent_id_;
00273 #else
00274   u_int16_t agent_id_;
00275 #endif // NS_DIFFUSION
00276 };
00277 
00278 #endif // !_DR_HH_

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