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
00045
00046
00047
00048
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
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();
00156 int getAgentId(int id = -1);
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
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
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
00198 handle addTimer(int timeout, TimerCallback *callback);
00199
00200
00201
00202 handle addTimer(int timeout, void *param, TimerCallbacks *cb);
00203
00204 bool removeTimer(handle hdl);
00205
00206
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
00217 protected:
00218 #endif // !NS_DIFFUSION
00219 void recvPacket(DiffPacket pkt);
00220 void recvMessage(Message *msg);
00221 #ifdef NS_DIFFUSION
00222
00223 protected:
00224
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
00248 Int2RecRmst rec_rmst_map_;
00249
00250
00251 int next_handle_;
00252 HandleList pub_list_;
00253 HandleList sub_list_;
00254 FilterList filter_list_;
00255
00256
00257 pthread_mutex_t *dr_mtx_;
00258
00259
00260 TimerManager *timers_manager_;
00261
00262
00263 DeviceList in_devices_;
00264 DeviceList local_out_devices_;
00265
00266
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_