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.in@gmail.com>
17  * Contributions: Makhtar Diouf <makhtar.diouf@gmail.com>
18  */
19 
20 #include "animpacket.h"
21 #include "animnode.h"
22 #include "animatorview.h"
23 #include "logqt.h"
24 
25 #define PI 3.14159265
26 NS_LOG_COMPONENT_DEFINE ("AnimPacket");
27 
28 namespace netanim
29 {
30 AnimPacketMgr * pAnimPacketMgr = 0;
31 
AnimPacket(uint32_t fromNodeId,uint32_t toNodeId,qreal firstBitTx,qreal firstBitRx,qreal lastBitTx,qreal lastBitRx,bool isWPacket,QString metaInfo,bool showMetaInfo,uint8_t numWirelessSlots)32 AnimPacket::AnimPacket (uint32_t fromNodeId,
33                         uint32_t toNodeId,
34                         qreal firstBitTx,
35                         qreal firstBitRx,
36                         qreal lastBitTx,
37                         qreal lastBitRx,
38                         bool isWPacket,
39                         QString metaInfo,
40                         bool showMetaInfo,
41                         uint8_t numWirelessSlots):
42   m_fromNodeId (fromNodeId),
43   m_toNodeId (toNodeId),
44   m_firstBitTx (firstBitTx),
45   m_firstBitRx (firstBitRx),
46   m_lastBitTx (lastBitTx),
47   m_lastBitRx (lastBitRx),
48   m_isWPacket (isWPacket),
49   m_infoText (0),
50   m_numWirelessSlots (numWirelessSlots),
51   m_currentWirelessSlot (0)
52 {
53   m_fromPos = AnimNodeMgr::getInstance ()->getNode (fromNodeId)->getCenter ();
54   m_toPos = AnimNodeMgr::getInstance ()->getNode (toNodeId)->getCenter ();
55   //NS_LOG_DEBUG ("FromPos:" << m_fromPos);
56   //NS_LOG_DEBUG ("ToPos:" << m_toPos);
57   m_line = QLineF (m_fromPos, m_toPos);
58   qreal propDelay = m_firstBitRx - m_firstBitTx;
59   m_velocity = m_line.length ()/propDelay;
60   m_cos = cos ((360 - m_line.angle ()) * PI/180);
61   m_sin = sin ((360 - m_line.angle ()) * PI/180);
62   setVisible(false);
63   setZValue(ANIMPACKET_ZVAVLUE);
64 
65   m_infoText = new QGraphicsSimpleTextItem (this);
66   if(showMetaInfo)
67     {
68       m_infoText->setText ("p");
69       m_infoText->setFlag (QGraphicsItem::ItemIgnoresTransformations);
70 
71 
72       qreal textAngle = m_line.angle ();
73       if(textAngle < 90)
74         {
75           textAngle = 360-textAngle;
76         }
77       else if (textAngle > 270)
78         {
79           textAngle = 360-textAngle;
80         }
81       else
82         {
83           textAngle = 180-textAngle;
84         }
85 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
86       m_infoText->setTransform (QTransform().rotate (textAngle));
87 #else
88       m_infoText->rotate (textAngle);
89 #endif
90       m_infoText->setText (getMeta(metaInfo));
91     }
92 
93 }
94 
~AnimPacket()95 AnimPacket::~AnimPacket ()
96 {
97   if(m_infoText)
98     {
99       delete m_infoText;
100     }
101 }
102 
103 QString
getMeta(QString metaInfo,int filter,bool & result,bool shortString)104 AnimPacket::getMeta (QString metaInfo, int filter, bool & result, bool shortString)
105 {
106   result = false;
107   QString metaInfoString = GET_DATA (metaInfo);
108 
109   bool aodvResult = false;
110   AodvInfo aodvInfo = parseAodv (metaInfoString, aodvResult);
111   bool olsrResult = false;
112   OlsrInfo olsrInfo = parseOlsr (metaInfoString, olsrResult);
113   bool dsdvResult = false;
114   DsdvInfo dsdvInfo = parseDsdv (metaInfoString, dsdvResult);
115   bool tcpResult = false;
116   TcpInfo tcpInfo = parseTcp (metaInfoString, tcpResult);
117   bool udpResult = false;
118   UdpInfo udpInfo = parseUdp (metaInfoString, udpResult);
119   bool arpResult = false;
120   ArpInfo arpInfo = parseArp (metaInfoString, arpResult);
121   bool icmpResult = false;
122   IcmpInfo icmpInfo = parseIcmp (metaInfoString, icmpResult);
123   bool ipv4Result = false;
124   Ipv4Info ipv4Info = parseIpv4 (metaInfoString, ipv4Result);
125   bool ipv6Result = false;
126   Ipv6Info ipv6Info = parseIpv6 (metaInfoString, ipv6Result);
127 
128   bool wifiResult = false;
129   WifiMacInfo wifiMacInfo = parseWifi (metaInfoString, wifiResult);
130   bool pppResult = false;
131   PppInfo pppInfo = parsePpp (metaInfoString, pppResult);
132   bool ethernetResult = false;
133   EthernetInfo ethernetInfo = parseEthernet (metaInfoString, ethernetResult);
134 
135   QString finalString = "";
136   if (filter == AnimPacket::ALL)
137     {
138       if (shortString)
139         {
140           finalString += aodvInfo.toShortString () +
141                          olsrInfo.toShortString () +
142                          dsdvInfo.toShortString () +
143                          tcpInfo.toShortString () +
144                          udpInfo.toShortString () +
145                          icmpInfo.toShortString () +
146                          ipv4Info.toShortString () +
147                          ipv6Info.toShortString () +
148                          arpInfo.toShortString () +
149                          wifiMacInfo.toShortString () +
150                          pppInfo.toShortString () +
151                          ethernetInfo.toShortString ();
152         }
153       else
154         {
155           finalString += aodvInfo.toString() +
156                          olsrInfo.toString () +
157                          dsdvInfo.toString () +
158                          tcpInfo.toString () +
159                          udpInfo.toString () +
160                          icmpInfo.toString () +
161                          ipv4Info.toString () +
162                          ipv6Info.toString () +
163                          arpInfo.toString () +
164                          wifiMacInfo.toString () +
165                          pppInfo.toString () +
166                          ethernetInfo.toString ();
167         }
168       result = true;
169     }
170   else
171     {
172       if (filter & AnimPacket::AODV)
173         if (aodvResult)
174           finalString += (shortString)?aodvInfo.toShortString ():aodvInfo.toString ();
175       if (filter & AnimPacket::OLSR)
176         if (olsrResult)
177           finalString += (shortString)?olsrInfo.toShortString ():olsrInfo.toString ();
178       if (filter & AnimPacket::DSDV)
179         if (dsdvResult)
180           finalString += (shortString)?dsdvInfo.toShortString ():dsdvInfo.toString ();
181       if (filter & AnimPacket::TCP)
182         if (tcpResult)
183           finalString += (shortString)?tcpInfo.toShortString ():tcpInfo.toString ();
184       if (filter & AnimPacket::UDP)
185         if (udpResult)
186           finalString += (shortString)?udpInfo.toShortString ():udpInfo.toString ();
187       if (filter & AnimPacket::ICMP)
188         if (icmpResult)
189           finalString += (shortString)?icmpInfo.toShortString ():icmpInfo.toString ();
190       if (filter & AnimPacket::IPV4)
191         if (ipv4Result)
192           finalString += (shortString)?ipv4Info.toShortString ():ipv4Info.toString ();
193       if (filter & AnimPacket::IPV6)
194         if (ipv6Result)
195           finalString += (shortString)?ipv6Info.toShortString ():ipv6Info.toString ();
196       if (filter & AnimPacket::ARP)
197         if (arpResult)
198           finalString += (shortString)?arpInfo.toShortString ():arpInfo.toString ();
199       if (filter & AnimPacket::WIFI)
200         if (wifiResult)
201           finalString += (shortString)?wifiMacInfo.toShortString ():wifiMacInfo.toString ();
202       if (filter & AnimPacket::PPP)
203         if (pppResult)
204           finalString += (shortString)?pppInfo.toShortString ():pppInfo.toString ();
205       if (filter & AnimPacket::ETHERNET)
206         if (ethernetResult)
207           finalString += (shortString)?ethernetInfo.toShortString ():ethernetInfo.toString ();
208       if (finalString != "")
209         result = true;
210 
211     }
212   return finalString;
213 
214 }
215 
216 QString
getMeta(QString metaInfo,bool shortString)217 AnimPacket::getMeta (QString metaInfo, bool shortString)
218 {
219   bool result = false;
220   QString metaInfoString = GET_DATA (metaInfo);
221 
222 
223   AodvInfo aodvInfo = parseAodv (metaInfoString, result);
224   if (result)
225     {
226       return (shortString)?aodvInfo.toShortString ():aodvInfo.toString ();
227     }
228 
229 
230   result = false;
231   OlsrInfo olsrInfo = parseOlsr (metaInfoString, result);
232   if(result)
233     {
234       return (shortString)?olsrInfo.toShortString ():olsrInfo.toString ();
235     }
236 
237 
238   result = false;
239   DsdvInfo dsdvInfo = parseDsdv (metaInfoString, result);
240   if(result)
241     {
242       return (shortString)?dsdvInfo.toShortString ():dsdvInfo.toString ();
243     }
244 
245 
246 
247   result = false;
248   TcpInfo tcpInfo = parseTcp (metaInfoString, result);
249   if(result)
250     {
251       return (shortString)?tcpInfo.toShortString ():tcpInfo.toString ();
252     }
253 
254 
255 
256   result = false;
257   UdpInfo udpInfo = parseUdp (metaInfoString, result);
258   if(result)
259     {
260       return (shortString)?udpInfo.toShortString ():udpInfo.toString ();
261     }
262 
263 
264 
265   result = false;
266   ArpInfo arpInfo = parseArp (metaInfoString, result);
267   if(result)
268     {
269       return (shortString)?arpInfo.toShortString ():arpInfo.toString ();
270     }
271 
272 
273 
274 
275   result = false;
276   IcmpInfo icmpInfo = parseIcmp (metaInfoString, result);
277   if(result)
278     {
279       return (shortString)?icmpInfo.toShortString ():icmpInfo.toString ();
280 
281     }
282 
283 
284   result = false;
285   Ipv4Info ipv4Info = parseIpv4 (metaInfoString, result);
286   if(result)
287     {
288       return (shortString)?ipv4Info.toShortString ():ipv4Info.toString ();
289     }
290 
291 
292   result = false;
293   WifiMacInfo wifiMacInfo = parseWifi (metaInfoString, result);
294   if(result)
295     {
296       return (shortString)?wifiMacInfo.toShortString ():wifiMacInfo.toString ();
297     }
298 
299 
300   result = false;
301   PppInfo pppInfo = parsePpp (metaInfoString, result);
302   if(result)
303     {
304       return (shortString)?pppInfo.toShortString ():pppInfo.toString ();
305     }
306 
307   result = false;
308   EthernetInfo ethernetInfo = parseEthernet (metaInfoString, result);
309   if(result)
310     {
311       return (shortString)?ethernetInfo.toShortString ():ethernetInfo.toString ();
312     }
313   return "";
314 
315 }
316 
317 uint32_t
getFromNodeId()318 AnimPacket::getFromNodeId ()
319 {
320   return m_fromNodeId;
321 }
322 
323 uint32_t
getToNodeId()324 AnimPacket::getToNodeId ()
325 {
326   return m_toNodeId;
327 }
328 
329 qreal
getFirstBitTx()330 AnimPacket::getFirstBitTx ()
331 {
332   return m_firstBitTx;
333 }
334 
335 qreal
getFirstBitRx()336 AnimPacket::getFirstBitRx ()
337 {
338   return m_firstBitRx;
339 }
340 
341 
342 qreal
getLastBitRx()343 AnimPacket::getLastBitRx ()
344 {
345   return m_lastBitRx;
346 }
347 
348 qreal
getLastBitTx()349 AnimPacket::getLastBitTx ()
350 {
351   return m_lastBitTx;
352 }
353 
354 bool
getIsWPacket()355 AnimPacket::getIsWPacket ()
356 {
357   return m_isWPacket;
358 }
359 
360 qreal
getRadius()361 AnimPacket::getRadius ()
362 {
363   QLineF l (m_fromPos, m_toPos);
364   return l.length ();
365 }
366 
367 bool
packetExpired()368 AnimPacket::packetExpired ()
369 {
370   return m_currentWirelessSlot >= m_numWirelessSlots;
371 }
372 
373 QGraphicsSimpleTextItem *
getInfoTextItem()374 AnimPacket::getInfoTextItem ()
375 {
376   return m_infoText;
377 }
378 
379 
380 
381 PppInfo
parsePpp(QString metaInfo,bool & result)382 AnimPacket::parsePpp(QString metaInfo, bool & result)
383 {
384   PppInfo pppInfo;
385   QRegExp rx("ns3::PppHeader.*");
386   int pos = 0;
387   if((pos = rx.indexIn (metaInfo)) == -1)
388     {
389       result = false;
390       return pppInfo;
391     }
392   result = true;
393   return pppInfo;
394 
395 }
396 
397 
398 ArpInfo
parseArp(QString metaInfo,bool & result)399 AnimPacket::parseArp (QString metaInfo, bool & result)
400 {
401   ArpInfo arpInfo;
402 
403   QRegExp rx ("ns3::ArpHeader\\s+\\((request|reply) source mac: ..-..-(..:..:..:..:..:..) source ipv4: (\\S+) (?:dest mac: ..-..-)?(..:..:..:..:..:.. )?dest ipv4: (\\S+)\\)");
404   int pos = 0;
405   if ((pos = rx.indexIn (metaInfo)) == -1)
406     {
407       result = false;
408       return arpInfo;
409     }
410 
411   arpInfo.type = GET_DATA (rx.cap (1));
412   arpInfo.sourceMac = GET_DATA (rx.cap (2));
413   arpInfo.sourceIpv4 = GET_DATA (rx.cap (3));
414   if(QString (GET_DATA (rx.cap (4))) != "")
415     arpInfo.destMac = GET_DATA (rx.cap (4));
416   arpInfo.destIpv4  = GET_DATA (rx.cap (5));
417   result = true;
418   return arpInfo;
419   /*qDebug (" Type:" + arpInfo->type +
420           " SMac:" + arpInfo->sourceMac +
421           " SIp:"  + arpInfo->sourceIpv4 +
422           " DMac:" + arpInfo->destMac +
423           " DIp:"  + arpInfo->destIpv4);*/
424 
425 
426 }
427 
428 EthernetInfo
parseEthernet(QString metaInfo,bool & result)429 AnimPacket::parseEthernet (QString metaInfo, bool & result)
430 {
431   EthernetInfo ethernetInfo;
432   QRegExp rx ("ns3::EthernetHeader \\( length/type\\S+ source=(..:..:..:..:..:..), destination=(..:..:..:..:..:..)\\)");
433   int pos = 0;
434   if ((pos = rx.indexIn (metaInfo)) == -1)
435     {
436       result = false;
437       return ethernetInfo;
438     }
439 
440   ethernetInfo.sourceMac = GET_DATA (rx.cap (1));
441   ethernetInfo.destMac = GET_DATA (rx.cap (2));
442 
443   result = true;
444   return ethernetInfo;
445 }
446 
447 
448 IcmpInfo
parseIcmp(QString metaInfo,bool & result)449 AnimPacket::parseIcmp (QString metaInfo, bool & result)
450 {
451   IcmpInfo icmpInfo;
452 
453   QRegExp rx ("ns3::Icmpv4Header \\(type=(.*), code=([^\\)]*)");
454   int pos = 0;
455   if ((pos = rx.indexIn (metaInfo)) == -1)
456     {
457       result = false;
458       return icmpInfo;
459     }
460 
461   icmpInfo.type =  GET_DATA (rx.cap (1));
462   icmpInfo.code =  GET_DATA (rx.cap (2));
463   result = true;
464   return icmpInfo;
465 
466 }
467 
468 UdpInfo
parseUdp(QString metaInfo,bool & result)469 AnimPacket::parseUdp (QString metaInfo, bool & result)
470 {
471   UdpInfo udpInfo;
472 
473   QRegExp rx ("ns3::UdpHeader \\(length: (\\S+) (\\S+) > (\\S+)\\)");
474   int pos = 0;
475   if ((pos = rx.indexIn (metaInfo)) == -1)
476     {
477       result = false;
478       return udpInfo;
479     }
480 
481   udpInfo.length = GET_DATA (rx.cap (1));
482   udpInfo.SPort = GET_DATA (rx.cap (2));
483   udpInfo.DPort = GET_DATA (rx.cap (3));
484   result = true;
485   return udpInfo;
486 }
487 
488 Ipv6Info
parseIpv6(QString metaInfo,bool & result)489 AnimPacket::parseIpv6 (QString metaInfo, bool & result)
490 {
491   Ipv6Info ipv6Info;
492   QRegExp rx ("ns3::Ipv6Header");
493   int pos = 0;
494   if ((pos = rx.indexIn (metaInfo)) == -1)
495     {
496       result = false;
497       return ipv6Info;
498     }
499   result = true;
500   return ipv6Info;
501 }
502 
503 Ipv4Info
parseIpv4(QString metaInfo,bool & result)504 AnimPacket::parseIpv4 (QString metaInfo, bool & result)
505 {
506   Ipv4Info ipv4Info;
507 
508   QRegExp rx ("ns3::Ipv4Header \\(tos (\\S+) DSCP (\\S+) ECN (\\S+) ttl (\\d+) id (\\d+) protocol (\\d+) .* length: (\\d+) (\\S+) > (\\S+)\\)");
509   int pos = 0;
510   if ((pos = rx.indexIn (metaInfo)) == -1)
511     {
512       result = false;
513       return ipv4Info;
514     }
515   ipv4Info.tos = GET_DATA (rx.cap (1));
516   ipv4Info.Dscp = GET_DATA (rx.cap (2));
517   ipv4Info.Ecn = GET_DATA (rx.cap (3));
518   ipv4Info.Ttl = GET_DATA (rx.cap (4));
519   ipv4Info.Id = GET_DATA (rx.cap (5));
520   ipv4Info.protocol = GET_DATA (rx.cap (6));
521   ipv4Info.length = GET_DATA (rx.cap (7));
522   ipv4Info.SrcIp = GET_DATA (rx.cap (8));
523   ipv4Info.DstIp = GET_DATA (rx.cap (9));
524   result = true;
525   return ipv4Info;
526 }
527 
528 TcpInfo
parseTcp(QString metaInfo,bool & result)529 AnimPacket::parseTcp (QString metaInfo, bool & result)
530 {
531   TcpInfo tcpInfo;
532 
533   QRegExp rx ("ns3::TcpHeader \\((\\d+) > (\\d+) \\[([^\\]]*)\\] Seq=(\\d+) Ack=(\\d+) Win=(\\d+)");
534   int pos = 0;
535   if ((pos = rx.indexIn (metaInfo)) == -1)
536     {
537       result = false;
538       return tcpInfo;
539     }
540   tcpInfo.SPort = GET_DATA (rx.cap (1));
541   tcpInfo.DPort = GET_DATA (rx.cap (2));
542   tcpInfo.flags = GET_DATA (rx.cap (3));
543   tcpInfo.seq = GET_DATA (rx.cap (4));
544   tcpInfo.ack = GET_DATA (rx.cap (5));
545   tcpInfo.window = GET_DATA (rx.cap (6));
546   result = true;
547   return tcpInfo;
548 }
549 
550 WifiMacInfo
parseWifi(QString metaInfo,bool & result)551 AnimPacket::parseWifi (QString metaInfo, bool & result)
552 {
553   QRegExp rxCTL_ACK ("ns3::WifiMacHeader \\(CTL_ACK .*RA=(..:..:..:..:..:..)");
554   WifiMacInfo wifiMacInfo;
555   int pos = 0;
556   if ((pos = rxCTL_ACK.indexIn (metaInfo)) != -1)
557     {
558       wifiMacInfo.type = "CTL_ACK";
559       wifiMacInfo.Ra = GET_DATA (rxCTL_ACK.cap (1));
560       result = true;
561       return wifiMacInfo;
562 
563     }
564   QRegExp rxCTL_RTS ("ns3::WifiMacHeader \\(CTL_RTS .*RA=(..:..:..:..:..:..), TA=(..:..:..:..:..:..)");
565   pos = 0;
566   if ((pos = rxCTL_RTS.indexIn (metaInfo)) != -1)
567     {
568       wifiMacInfo.type = "CTL_RTS";
569       wifiMacInfo.Ra = GET_DATA (rxCTL_RTS.cap (1));
570       wifiMacInfo.Sa = GET_DATA (rxCTL_RTS.cap (2));
571       result = true;
572       return wifiMacInfo;
573 
574     }
575 
576   QRegExp rxCTL_CTS ("ns3::WifiMacHeader \\(CTL_CTS .*RA=(..:..:..:..:..:..)");
577   pos = 0;
578   if ((pos = rxCTL_CTS.indexIn (metaInfo)) != -1)
579     {
580       wifiMacInfo.type = "CTL_CTS";
581       wifiMacInfo.Ra = GET_DATA (rxCTL_CTS.cap (1));
582       result = true;
583       return wifiMacInfo;
584 
585     }
586 
587   QRegExp rx ("ns3::WifiMacHeader \\((\\S+) ToDS=(0|1), FromDS=(0|1), .*DA=(..:..:..:..:..:..), SA=(..:..:..:..:..:..), BSSID=(..:..:..:..:..:..)");
588   pos = 0;
589   if ((pos = rx.indexIn (metaInfo)) == -1)
590     {
591       result = false;
592       return wifiMacInfo;
593     }
594   wifiMacInfo.type = GET_DATA (rx.cap (1));
595   wifiMacInfo.toDs = GET_DATA (rx.cap (2));
596   wifiMacInfo.fromDs = GET_DATA (rx.cap (3));
597   wifiMacInfo.Da = GET_DATA (rx.cap (4));
598   wifiMacInfo.Sa = GET_DATA (rx.cap (5));
599   wifiMacInfo.Bssid = GET_DATA (rx.cap (6));
600 
601   if(wifiMacInfo.type == "MGT_ASSOCIATION_REQUEST")
602     {
603       QRegExp rx ("ns3::MgtAssocRequestHeader \\(ssid=(\\S+),");
604       int pos = 0;
605       if ((pos = rx.indexIn (metaInfo)) == -1)
606         {
607           result = false;
608           return wifiMacInfo;
609         }
610       wifiMacInfo.SSid = GET_DATA (rx.cap (1));
611     }
612   if(wifiMacInfo.type == "MGT_ASSOCIATION_RESPONSE")
613     {
614       QRegExp rx ("ns3::MgtAssocResponseHeader \\(status code=(\\S+), rates");
615       int pos = 0;
616       if ((pos = rx.indexIn (metaInfo)) == -1)
617         {
618           result = false;
619           return wifiMacInfo;
620         }
621       wifiMacInfo.assocResponseStatus = GET_DATA (rx.cap (1));
622     }
623   result = true;
624   return wifiMacInfo;
625 }
626 
627 AodvInfo
parseAodv(QString metaInfo,bool & result)628 AnimPacket::parseAodv (QString metaInfo, bool & result)
629 {
630   AodvInfo aodvInfo;
631 
632   QRegExp rx ("ns3::aodv::TypeHeader \\((\\S+)\\) ");
633   int pos = 0;
634   if ((pos = rx.indexIn (metaInfo)) == -1)
635     {
636       result = false;
637       return aodvInfo;
638     }
639   aodvInfo.type = GET_DATA (rx.cap (1));
640   if(aodvInfo.type == "RREQ")
641     {
642       QRegExp rx ("ns3::aodv::RreqHeader \\(RREQ ID \\d+ destination: ipv4 (\\S+) sequence number (\\d+) source: ipv4 (\\S+) sequence number \\d+");
643       int pos = 0;
644       if ((pos = rx.indexIn (metaInfo)) == -1)
645         {
646           result = false;
647           return aodvInfo;
648         }
649       aodvInfo.destination = GET_DATA (rx.cap (1));
650       aodvInfo.seq = GET_DATA (rx.cap (2));
651       aodvInfo.source = GET_DATA (rx.cap (3));
652 
653     }
654   if(aodvInfo.type == "RREP")
655     {
656       QRegExp rx ("ns3::aodv::RrepHeader \\(destination: ipv4 (\\S+) sequence number (\\d+) source ipv4 (\\S+) ");
657       int pos = 0;
658       if ((pos = rx.indexIn (metaInfo)) == -1)
659         {
660           result = false;
661           return aodvInfo;
662         }
663       aodvInfo.destination = GET_DATA (rx.cap (1));
664       aodvInfo.seq = GET_DATA (rx.cap (2));
665       aodvInfo.source = GET_DATA (rx.cap (3));
666     }
667   if(aodvInfo.type == "RERR")
668     {
669       QRegExp rx ("ns3::aodv::RerrHeader \\(([^\\)]+) \\(ipv4 address, seq. number):(\\S+) ");
670       int pos = 0;
671       if ((pos = rx.indexIn (metaInfo)) == -1)
672         {
673           result = false;
674           return aodvInfo;
675         }
676       aodvInfo.rerrInfo = GET_DATA (rx.cap (1));
677       aodvInfo.destination = GET_DATA (rx.cap (2));
678     }
679   result = true;
680   return aodvInfo;
681 
682 }
683 
684 DsdvInfo
parseDsdv(QString metaInfo,bool & result)685 AnimPacket::parseDsdv (QString metaInfo, bool & result)
686 {
687   DsdvInfo dsdvInfo;
688 
689   QRegExp rx ("ns3::dsdv::DsdvHeader");
690   int pos = 0;
691   if ((pos = rx.indexIn (metaInfo)) == -1)
692     {
693       result = false;
694       return dsdvInfo;
695     }
696   result = true;
697   return dsdvInfo;
698 
699 }
700 
701 OlsrInfo
parseOlsr(QString metaInfo,bool & result)702 AnimPacket::parseOlsr (QString metaInfo, bool & result)
703 {
704   OlsrInfo olsrInfo;
705 
706   QRegExp rx ("ns3::olsr::MessageHeader");
707   int pos = 0;
708   if ((pos = rx.indexIn (metaInfo)) == -1)
709     {
710       result = false;
711       return olsrInfo;
712     }
713   result = true;
714   return olsrInfo;
715 }
716 
717 
718 
719 
720 
721 
722 
723 #if 0
724 void
725 AnimPacket::update (qreal t)
726 {
727   qreal timeElapsed = t - getFirstBitTx ();
728   qreal distanceTravelled = m_velocity * timeElapsed;
729   m_distanceTraveled = distanceTravelled;
730   qreal x = m_distanceTraveled * m_cos;
731   qreal y = m_distanceTraveled * m_sin;
732   m_head = QPointF (m_fromPos.x () + x,  m_fromPos.y () + y);
733   //NS_LOG_DEBUG ("Upd Time:" << t << " Head:" << m_head << " Distance traveled:" << m_distanceTraveled << " time elapsed:" << timeElapsed  << " velocity:" << m_velocity);
734 }
735 
736 #else
737 void
update(qreal t)738 AnimPacket::update (qreal t)
739 {
740   //NS_LOG_DEBUG ("Updating");
741   m_currentTime = t;
742   //qreal midPointX = (m_toPos.x () + m_fromPos.x ())/2;
743   //qreal midPointY = (m_toPos.y () + m_fromPos.y ())/2;
744   if (m_isWPacket)
745     {
746       //m_head = QPointF (midPointX, midPointY);
747       m_head = m_toPos;
748     }
749   else
750     {
751       if (t > getFirstBitRx ())
752         {
753           m_head = m_toPos;
754         }
755       else
756         {
757           qreal timeElapsed = t - getFirstBitTx ();
758           qreal distanceTravelled = m_velocity * timeElapsed;
759           qreal x = distanceTravelled * m_cos;
760           qreal y = distanceTravelled * m_sin;
761           m_head = QPointF (m_fromPos.x () + x, m_fromPos.y () + y);
762         }
763 
764     }
765   //m_head = QPointF (100,100);
766   //NS_LOG_DEBUG ("m_head:" << m_head);
767 }
768 
769 #endif
770 QRectF
boundingRect() const771 AnimPacket::boundingRect () const
772 {
773   return m_boundingRect;
774   //QRectF r = QRectF(-m_boundingRect.width(),  -m_boundingRect.height(), m_boundingRect.width(), m_boundingRect.height());
775   //return r;
776 }
777 
778 
779 void
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)780 AnimPacket::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
781 {
782   Q_UNUSED(option)
783   Q_UNUSED(widget)
784   //NS_LOG_DEBUG ("Packet Transform:" << transform());
785   //NS_LOG_DEBUG ("Device Transform:" << painter->deviceTransform());
786   //NS_LOG_DEBUG ("Scene Transform:" << sceneTransform());
787   QPen p;
788   QTransform viewTransform = AnimatorView::getInstance ()->transform ();
789   painter->save ();
790   QPainterPath arrowTailPath;
791   arrowTailPath.moveTo (0, 0);
792   qreal mag = getRadius ();
793 
794   qreal transformedMag = 2 * (10/viewTransform.m22 ());
795 
796   if (!m_isWPacket)
797     {
798       qreal timeElapsed = m_currentTime - getFirstBitTx ();
799       mag = m_velocity * timeElapsed;
800       qreal magLbTx = 0;
801       //NS_LOG_DEBUG ("First Bit Mag:" << mag);
802       if (m_currentTime > getLastBitTx ())
803         {
804           timeElapsed = m_currentTime - getLastBitTx ();
805           magLbTx = m_velocity * timeElapsed;
806         }
807       if (m_currentTime > getFirstBitRx ())
808         {
809           qreal fullDistance = m_velocity * (getFirstBitRx () - getFirstBitTx ());
810           mag = fullDistance;
811 
812         }
813       mag -= magLbTx;
814       //NS_LOG_DEBUG ("Mag:" << mag << " MagLbTx:" << magLbTx);
815       arrowTailPath.lineTo (-mag, 0);
816       arrowTailPath.addEllipse( QPointF (-mag, 0), transformedMag/10, transformedMag/10);
817     }
818   else
819     {
820       arrowTailPath.lineTo (-mag , 0);
821 
822     }
823   //arrowTailPath.addText (-mag, 0, f, "HOLA");
824   p.setColor (Qt::blue);
825   //p.setWidthF (0.75);
826   p.setCosmetic (true);
827   painter->setPen (p);
828   painter->rotate (360 - m_line.angle ());
829   painter->drawPath (arrowTailPath);
830   painter->restore ();
831 
832 
833 
834   QPolygonF arrowHeadPolygon;
835 
836   QPainterPath arrowHeadPath;
837   qreal arrowHeadLength = transformedMag;
838 
839   arrowHeadPolygon << QPointF (0, 0)
840                    << QPointF (-arrowHeadLength * cos (PI/10), -arrowHeadLength * sin (PI/10))
841                    << QPointF (-(arrowHeadLength/2) * cos (PI/10), 0)
842                    << QPointF (-arrowHeadLength * cos (PI/10), arrowHeadLength * sin (PI/10));
843 
844   arrowHeadPath.lineTo (-arrowHeadLength * cos (PI/10), -arrowHeadLength * sin (PI/10));
845   arrowHeadPath.moveTo (0, 0);
846   arrowHeadPath.lineTo (-arrowHeadLength * cos (PI/10), arrowHeadLength * sin (PI/10));
847   arrowHeadPath.moveTo (0, 0);
848 
849   arrowHeadPath.moveTo (0, 0);
850   painter->save();
851   QPen arrowHeadPen;
852   arrowHeadPen.setCosmetic (true);
853   //arrowHeadPen.setWidthF (0.75);
854 
855   QColor black (0, 0, 5, 130);
856   arrowHeadPen.setColor (black);
857 
858   painter->setPen(arrowHeadPen);
859   painter->rotate (360 - m_line.angle ());
860   QBrush brush;
861   brush.setColor (black);
862   brush.setStyle (Qt::SolidPattern);
863   painter->setBrush (brush);
864   painter->drawPolygon (arrowHeadPolygon);
865   painter->restore ();
866 
867 
868 
869 
870   QPainterPath path;
871   path.moveTo (0, 0);
872   path.addPath (arrowHeadPath);
873   //path.moveTo (0, 0);
874   path.addPath (arrowTailPath);
875 
876 
877   m_boundingRect = path.boundingRect ();
878   QTransform t;
879   t.rotate(360 - m_line.angle ());
880   m_boundingRect = t.mapRect (m_boundingRect);
881 
882 
883 
884 
885 
886   painter->save ();
887   QTransform textTransform;
888   qreal textAngle = m_line.angle ();
889   if(textAngle < 90)
890     {
891       textAngle = 360-textAngle;
892     }
893   else if (textAngle > 270)
894     {
895       textAngle = 360-textAngle;
896     }
897   else
898     {
899       textAngle = 180-textAngle;
900     }
901   textTransform.rotate (textAngle);
902   QPainterPath textPath;
903 
904 
905   QPen p1 = painter->pen ();
906   p1.setColor (Qt::red);
907   p1.setStyle (Qt::SolidLine);
908   QBrush brush2 = painter->brush ();
909   brush2.setStyle (Qt::SolidPattern);
910   brush2.setColor (Qt::black);
911   p1.setWidthF (1/viewTransform.m22 ());
912   p1.setBrush (brush2);
913   painter->setBrush (brush2);
914   painter->setPen (p1);
915   painter->setTransform (m_infoText->transform ());
916   QRectF textBoundingRect = textTransform.mapRect (textPath.boundingRect ());
917 
918   painter->restore ();
919   m_boundingRect = QRectF (QPointF(qMin (m_boundingRect.left (), textBoundingRect.left ()),
920                                   qMin (m_boundingRect.top (), textBoundingRect.top ())),
921                           QPointF(qMax (m_boundingRect.right (), textBoundingRect.right ()),
922                                   qMax (m_boundingRect.bottom (), textBoundingRect.bottom ())));
923 
924 
925 
926 
927 
928   QPointF p_1;// = mapFromScene(m_line.p1());
929   QPointF p_2;// = mapFromScene(m_line.p2());
930   p_1 = mapFromScene(m_line.p1());
931   p_2 = mapFromScene(m_line.p2());
932 
933   bool leftToRight = false;
934   if (p_1.x () < p_2.x ())
935     {
936       leftToRight = true;
937 
938     }
939 
940   if (m_isWPacket)
941     {
942       if (leftToRight)
943         {
944           QTransform tempTransform;
945           tempTransform.rotate(360 - m_line.angle());
946           m_infoText->setPos(tempTransform.map(QPointF(-mag * 3/4, 0)));
947         }
948       else
949         {
950           QTransform tempTransform;
951           tempTransform.rotate(360 - m_line.angle());
952 
953           m_infoText->setPos(tempTransform.map(QPointF(-mag / 4, 0)));
954         }
955     }
956   else
957     {
958       if (leftToRight)
959         {
960           QTransform tempTransform;
961           tempTransform.rotate(360 - m_line.angle());
962           m_infoText->setPos(tempTransform.map(QPointF(-mag, 0)));
963         }
964       else
965         {
966           QTransform tempTransform;
967           tempTransform.rotate(360 - m_line.angle());
968 
969           m_infoText->setPos(tempTransform.map(QPointF(0, 0)));
970         }
971     }
972 
973 
974 }
975 
976 
977 QPointF
getHead()978 AnimPacket::getHead ()
979 {
980   return m_head;
981 }
982 
983 QPointF
getFromPos()984 AnimPacket::getFromPos ()
985 {
986   return m_fromPos;
987 }
988 
989 QPointF
getToPos()990 AnimPacket::getToPos ()
991 {
992   return m_toPos;
993 }
994 
AnimPacketMgr()995 AnimPacketMgr::AnimPacketMgr ()
996 {
997 }
998 AnimPacketMgr *
getInstance()999 AnimPacketMgr::getInstance ()
1000 {
1001   if(!pAnimPacketMgr)
1002     {
1003       pAnimPacketMgr = new AnimPacketMgr;
1004     }
1005   return pAnimPacketMgr;
1006 }
1007 
1008 AnimPacket *
add(uint32_t fromId,uint32_t toId,qreal fbTx,qreal fbRx,qreal lbTx,qreal lbRx,bool isWPacket,QString metaInfo,bool showMetaInfo,uint8_t numWirelessSlots)1009 AnimPacketMgr::add (uint32_t fromId,
1010                     uint32_t toId,
1011                     qreal fbTx,
1012                     qreal fbRx,
1013                     qreal lbTx,
1014                     qreal lbRx,
1015                     bool isWPacket,
1016                     QString metaInfo,
1017                     bool showMetaInfo,
1018                     uint8_t numWirelessSlots)
1019 {
1020   AnimPacket * pkt = new AnimPacket (fromId, toId, fbTx, fbRx, lbTx, lbRx, isWPacket, metaInfo, showMetaInfo, numWirelessSlots);
1021   return pkt;
1022 }
1023 
1024 
1025 
1026 }
1027 
1028