00001 #ifndef _INTERF_H__
00002 #define _INTERF_H__
00003
00004 #include <map>
00005 #include <cstring>
00006
00007 #include <awds/routing.h>
00008 #include <awds/FlowRouting.h>
00009 #include <awds/basic.h>
00010
00011 #include <gea/API.h>
00012 #include <gea/UdpHandle.h>
00013 #include <gea/Time.h>
00014 #include <gea/Blocker.h>
00015
00016 #include <awds/AbstractId.h>
00017 #include <awds/NodeDescr.h>
00018
00019 #include <awds/NodeId.h>
00020 #include <awds/beacon.h>
00021 #include <awds/settings.h>
00022 #include <awds/RateMonitor.h>
00023
00024 namespace awds {
00025
00031 class AwdsRouting : public FlowRouting {
00032
00033 public:
00034 bool verbose;
00036 basic * base;
00037
00038 static const int UdpPort = 4921;
00039 static const int period = BEACON_INTERVAL;
00040 int topoPeriod;
00042 gea::Handle *udpSend;
00043 gea::Handle *udpRecv;
00044 gea::Blocker blocker;
00045 class RTopology * topology;
00046 class FloodHistory *floodHistory;
00048
00049
00050
00051 AwdsRouting(basic *base);
00052 virtual ~AwdsRouting();
00053
00054 virtual size_t getMTU();
00055
00056
00057 static void send_beacon(gea::Handle *h, gea::AbsTime t, void *data);
00058 static void recv_packet(gea::Handle *h, gea::AbsTime t, void *data);
00059
00060 static void trigger_topo(gea::Handle *h, gea::AbsTime t, void *data);
00061
00062
00063 static void repeat_flood(gea::Handle *h, gea::AbsTime t, void *data);
00064 static void send_unicast(gea::Handle *h, gea::AbsTime t, void *data);
00065
00071 virtual std::string getNameOfNode(const awds::NodeId& id) const;
00072
00079 virtual bool getNodeByName(awds::NodeId& id, const char *name) const;
00080
00081 virtual int foreachNode(awds::Routing::NodeFunctor, void *data) const;
00082 virtual int foreachEdge(awds::Routing::EdgeFunctor, void *data) const;
00083
00084 virtual void addNodeObserver(struct awds::Routing::NodesObserver *observer);
00085 virtual void addLinkObserver(struct LinksObserver *observer);
00086
00087 void recv_beacon(BasePacket *p);
00088 void recv_flood(BasePacket *p);
00089 void recv_unicast(BasePacket *p);
00090
00091 virtual bool isReachable(const NodeId& id) const;
00092
00093 virtual BasePacket *newFloodPacket(int floodType);
00094 virtual BasePacket *newUnicastPacket(int type);
00095
00096 virtual void sendBroadcast(BasePacket *p);
00097 virtual void sendUnicast(BasePacket *p);
00098 virtual void sendUnicastVia(BasePacket *p,NodeId nextHop);
00099
00100 struct Hop2RefCount {
00101 short stat;
00102 short dyn;
00103 Hop2RefCount() {}
00104 Hop2RefCount(short stat) : stat(stat) {}
00105 };
00106
00107 typedef std::map<NodeId, Hop2RefCount> Hop2List;
00108 Hop2List hop2list;
00109
00110
00111 typedef std::pair<recv_callback, void*> RegisterEntry;
00112 typedef std::map<int, RegisterEntry > ProtocolRegister;
00113
00114 ProtocolRegister unicastRegister;
00115 ProtocolRegister broadcastRegister;
00116
00117 virtual void registerUnicastProtocol(int num, recv_callback cb, void* data);
00118 virtual void registerBroadcastProtocol(int num, recv_callback cb, void* data);
00119
00120
00121 static const int MaxNeighbors = 40;
00122 NodeDescr *neighbors;
00123 int numNeigh;
00124
00125 u_int16_t beaconSeq;
00126 gea::Duration beaconPeriod;
00127 gea::AbsTime nextBeacon;
00128
00129 u_int16_t floodSeq;
00130 u_int16_t unicastSeq;
00131
00132
00133 int findNeigh(const NodeId& id) const {
00134
00135
00136 int a = 0;
00137 int b = numNeigh;
00138
00139 while (a != b) {
00140 int m = (a+b)/2;
00141 if (neighbors[m].id == id) return m;
00142 if (neighbors[m].id < id)
00143 a = m+1;
00144 else if (neighbors[m].id > id)
00145 b = m;
00146
00147 }
00148 return -a - 1;
00149
00150 }
00151
00152 bool hasNeigh(const NodeId& id) const {
00153 return findNeigh(id) >= 0;
00154 }
00155
00156 void removeOldNeigh();
00157
00158 bool refreshNeigh(BasePacket *p);
00159
00160 void stat2dyn();
00161
00162 void assert_stat();
00163
00164 void calcMpr();
00165
00166
00167
00168 struct FlowReceiverData {
00169 FlowRouting::FlowReceiver receiver;
00170 void *data;
00171 };
00172
00173 typedef std::map<FlowRouting::FlowId, struct FlowReceiverData> FlowReceiverMap;
00174 FlowReceiverMap flowReceiverMap;
00175
00176 typedef std::map<FlowRouting::FlowId, NodeId> ForwardingTable;
00177 ForwardingTable forwardingTable;
00178
00179 virtual int addForwardingRule(FlowRouting::FlowId flowid, NodeId nextHop);
00180 virtual int delForwardingRule(FlowRouting::FlowId flowid);
00181
00182
00183 virtual int addFlowReceiver(FlowRouting::FlowId flowid, FlowRouting::FlowReceiver, void *data);
00184 virtual int delFlowReceiver(FlowRouting::FlowId);
00185
00186 virtual BasePacket *newFlowPacket(FlowRouting::FlowId flowid);
00187 virtual int sendFlowPacket(BasePacket *p);
00188
00189
00190 };
00191
00192 }
00193
00194
00195
00196 #endif //INTERF_H__
00197
00198
00199
00200
00201
00202