DESERT 3.5.1
Loading...
Searching...
No Matches
uwPosBasedRtROV.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET \‍) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include "uwPosBasedRtROV.h"
40#include <iostream>
41
42extern packet_t PT_UWPOSITIONBASEDROUTING;
43
44
45static class UwPosBasedRtROVModuleClass : public TclClass
46{
47public:
49 : TclClass("Module/UW/PosBasedRt/ROV")
50 {
51 }
52
53 TclObject *
54 create(int, const char *const *)
55 {
56 return (new UwPosBasedRtROV());
57 }
59
60
61
63 : ipAddr(0)
64 , maxTxRange(3000)
65 , ROV_pos()
66 , list_posIP()
67{
68 bind("debug_", &debug_);
69 bind("maxTxRange_",(double *) &maxTxRange);
70 if (maxTxRange <= 0) {
71 std::cout << " UwPosBasedRtROV::maxTxRange value not valid, value set "
72 << " by default to 3000 m" << std::endl;
73 maxTxRange = 3000;
74 }
75}
76
80
81
82
83int UwPosBasedRtROV::command(int argc, const char *const *argv)
84{
85 Tcl &tcl = Tcl::instance();
86
87 if(argc == 3) {
88 if (strcasecmp(argv[1],"setMaxTxRange") == 0) {
89 if (atof(argv[2]) <= 0) {
90 std::cerr << " UwPosBasedRtROV(IP=" <<(int)ipAddr
91 << ")::invalid value for max transmission range, "
92 << "use a value >= 0";
93 return TCL_ERROR;
94 }
95 setMaxTxRange(atof(argv[2]));
96 return TCL_OK;
97 }
98 if (strcasecmp(argv[1], "addr") == 0) {
99 ipAddr = static_cast<uint8_t>(atoi(argv[2]));
100 if (ipAddr == 0) {
101 std::cerr << "UwPosBasedRtROV::0 is not a valid IP address"
102 << std::endl;
103 return TCL_ERROR;
104 }
105 return TCL_OK;
106 }
107 if (strcasecmp(argv[1],"setROVPosition") == 0) {
108 UWSMPosition *p = dynamic_cast<UWSMPosition*> (tcl.lookup(argv[2]));
109 ROV_pos = p;
110 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
111 << ")::ROV position, x: " <<ROV_pos->getX() << ", y: "
112 << ROV_pos->getY()<< ", z: " <<ROV_pos->getZ() << std::endl;
113 return TCL_OK;
114 }
115 } else if (argc == 4) {
116 if (strcasecmp(argv[1], "addPosition_IPotherNodes") == 0) {
117 //add node position with its IP address
118 Position *p = dynamic_cast<Position*> (tcl.lookup(argv[2]));
119 uint8_t ip = static_cast<uint8_t>(atoi(argv[3]));
120 if (ip == 0) {
121 std::cerr << "UwPosBasedRtROV::0 is not a valid IP address"
122 << std::endl;
123 return TCL_ERROR;
124 }
125 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
126 << ")::add node position, x:" << p->getX() << " y: "<< p->getY()
127 << " z: " << p->getZ() << " with IP " << (int)ip << std::endl;
128 pair_posIP provv = std::pair<Position,uint8_t>(*p,ip);
129 list_posIP.push_back(provv);
130
131 return TCL_OK;
132 }
133 }
134 return Module::command(argc, argv);
135}
136
137
138void UwPosBasedRtROV::recv(Packet* p)
139{
140
141 if (ipAddr == 0) {
142 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
143 << ")::node IP addres not set, drop packet" << std::endl;
144 drop(p, 1, DROP_IP_NOT_SET);
145 }
146
147 hdr_cmn *ch = HDR_CMN(p);
148 hdr_uwip *iph = HDR_UWIP(p);
149
150 if (ch->direction() == hdr_cmn::UP) {
151
152 if (iph->daddr() == ipAddr) {//I'm the intended node
153
154 sendUp(p);
155 return;
156 } else { //find next hop
157 ch->next_hop() = (nsaddr_t)findNextHop(p);
158 if (ch->next_hop() == 0) {
159 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
160 << ")::next hop not found, drop packet" << std::endl;
161 drop(p, 1, DROP_NEXT_HOP_NOT_FOUND);
162 }
163 if (debug_) {
164 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
165 << ")::next hop: " << (int)ch->next_hop() << std::endl;
166 }
167 sendDown(p);
168 return;
169 }
170
171 } else {//DOWN
172
173 initPkt(p);
174
175 ch->next_hop() = (nsaddr_t)findNextHop(p);
176 if (ch->next_hop() == 0) {
177 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
178 << ")::next hop not found, drop packet" << std::endl;
179 drop(p, 1, DROP_NEXT_HOP_NOT_FOUND);
180 }
181 if (debug_) {
182 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
183 << ")::next hop: " << (int)ch->next_hop() << std::endl;
184 }
185 sendDown(p);
186 return;
187 }
188}
189
191{
192 if (debug_)
193 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
194 << ")::initPkt, init header fields" << std::endl;
195
197
198
199 pbrh->x_ROV() = ROV_pos->getX();
200 pbrh->y_ROV() = ROV_pos->getY();
201 pbrh->z_ROV() = ROV_pos->getZ();
202 pbrh->x_waypoint() = ROV_pos->getXdest();
203 pbrh->y_waypoint() = ROV_pos->getYdest();
204 pbrh->z_waypoint() = ROV_pos->getZdest();
205 pbrh->timestamp() = NOW;
206 pbrh->ROV_speed() = ROV_pos->getSpeed();
207 pbrh->IP_ROV() = ipAddr;
208
209 if (debug_) {
210 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
211 << ")::initPkt, init header fields" << std::endl;
212 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
213 << ")::Inserted information"
214 << ", x waypoint: " << pbrh->x_waypoint()
215 << " ,y waypoint: " << pbrh->y_waypoint()
216 << " ,z waypoint: " << pbrh->z_waypoint()
217 << " ,x ROV: " << pbrh->x_ROV()
218 << " ,y ROV: " << pbrh->y_ROV()
219 << " ,z ROV: " << pbrh->z_ROV()
220 << " ,timestamp: " << pbrh->timestamp()
221 << " ,ROV speed: " << pbrh->ROV_speed() << std::endl;
222 }
223
224}
225
226
227uint8_t UwPosBasedRtROV::findNextHop(const Packet* p)
228{
229 if (list_posIP.empty()) {
230 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
231 << ")::List of position not setted" << std::endl;
232 return 0;
233 } else {
234 double minDist = 10000000;
235 double tempDist;
236 uint8_t ipCloserNode;
237 for (std::list<pair_posIP>::iterator it=list_posIP.begin();
238 it != list_posIP.end(); ++it) {
239 tempDist = nodesDistance(it->first, *ROV_pos);
240 if (tempDist < minDist) {
241 minDist = tempDist;
242 ipCloserNode = it->second;
243 }
244 }
245 if (minDist < maxTxRange) {
246 if (debug_)
247 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
248 << ")::findNextHop,the closest node in the tx range "
249 << "has IP equal to " << (int)ipCloserNode << std::endl;
250 return ipCloserNode;
251 } else {
252 return (uint8_t)0;
253 }
254 }
255}
256
257
258double UwPosBasedRtROV::nodesDistance(Position& p1, Position& p2)
259{
260 double x1 = p1.getX();
261 double y1 = p1.getY();
262 double z1 = p1.getZ();
263 double x2 = p2.getX();
264 double y2 = p2.getY();
265 double z2 = p2.getZ();
266
267 return sqrt(pow(x2-x1,2) + pow(y2-y1,2) + pow(z2-z1,2));
268}
269
270
272{
273 if (newRange == 0) {
274 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
275 << ")::value for max transmission range not valid" << std::endl;
276 return;
277 }
278 maxTxRange = newRange;
279 if (debug_)
280 std::cout << NOW << " UwPosBasedRtROV(IP=" <<(int)ipAddr
281 << ")::max transmission range set to " << maxTxRange << std::endl;
282}
virtual double getY()
Method that return the current projection of the node on the y-axis.
virtual double getX()
Method that return the current projection of the node on the x-axis.
double getSpeed() const
Method that return the actual speed.
virtual double getZdest() const
Method that return the z cooridnate of the destination point.
virtual double getYdest() const
Method that return the y cooridnate of the destination point.
virtual double getZ()
Method that return the current projection of the node on the z-axis.
virtual double getXdest() const
Method that return the x cooridnate of the destination point.
TclObject * create(int, const char *const *)
virtual void initPkt(Packet *p)
Initialize field of hdr_uwpos_based_rt
double maxTxRange
Maximum transmission range, in meters, for this node.
std::pair< Position, uint8_t > pair_posIP
UWSMPosition * ROV_pos
Pointer to ROV position.
virtual uint8_t findNextHop(const Packet *p)
Find next hop of a packet passed as input.
virtual double nodesDistance(Position &p1, Position &p2)
Compute absoulute distance between 2 nodes.
virtual int command(int, const char *const *)
TCL command interpreter.
virtual void setMaxTxRange(double newRange)
Set maximum transmission range.
int debug_
Flag to enable or disable dirrefent levels of debug.
virtual ~UwPosBasedRtROV()
Destructor of UwPosBasedRtROV class.
UwPosBasedRtROV()
Constructor of UwPosBasedRtROV class.
std::list< pair_posIP > list_posIP
List with position of all the other nodes with its IP.
virtual void recv(Packet *p)
Performs the reception of packets from upper and lower layers.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
hdr_uwpos_based_rt describes packets used by UWPOSBASEDRT.
#define HDR_UWPOS_BASED_RT(p)
packet_t PT_UWPOSITIONBASEDROUTING
UwPosBasedRtROVModuleClass class_module_uwposbasedrt_rov
Routing protocol for vehicles based on ROV position.
#define DROP_NEXT_HOP_NOT_FOUND
#define DROP_IP_NOT_SET
#define HDR_UWIP(P)
Definition uwip-module.h:58