00001 #ifndef _INTERF_H__
00002 #define _INTERF_H__
00003
00004 #include <map>
00005 #include <vector>
00006 #include <cstring>
00007
00008 #include <awds/routing.h>
00009 #include <awds/FlowRouting.h>
00010 #include <awds/basic.h>
00011
00012 #include <gea/API.h>
00013
00014 #include <gea/Time.h>
00015 #include <gea/Blocker.h>
00016
00017 #include <awds/AbstractId.h>
00018 #include <awds/NodeDescr.h>
00019
00020 #include <awds/NodeId.h>
00021 #include <awds/beacon.h>
00022 #include <awds/settings.h>
00023
00024 #include <awds/Firewall.h>
00025
00026 namespace awds {
00027
00028
00029
00035 class AwdsRouting : public FlowRouting {
00036
00037 public:
00038 bool verbose;
00040 basic * base;
00041
00042
00043 static const int period = BEACON_INTERVAL;
00045 int topoPeriod_ms;
00047 enum PeriodType {
00048 Constant,
00049 Adaptive
00050 };
00051 PeriodType topoPeriodType;
00053
00054
00055
00056 gea::Blocker beaconBlocker;
00057 gea::Blocker blocker;
00058 class RTopology *topology;
00059 class FloodHistory *floodHistory;
00061 class awds::Firewall *firewall;
00063 AwdsRouting(basic *base);
00064
00066 virtual ~AwdsRouting();
00067
00069 virtual size_t getMTU();
00070
00071 static void send_beacon(gea::Handle *h, gea::AbsTime t, void *data);
00072 static void recv_packet(BasePacket *p, void *data);
00073
00074 static void send_periodic_topo(gea::Handle *h, gea::AbsTime t, void *data);
00075
00077 virtual void send_topo();
00078
00084 virtual std::string getNameOfNode(const awds::NodeId& id) const;
00085
00092 virtual bool getNodeByName(awds::NodeId& id, const char *name) const;
00093
00094 virtual int foreachNode(awds::Routing::NodeFunctor, void *data) const;
00095 virtual int foreachEdge(awds::Routing::EdgeFunctor, void *data) const;
00096
00097 virtual void addNodeObserver(struct awds::Routing::NodesObserver *observer);
00098 virtual void addLinkObserver(struct LinksObserver *observer);
00099
00100 void recv_beacon(BasePacket *p);
00101 void recv_flood(BasePacket *p);
00102 void recv_unicast(BasePacket *p);
00103
00104 virtual bool isReachable(const NodeId& id) const;
00105
00106 virtual BasePacket *newFloodPacket(int floodType);
00107 virtual BasePacket *newUnicastPacket(int type);
00108
00109 virtual void sendBroadcast(BasePacket *p);
00110 virtual void sendUnicast(BasePacket *p);
00111 virtual void sendUnicastVia(BasePacket *p,NodeId nextHop);
00112
00113 struct Hop2RefCount {
00114 short stat;
00115 short dyn;
00116 Hop2RefCount() {}
00117 Hop2RefCount(short stat) : stat(stat) {}
00118 };
00119
00120 typedef std::map<NodeId, Hop2RefCount> Hop2List;
00121 Hop2List hop2list;
00122
00123
00124 typedef std::pair<recv_callback, void*> RegisterEntry;
00125 typedef std::vector<RegisterEntry> RegisterList;
00126 typedef std::map<int, RegisterList > ProtocolRegister;
00127
00128 ProtocolRegister unicastRegister;
00129 ProtocolRegister broadcastRegister;
00130
00131 virtual void registerUnicastProtocol(int num, recv_callback cb, void* data);
00132 virtual void registerBroadcastProtocol(int num, recv_callback cb, void* data);
00133
00134
00135 static const int MaxNeighbors = 40;
00136 NodeDescr *neighbors;
00137 int numNeigh;
00138
00139 gea::Blocker linkFailBlocker;
00140
00144 static void checkLinkFailure(gea::Handle *h, gea::AbsTime t, void *data);
00145
00146 u_int16_t beaconSeq;
00147 gea::Duration beaconPeriod;
00148 gea::AbsTime nextBeacon;
00150 u_int16_t floodSeq;
00151 u_int16_t unicastSeq;
00153 int findNeigh(const NodeId& id) const {
00154
00155
00156 int a = 0;
00157 int b = numNeigh;
00158
00159 while (a != b) {
00160 int m = (a+b)/2;
00161 if (neighbors[m].id == id) return m;
00162 if (neighbors[m].id < id)
00163 a = m+1;
00164 else if (neighbors[m].id > id)
00165 b = m;
00166
00167 }
00168 return -a - 1;
00169
00170 }
00171
00172 bool hasNeigh(const NodeId& id) const {
00173 return findNeigh(id) >= 0;
00174 }
00175
00176 void removeOldNeigh();
00177
00178 bool refreshNeigh(BasePacket *p);
00179
00180 void stat2dyn();
00181
00182 void assert_stat();
00183
00184 void calcMpr();
00185
00186
00187
00188 struct FlowReceiverData {
00189 FlowRouting::FlowReceiver receiver;
00190 void *data;
00191 };
00192
00193 typedef std::map<FlowRouting::FlowId, struct FlowReceiverData> FlowReceiverMap;
00194 FlowReceiverMap flowReceiverMap;
00195
00196 typedef std::map<FlowRouting::FlowId, NodeId> ForwardingTable;
00197 ForwardingTable forwardingTable;
00198
00199 virtual int addForwardingRule(FlowRouting::FlowId flowid, NodeId nextHop);
00200 virtual int delForwardingRule(FlowRouting::FlowId flowid);
00201
00202
00203 virtual int addFlowReceiver(FlowRouting::FlowId flowid, FlowRouting::FlowReceiver, void *data);
00204 virtual int delFlowReceiver(FlowRouting::FlowId);
00205
00206 virtual BasePacket *newFlowPacket(FlowRouting::FlowId flowid);
00207 virtual int sendFlowPacket(BasePacket *p);
00208
00209
00210 };
00211
00212 }
00213
00214 #endif //INTERF_H__
00215
00216
00217
00218
00219
00220