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

nr.hh

Go to the documentation of this file.
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-2002 by the University of Southern California
00007 // $Id: nr.hh,v 1.7 2003/01/22 06:20:21 xuanc 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 //
00023 
00024 #ifndef _NR_HH_
00025 #define _NR_HH_
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include "config.h"
00029 #endif // HAVE_CONFIG_H
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <assert.h>
00034 #include <vector>
00035 
00036 #ifdef NS_DIFFUSiON
00037 #include "config.h"
00038 #endif // NS_DIFFUSION
00039 
00040 using namespace std;
00041 
00042 typedef signed int int32_t;
00043 typedef signed short int16_t;
00044 #if defined (sparc) || defined (__CYGWIN__)
00045 typedef char int8_t;
00046 #else
00047 // Conflicts with system declaration of int8_t in Solaris and Cygwin
00048 typedef signed char int8_t;
00049 #endif // sparc/cygwin
00050 
00051 #define FAIL -1
00052 #define OK    0
00053 
00054 class NRAttribute;
00055 
00056 typedef vector<NRAttribute *> NRAttrVec;
00057 
00058 /*
00059  * xxx: gcc-2.91.66 doesn't handle member templates completely
00060  * (it worked for NRSimpleFactory<> but not for Blob and String
00061  * factories.  A work-around is to move all the templates out
00062  * to top-level code.
00063  * I'm told (by a gcc-developer) that this limitation is fixed
00064  * in gcc-2.95.2.
00065  */
00066 
00067 /*
00068  * NRAttribute encapsulates a single, generic attribute.
00069  */
00070 class NRAttribute {
00071 public:
00072   enum keys {
00073     // reserved constant values used for key
00074     // range 1000-1499 is diffusion-specific
00075     SCOPE_KEY = 1001,             // INT32_TYPE
00076     CLASS_KEY = 1002,             // INT32_TYPE
00077 
00078     // range 1500-1999 is reserved for system filters
00079     REINFORCEMENT_KEY = 1500,     // BLOB_TYPE
00080     LATITUDE_KEY = 1501,          // FLOAT_TYPE
00081     LONGITUDE_KEY = 1502,         // FLOAT_TYPE
00082     ROUTE_KEY = 1503,             // STRING_TYPE
00083     SOURCE_ROUTE_KEY = 1504,      // STRING_TYPE
00084 
00085     // range 2000-2999 is app specific
00086     DATABLOCK_KEY = 2001,         // BLOB_TYPE
00087     TASK_FREQUENCY_KEY = 2002,    // FLOAT_TYPE, in secs
00088     TASK_NAME_KEY = 2003,         // STRING_TYPE
00089     TASK_QUERY_DETAIL_KEY = 2004, // BLOB_TYPE
00090     TARGET_KEY = 2005,            // STRING_TYPE
00091     TARGET_RANGE_KEY = 2006,      // FLOAT_TYPE
00092     CONFIDENCE_KEY = 2007         // FLOAT_TYPE
00093 
00094     // range 3000-3999 is reserved for experimentation
00095     // and user-defined keys
00096   };
00097 
00098   // Values for diffusion-specific keys (start these at high values,
00099   // so we can do simple type checking)
00100   enum classes { INTEREST_CLASS = 10010, DISINTEREST_CLASS, DATA_CLASS };
00101   enum scopes { NODE_LOCAL_SCOPE = 11010, GLOBAL_SCOPE };
00102 
00103   // Key Type values
00104   enum types { INT32_TYPE,    // 32-bit signed integer
00105                FLOAT32_TYPE,  // 32-bit
00106                FLOAT64_TYPE,  // 64-bit
00107                STRING_TYPE,   // UTF-8 format, max length 1024 chars
00108                BLOB_TYPE };   // uninterpreted binary data
00109 
00110   // Match Operator values
00111   enum operators { IS, LE, GE, LT, GT, EQ, NE, EQ_ANY };
00112   // with EQ_ANY, the val is ignored
00113 
00114   NRAttribute();
00115   NRAttribute(int key, int type, int op, int len, void *val = NULL);
00116   NRAttribute(const NRAttribute &rhs);
00117   virtual ~NRAttribute();
00118 
00119   static NRAttribute * find_key(int key, NRAttrVec *attrs,
00120                                 NRAttrVec::iterator *place = NULL) {
00121 
00122     return find_key_from(key, attrs, attrs->begin(), place);
00123   };
00124 
00125   static NRAttribute * find_key_from(int key, NRAttrVec *attrs,
00126                                      NRAttrVec::iterator start,
00127                                      NRAttrVec::iterator *place = NULL);
00128 
00129   NRAttribute * find_matching_key_from(NRAttrVec *attrs,
00130                                        NRAttrVec::iterator start,
00131                                        NRAttrVec::iterator *place = NULL) {
00132     return find_key_from(key_, attrs, start, place);
00133   };
00134 
00135   NRAttribute * find_matching_key(NRAttrVec *attrs,
00136                                   NRAttrVec::iterator *place = NULL) {
00137     return find_key_from(key_, attrs, attrs->begin(), place);
00138   };
00139 
00140   int32_t getKey() { return key_; };
00141   int8_t getType() { return type_; };
00142   int8_t getOp() { return op_; };
00143   int16_t getLen() { return len_; };
00144 
00145   void * getGenericVal() { return val_; };
00146 
00147   bool isSameKey(NRAttribute *attr) {
00148     return ((type_ == attr->getType()) && (key_ == attr->getKey()));
00149   };
00150 
00151   bool isEQ(NRAttribute *attr);
00152 
00153   bool isGT(NRAttribute *attr);
00154 
00155   bool isGE(NRAttribute *attr);
00156 
00157   bool isNE(NRAttribute *attr) {
00158     return (!isEQ(attr));
00159   };
00160 
00161   bool isLT(NRAttribute *attr) {
00162     return (!isGE(attr));
00163   };
00164 
00165   bool isLE(NRAttribute *attr) {
00166     return (!isGT(attr));
00167   };
00168 
00169 protected:
00170 
00171   int32_t key_;
00172   int8_t  type_;
00173   int8_t  op_;
00174   int16_t len_;
00175   void *val_;
00176 };
00177 
00178 
00179 /*
00180  * NRSimpleAttribute<X> provide type-safe ways to accesss generic attributes
00181  * of type X.
00182  *
00183  * We specialize for strings and blobs to handle lengths properly.
00184  */
00185 
00186 template<class T>
00187 class NRSimpleAttribute : public NRAttribute{
00188 public:
00189   NRSimpleAttribute(int key, int type, int op, T val, int size = 0) :
00190     NRAttribute(key, type, op, sizeof(T)) {
00191 
00192     assert(type != STRING_TYPE && type != BLOB_TYPE);
00193     val_ = new T(val);
00194   }
00195 
00196   ~NRSimpleAttribute() {
00197     assert(type_ != STRING_TYPE && type_ != BLOB_TYPE);
00198     delete (T*) val_;
00199   };
00200 
00201   T getVal() { return *(T*)val_; };
00202   int getLen() { return len_; };
00203   void setVal(T value) { *(T *)val_ = value; };
00204 };
00205 
00206 // string specialization
00207 class NRSimpleAttribute<char *>: public NRAttribute {
00208 public:
00209   NRSimpleAttribute(int key, int type, int op, char *val, int size = 0);
00210 
00211   ~NRSimpleAttribute() {
00212     assert(type_ == STRING_TYPE);
00213     delete [] (char *) val_;
00214   };
00215 
00216   char * getVal() { return (char *)val_; };
00217   int getLen() {return len_; };
00218   void setVal(char *value);
00219 };
00220 
00221 // blob specialization
00222 class NRSimpleAttribute<void *>: public NRAttribute {
00223 public:
00224   NRSimpleAttribute(int key, int type, int op, void *val, int size);
00225 
00226   ~NRSimpleAttribute() {
00227     assert(type_ == BLOB_TYPE);
00228     delete [] (char *) val_;
00229   };
00230 
00231   void * getVal() { return (void *)val_; };
00232   int getLen() {return len_; };
00233   void setVal(void *value, int len);
00234 };
00235 
00236 /*
00237  * NRAttributeFactory and NRSimpleAttributeFactory
00238  * are used to define "factories" that make attributes of a given
00239  * key and type.
00240  *
00241  * NRAttributeFactory only for internal use.
00242  * (This class cannot be a template because of the static variables.)
00243  */
00244 class NRAttributeFactory {
00245 protected:
00246   int16_t key_;
00247   int8_t type_;
00248 
00249   NRAttributeFactory *next_;
00250   static NRAttributeFactory *first_;
00251 
00252   // Keep a list of all factories and verify that they don't conflict.
00253   static void verify_unique(NRAttributeFactory *baby);
00254 
00255   NRAttributeFactory(int key, int type) : key_(key), type_(type), next_(NULL) {};
00256 };
00257 
00258 /*
00259  * NRAttributeFactory for users
00260  */
00261 template<class T>
00262 class NRSimpleAttributeFactory : public NRAttributeFactory {
00263 public:
00264   NRSimpleAttributeFactory(int key, int type) : NRAttributeFactory(key, type) {
00265     verify_unique(this);
00266   }
00267   NRSimpleAttribute<T>* make(int op, T val, int size = -1) {
00268     return new NRSimpleAttribute<T>(key_, type_, op, val, size);
00269   };
00270   NRSimpleAttribute<T>* find(NRAttrVec *attrs,
00271                              NRAttrVec::iterator *place = NULL) {
00272     return (NRSimpleAttribute<T>*)NRAttribute::find_key_from(key_, attrs, attrs->begin(), place);
00273   };
00274 
00275   NRSimpleAttribute<T>* find_from(NRAttrVec *attrs,
00276                                   NRAttrVec::iterator start,
00277                                   NRAttrVec::iterator *place = NULL) {
00278     return (NRSimpleAttribute<T>*)NRAttribute::find_key_from(key_, attrs, start, place);
00279   };
00280   int getKey() { return key_; };
00281   int getType() { return type_; };
00282 };
00283 
00284 /*
00285  * Some pre-defined attribute factories.
00286  * There's no reason these can't also appear in user code.
00287  * (Not all factories need to be defined here.)
00288  */
00289 extern NRSimpleAttributeFactory<int> NRScopeAttr;
00290 extern NRSimpleAttributeFactory<int> NRClassAttr;
00291 extern NRSimpleAttributeFactory<float> LatitudeAttr;
00292 extern NRSimpleAttributeFactory<float> LongitudeAttr;
00293 extern NRSimpleAttributeFactory<char *> RouteAttr;
00294 extern NRSimpleAttributeFactory<char *> SourceRouteAttr;
00295 
00296 #ifdef NS_DIFFUSION
00297 class DiffAppAgent;
00298 #endif // NS_DIFFUSION
00299 
00300 class NR {
00301 public:
00302   typedef long handle;
00303 
00304   class Callback {
00305   public:
00306     virtual void recv(NRAttrVec *data, handle h) = 0;
00307   };
00308 
00309   // Factory to create an NR class specialized for ISI-W or MIT-LL's
00310   // implementation (whichever is compiled in).
00311 #ifdef NS_DIFFUSION
00312   static NR * create_ns_NR(u_int16_t port, DiffAppAgent *da);
00313 #else
00314   static NR * createNR(u_int16_t port = 0);
00315 #endif // NS_DIFFUSION
00316 
00317   virtual handle subscribe(NRAttrVec *interest_declarations,
00318                            NR::Callback * cb) = 0;
00319 
00320   virtual int unsubscribe(handle subscription_handle) = 0;
00321 
00322   virtual handle publish(NRAttrVec *publication_declarations) = 0;
00323 
00324   virtual int unpublish(handle publication_handle) = 0;
00325 
00326   virtual int send(handle publication_handle, NRAttrVec *) = 0;
00327 };
00328                             
00329 #endif // !_NR_HH_
00330 

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