00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 class NRAttribute {
00071 public:
00072 enum keys {
00073
00074
00075 SCOPE_KEY = 1001,
00076 CLASS_KEY = 1002,
00077
00078
00079 REINFORCEMENT_KEY = 1500,
00080 LATITUDE_KEY = 1501,
00081 LONGITUDE_KEY = 1502,
00082 ROUTE_KEY = 1503,
00083 SOURCE_ROUTE_KEY = 1504,
00084
00085
00086 DATABLOCK_KEY = 2001,
00087 TASK_FREQUENCY_KEY = 2002,
00088 TASK_NAME_KEY = 2003,
00089 TASK_QUERY_DETAIL_KEY = 2004,
00090 TARGET_KEY = 2005,
00091 TARGET_RANGE_KEY = 2006,
00092 CONFIDENCE_KEY = 2007
00093
00094
00095
00096 };
00097
00098
00099
00100 enum classes { INTEREST_CLASS = 10010, DISINTEREST_CLASS, DATA_CLASS };
00101 enum scopes { NODE_LOCAL_SCOPE = 11010, GLOBAL_SCOPE };
00102
00103
00104 enum types { INT32_TYPE,
00105 FLOAT32_TYPE,
00106 FLOAT64_TYPE,
00107 STRING_TYPE,
00108 BLOB_TYPE };
00109
00110
00111 enum operators { IS, LE, GE, LT, GT, EQ, NE, EQ_ANY };
00112
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
00181
00182
00183
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
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
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
00238
00239
00240
00241
00242
00243
00244 class NRAttributeFactory {
00245 protected:
00246 int16_t key_;
00247 int8_t type_;
00248
00249 NRAttributeFactory *next_;
00250 static NRAttributeFactory *first_;
00251
00252
00253 static void verify_unique(NRAttributeFactory *baby);
00254
00255 NRAttributeFactory(int key, int type) : key_(key), type_(type), next_(NULL) {};
00256 };
00257
00258
00259
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
00286
00287
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
00310
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