DESERT 3.5.1
Loading...
Searching...
No Matches
uwtdma-frame.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 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//
29
39#include "uwtdma-frame.h"
40#include <iostream>
41#include <stdint.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <mac.h>
45#include <string>
46
47extern packet_t PT_UWHEALTHCBR;
48extern packet_t PT_UWCONTROLCBR;
52static class TDMA_FRAMEModuleClass : public TclClass
53{
54
55public:
60 : TclClass("Module/UW/TDMA_FRAME")
61 {
62 }
67 TclObject *
68 create(int, const char *const *)
69 {
70 return (new UwTDMA_frame());
71 }
72
74
75/*void UwTDMATimer::expire(Event *e)
76{
77 ((UwTDMA *)module)->changeStatus();
78}*/
79
81 : UwTDMA()
82 , tot_nodes(0)
83 , my_slots_counter(0)
84 , topology_S_file_name_("")
85 , topology_S_token_separator_(',')
86 , topology_index(0)
87{
88 fair_mode = 1;
89 {
90 bind("guard_time", (double *) &guard_time);
91 tot_slots = 0;
92 }
93}
94
98
99void
101{
105 if (debug_)
106 std::cout << NOW << " ID:" << addr
107 << ", my_slots_counter:" << my_slots_counter << std::endl;
108 }
111 int num_jumping_slots =
112 getNextMySlot()->first - getCurrentSlot()->first;
113 num_jumping_slots = num_jumping_slots > 0
114 ? num_jumping_slots
115 : num_jumping_slots + tot_slots;
116 double nextSlotTime =
117 (num_jumping_slots - 1) * slot_duration + guard_time;
118 tdma_timer.resched(nextSlotTime);
119
120 if (debug_ < -5)
121 std::cout << NOW << " Off ID " << addr << " " << nextSlotTime << ""
122 << std::endl;
123 if (sea_trial_)
124 out_file_stats << left << "[" << getEpoch() << "]::" << NOW
125 << "::TDMA_node(" << addr << ")::Off timeslot "
126 << getCurrentSlot()->first << std::endl;
127 } else
129}
130
131int
132UwTDMA_frame::command(int argc, const char *const *argv)
133{
134 Tcl &tcl = Tcl::instance();
135 if (argc == 2) {
136 if (strcasecmp(argv[1], "start") == 0) {
137 if (fair_mode == 0) {
138 std::cout << "Error: the near far tdma works only in fair mode"
139 << std::endl;
140 return TCL_ERROR;
141 }
142 if (tot_slots == 0) {
143 std::cout << "Error: number of slots set to 0, please, "
144 "initialize the topology"
145 << std::endl;
146 return TCL_ERROR;
147 } else {
149 if (slot_duration - guard_time < 0) {
150 std::cout << "Error: guard time or frame set incorrectly"
151 << std::endl;
152 return TCL_ERROR;
153 } else {
154 Slot::iterator iter = my_slot_numbers_.begin();
155 start_time = iter->first * slot_duration;
156 start(iter->first * slot_duration);
157 return TCL_OK;
158 }
159 }
160 } else if (strcasecmp(argv[1], "stop") == 0) {
161 stop();
162 return TCL_OK;
163 }
164 } else if (argc == 3) {
165 if (strcasecmp(argv[1], "setSlotNumber") == 0) {
166 std::cout << "Use near far topology!!" << std::endl;
167 return TCL_ERROR;
168 } else if (strcasecmp(argv[1], "setTopologyIndex") == 0) {
169 topology_index = atoi(argv[2]);
170 return TCL_OK;
171 } else if (strcasecmp(argv[1], "setSTopologyFileName") == 0) {
172 string tmp_ = ((char *) argv[2]);
173 if (tmp_.size() == 0) {
174 fprintf(stderr, "Empty string for the file name");
175 return TCL_ERROR;
176 }
179 return TCL_OK;
180 } else if (strcasecmp(argv[1], "setTopologySeparator") == 0) {
181 string tmp_ = ((char *) argv[2]);
182 if (tmp_.size() == 0) {
183 fprintf(stderr, "Empty char for the file name");
184 return TCL_ERROR;
185 }
186 topology_S_token_separator_ = tmp_.at(0);
187 return TCL_OK;
188 }
189 }
190 return UwTDMA::command(argc, argv);
191}
192
193Slot::iterator
195{
196 Slot::iterator iter = my_slot_numbers_.begin();
197 int pos = ((my_slots_counter - 1) % my_slot_numbers_.size());
198 // std::cout<< NOW << " ID:" << addr << " Slots SIZE:" <<
199 // my_slot_numbers_.size() << std::endl;
200 if (debug_ < -5)
201 std::cout << NOW << " ID:" << addr << " Slot Pos:" << pos << std::endl;
202 for (int i = 0; i < pos; i++) {
203 iter++;
204 }
205 return iter;
206}
207
208Slot::iterator
210{
211 Slot::iterator iter = my_slot_numbers_.begin();
212 int pos = ((my_slots_counter + skip) % my_slot_numbers_.size());
213 if (debug_ < -5)
214 std::cout << NOW << " ID:" << addr << " Pos:" << pos << std::endl;
215 for (int i = 0; i < pos; i++) {
216 iter++;
217 }
218 return iter;
219}
220
221void
223{
225 ifstream input_file_;
226 string line_;
227 char *tmp_ = new char[topology_S_file_name_.length() + 1];
228 strcpy(tmp_, topology_S_file_name_.c_str());
229 input_file_.open(tmp_);
230 int slot_number = 0;
231 if(tot_slots || tot_nodes) { //initialize the frame again due to frame change
232 tot_slots = 0;
233 tot_nodes = 0;
234 s_.clear();
235 my_slot_numbers_.clear();
236 }
237 if (input_file_.is_open()) {
238 while (std::getline(input_file_, line_)) {
239 ::std::stringstream line_stream(line_);
240 string result_;
241 slot_number = 0;
242 while (std::getline(
243 line_stream, result_, topology_S_token_separator_)) {
244 slot_number++;
245 int tx_status = atoi(result_.c_str());
246 s_[tot_nodes + 1][slot_number] = tx_status;
247 if (tot_nodes + 1 == topology_index && tx_status > 0) {
248 // cout << "Node " <<addr << "S[" << slot_number << "] = "
249 // << tx_status <<endl;
250 my_slot_numbers_[slot_number] = tx_status;
251 }
252 if (!tot_nodes) {
253 tot_slots++;
254 }
255 }
256 tot_nodes++;
257 }
258 // std::cout << NOW << " ID " << addr << " num slots: "
259 // <<my_slot_numbers_.size() << std::endl;
260 } else {
261 cerr << "Impossible to open file " << topology_S_file_name_.c_str() <<
262 endl;
263 }
264 if (debug_) {
265 std::cout << NOW << " ID " << addr
266 << ": Topology S initialized, tot_nodes = " << tot_nodes
267 << ", Slots in a frame = " << tot_slots << std::endl;
268 }
269}
Class that represent the binding of the protocol with tcl.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
TDMA_FRAMEModuleClass()
Constructor of the TDMAGenericModule class.
Class that represents a TDMA_frame MAC layer of a Node.
Slot::iterator getCurrentSlot()
virtual void changeStatus()
Alternate TDMA status between MY_STATUS and NOT_MY_STATUS.
SlotTopology s_
matrix cointaining the transmission schedule of all the network
virtual ~UwTDMA_frame()
Destructor of the TDMA_frame class.
int topology_index
index in the topology matrix
string topology_S_file_name_
Topology S file name.
virtual void initializeTopologyS()
Initialize the topology S 2D matrix from file.
char topology_S_token_separator_
character used as token separator when importing the S topology from file
UwTDMA_frame()
Constructor of the TDMA_frame class.
Slot::iterator getNextMySlot(int skip=0)
int tot_nodes
total number of nodes in the network
int my_slots_counter
count the passed number of slots in which it was active
Slot my_slot_numbers_
set the position of the node in the frame (fair_mode) (starting from 0 to tot_slots-1)
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
int packet_sent_curr_slot_
counter of packet has been sent in the current slot
Class that represents a TDMA Node.
Definition uwtdma.h:92
virtual void start(double delay)
Schedule the beginning of each TDMA cycle, each one after delay.
Definition uwtdma.cpp:389
int tot_slots
Number of slots in the frame (fair_mode)
Definition uwtdma.h:217
double guard_time
Guard time between slots.
Definition uwtdma.h:223
int sea_trial_
Written log variable.
Definition uwtdma.h:213
int debug_
Debug variable: 0 for no info, >-5 for small info, <-5 for complete info.
Definition uwtdma.h:211
int fair_mode
Fair modality on if 1: then only set tot_slots and common guard_time.
Definition uwtdma.h:214
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
Definition uwtdma.cpp:423
UwTDMATimer tdma_timer
TDMA timer handler.
Definition uwtdma.h:226
std::ofstream out_file_stats
File stream for the log file.
Definition uwtdma.h:228
virtual void stop()
Terminate a TDMA cycle, essentially cancel the TDMA timer.
Definition uwtdma.cpp:412
unsigned long int getEpoch()
Calculate the epoch of the event.
Definition uwtdma.h:186
double frame_duration
Frame duration.
Definition uwtdma.h:222
double start_time
Time to wait before starting the protocol.
Definition uwtdma.h:225
int slot_status
Is it my turn to transmit data?
Definition uwtdma.h:210
virtual void changeStatus()
Alternate TDMA status between MY_STATUS and NOT_MY_STATUS.
Definition uwtdma.cpp:359
double slot_duration
Slot duration.
Definition uwtdma.h:224
int slot_number
set the position of the node in the frame (fair_mode) (starting from 0 to tot_slots-1)
Definition uwtdma.h:218
TDMA_FRAMEModuleClass class_uwtdma
packet_t PT_UWHEALTHCBR
packet_t PT_UWCONTROLCBR
Provides the definition of the class UWTDMA_FRAME.
#define UW_TDMA_STATUS_NOT_MY_SLOT
Status slot not active >
Definition uwtdma.h:53
#define UW_TDMA_STATUS_MY_SLOT
Status slot active>
Definition uwtdma.h:52