View | Details | Raw Unified | Return to bug 1223
Collapse All | Expand All

(-)a/src/netanim/examples/star-animation.cc (-3 / +5 lines)
 Lines 117-123    Link Here 
117
  star.BoundingBox (1, 1, 100, 100);
117
  star.BoundingBox (1, 1, 100, 100);
118
118
119
  // Create the animation object and configure for specified output
119
  // Create the animation object and configure for specified output
120
  AnimationInterface anim;
120
/*  AnimationInterface anim;
121
  if (animPort > 0)
121
  if (animPort > 0)
122
    {
122
    {
123
      anim.SetServerPort (animPort);
123
      anim.SetServerPort (animPort);
 Lines 125-132    Link Here 
125
  else if (!animFile.empty ())
125
  else if (!animFile.empty ())
126
    {
126
    {
127
      anim.SetOutputFile (animFile);
127
      anim.SetOutputFile (animFile);
128
    }
128
    }*/
129
  anim.StartAnimation ();
129
  AnimationInterface anim (5000);
130
  anim.StartAnimServer ();
131
  //anim.StartAnimation ();
130
132
131
  NS_LOG_INFO ("Run Simulation.");
133
  NS_LOG_INFO ("Run Simulation.");
132
  Simulator::Run ();
134
  Simulator::Run ();
