• Main Page
  • Classes
  • Files
  • File List

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

00001 // 
00002 // nr.hh         : Network Routing Class Definitions
00003 // authors       : Dan Coffin, John Heidemann, Dan Van Hook
00004 // authors       : Fabio Silva
00005 // 
00006 // Copyright (C) 2000-2003 by the University of Southern California
00007 // $Id: nr.hh,v 1.13 2005/09/13 04:53:50 tomh Exp $
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License,
00011 // version 2, as published by the Free Software Foundation.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License along
00019 // with this program; if not, write to the Free Software Foundation, Inc.,
00020 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00021 //
00022 // Linking this file statically or dynamically with other modules is making
00023 // a combined work based on this file.  Thus, the terms and conditions of
00024 // the GNU General Public License cover the whole combination.
00025 //
00026 // In addition, as a special exception, the copyright holders of this file
00027 // give you permission to combine this file with free software programs or
00028 // libraries that are released under the GNU LGPL and with code included in
00029 // the standard release of ns-2 under the Apache 2.0 license or under
00030 // otherwise-compatible licenses with advertising requirements (or modified
00031 // versions of such code, with unchanged license).  You may copy and
00032 // distribute such a system following the terms of the GNU GPL for this
00033 // file and the licenses of the other code concerned, provided that you
00034 // include the source code of that other code when and as the GNU GPL
00035 // requires distribution of source code.
00036 //
00037 // Note that people who make modified versions of this file are not
00038 // obligated to grant this special exception for their modified versions;
00039 // it is their choice whether to do so.  The GNU General Public License
00040 // gives permission to release a modified version without this exception;
00041 // this exception also makes it possible to release a modified version
00042 // which carries forward this exception.
00043 
00044 #ifndef _NR_HH_
00045 #define _NR_HH_
00046 
00047 #ifdef HAVE_CONFIG_H
00048 #include "config.h"
00049 #endif // HAVE_CONFIG_H
00050 
00051 #include <stdio.h>
00052 #include <stdlib.h>
00053 #include <assert.h>
00054 #include <vector>
00055 
00056 #ifdef NS_DIFFUSiON
00057 #include "config.h"
00058 #endif // NS_DIFFUSION
00059 
00060 using namespace std;
00061 
00062 #ifndef __CYGWIN__
00063 typedef signed int int32_t;
00064 #endif
00065 typedef signed short int16_t;
00066 
00067 #if defined (__SVR4) && defined (__sun)
00068 typedef char int8_t;
00069 #else
00070 // Conflicts with system declaration of int8_t in Solaris
00071 typedef signed char int8_t;
00072 #endif // sparc
00073 
00074 #define FAIL -1
00075 #define OK    0
00076 
00077 class NRAttribute;
00078 
00079 typedef vector<NRAttribute *> NRAttrVec;
00080 
00081 /*
00082  * xxx: gcc-2.91.66 doesn't handle member templates completely
00083  * (it worked for NRSimpleFactory<> but not for Blob and String
00084  * factories.  A work-around is to move all the templates out
00085  * to top-level code.
00086  * I'm told (by a gcc-developer) that this limitation is fixed
00087  * in gcc-2.95.2.
00088  */
00089 
00090 /*
00091  * NRAttribute encapsulates a single, generic attribute.
00092  */
00093 class NRAttribute {
00094 public:
00095   enum keys {
00096     // reserved constant values used for key
00097     // range 1000-1499 is diffusion-specific
00098     SCOPE_KEY = 1001,             // INT32_TYPE
00099     CLASS_KEY = 1002,             // INT32_TYPE
00100     ALGORITHM_KEY = 1003,         // INT32_TYPE
00101     SUBSCRIPTION_ID_KEY = 1004,   // INT32_TYPE
00102     FLOWS_KEY = 1005,             // BLOB_TYPE
00103 
00104     // range 1500-1999 is reserved for system filters
00105     REINFORCEMENT_KEY = 1500,     // BLOB_TYPE
00106     LATITUDE_KEY = 1501,          // FLOAT_TYPE
00107     LONGITUDE_KEY = 1502,         // FLOAT_TYPE
00108     ROUTE_KEY = 1503,             // STRING_TYPE
00109     SOURCE_ROUTE_KEY = 1504,      // STRING_TYPE
00110 
00111     // range 2000-2999 is app specific
00112     DATABLOCK_KEY = 2001,         // BLOB_TYPE
00113     TASK_FREQUENCY_KEY = 2002,    // FLOAT_TYPE, in secs
00114     TASK_NAME_KEY = 2003,         // STRING_TYPE
00115     TASK_QUERY_DETAIL_KEY = 2004, // BLOB_TYPE
00116     TARGET_KEY = 2005,            // STRING_TYPE
00117     TARGET_RANGE_KEY = 2006,      // FLOAT_TYPE
00118     CONFIDENCE_KEY = 2007,        // FLOAT_TYPE
00119 
00120     // RMST Keys
00121     RMST_DATA_KEY = 2010,         // BLOB_TYPE  The actual data (fragment)
00122     RMST_ID_KEY = 2011,           // INT32_TYPE Unique ID for the frag set
00123     RMST_FRAG_KEY = 2012,         // INT32_TYPE Fragment ID
00124     RMST_MAX_FRAG_KEY = 2013,     // INT32_TYPE Largest frag id in set
00125     RMST_TSPRT_CTL_KEY = 2014,    // INT32_TYPE Transport Control Messages
00126     RMST_PKTS_SENT_KEY = 2015,    // INT32_TYPE Support for blacklisting
00127     RMST_TARGET_KEY = 2016        // STRING_TYPE Type of Rmst Data
00128 
00129     // range 3000-3999 is reserved for experimentation
00130     // and user-defined keys
00131   };
00132 
00133   // Values for diffusion-specific keys (start these at high values,
00134   // so we can do simple type checking)
00135   enum classes { INTEREST_CLASS = 10010, DISINTEREST_CLASS, DATA_CLASS };
00136   enum scopes { NODE_LOCAL_SCOPE = 11010, GLOBAL_SCOPE };
00137   enum algorithms { ONE_PHASE_PULL_ALGORITHM = 12010,
00138                     TWO_PHASE_PULL_ALGORITHM,
00139                     ONE_PHASE_PUSH_ALGORITHM };
00140 
00141   // Key Type values
00142   enum types { INT32_TYPE,    // 32-bit signed integer
00143                FLOAT32_TYPE,  // 32-bit
00144                FLOAT64_TYPE,  // 64-bit
00145                STRING_TYPE,   // UTF-8 format, max length 1024 chars
00146                BLOB_TYPE };   // uninterpreted binary data
00147 
00148   // Match Operator values
00149   enum operators { IS, LE, GE, LT, GT, EQ, NE, EQ_ANY };
00150   // with EQ_ANY, the val is ignored
00151 
00152   NRAttribute();
00153   NRAttribute(int key, int type, int op, int len, void *val = NULL);
00154   NRAttribute(const NRAttribute &rhs);
00155   virtual ~NRAttribute();
00156 
00157   static NRAttribute * find_key(int key, NRAttrVec *attrs,
00158                                 NRAttrVec::iterator *place = NULL) {
00159 
00160     return find_key_from(key, attrs, attrs->begin(), place);
00161   };
00162 
00163   static NRAttribute * find_key_from(int key, NRAttrVec *attrs,
00164                                      NRAttrVec::iterator start,
00165                                      NRAttrVec::iterator *place = NULL);
00166 
00167   NRAttribute * find_matching_key_from(NRAttrVec *attrs,
00168                                        NRAttrVec::iterator start,
00169                                        NRAttrVec::iterator *place = NULL) {
00170     return find_key_from(key_, attrs, start, place);
00171   };
00172 
00173   NRAttribute * find_matching_key(NRAttrVec *attrs,
00174                                   NRAttrVec::iterator *place = NULL) {
00175     return find_key_from(key_, attrs, attrs->begin(), place);
00176   };
00177 
00178   int32_t getKey() { return key_; };
00179   int8_t getType() { return type_; };
00180   int8_t getOp() { return op_; };
00181   int16_t getLen() { return len_; };
00182 
00183   void * getGenericVal() { return val_; };
00184 
00185   bool isSameKey(NRAttribute *attr) {
00186     return ((type_ == attr->getType()) && (key_ == attr->getKey()));
00187   };
00188 
00189   bool isEQ(NRAttribute *attr);
00190 
00191   bool isGT(NRAttribute *attr);
00192 
00193   bool isGE(NRAttribute *attr);
00194 
00195   bool isNE(NRAttribute *attr) {
00196     return (!isEQ(attr));
00197   };
00198 
00199   bool isLT(NRAttribute *attr) {
00200     return (!isGE(attr));
00201   };
00202 
00203   bool isLE(NRAttribute *attr) {
00204     return (!isGT(attr));
00205   };
00206 
00207 protected:
00208 
00209   int32_t key_;
00210   int8_t  type_;
00211   int8_t  op_;
00212   int16_t len_;
00213   void *val_;
00214 };
00215 
00216 
00217 /*
00218  * NRSimpleAttribute<X> provide type-safe ways to accesss generic attributes
00219  * of type X.
00220  *
00221  * We specialize for strings and blobs to handle lengths properly.
00222  */
00223 
00224 template<class T>
00225 class NRSimpleAttribute : public NRAttribute{
00226 public:
00227   NRSimpleAttribute(int key, int type, int op, T val, int size = 0) :
00228     NRAttribute(key, type, op, sizeof(T)) {
00229 
00230     assert(type != STRING_TYPE && type != BLOB_TYPE);
00231     val_ = new T(val);
00232   }
00233 
00234   ~NRSimpleAttribute() {
00235     assert(type_ != STRING_TYPE && type_ != BLOB_TYPE);
00236     delete (T*) val_;
00237   };
00238 
00239   T getVal() { return *(T*)val_; };
00240   int getLen() { return len_; };
00241   void setVal(T value) { *(T *)val_ = value; };
00242 };
00243 
00244 // string specialization
00245 template <>
00246 class NRSimpleAttribute<char *>: public NRAttribute {
00247 public:
00248   NRSimpleAttribute(int key, int type, int op, char *val, int size = 0);
00249 
00250   ~NRSimpleAttribute() {
00251     assert(type_ == STRING_TYPE);
00252     delete [] (char *) val_;
00253   };
00254 
00255   char * getVal() { return (char *)val_; };
00256   int getLen() {return len_; };
00257   void setVal(char *value);
00258 };
00259 
00260 // blob specialization
00261 template <>
00262 class NRSimpleAttribute<void *>: public NRAttribute {
00263 public:
00264   NRSimpleAttribute(int key, int type, int op, void *val, int size);
00265 
00266   ~NRSimpleAttribute() {
00267     assert(type_ == BLOB_TYPE);
00268     delete [] (char *) val_;
00269   };
00270 
00271   void * getVal() { return (void *)val_; };
00272   int getLen() {return len_; };
00273   void setVal(void *value, int len);
00274 };
00275 
00276 /*
00277  * NRAttributeFactory and NRSimpleAttributeFactory
00278  * are used to define "factories" that make attributes of a given
00279  * key and type.
00280  *
00281  * NRAttributeFactory only for internal use.
00282  * (This class cannot be a template because of the static variables.)
00283  */
00284 class NRAttributeFactory {
00285 protected:
00286   int16_t key_;
00287   int8_t type_;
00288 
00289   NRAttributeFactory *next_;
00290   static NRAttributeFactory *first_;
00291 
00292   // Keep a list of all factories and verify that they don't conflict.
00293   static void verify_unique(NRAttributeFactory *baby);
00294 
00295   NRAttributeFactory(int key, int type) : key_(key), type_(type), next_(NULL) {};
00296 };
00297 
00298 /*
00299  * NRAttributeFactory for users
00300  */
00301 template<class T>
00302 class NRSimpleAttributeFactory : public NRAttributeFactory {
00303 public:
00304   NRSimpleAttributeFactory(int key, int type) : NRAttributeFactory(key, type) {
00305     verify_unique(this);
00306   }
00307   NRSimpleAttribute<T>* make(int op, T val, int size = -1) {
00308     return new NRSimpleAttribute<T>(key_, type_, op, val, size);
00309   };
00310   NRSimpleAttribute<T>* find(NRAttrVec *attrs,
00311                              NRAttrVec::iterator *place = NULL) {
00312     return (NRSimpleAttribute<T>*)NRAttribute::find_key_from(key_, attrs, attrs->begin(), place);
00313   };
00314 
00315   NRSimpleAttribute<T>* find_from(NRAttrVec *attrs,
00316                                   NRAttrVec::iterator start,
00317                                   NRAttrVec::iterator *place = NULL) {
00318     return (NRSimpleAttribute<T>*)NRAttribute::find_key_from(key_, attrs, start, place);
00319   };
00320   int getKey() { return key_; };
00321   int getType() { return type_; };
00322 };
00323 
00324 /*
00325  * Some pre-defined attribute factories.
00326  * There's no reason these can't also appear in user code.
00327  * (Not all factories need to be defined here.)
00328  */
00329 extern NRSimpleAttributeFactory<int> NRScopeAttr;
00330 extern NRSimpleAttributeFactory<int> NRClassAttr;
00331 extern NRSimpleAttributeFactory<int> NRAlgorithmAttr;
00332 extern NRSimpleAttributeFactory<int> NRSubscriptionAttr;
00333 extern NRSimpleAttributeFactory<void *> NRFlowAttr;
00334 extern NRSimpleAttributeFactory<float> LatitudeAttr;
00335 extern NRSimpleAttributeFactory<float> LongitudeAttr;
00336 extern NRSimpleAttributeFactory<char *> RouteAttr;
00337 extern NRSimpleAttributeFactory<char *> SourceRouteAttr;
00338 extern NRSimpleAttributeFactory<void *> ReinforcementAttr;
00339 
00340 /*
00341  * RMST related attribute factories (used by the rmst filter as well as
00342  * the rmst sample applications and the library api)
00343  */
00344 
00345 extern NRSimpleAttributeFactory<int> RmstIdAttr;
00346 extern NRSimpleAttributeFactory<int> RmstFragAttr;
00347 extern NRSimpleAttributeFactory<int> RmstMaxFragAttr;
00348 extern NRSimpleAttributeFactory<void *> RmstDataAttr;
00349 extern NRSimpleAttributeFactory<int> RmstTsprtCtlAttr;
00350 extern NRSimpleAttributeFactory<int> RmstPktsSentAttr;
00351 extern NRSimpleAttributeFactory<char *> RmstTargetAttr;
00352 
00353 /* Defines for RmstTsprtCtlAttr */
00354 #define RMST_RESP 1   // Normal fragment message
00355 #define NAK_REQ   2   // Request from sink for missing fragment
00356 #define ACK_RESP  3   // Ack Response (Blob complete) from sink or caching node
00357 #define RMST_CONT 4   // Flow control (temporary)
00358 #define EXP_REQ   5   // Request for new path
00359 
00360 
00361 #ifdef NS_DIFFUSION
00362 class DiffAppAgent;
00363 #endif // NS_DIFFUSION
00364 
00365 class NR {
00366 public:
00367   typedef long handle;
00368 
00369   virtual ~NR () {}
00370 
00371   class Callback {
00372   public:
00373     virtual ~Callback () {}
00374     virtual void recv(NRAttrVec *data, handle h) = 0;
00375   };
00376 
00377   // Factory to create an NR class specialized for ISI-W or MIT-LL's
00378   // implementation (whichever is compiled in).
00379 #ifdef NS_DIFFUSION
00380   static NR * create_ns_NR(u_int16_t port, DiffAppAgent *da);
00381 #else
00382   static NR * createNR(u_int16_t port = 0);
00383 #endif // NS_DIFFUSION
00384 
00385   virtual handle subscribe(NRAttrVec *interest_declarations,
00386                            NR::Callback * cb) = 0;
00387 
00388   virtual int unsubscribe(handle subscription_handle) = 0;
00389 
00390   virtual handle publish(NRAttrVec *publication_declarations) = 0;
00391 
00392   virtual int unpublish(handle publication_handle) = 0;
00393 
00394   virtual int send(handle publication_handle, NRAttrVec *) = 0;
00395 };
00396                             
00397 #endif // !_NR_HH_
00398 

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