|
|
| 1 |
|
| 2 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 3 |
/* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License version 2 as |
| 6 |
* published by the Free Software Foundation; |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program; if not, write to the Free Software |
| 15 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 |
* |
| 17 |
* Author: John Abraham <john.abraham@gatech.edu> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "ns3/simulator.h" |
| 21 |
#include "ns3/anim-socket.h" |
| 22 |
#include "ns3/netanim-config.h" |
| 23 |
#include "ns3/anim-packet.h" |
| 24 |
#include "ns3/log.h" |
| 25 |
#include <pthread.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
// Socket related includes |
| 29 |
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
| 30 |
#include <sys/socket.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <fcntl.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
NS_LOG_COMPONENT_DEFINE ("AnimServer"); |
| 36 |
|
| 37 |
namespace ns3 |
| 38 |
{ |
| 39 |
|
| 40 |
void * thread_func (void * arg) |
| 41 |
{ |
| 42 |
AnimServer *server = static_cast <AnimServer *> (arg); |
| 43 |
NS_ASSERT (server); |
| 44 |
server->Run (); |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
AnimServer::AnimServer (): m_port (0), m_listeningSocket (-1), |
| 49 |
m_connectedSocket (-1) |
| 50 |
{ |
| 51 |
pthread_mutex_init (&queue_mutex, NULL); |
| 52 |
pthread_cond_init (&queue_condition, NULL); |
| 53 |
} |
| 54 |
AnimServer::AnimServer (uint16_t port): m_port (port), |
| 55 |
m_listeningSocket (-1), m_connectedSocket (-1) |
| 56 |
{ |
| 57 |
pthread_mutex_init (&queue_mutex, NULL); |
| 58 |
pthread_cond_init (&queue_condition, NULL); |
| 59 |
} |
| 60 |
|
| 61 |
bool AnimServer::Start (void) |
| 62 |
{ |
| 63 |
// Create Server thread |
| 64 |
|
| 65 |
AnimPacket * p = AnimServer::GenTestPacket (); |
| 66 |
Queue (p); |
| 67 |
int32_t err = pthread_create (&m_tid, NULL, thread_func, this); |
| 68 |
if (err != 0) |
| 69 |
{ |
| 70 |
NS_FATAL_ERROR ("Unable to create AnimServer thread"); |
| 71 |
return false; |
| 72 |
} |
| 73 |
NS_LOG_INFO ("AnimServer thread created"); |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
void AnimServer::Run (void) |
| 78 |
{ |
| 79 |
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
| 80 |
int ret = 0; |
| 81 |
fd_set allfdSet; |
| 82 |
fd_set readfdSet; |
| 83 |
struct sockaddr_in servaddr; |
| 84 |
m_listeningSocket = socket (AF_INET, SOCK_STREAM, 0); |
| 85 |
|
| 86 |
// Set Non-blocking |
| 87 |
int opts = fcntl (m_listeningSocket, O_NONBLOCK); |
| 88 |
if (opts < 0) |
| 89 |
{ |
| 90 |
perror ("AnimServer: Set Non-blocking failed"); |
| 91 |
NS_FATAL_ERROR ("AnimServer: Set Non-blocking failed"); |
| 92 |
} |
| 93 |
|
| 94 |
bzero (&servaddr, sizeof (servaddr)); |
| 95 |
servaddr.sin_family = AF_INET; |
| 96 |
servaddr.sin_addr.s_addr = htonl (INADDR_ANY); |
| 97 |
servaddr.sin_port = htons (m_port); |
| 98 |
|
| 99 |
ret = bind (m_listeningSocket, (struct sockaddr *) &servaddr, sizeof(servaddr)); |
| 100 |
if (ret == -1) |
| 101 |
{ |
| 102 |
std::string ErrorString ("AnimServer: Bind Failed for port:" + m_port); |
| 103 |
perror (ErrorString.c_str ()); |
| 104 |
NS_FATAL_ERROR (ErrorString); |
| 105 |
} |
| 106 |
|
| 107 |
ret = listen (m_listeningSocket, 10); //XXXXXX |
| 108 |
if (ret == -1) |
| 109 |
{ |
| 110 |
std::string ErrorString = "Listen Failed"; |
| 111 |
perror (ErrorString.c_str ()); |
| 112 |
NS_FATAL_ERROR (ErrorString); |
| 113 |
} |
| 114 |
NS_LOG_INFO ("Awaiting connections on port:" << m_port); |
| 115 |
FD_ZERO (&allfdSet); |
| 116 |
FD_SET (m_listeningSocket, &allfdSet); |
| 117 |
|
| 118 |
struct timeval timeout; |
| 119 |
timeout.tv_sec = 1; |
| 120 |
timeout.tv_usec = 0; |
| 121 |
while (1) |
| 122 |
{ |
| 123 |
readfdSet = allfdSet; |
| 124 |
// NS_LOG_INFO ("Infinite loop"); |
| 125 |
ret = select (m_listeningSocket + 1, &readfdSet, NULL, NULL, &timeout); |
| 126 |
if (ret == -1) |
| 127 |
{ |
| 128 |
std::string ErrorString = "Listening Socket Select Failed"; |
| 129 |
perror (ErrorString.c_str ()); |
| 130 |
NS_FATAL_ERROR (ErrorString); |
| 131 |
} |
| 132 |
if (FD_ISSET (m_listeningSocket, &readfdSet)) |
| 133 |
{ |
| 134 |
NS_LOG_INFO ("Connection Received"); |
| 135 |
ProcessNewConnection (); |
| 136 |
} |
| 137 |
if (m_connectedSocket < 0) |
| 138 |
{ |
| 139 |
continue; |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
SetReadWriteFlags (); |
| 144 |
} |
| 145 |
ProcessRead (); |
| 146 |
ProcessWrite (); |
| 147 |
} |
| 148 |
#else |
| 149 |
NS_FATAL_ERROR ("Feature not supported"); |
| 150 |
#endif |
| 151 |
} |
| 152 |
|
| 153 |
void AnimServer::ProcessRead () |
| 154 |
{ |
| 155 |
if (m_connectedSocket < 0) |
| 156 |
{ |
| 157 |
return; |
| 158 |
} |
| 159 |
if (FD_ISSET (m_connectedSocket, &read_flags)) |
| 160 |
{ |
| 161 |
NS_LOG_INFO ("Socket Ready for read"); |
| 162 |
AnimPacket * p = new AnimPacket (); |
| 163 |
DeSerialize (p); |
| 164 |
p->Print (); |
| 165 |
FD_CLR (m_connectedSocket, &read_flags); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
void AnimServer::ProcessWrite () |
| 172 |
{ |
| 173 |
if (m_connectedSocket <0) |
| 174 |
{ |
| 175 |
return; |
| 176 |
} |
| 177 |
if (FD_ISSET (m_connectedSocket, &write_flags)) |
| 178 |
{ |
| 179 |
NS_LOG_INFO ("Socket Ready for write"); |
| 180 |
AnimPacket * p = DeQueue (); |
| 181 |
if (!p) |
| 182 |
{ |
| 183 |
return; |
| 184 |
} |
| 185 |
Serialize (p); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
void AnimServer::ProcessNewConnection () |
| 192 |
{ |
| 193 |
struct sockaddr_in cliaddr; |
| 194 |
socklen_t clilen = sizeof (cliaddr); |
| 195 |
m_connectedSocket = accept (m_listeningSocket, (struct sockaddr *) &cliaddr, &clilen); |
| 196 |
if (m_connectedSocket < 0) |
| 197 |
{ |
| 198 |
std::string ErrorString ("Unable to process incoming connection"); |
| 199 |
perror (ErrorString.c_str ()); |
| 200 |
NS_FATAL_ERROR (ErrorString); |
| 201 |
} |
| 202 |
NS_LOG_INFO ("New connection Processed"); |
| 203 |
} |
| 204 |
|
| 205 |
void AnimServer::SetReadWriteFlags () |
| 206 |
{ |
| 207 |
int ret; |
| 208 |
if (m_connectedSocket < 0) |
| 209 |
{ |
| 210 |
return; |
| 211 |
} |
| 212 |
FD_ZERO (&read_flags); |
| 213 |
FD_ZERO (&write_flags); |
| 214 |
FD_SET (m_connectedSocket, &read_flags); |
| 215 |
FD_SET (m_connectedSocket, &write_flags); |
| 216 |
struct timeval timeout; |
| 217 |
timeout.tv_sec = 1; |
| 218 |
timeout.tv_usec = 0; |
| 219 |
ret = select (m_connectedSocket + 1, &read_flags,&write_flags, NULL, &timeout); |
| 220 |
if (ret == -1) |
| 221 |
{ |
| 222 |
std::string ErrorString = "Connected Socket Select Failed"; |
| 223 |
perror (ErrorString.c_str ()); |
| 224 |
NS_FATAL_ERROR (ErrorString); |
| 225 |
} |
| 226 |
NS_LOG_INFO ("Setting Read Write Flags complete"); |
| 227 |
} |
| 228 |
|
| 229 |
void AnimServer::Stop (bool waitCompletion) |
| 230 |
{ |
| 231 |
void *ret; |
| 232 |
pthread_join (m_tid, &ret); |
| 233 |
} |
| 234 |
|
| 235 |
void AnimServer::Queue (AnimPacket *p) |
| 236 |
{ |
| 237 |
NS_ASSERT (p); |
| 238 |
NS_LOG_FUNCTION (this); |
| 239 |
pthread_mutex_lock (&queue_mutex); |
| 240 |
NS_LOG_DEBUG (__FUNCTION__ << "Queue locked"); |
| 241 |
//pthread_cond_wait (&queue_condition, &queue_mutex); |
| 242 |
q.push (p); |
| 243 |
NS_LOG_DEBUG ("Queue push complete"); |
| 244 |
pthread_mutex_unlock (&queue_mutex); |
| 245 |
NS_LOG_DEBUG ("Queue unlocked"); |
| 246 |
} |
| 247 |
|
| 248 |
AnimPacket * AnimServer::DeQueue () |
| 249 |
{ |
| 250 |
NS_LOG_FUNCTION (this); |
| 251 |
pthread_mutex_lock (&queue_mutex); |
| 252 |
NS_LOG_DEBUG (__FUNCTION__ << "Queue locked"); |
| 253 |
AnimPacket * p = q.front (); |
| 254 |
q.pop (); |
| 255 |
NS_LOG_DEBUG ("Queue pop complete"); |
| 256 |
//pthread_cond_signal (&queue_condition); |
| 257 |
pthread_mutex_unlock (&queue_mutex); |
| 258 |
NS_LOG_DEBUG ("Queue unlocked"); |
| 259 |
return p; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
const uint32_t AnimServer::magicStart = 0xDEADFEE1; |
| 265 |
const uint32_t AnimServer::magicEnd = 0xDEADF00D; |
| 266 |
|
| 267 |
|
| 268 |
void AnimServer::ReadMagicStart () |
| 269 |
{ |
| 270 |
int ret = 0; |
| 271 |
NS_ASSERT (readp); |
| 272 |
uint8_t fieldLength = 4; |
| 273 |
uint8_t rdbuf [4]; |
| 274 |
if ((ret = read (m_connectedSocket, rdbuf, fieldLength)) <= 0) |
| 275 |
{ |
| 276 |
NS_LOG_ERROR ("Read Magic Start Failed. Closing socket"); |
| 277 |
close (m_connectedSocket); |
| 278 |
return; |
| 279 |
} |
| 280 |
VALIDATELENGTH; |
| 281 |
if (memcmp (rdbuf, &AnimServer::magicStart, fieldLength)) |
| 282 |
{ |
| 283 |
NS_LOG_ERROR ("AnimPacketMagicStart Not available.Closing socket"); |
| 284 |
close (m_connectedSocket); |
| 285 |
return; |
| 286 |
} |
| 287 |
NS_LOG_INFO ("Got Magic Start"); |
| 288 |
} |
| 289 |
|
| 290 |
void AnimServer::ReadMagicEnd () |
| 291 |
{ |
| 292 |
int ret = 0; |
| 293 |
NS_ASSERT (readp); |
| 294 |
uint8_t fieldLength = 4; |
| 295 |
uint8_t rdbuf[4]; |
| 296 |
if ((ret = read (m_connectedSocket, rdbuf, fieldLength)) <= 0) |
| 297 |
{ |
| 298 |
NS_LOG_ERROR ("Read Magic End Failed. Closing socket"); |
| 299 |
close (m_connectedSocket); |
| 300 |
return; |
| 301 |
} |
| 302 |
VALIDATELENGTH; |
| 303 |
if (memcmp (rdbuf, &AnimServer::magicEnd, fieldLength)) |
| 304 |
{ |
| 305 |
NS_LOG_ERROR ("AnimPacketMagicEnd Not available.Closing socket"); |
| 306 |
close (m_connectedSocket); |
| 307 |
return; |
| 308 |
} |
| 309 |
NS_LOG_INFO ("Got Magic End"); |
| 310 |
} |
| 311 |
|
| 312 |
void AnimServer::ReadRequestResponseType () |
| 313 |
{ |
| 314 |
int ret = 0; |
| 315 |
NS_ASSERT (readp); |
| 316 |
uint8_t fieldLength = 1; |
| 317 |
uint8_t rdbuf[1]; |
| 318 |
if ((ret = read (m_connectedSocket, rdbuf, fieldLength)) <= 0) |
| 319 |
{ |
| 320 |
NS_LOG_ERROR ("Read Request Reponsei Type Failed. Closing socket"); |
| 321 |
close (m_connectedSocket); |
| 322 |
return; |
| 323 |
} |
| 324 |
VALIDATELENGTH; |
| 325 |
readp->SetRequestType (rdbuf[0]); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
void AnimServer::ReadRequestId () |
| 330 |
{ |
| 331 |
int ret = 0; |
| 332 |
NS_ASSERT (readp); |
| 333 |
uint8_t fieldLength = 4; |
| 334 |
uint8_t rdbuf[4]; |
| 335 |
if ((ret = read (m_connectedSocket, rdbuf, fieldLength)) <= 0) |
| 336 |
{ |
| 337 |
NS_LOG_ERROR ("Read Request Id Failed. Closing socket"); |
| 338 |
close (m_connectedSocket); |
| 339 |
return; |
| 340 |
} |
| 341 |
VALIDATELENGTH; |
| 342 |
readp->SetRequestId (rdbuf); |
| 343 |
} |
| 344 |
|
| 345 |
void AnimServer::ReadCommandId () |
| 346 |
{ |
| 347 |
int ret = 0; |
| 348 |
NS_ASSERT (readp); |
| 349 |
uint8_t fieldLength = 4; |
| 350 |
uint8_t rdbuf[4]; |
| 351 |
if ((ret = read (m_connectedSocket, rdbuf, fieldLength)) <= 0) |
| 352 |
{ |
| 353 |
NS_LOG_ERROR ("Read Command Id Failed. Closing socket"); |
| 354 |
close (m_connectedSocket); |
| 355 |
return; |
| 356 |
} |
| 357 |
VALIDATELENGTH; |
| 358 |
readp->SetCommandId (rdbuf); |
| 359 |
} |
| 360 |
|
| 361 |
void AnimServer::ReadPayloadLength () |
| 362 |
{ |
| 363 |
|
| 364 |
int ret = 0; |
| 365 |
NS_ASSERT (readp); |
| 366 |
uint8_t fieldLength = 4; |
| 367 |
uint8_t rdbuf[4]; |
| 368 |
if ((ret = read (m_connectedSocket, rdbuf, fieldLength)) <= 0) |
| 369 |
{ |
| 370 |
NS_LOG_ERROR ("Read Payload Length Failed. Closing socket"); |
| 371 |
close (m_connectedSocket); |
| 372 |
return; |
| 373 |
} |
| 374 |
VALIDATELENGTH; |
| 375 |
readp->SetPayloadLength (rdbuf); |
| 376 |
} |
| 377 |
|
| 378 |
void AnimServer::ReadPayload () |
| 379 |
{ |
| 380 |
|
| 381 |
int ret = 0; |
| 382 |
NS_ASSERT (readp); |
| 383 |
uint8_t fieldLength = (*readp->GetPayloadLength ()) * sizeof (uint32_t); |
| 384 |
uint8_t rdbuf[1500]; |
| 385 |
|
| 386 |
if ((ret = read (m_connectedSocket, rdbuf, fieldLength)) <= 0) |
| 387 |
{ |
| 388 |
NS_LOG_ERROR ("Read Payload Failed. Closing socket"); |
| 389 |
close (m_connectedSocket); |
| 390 |
return; |
| 391 |
} |
| 392 |
VALIDATELENGTH; |
| 393 |
readp->SetPayload (rdbuf); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
void AnimServer::DeSerialize (AnimPacket *p) |
| 398 |
{ |
| 399 |
NS_ASSERT (m_connectedSocket >= 0); |
| 400 |
NS_ASSERT (p); |
| 401 |
readp = p; |
| 402 |
ReadMagicStart (); |
| 403 |
ReadRequestResponseType (); |
| 404 |
ReadRequestId (); |
| 405 |
ReadCommandId (); |
| 406 |
ReadPayloadLength (); |
| 407 |
ReadPayload (); |
| 408 |
ReadMagicEnd (); |
| 409 |
} |
| 410 |
|
| 411 |
void AnimServer::Serialize (AnimPacket *p) |
| 412 |
{ |
| 413 |
|
| 414 |
NS_ASSERT (m_connectedSocket >= 0); |
| 415 |
NS_ASSERT (p); |
| 416 |
writep = p; |
| 417 |
WriteMagicStart (); |
| 418 |
WriteRequestResponseType (); |
| 419 |
WriteRequestId (); |
| 420 |
WriteCommandId (); |
| 421 |
WritePayloadLength (); |
| 422 |
WritePayload (); |
| 423 |
WriteMagicEnd (); |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
void AnimServer::DoWrite (const void * wrbuf, uint8_t fieldLength) |
| 428 |
{ |
| 429 |
int ret = 0; |
| 430 |
if ((ret = write (m_connectedSocket, reinterpret_cast <const uint8_t *> (wrbuf), |
| 431 |
fieldLength)) <= 0) |
| 432 |
{ |
| 433 |
NS_LOG_ERROR (__FUNCTION__ << " Unexpected Write Failure.Closing socket"); |
| 434 |
close (m_connectedSocket); |
| 435 |
} |
| 436 |
VALIDATELENGTH; |
| 437 |
} |
| 438 |
|
| 439 |
void AnimServer::WriteMagicStart () |
| 440 |
{ |
| 441 |
NS_LOG_FUNCTION (this); |
| 442 |
DoWrite (&magicStart, 4); |
| 443 |
} |
| 444 |
|
| 445 |
void AnimServer::WriteMagicEnd () |
| 446 |
{ |
| 447 |
NS_LOG_FUNCTION (this); |
| 448 |
DoWrite (&magicEnd, 4); |
| 449 |
} |
| 450 |
|
| 451 |
void AnimServer::WriteRequestResponseType () |
| 452 |
{ |
| 453 |
NS_LOG_FUNCTION (this); |
| 454 |
const char wrbuf[1] = {writep->GetRequestTypeAsByte ()}; |
| 455 |
DoWrite (wrbuf, 1); |
| 456 |
} |
| 457 |
|
| 458 |
void AnimServer::WriteRequestId () |
| 459 |
{ |
| 460 |
NS_LOG_FUNCTION (this); |
| 461 |
DoWrite (writep->GetRequestId (), 4); |
| 462 |
} |
| 463 |
|
| 464 |
void AnimServer::WriteCommandId () |
| 465 |
{ |
| 466 |
NS_LOG_FUNCTION (this); |
| 467 |
DoWrite (writep->GetCommandId (), 4); |
| 468 |
} |
| 469 |
|
| 470 |
void AnimServer::WritePayloadLength () |
| 471 |
{ |
| 472 |
NS_LOG_FUNCTION (this); |
| 473 |
DoWrite (writep->GetPayloadLength (), 4); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
void AnimServer::WritePayload () |
| 478 |
{ |
| 479 |
NS_LOG_FUNCTION (this); |
| 480 |
uint32_t numWords = *writep->GetPayloadLength (); |
| 481 |
std::vector <uint32_t> * data = writep->GetPayload (); |
| 482 |
NS_ASSERT (data); |
| 483 |
uint32_t size = (*data).size (); |
| 484 |
for (uint32_t i = 0; ((i < numWords) && (i < size)); i++) |
| 485 |
{ |
| 486 |
DoWrite (&((*data)[i]), 4); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
void AnimServer::ValidateLength (std::string fname, int ret, |
| 491 |
uint32_t validLength) |
| 492 |
{ |
| 493 |
if (static_cast <uint32_t> (ret) != validLength) |
| 494 |
{ |
| 495 |
close (m_connectedSocket); |
| 496 |
NS_FATAL_ERROR (fname << ": Bytes expected:" << validLength |
| 497 |
<< " Bytes got:" << ret << " Socket close"); |
| 498 |
return; |
| 499 |
} |
| 500 |
else |
| 501 |
{ |
| 502 |
return; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
AnimPacket * AnimServer::GenTestPacket () |
| 507 |
{ |
| 508 |
AnimPacket * p = new AnimPacket (); |
| 509 |
p->SetRequestType (1); |
| 510 |
|
| 511 |
uint32_t ReqId = 3; |
| 512 |
p->SetRequestId (&ReqId); |
| 513 |
|
| 514 |
uint32_t CommandId = 45; |
| 515 |
p->SetCommandId (&CommandId); |
| 516 |
|
| 517 |
p->AppendPayload (4); |
| 518 |
p->AppendPayload (5); |
| 519 |
p->AppendPayload (6); |
| 520 |
return p; |
| 521 |
|
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
} // namespace ns3 |