DESERT 3.5.1
Loading...
Searching...
No Matches
uwApplication_UDP_socket.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
40#include <sstream>
41#include <time.h>
44#include <error.h>
45#include <errno.h>
46
47pthread_mutex_t mutex_udp = PTHREAD_MUTEX_INITIALIZER;
48
49int
51{
52 int sockoptval = 1;
53 // Create socket for incoming connections
54 if ((servSockDescr = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
55 if (debug_ >= 0)
56 std::cout << "[" << getEpoch() << "]::" << NOW
57 << "::UWAPPLICATION::OPEN_CONNECTION_UDP::SOCKET_"
58 "CREATION_FAILED"
59 << endl;
60 if (logging)
61 out_log << left << "[" << getEpoch() << "]::" << NOW
62 << "::UWAPPLICATION::OPEN_CONNECTION_UDP::SOCKET_CREATION_"
63 "FAILED"
64 << endl;
65 exit(1);
66 }
67
68 if (setsockopt(servSockDescr, SOL_SOCKET, SO_REUSEADDR, &sockoptval, sizeof(int)) == -1) {
69 if(debug_ >=0)
70 std::cout << "[" << getEpoch() << "]::" << NOW
71 << "UWAPPLICATION::ERROR::REUSABLE_FAIL"
72 << std::endl;
73 }
74
75 if (debug_ >= 2)
76 std::cout << "[" << getEpoch() << "]::" << NOW
77 << "::UWAPPLICATION::OPEN_CONNECTION_UDP::SOCKET_CREATED"
78 << endl;
79 memset(&servAddr, 0, sizeof(servAddr));
80 servAddr.sin_family = AF_INET;
81 servAddr.sin_port = htons(servPort);
82 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
83
84 if (::bind(servSockDescr, (struct sockaddr *) &servAddr, sizeof(servAddr)) <
85 0) {
86 if (debug_ >= 0)
87 std::cout << "[" << getEpoch() << "]::" << NOW
88 << "::UWAPPLICATION::OPEN_CONNECTION_UDP::BINDING_FAILED_"
89 << strerror(errno) << endl;
90 if (logging)
91 out_log << left << "[" << getEpoch() << "]::" << NOW
92 << "::UWAPPLICATION::OPEN_CONNECTION_UDP::BINDING_FAILED_"
93 << strerror(errno) << endl;
94 exit(1);
95 }
96 pthread_t pth;
97 if (pthread_create(&pth, NULL, read_process_UDP, (void *) this) != 0) {
98 if (debug_ >= 0)
99 std::cout << "[" << getEpoch() << "]::" << NOW
100 << "::UWAPPLICATION::OPEN_CONNECTION_UDP::CANNOT_CREATE_"
101 "PARRALEL_THREAD"
102 << endl;
103 exit(1);
104 }
105 chkTimerPeriod.resched(getPeriod());
106
107 return servSockDescr;
108}; // end openConnectionUDP() method
109
110void *
112{
113 int debug_ = 1;
114 char buffer_msg[MAX_LENGTH_PAYLOAD]; // Buffer in which the DATA packets
115 // payload are stored
116 int recvMsgSize; // Size of the DATA payload received;
118
119 socklen_t clnLen = sizeof(sockaddr_in);
120
121 while (true) {
122 clnLen = sizeof(obj->clnAddr);
123 // Put the message to 0
124 for (int i = 0; i < MAX_LENGTH_PAYLOAD; i++) {
125 buffer_msg[i] = 0;
126 }
127 // Block until receive message from a client
128 if ((recvMsgSize = recvfrom(obj->servSockDescr,
129 buffer_msg,
131 0,
132 (struct sockaddr *) &(obj->clnAddr),
133 &clnLen)) < 0) {
134 if (debug_ >= 0)
135 std::cout << "[" << obj->getEpoch() << "]::" << NOW
136 << "::UWAPPLICATION::READ_PROCESS_UDP::CONNECTION_"
137 "NOT_ACCEPTED"
138 << endl;
139 if (obj->logging)
140 obj->out_log << left << "[" << obj->getEpoch() << "]::" << NOW
141 << "::UWAPPLICATION::READ_PROCESS_UDP::CONNECTION_"
142 "NOT_ACCEPTED"
143 << endl;
144 }
145 if (debug_ >= 1)
146 std::cout << "[" << obj->getEpoch() << "]::" << NOW
147 << "::UWAPPLICATION::READ_PROCESS_TCP::NEW_CLIENT_IP_"
148 << inet_ntoa(obj->clnAddr.sin_addr) << std::endl;
149 if (obj->logging)
150 obj->out_log << left << "[" << obj->getEpoch() << "]::" << NOW
151 << "::UWAPPLICATION::READ_PROCESS_UDP::NEW_CLIENT_IP_"
152 << inet_ntoa(obj->clnAddr.sin_addr) << std::endl;
153 int status = pthread_mutex_lock(&mutex_udp);
154 if (status != 0) {
155 if (debug_ >= 0)
156 std::cout << "[" << obj->getEpoch() << "]::" << NOW
157 << "::UWAPPLICATION::PTHREAD_MUTEX_LOCK_FAILED "
158 << endl;
159 }
160 if (recvMsgSize > 0) {
161 Packet *p = Packet::alloc();
162 hdr_cmn *ch = HDR_CMN(p);
164 hdr_Appl->payload_size() = recvMsgSize;
165 ch->size() = recvMsgSize;
166 if (debug_ >= 0) {
167 std::cout << "[" << obj->getEpoch() << "]::" << NOW
168 << "::UWAPPLICATION::READ_PROCESS_UDP::NEW_PACKET_"
169 "CREATED--> "
170 << endl;
171 if (obj->logging)
172 obj->out_log << left << "[" << obj->getEpoch()
173 << "]::" << NOW
174 << "::UWAPPLICATION::READ_PROCESS_UDP::NEW_"
175 "PACKET_CREATED"
176 << endl;
177 std::cout << "[" << obj->getEpoch() << "]::" << NOW
178 << "::UWAPPLICATION::READ_PROCESS_UDP::PAYLOAD_"
179 "MESSAGE--> ";
180 for (int i = 0; i < recvMsgSize; i++) {
181 hdr_Appl->payload_msg[i] = buffer_msg[i];
182 cout << buffer_msg[i];
183 }
184 }
185 obj->queuePckReadUDP.push(p);
186 obj->incrPktsPushQueue();
187 }
188 status = pthread_mutex_unlock(&mutex_udp);
189 if (status != 0) {
190 if (debug_ >= 0)
191 std::cout << "[" << obj->getEpoch() << "]::" << NOW
192 << "::UWAPPLICATION::PTHREAD_MUTEX_UNLOCK_FAILED "
193 << endl;
194 }
195 }
196
197} // end read_process_UDP() method
198
199void
201{
202 if (!queuePckReadUDP.empty()) {
203 Packet *ptmp = queuePckReadUDP.front();
204 queuePckReadUDP.pop();
205 hdr_cmn *ch = HDR_CMN(ptmp);
206 hdr_uwudp *uwudph = hdr_uwudp::access(ptmp);
207 hdr_uwip *uwiph = hdr_uwip::access(ptmp);
209
210 ch->uid_ = uidcnt++;
211 ch->ptype_ = PT_DATA_APPLICATION;
212 ch->direction_ = hdr_cmn::DOWN;
213 ch->timestamp() = Scheduler::instance().clock();
214
215 uwudph->dport() = port_num;
216
217 uwiph->daddr() = dst_addr;
218
219 uwApph->sn_ = txsn++; // Sequence number to the data packet
220 if (rftt >= 0) {
221 uwApph->rftt_ = (int) (rftt * 10000); // Forward Trip Time
222 uwApph->rftt_valid_ = true;
223 } else {
224 uwApph->rftt_valid_ = false;
225 }
226 uwApph->priority_ = 0; // Priority of the message
227
228 if (debug_ >= 2)
229 std::cout << "[" << getEpoch() << "]::" << NOW
230 << "::UWAPPLICATION::INIT_PACKET_UDP::UID_" << ch->uid_
231 << endl;
232 if (debug_ >= 0)
233 std::cout << "[" << getEpoch() << "]::" << NOW
234 << "::UWAPPLICATION::INIT_PACKET_UDP::DEST_"
235 << (int) uwiph->daddr() << endl;
236 if (debug_ >= 0)
237 std::cout << "[" << getEpoch() << "]::" << NOW
238 << "::UWAPPLICATION::INIT_PACKET_UDP::SIZE_"
239 << (int) uwApph->payload_size() << endl;
240 if (debug_ >= 0)
241 std::cout << "[" << getEpoch() << "]::" << NOW
242 << "::UWAPPLICATION::INIT_PACKET_UDP::SN_"
243 << (int) uwApph->sn_ << endl;
244 if (debug_ >= 0)
245 std::cout << "[" << getEpoch() << "]::" << NOW
246 << "::UWAPPLICATION::INIT_PACKET_UDP::SEND_DOWN_PACKET"
247 << endl;
248
249 if (logging)
250 out_log << left << "[" << getEpoch() << "]::" << NOW
251 << "::UWAPPLICATION::INIT_PACKET_UDP::UID_" << ch->uid_
252 << endl;
253 if (logging)
254 out_log << left << "[" << getEpoch() << "]::" << NOW
255 << "::UWAPPLICATION::INIT_PACKET_UDP::DEST_"
256 << (int) uwiph->daddr() << endl;
257 if (logging)
258 out_log << left << "[" << getEpoch() << "]::" << NOW
259 << "::UWAPPLICATION::INIT_PACKET_UDP::SIZE_"
260 << (int) uwApph->payload_size() << endl;
261 if (logging)
262 out_log << left << "[" << getEpoch() << "]::" << NOW
263 << "::UWAPPLICATION::INIT_PACKET_UDP::SN_"
264 << (int) uwApph->sn_ << endl;
265 if (logging)
266 out_log << left << "[" << getEpoch() << "]::" << NOW
267 << "::UWAPPLICATION::INIT_PACKET_UDP::SEND_DOWN_PACKET"
268 << endl;
269
270 sendDown(ptmp);
271 }
272} // end init_Packet_UDP() method
uwSendTimerAppl chkTimerPeriod
Timer that schedule the period between two successive generation of DATA packets.
int servSockDescr
socket descriptor for server
int rftt
Forward trip time.
int debug_
Used for debug purposes 1 debug activated 0 debug not activated.
std::queue< Packet * > queuePckReadUDP
Queue that store the DATA packets recevied from the client by the server using a UDP protocol.
virtual int openConnectionUDP()
When socket communication is used, this method establish a connection between client and server.
virtual void init_Packet_UDP()
Set all the field of DATA packet and take from the specific queue the payload of DATA packet that wil...
virtual void incrPktsPushQueue()
Increase the number of DATA packets stored in the Server queue.
int uidcnt
Identifier counter that identify uniquely the DATA packet generated.
unsigned long int getEpoch()
Calculate the epoch of the event.
struct sockaddr_in servAddr
Server address.
std::ofstream out_log
Variable that handle the file in which the protocol write the statistics.
int port_num
Number of the port in which the server provide the service.
struct sockaddr_in clnAddr
Client address.
uint8_t dst_addr
IP destination address.
virtual double getPeriod()
return period generation time
int txsn
Transmission sequence number of DATA packet.
Content header of TRIGGER packet.
int rftt_
Forward Trip Time of the packet.
char payload_msg[MAX_LENGTH_PAYLOAD]
Message payload.
uint16_t sn_
Serial number of the packet.
bool rftt_valid_
Flag used to set the validity of the fft field.
uint8_t priority_
Priority flag: 1 means high priority, 0 normal priority.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
static hdr_uwip * access(const Packet *p)
Definition uwip-module.h:86
hdr_uwudp describes UWUDP packets.
static struct hdr_uwudp * access(const Packet *p)
uint8_t & dport()
Reference to the dport_ variable.
pthread_mutex_t mutex_udp
void * read_process_UDP(void *arg)
Provides the headers of the data packet.
#define MAX_LENGTH_PAYLOAD
#define HDR_DATA_APPLICATION(p)
alias defined to access the TRIGGER HEADER
packet_t PT_DATA_APPLICATION
Trigger packet type for UFetch protocol.
void * read_process_UDP(void *arg)