(-)eaf06b48cb5c (+143 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: John Abraham <john.abraham@gatech.edu>
17
 */
18
19
20
#include "ns3/log.h"
21
#include "ns3/anim-packet.h"
22
23
NS_LOG_COMPONENT_DEFINE ("AnimPacket");
24
25
namespace ns3 {
26
27
uint32_t AnimPacket::greqId = 0;
28
29
AnimPacket::AnimPacket ()
30
{
31
32
}
33
34
AnimPacket::~AnimPacket ()
35
{
36
}
37
38
uint32_t AnimPacket::getnewreqId ()
39
{
40
  return greqId++;
41
}
42
43
void AnimPacket::SetRequestType (uint8_t reqType)
44
{
45
  if ((reqType != 0) && (reqType != 1))
46
    {
47
      NS_FATAL_ERROR ("Invalid Request Type");
48
    }
49
  m_reqType = (reqType)? true : false;
50
  NS_LOG_INFO ("Request Type:" << (m_reqType? "true":"false"));
51
}
52
53
void AnimPacket::SetRequestId (void * reqId)
54
{
55
  m_reqId = *(reinterpret_cast <uint32_t*> (reqId));
56
  NS_LOG_INFO ("Request Id:" << m_reqId); 
57
}
58
59
void AnimPacket::SetCommandId (void * commandId)
60
{
61
  m_commandId = *(reinterpret_cast <uint32_t*> (commandId));
62
  NS_LOG_INFO ("Command Id:" << m_commandId);
63
}
64
65
void AnimPacket::SetPayloadLength (void * payloadLength)
66
{
67
  m_payloadLength = *(reinterpret_cast <uint32_t*> (payloadLength));
68
  NS_LOG_INFO ("PayloadLength:" << m_payloadLength);
69
} 
70
71
72
void AnimPacket::SetPayload (void * data)
73
{
74
  uint32_t * p =  reinterpret_cast <uint32_t *> (data);
75
  for (uint32_t i = 0; i < m_payloadLength; i++)
76
    {
77
     
78
      m_payload.push_back (static_cast <uint32_t> (*(p + i))); 
79
    }
80
  NS_LOG_INFO ("Data:" << GetHexString (m_payload));
81
}
82
83
std::string AnimPacket::GetHexString (uint32_t value)
84
{
85
  std::stringstream ss;
86
  ss << "0x" << std::hex << (uint32_t) value;
87
  return ss.str ();
88
}
89
90
91
std::string AnimPacket::GetHexString (std::vector <uint32_t> values)
92
{
93
  std::stringstream ss;
94
  ss << "0x" << std::hex;
95
  for (uint32_t i = 0; i <values.size (); i++)
96
    {
97
      ss << (int) values[i];
98
    }
99
  return ss.str ();
100
}
101
102
uint8_t AnimPacket::GetRequestTypeAsByte ()
103
{
104
  return (m_reqType ? 1 : 0);
105
}
106
107
uint32_t * AnimPacket::GetRequestId ()
108
{
109
  return &m_reqId;
110
}
111
112
uint32_t * AnimPacket::GetCommandId ()
113
{
114
  return &m_commandId;
115
}
116
117
uint32_t * AnimPacket::GetPayloadLength ()
118
{
119
  return &m_payloadLength;
120
}
121
122
std::vector <uint32_t> * AnimPacket::GetPayload ()
123
{
124
  return &m_payload;
125
}
126
127
void AnimPacket::AppendPayload (uint32_t value)
128
{
129
  m_payload.push_back (value);
130
  m_payloadLength = m_payload.size ();
131
}
132
133
void AnimPacket::Print ()
134
{
135
  NS_LOG_UNCOND ("Packet details: Type:" << (m_reqType? "Request":"Response")
136
                 << " Request Id:" << m_reqId
137
                 << " Command Id:" << m_commandId
138
                 << " Data Length:" << m_payloadLength
139
                 << " Data:" << GetHexString (m_payload));
140
}
141
142
} // namespace ns3
143
(-)eaf06b48cb5c (+61 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: John Abraham <john.abraham@gatech.edu>
17
 */
18
19
20
#ifndef _ANIMPACKET_H_
21
#define _ANIMPACKET_H_
22
23
#include "ns3/simulator.h"
24
25
namespace ns3 {
26
27
#define VALIDATELENGTH ValidateLength(__FUNCTION__, ret, fieldLength)
28
29
class AnimPacket {
30
31
public:
32
  AnimPacket ();
33
  ~AnimPacket ();
34
  static uint32_t greqId; // Unique Request Id
35
  static uint32_t getnewreqId ();
36
  void SetRequestType (uint8_t reqType);
37
  void SetRequestId (void * reqId);
38
  void SetCommandId (void * commandId);
39
  void SetPayloadLength (void * dataLength);
40
  void SetPayload (void * data);
41
  void AppendPayload (uint32_t value);
42
  uint8_t GetRequestTypeAsByte ();
43
  uint32_t * GetRequestId ();
44
  uint32_t * GetCommandId ();
45
  uint32_t * GetPayloadLength ();
46
  std::vector <uint32_t> * GetPayload ();
47
  std::string GetHexString (uint32_t value);
48
  std::string GetHexString (std::vector <uint32_t> values);
49
  void Print ();
50
private:
51
  bool m_reqType;                  // True if Request , False if Response
52
  uint32_t m_reqId;                // Id to match Request and Responses
53
  uint32_t m_commandId;            // Command Id
54
  uint32_t m_payloadLength;        // Payload Length in 32 bit chunks
55
  std::vector <uint32_t> m_payload;// Payload
56
  
57
}; // class AnimPacket
58
59
} // namespace ns3
60
61
#endif
(-)eaf06b48cb5c (+526 lines)
Added Link Here 
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
(-)eaf06b48cb5c (+107 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: John Abraham <john.abraham@gatech.edu>
17
 */
18
19
20
#ifndef _ANIMSOCKET_H_
21
#define _ANIMSOCKET_H_
22
23
#include "ns3/simulator.h"
24
#include "ns3/anim-packet.h"
25
26
#include <pthread.h>
27
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H)
28
#include <sys/socket.h>
29
#include <netinet/in.h>
30
#include <fcntl.h>
31
#endif
32
#include <queue>
33
34
namespace ns3 {
35
36
#define VALIDATELENGTH ValidateLength(__FUNCTION__, ret, fieldLength)
37
38
39
class AnimServer
40
{
41
42
public:
43
  AnimServer ();
44
  AnimServer (uint16_t port);
45
  bool Start ();
46
  void Run ();
47
  void Stop (bool waitCompletion = false);
48
49
  void Queue (AnimPacket *p);
50
  AnimPacket * DeQueue ();
51
  static const uint32_t  magicStart;
52
  static const uint32_t  magicEnd;
53
54
private:
55
  pthread_t m_tid;   // Thread Id 
56
  uint16_t m_port;   // Port to listen to
57
  int m_listeningSocket;
58
  int m_connectedSocket;
59
60
  fd_set read_flags;
61
  fd_set write_flags;
62
63
  void SetReadWriteFlags ();
64
  void ProcessNewConnection ();
65
  void ProcessRead ();
66
  void ProcessWrite ();
67
  
68
  pthread_mutex_t queue_mutex; 
69
  pthread_cond_t queue_condition;
70
  std::queue <AnimPacket*> q;
71
72
  void DeSerialize (AnimPacket *p);
73
  void Serialize (AnimPacket * p);
74
75
  static AnimPacket * GenTestPacket ();
76
77
  void ValidateLength (std::string fname,
78
                       int ret, uint32_t validLength);
79
  void ReadMagicStart ();
80
  void ReadMagicEnd ();
81
  void ReadRequestResponseType ();
82
  void ReadRequestId ();
83
  void ReadCommandId ();
84
  void ReadPayloadLength ();
85
  void ReadPayload ();
86
87
  void DoWrite (const void * wrbuf, uint8_t fieldLength);
88
  void WriteMagicStart ();
89
  void WriteMagicEnd ();
90
  void WriteRequestResponseType ();
91
  void WriteRequestId ();
92
  void WriteCommandId ();
93
  void WritePayloadLength ();
94
  void WritePayload ();
95
96
  AnimPacket * readp;
97
  AnimPacket * writep;
98
 
99
100
101
102
};  // class AnimServer
103
104
}   // namespace ns3
105
106
107
#endif
(-)a/src/netanim/model/animation-interface.cc (-1 / +11 lines)
 Lines 75-80    Link Here 
75
75
76
AnimationInterface::~AnimationInterface ()
76
AnimationInterface::~AnimationInterface ()
77
{
77
{
78
  server.Stop ();
78
  StopAnimation ();
79
  StopAnimation ();
79
}
80
}
80
81
 Lines 128-133    Link Here 
128
  usingSockets = true;
129
  usingSockets = true;
129
  ServerPortSet = true;
130
  ServerPortSet = true;
130
  return true;
131
  return true;
132
#else
133
 NS_FATAL_ERROR ("AnimationInterface");
131
#endif
134
#endif
132
  return false; // never reached unless the above is disabled
135
  return false; // never reached unless the above is disabled
133
                // which is done to support a platform like MinGW
136
                // which is done to support a platform like MinGW
 Lines 672-679    Link Here 
672
    }
675
    }
