00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
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
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
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 class NRAttribute {
00094 public:
00095 enum keys {
00096
00097
00098 SCOPE_KEY = 1001,
00099 CLASS_KEY = 1002,
00100 ALGORITHM_KEY = 1003,
00101 SUBSCRIPTION_ID_KEY = 1004,
00102 FLOWS_KEY = 1005,
00103
00104
00105 REINFORCEMENT_KEY = 1500,
00106 LATITUDE_KEY = 1501,
00107 LONGITUDE_KEY = 1502,
00108 ROUTE_KEY = 1503,
00109 SOURCE_ROUTE_KEY = 1504,
00110
00111
00112 DATABLOCK_KEY = 2001,
00113 TASK_FREQUENCY_KEY = 2002,
00114 TASK_NAME_KEY = 2003,
00115 TASK_QUERY_DETAIL_KEY = 2004,
00116 TARGET_KEY = 2005,
00117 TARGET_RANGE_KEY = 2006,
00118 CONFIDENCE_KEY = 2007,
00119
00120
00121 RMST_DATA_KEY = 2010,
00122 RMST_ID_KEY = 2011,
00123 RMST_FRAG_KEY = 2012,
00124 RMST_MAX_FRAG_KEY = 2013,
00125 RMST_TSPRT_CTL_KEY = 2014,
00126 RMST_PKTS_SENT_KEY = 2015,
00127 RMST_TARGET_KEY = 2016
00128
00129
00130
00131 };
00132
00133
00134
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
00142 enum types { INT32_TYPE,
00143 FLOAT32_TYPE,
00144 FLOAT64_TYPE,
00145 STRING_TYPE,
00146 BLOB_TYPE };
00147
00148
00149 enum operators { IS, LE, GE, LT, GT, EQ, NE, EQ_ANY };
00150
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
00219
00220
00221
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
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
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
00278
00279
00280
00281
00282
00283
00284 class NRAttributeFactory {
00285 protected:
00286 int16_t key_;
00287 int8_t type_;
00288
00289 NRAttributeFactory *next_;
00290 static NRAttributeFactory *first_;
00291
00292
00293 static void verify_unique(NRAttributeFactory *baby);
00294
00295 NRAttributeFactory(int key, int type) : key_(key), type_(type), next_(NULL) {};
00296 };
00297
00298
00299
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
00326
00327
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
00342
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
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
00378
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