src/olsr/model/olsr-header.cc
changeset 7184 a15feb312428
parent 6972 01039b15c8eb
child 7256 b04ba6772f8c
equal deleted inserted replaced
7183:13fae068d099 7184:a15feb312428
    44 ///
    44 ///
    45 uint8_t
    45 uint8_t
    46 SecondsToEmf (double seconds)
    46 SecondsToEmf (double seconds)
    47 {
    47 {
    48   int a, b = 0;
    48   int a, b = 0;
    49   
    49 
    50   // find the largest integer 'b' such that: T/C >= 2^b
    50   // find the largest integer 'b' such that: T/C >= 2^b
    51   for (b = 0; (seconds/OLSR_C) >= (1 << b); ++b)
    51   for (b = 0; (seconds/OLSR_C) >= (1 << b); ++b)
    52     ;
    52     ;
    53   NS_ASSERT ((seconds/OLSR_C) < (1 << b));
    53   NS_ASSERT ((seconds/OLSR_C) < (1 << b));
    54   b--;
    54   b--;
    70   // now, 'a' and 'b' should be integers between 0 and 15,
    70   // now, 'a' and 'b' should be integers between 0 and 15,
    71   NS_ASSERT (a >= 0 && a < 16);
    71   NS_ASSERT (a >= 0 && a < 16);
    72   NS_ASSERT (b >= 0 && b < 16);
    72   NS_ASSERT (b >= 0 && b < 16);
    73 
    73 
    74   // the field will be a byte holding the value a*16+b
    74   // the field will be a byte holding the value a*16+b
    75   return (uint8_t) ((a << 4) | b);
    75   return (uint8_t)((a << 4) | b);
    76 }
    76 }
    77 
    77 
    78 ///
    78 ///
    79 /// \brief Converts a number of seconds in the mantissa/exponent format to a decimal number.
    79 /// \brief Converts a number of seconds in the mantissa/exponent format to a decimal number.
    80 ///
    80 ///
    95 // ---------------- OLSR Packet -------------------------------
    95 // ---------------- OLSR Packet -------------------------------
    96 
    96 
    97 NS_OBJECT_ENSURE_REGISTERED (PacketHeader);
    97 NS_OBJECT_ENSURE_REGISTERED (PacketHeader);
    98 
    98 
    99 PacketHeader::PacketHeader ()
    99 PacketHeader::PacketHeader ()
   100 {}
   100 {
       
   101 }
   101 
   102 
   102 PacketHeader::~PacketHeader ()
   103 PacketHeader::~PacketHeader ()
   103 {}
   104 {
       
   105 }
   104 
   106 
   105 TypeId
   107 TypeId
   106 PacketHeader::GetTypeId (void)
   108 PacketHeader::GetTypeId (void)
   107 {
   109 {
   108   static TypeId tid = TypeId ("ns3::olsr::PacketHeader")
   110   static TypeId tid = TypeId ("ns3::olsr::PacketHeader")
   109     .SetParent<Header> ()
   111     .SetParent<Header> ()
   110     .AddConstructor<PacketHeader> ()
   112     .AddConstructor<PacketHeader> ()
   111     ;
   113   ;
   112   return tid;
   114   return tid;
   113 }
   115 }
   114 TypeId
   116 TypeId
   115 PacketHeader::GetInstanceTypeId (void) const
   117 PacketHeader::GetInstanceTypeId (void) const
   116 {
   118 {
   151 
   153 
   152 NS_OBJECT_ENSURE_REGISTERED (MessageHeader);
   154 NS_OBJECT_ENSURE_REGISTERED (MessageHeader);
   153 
   155 
   154 MessageHeader::MessageHeader ()
   156 MessageHeader::MessageHeader ()
   155   : m_messageType (MessageHeader::MessageType (0))
   157   : m_messageType (MessageHeader::MessageType (0))
   156 {}
   158 {
       
   159 }
   157 
   160 
   158 MessageHeader::~MessageHeader ()
   161 MessageHeader::~MessageHeader ()
   159 {}
   162 {
       
   163 }
   160 
   164 
   161 TypeId
   165 TypeId
   162 MessageHeader::GetTypeId (void)
   166 MessageHeader::GetTypeId (void)
   163 {
   167 {
   164   static TypeId tid = TypeId ("ns3::olsr::MessageHeader")
   168   static TypeId tid = TypeId ("ns3::olsr::MessageHeader")
   165     .SetParent<Header> ()
   169     .SetParent<Header> ()
   166     .AddConstructor<MessageHeader> ()
   170     .AddConstructor<MessageHeader> ()
   167     ;
   171   ;
   168   return tid;
   172   return tid;
   169 }
   173 }
   170 TypeId
   174 TypeId
   171 MessageHeader::GetInstanceTypeId (void) const
   175 MessageHeader::GetInstanceTypeId (void) const
   172 {
   176 {
   302 {
   306 {
   303   Buffer::Iterator i = start;
   307   Buffer::Iterator i = start;
   304 
   308 
   305   this->interfaceAddresses.clear ();
   309   this->interfaceAddresses.clear ();
   306   NS_ASSERT (messageSize % IPV4_ADDRESS_SIZE == 0);
   310   NS_ASSERT (messageSize % IPV4_ADDRESS_SIZE == 0);
   307   
   311 
   308   int numAddresses = messageSize / IPV4_ADDRESS_SIZE;
   312   int numAddresses = messageSize / IPV4_ADDRESS_SIZE;
   309   this->interfaceAddresses.erase (this->interfaceAddresses.begin(),
   313   this->interfaceAddresses.erase (this->interfaceAddresses.begin(),
   310                                   this->interfaceAddresses.end ());
   314                                   this->interfaceAddresses.end ());
   311   for (int n = 0; n < numAddresses; ++n)
   315   for (int n = 0; n < numAddresses; ++n)
   312       this->interfaceAddresses.push_back (Ipv4Address (i.ReadNtohU32 ()));
   316     this->interfaceAddresses.push_back (Ipv4Address (i.ReadNtohU32 ()));
   313   return GetSerializedSize ();
   317   return GetSerializedSize ();
   314 }
   318 }
   315 
   319 
   316 
   320 
   317 
   321 
   343   Buffer::Iterator i = start;
   347   Buffer::Iterator i = start;
   344 
   348 
   345   i.WriteU16 (0); // Reserved
   349   i.WriteU16 (0); // Reserved
   346   i.WriteU8 (this->hTime);
   350   i.WriteU8 (this->hTime);
   347   i.WriteU8 (this->willingness);
   351   i.WriteU8 (this->willingness);
   348   
   352 
   349   for (std::vector<LinkMessage>::const_iterator iter = this->linkMessages.begin ();
   353   for (std::vector<LinkMessage>::const_iterator iter = this->linkMessages.begin ();
   350        iter != this->linkMessages.end (); iter++)
   354        iter != this->linkMessages.end (); iter++)
   351     {
   355     {
   352       const LinkMessage &lm = *iter;
   356       const LinkMessage &lm = *iter;
   353 
   357 
   357       // The size of the link message, counted in bytes and measured
   361       // The size of the link message, counted in bytes and measured
   358       // from the beginning of the "Link Code" field and until the
   362       // from the beginning of the "Link Code" field and until the
   359       // next "Link Code" field (or - if there are no more link types
   363       // next "Link Code" field (or - if there are no more link types
   360       // - the end of the message).
   364       // - the end of the message).
   361       i.WriteHtonU16 (4 + lm.neighborInterfaceAddresses.size () * IPV4_ADDRESS_SIZE);
   365       i.WriteHtonU16 (4 + lm.neighborInterfaceAddresses.size () * IPV4_ADDRESS_SIZE);
   362       
   366 
   363       for (std::vector<Ipv4Address>::const_iterator neigh_iter = lm.neighborInterfaceAddresses.begin ();
   367       for (std::vector<Ipv4Address>::const_iterator neigh_iter = lm.neighborInterfaceAddresses.begin ();
   364            neigh_iter != lm.neighborInterfaceAddresses.end (); neigh_iter++)
   368            neigh_iter != lm.neighborInterfaceAddresses.end (); neigh_iter++)
   365         {
   369         {
   366           i.WriteHtonU32 (neigh_iter->Get ());
   370           i.WriteHtonU32 (neigh_iter->Get ());
   367         }
   371         }
   374   Buffer::Iterator i = start;
   378   Buffer::Iterator i = start;
   375 
   379 
   376   NS_ASSERT (messageSize >= 4);
   380   NS_ASSERT (messageSize >= 4);
   377 
   381 
   378   this->linkMessages.clear ();
   382   this->linkMessages.clear ();
   379   
   383 
   380   uint16_t helloSizeLeft = messageSize;
   384   uint16_t helloSizeLeft = messageSize;
   381   
   385 
   382   i.ReadNtohU16 (); // Reserved
   386   i.ReadNtohU16 (); // Reserved
   383   this->hTime = i.ReadU8 ();
   387   this->hTime = i.ReadU8 ();
   384   this->willingness = i.ReadU8 ();
   388   this->willingness = i.ReadU8 ();
   385   
   389 
   386   helloSizeLeft -= 4;
   390   helloSizeLeft -= 4;
   387 
   391 
   388   while (helloSizeLeft)
   392   while (helloSizeLeft)
   389     {
   393     {
   390       LinkMessage lm;
   394       LinkMessage lm;
   443   this->neighborAddresses.clear ();
   447   this->neighborAddresses.clear ();
   444   NS_ASSERT (messageSize >= 4);
   448   NS_ASSERT (messageSize >= 4);
   445 
   449 
   446   this->ansn = i.ReadNtohU16 ();
   450   this->ansn = i.ReadNtohU16 ();
   447   i.ReadNtohU16 (); // Reserved
   451   i.ReadNtohU16 (); // Reserved
   448   
   452 
   449   NS_ASSERT ((messageSize - 4) % IPV4_ADDRESS_SIZE == 0);
   453   NS_ASSERT ((messageSize - 4) % IPV4_ADDRESS_SIZE == 0);
   450   int numAddresses = (messageSize - 4) / IPV4_ADDRESS_SIZE;
   454   int numAddresses = (messageSize - 4) / IPV4_ADDRESS_SIZE;
   451   this->neighborAddresses.clear ();
   455   this->neighborAddresses.clear ();
   452   for (int n = 0; n < numAddresses; ++n)
   456   for (int n = 0; n < numAddresses; ++n)
   453     this->neighborAddresses.push_back (Ipv4Address (i.ReadNtohU32 ()));
   457     this->neighborAddresses.push_back (Ipv4Address (i.ReadNtohU32 ()));
   492   this->associations.clear ();
   496   this->associations.clear ();
   493   for (int n = 0; n < numAddresses; ++n)
   497   for (int n = 0; n < numAddresses; ++n)
   494     {
   498     {
   495       Ipv4Address address (i.ReadNtohU32 ());
   499       Ipv4Address address (i.ReadNtohU32 ());
   496       Ipv4Mask mask (i.ReadNtohU32 ());
   500       Ipv4Mask mask (i.ReadNtohU32 ());
   497       this->associations.push_back ((Association) {address, mask});
   501       this->associations.push_back ((Association) { address, mask});
   498     }
   502     }
   499   return messageSize;
   503   return messageSize;
   500 }
   504 }
   501 
   505 
   502 }} // namespace olsr, ns3
   506 }
   503 
   507 }  // namespace olsr, ns3
       
   508