673
}
676
}
674
677
678
bool AnimationInterface::StartAnimServer ()
679
{
680
  server = AnimServer (mport);
681
  server.Start ();
682
  return true;
683
}
675
684
676
// Helper to output a wireless packet.
685
686
// Helpers to output a wireless packet.
677
// For now, only the XML interface is supported
687
// For now, only the XML interface is supported
678
688
679
689
(-)a/src/netanim/model/animation-interface.h (+8 lines)
 Lines 33-38    Link Here 
33
#include "ns3/config.h"
33
#include "ns3/config.h"
34
#include "ns3/animation-interface-helper.h"
34
#include "ns3/animation-interface-helper.h"
35
#include "ns3/mac48-address.h"
35
#include "ns3/mac48-address.h"
36
#include "ns3/anim-socket.h"
36
37
37
namespace ns3 {
38
namespace ns3 {
38
39
 Lines 148-155    Link Here 
148
   */
149
   */
149
  void SetMobilityPollInterval (Time t);
150
  void SetMobilityPollInterval (Time t);
150
151
152
  /**
153
   * \brief Start Anim Server
154
   *
155
   */
156
  bool StartAnimServer ();
157
151
private:
158
private:
152
  
159
  
160
  AnimServer server;    // Anim Server
153
  int       m_fHandle;  // File handle for output (-1 if none)
161
  int       m_fHandle;  // File handle for output (-1 if none)
154
  bool      m_xml;      // True if xml format desired
162
  bool      m_xml;      // True if xml format desired
155
  Time mobilitypollinterval;
163
  Time mobilitypollinterval;
(-)a/src/netanim/wscript (+4 lines)
 Lines 6-11    Link Here 
6
	module.source = [
6
	module.source = [
7
			  'model/animation-interface.cc',
7
			  'model/animation-interface.cc',
8
			  'helper/animation-interface-helper.cc',
8
			  'helper/animation-interface-helper.cc',
9
			  'model/anim-socket.cc',
10
                          'model/anim-packet.cc'
9
		        ]
11
		        ]
10
12
11
	headers = bld.new_task_gen ('ns3header')
13
	headers = bld.new_task_gen ('ns3header')
 Lines 13-18    Link Here 
13
	headers.source = [
15
	headers.source = [
14
			  'model/animation-interface.h',
16
			  'model/animation-interface.h',
15
			  'helper/animation-interface-helper.h',
17
			  'helper/animation-interface-helper.h',
18
			  'model/anim-socket.h',
19
                          'model/anim-packet.h'
16
  			 ]
20
  			 ]
17
21
18
	if (bld.env['ENABLE_EXAMPLES']) :
22
	if (bld.env['ENABLE_EXAMPLES']) :

Return to bug 1223