DESERT 3.5.1
Loading...
Searching...
No Matches
uwrov-module.cc
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 lab) 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
38#include "uwrov-module.h"
39#include <iostream>
40#include <rng.h>
41#include <stdint.h>
42extern packet_t PT_UWCBR;
43extern packet_t PT_UWROV;
44extern packet_t PT_UWROV_CTR;
60static class UwROVModuleClass : public TclClass {
61public:
62
66 UwROVModuleClass() : TclClass("Module/UW/ROV") {
67 }
68
73 TclObject* create(int, const char*const*) {
74 return (new UwROVModule());
75 }
77
79 module->sendAck();
80}
81
83 : UwCbrModule()
84 , last_sn_confirmed(0)
85 , ack(0)
86 , ackPolicy(ACK_PIGGYBACK)
87 , ackTimeout(10)
88 , ackTimer_(this)
89 , ackPriority(0)
90 , ackNotPgbk(0)
91 , drop_old_waypoints(0)
92 , log_flag(0)
93 , out_file_stats(0)
94{
96 posit=&p;
97 bind("ackTimeout_", (int*) &ackTimeout);
98 bind("ackPriority_", (int*) &ackPriority);
99 bind("drop_old_waypoints_", (int*) &drop_old_waypoints);
100 bind("log_flag_", (int*) &log_flag );
101 if (ackTimeout < 0) {
102 cerr << NOW << " Invalide ACK timout < 0, timeout set to 10 by defaults"
103 << std::endl;
104 ackTimeout = 10;
105 }
106
107}
108
110 : UwCbrModule()
111 , last_sn_confirmed(0)
112 , ack(0)
113 , ackPolicy(ACK_PIGGYBACK)
114 , ackTimeout(10)
115 , ackTimer_(this)
116 , ackPriority(0)
117 , ackNotPgbk(0)
118 , drop_old_waypoints(0)
119 , log_flag(0)
120 , out_file_stats(0)
121{
122 posit = p;
123 bind("ackTimeout_", (int*) &ackTimeout);
124 bind("ackPriority_", (int*) &ackPriority);
125 bind("drop_old_waypoints_", (int*) &drop_old_waypoints);
126 bind("log_flag_", (int*) &log_flag );
127 if (ackTimeout < 0) {
128 cerr << NOW << " Invalide ACK timout < 0, timeout set to 10 by defaults"
129 << std::endl;
130 ackTimeout = 10;
131 }
132
133
134}
135
137
141
142int UwROVModule::command(int argc, const char*const* argv) {
143 Tcl& tcl = Tcl::instance();
144 if(argc == 2){
145 if (strcasecmp(argv[1], "getROVMonheadersize") == 0) {
146 tcl.resultf("%d", getROVMonHeaderSize());
147 return TCL_OK;
148 }
149 else if(strcasecmp(argv[1], "getROVctrheadersize") == 0) {
150 tcl.resultf("%d", getROVCTRHeaderSize());
151 return TCL_OK;
152 }
153 else if(strcasecmp(argv[1], "getX") == 0) {
154 tcl.resultf("%f", posit->getX());
155 return TCL_OK;
156 }
157 else if(strcasecmp(argv[1], "getY") == 0) {
158 tcl.resultf("%f", posit->getY());
159 return TCL_OK;
160 }
161 else if(strcasecmp(argv[1], "getZ") == 0) {
162 tcl.resultf("%f", posit->getZ());
163 return TCL_OK;
164 }
165 else if(strcasecmp(argv[1], "getAckNotPgbk") == 0) {
166 tcl.resultf("%d", ackNotPgbk);
167 return TCL_OK;
168 }
169 }
170 else if(argc == 3){
171 if (strcasecmp(argv[1], "setPosition") == 0) {
172 UWSMPosition* p = dynamic_cast<UWSMPosition*> (tcl.lookup(argv[2]));
173 posit=p;
174 tcl.resultf("%s", "position Setted\n");
175 return TCL_OK;
176 }
177 if (strcasecmp(argv[1], "setAckPolicy") == 0) {
178 if (atof(argv[2]) == 1) {
180 return TCL_OK;
181 }
182 if (atof(argv[2]) == 2) {
184 return TCL_OK;
185 }
186 if (atof(argv[2]) == 3) {
188 return TCL_OK;
189 }
190 }
191 if (strcasecmp(argv[1], "setAckTimeout") == 0) {
192 ackTimeout = atof(argv[2]);
193 return TCL_OK;
194 }
195 if (strcasecmp(argv[1], "setAckPriority") == 0) {
196 ackPriority = atof(argv[2]);
197 return TCL_OK;
198 }
199 }
200 else if(argc == 5){
201 if (strcasecmp(argv[1], "setdest") == 0) {
202 posit->setdest(atof(argv[2]),atof(argv[3]),atof(argv[4]));
203 return TCL_OK;
204 }
205 }
206 else if(argc == 6){
207 if (strcasecmp(argv[1], "setdest") == 0) {
208 posit->setdest(atof(argv[2]),atof(argv[3]),atof(argv[4]),atof(argv[5]));
209 return TCL_OK;
210 }
211 }
212 return UwCbrModule::command(argc,argv);
213}
214
215void UwROVModule::initPkt(Packet* p) {
217 hdr_uwcbr *uwcbrh = HDR_UWCBR(p);
218 uwROVh->x() = posit->getX();
219 uwROVh->y() = posit->getY();
220 uwROVh->z() = posit->getZ();
221 uwROVh->ack() = ack;
222
223 ack=0;
224
225 if (debug_)
226 std::cout << NOW << " UwROVModule::initPkt(Packet *p) ROV current "
227 << "position: X = " << uwROVh->x() << ", Y = " << uwROVh->y()
228 << ", Z = " << uwROVh->z()<< std::endl;
229 ackTimer_.force_cancel();
231 if (debug_) {
232 std::cout << NOW << " UwROVModule::sending packet with priority "
233 << (int)uwcbrh->priority() << std::endl;
234 }
235 priority_ = 0;
236}
237
238void UwROVModule::recv(Packet* p, Handler* h) {
239 recv(p);
240}
241
242void UwROVModule::recv(Packet* p) {
243
244 hdr_uwROV_ctr* uwROVh = HDR_UWROV_CTR(p);
245
246 if (drop_old_waypoints == 1 && uwROVh->sn() <= last_sn_confirmed) { //obsolete packets
247 if (debug_) {
248 std::cout << NOW << " UwROVModule::old waypoint with sn "
249 << uwROVh->sn() << " dropped " << std::endl;
250 }
251
252 } else { //packet in order
253 posit->setdest(uwROVh->x(),uwROVh->y(),uwROVh->z(),uwROVh->speed());
254 last_sn_confirmed = uwROVh->sn();
255 }
256
258 priority_ = (char) ackPriority;
259
260 if (log_flag == 1) {
261 out_file_stats.open("my_log_file.csv",std::ios_base::app);
262 out_file_stats << left << "time: " << NOW << ", positions ROV: x = "
263 << posit->getX() << ", y = " << posit->getY()
264 << ", z = " << posit->getZ() << std::endl;
265 out_file_stats.close();
266 }
267
268
269 if (debug_) {
270 std::cout << NOW << " UwROVModule::recv(Packet *p) ROV received new "
271 "way point: X = " << uwROVh->x() << ", Y = " << uwROVh->y()
272 << ", Z = " << uwROVh->z()<< std::endl;
273 }
275 if (ackPolicy == ACK_IMMEDIATELY) {
276
277 if (ackPriority == 0) {
279 if (debug_)
280 cout << NOW << " ACK sent immediately with standard priority "
281 << std::endl;
282 } else {
284 if (debug_)
285 cout << NOW << " ACK sent immediately with high priority "
286 << std::endl;
287 }
288 }
289
290 if (ackPolicy == ACK_PGBK_OR_TO) {
291 ackTimer_.resched(ackTimeout);
292 }
293}
294
296 ackNotPgbk++;
297 if (ackPriority == 0) {
299 if (debug_)
300 cout << NOW << " ACK timeout expired, ACK sent with standard "
301 << "priority " << std::endl;
302 } else {
304 if (debug_)
305 cout << NOW << " ACK timeout expired, ACK sent with high priority "
306 << std::endl;
307 }
308}
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.
virtual double getZ()
Method that return the current projection of the node on the z-axis.
virtual void setdest(double x_dest, double y_dest, double z_dest, double spead)
UwCbrModule class is used to manage UWCBR packets and to collect statistics about them.
virtual void initPkt(Packet *p)
Initializes a data packet passed as argument with the default values.
int debug_
Flag to enable several levels of debug.
char priority_
Priority of the data packets.
virtual void sendPktHighPriority()
Allocates, initialize and sends a packet with the default priority flag set from tcl.
virtual void recv(Packet *)
Performs the reception of packets from upper and lower layers.
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
virtual void sendPkt()
Allocates, initialize and sends a packet with the default priority flag set from tcl.
Adds the header for hdr_uwROV packets in ns2.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
UwROVModuleClass()
Constructor of the class.
UwROVModule class is used to manage UWROV packets and to collect statistics about them.
virtual void recv(Packet *)
Performs the reception of packets from upper and lower layers.
virtual void setPosition(UWSMPosition *p)
Sets the position of the ROV.
int drop_old_waypoints
< Number of ACK not sent in piggyback when ackPolicy = 2.
virtual ~UwROVModule()
Destructor of UwROVModule class.
int ackNotPgbk
< Flag to give higher priority to ACK or not.
int ack
If not zero, contains the ACK to the last command Packete received.
virtual void sendAck()
Sends ACK if ackTimeout expire;.
UwROVModule()
Default Constructor of UwROVModule class.
static int getROVMonHeaderSize()
Returns the size in byte of a hdr_uwROV_monitoring packet header.
int ackTimeout
Timeout after which ACK is sent if ackPolicy = ACK_PGBK_OR_TO.
UWSMPosition * posit
ROV position.
UWROV_ACK_POLICY ackPolicy
Flag to set the policy for ACK transimission, ACK_PIGGYBACK: ACK is always sent in piggyback,...
std::ofstream out_file_stats
Output stream for the textual file of debug.
virtual void initPkt(Packet *p)
Initializes a monitoring data packet passed as argument with the default values.
UwROVSendAckTimer ackTimer_
Timer to schedule ACK transmission.
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
int last_sn_confirmed
Sequence number of the last command Packete received.
int log_flag
< Flag set to 1 to drop waypoints with sequence number lower or equal than last_sn_confirmed.
static int getROVCTRHeaderSize()
Returns the size in byte of a hdr_uwROV_ctr packet header.
virtual void expire(Event *e)
hdr_uwROV_ctr describes UWROV_ctr packets for controlling the ROV.
float & z()
static int offset_
Required by the PacketHeaderManager.
double & sn()
float & speed()
float & x()
float & y()
hdr_uwROV_monitoring describes UWROV_monitoring packets sent by the ROV to the base station for monit...
static int offset_
Required by the PacketHeaderManager.
hdr_uwcbr describes UWCBR packets.
char & priority()
Reference to the priority_ variable.
#define HDR_UWCBR(p)
packet_t PT_UWROV
UwROVModuleClass class_module_uwROV
packet_t PT_UWROV_CTR
Provides the definition of the class UWROV.
#define HDR_UWROV_MONITORING(p)
#define HDR_UWROV_CTR(p)