src/routing/click/ipv4-click-routing.cc
changeset 6289 e3f16dd7456b
parent 6288 9056138c8223
child 6290 4fbbb7485b93
equal deleted inserted replaced
6288:9056138c8223 6289:e3f16dd7456b
    31 #include <map>
    31 #include <map>
    32 
    32 
    33 #include <cstdlib>
    33 #include <cstdlib>
    34 #include <cstdarg>
    34 #include <cstdarg>
    35 
    35 
       
    36 ///
       
    37 /// \brief Gets the delay between a given time and the current time.
       
    38 ///
       
    39 /// If given time is previous to the current one, then this macro returns
       
    40 /// a number close to 0. This is used for scheduling events at a certain moment.
       
    41 ///
       
    42 #define DELAY(time) (((time) < (Simulator::Now ())) ? Seconds (0.000001) : \
       
    43                      (time - Simulator::Now () + Seconds (0.000001)))
       
    44 
    36 namespace ns3 {
    45 namespace ns3 {
    37 
    46 
    38 NS_LOG_COMPONENT_DEFINE ("Ipv4ClickRouting");
    47 NS_LOG_COMPONENT_DEFINE ("Ipv4ClickRouting");
    39 
    48 
    40 std::map < simclick_node_t *, Ptr<Ipv4ClickRouting> > Ipv4ClickRouting::m_clickInstanceFromSimNode;
    49 std::map < simclick_node_t *, Ptr<Ipv4ClickRouting> > Ipv4ClickRouting::m_clickInstanceFromSimNode;
   199   NS_LOG_DEBUG ("HandlePacketFromClick"); 
   208   NS_LOG_DEBUG ("HandlePacketFromClick"); 
   200 
   209 
   201   // Figure out packet's destination here:
   210   // Figure out packet's destination here:
   202   // If ifid == 0, then packet's going up
   211   // If ifid == 0, then packet's going up
   203   // else, packet's going down
   212   // else, packet's going down
   204   
   213 
       
   214   Simulator::Schedule (Seconds (1.0), &Ipv4ClickRouting::SendPacketToClick, this, 1, ptype,data,len);
   205 }
   215 }
   206 
   216 
   207 void
   217 void
   208 Ipv4ClickRouting::SendPacketToClick (int ifid, int ptype, const unsigned char* data, int len)
   218 Ipv4ClickRouting::SendPacketToClick (int ifid, int ptype, const unsigned char* data, int len)
   209 {
   219 {
       
   220   m_simNode->curtime.tv_sec = Simulator::Now ().GetSeconds ();
       
   221   m_simNode->curtime.tv_usec = (Simulator::Now ().GetMicroSeconds () - (Simulator::Now ().GetSeconds () * 1.0e6));
       
   222 
       
   223   // Since packets in ns-3 don't have global Packet ID's and Flow ID's, we
       
   224   // avoid making changes in the Click code and feed dummy values into pinfo
   210   simclick_simpacketinfo pinfo;
   225   simclick_simpacketinfo pinfo;
       
   226   pinfo.id = 0;
       
   227   pinfo.fid = 0;
   211   simclick_click_send(m_simNode,ifid,ptype,data,len,&pinfo);
   228   simclick_click_send(m_simNode,ifid,ptype,data,len,&pinfo);
       
   229 }
       
   230 
       
   231 void
       
   232 Ipv4ClickRouting::Send (Ptr<Packet> p, Ipv4Address src, Ipv4Address dst, uint16_t protocol)
       
   233 {
       
   234   uint32_t ifid;
       
   235 
       
   236   for (ifid = 0; ifid < m_ipv4->GetNInterfaces (); ifid++)
       
   237     {
       
   238       Ipv4Address addr = m_ipv4->GetAddress (ifid, 0).GetLocal ();
       
   239 
       
   240       if (addr == src)
       
   241         {
       
   242           break;
       
   243         }
       
   244     }
       
   245 
       
   246   NS_ASSERT (ifid < m_ipv4->GetNInterfaces ()); // I doubt this scenario would arise, but just playing safe.
       
   247 
       
   248   uint8_t *buf = new uint8_t [p->GetSerializedSize ()];
       
   249   p->Serialize (buf, p->GetSerializedSize ());
       
   250 
       
   251   SendPacketToClick ((int) ifid, 1, buf, (int) p->GetSerializedSize ()); 
   212 }
   252 }
   213 
   253 
   214 void
   254 void
   215 Ipv4ClickRouting::TestInit ()
   255 Ipv4ClickRouting::TestInit ()
   216 {
   256 {
   217 //  NS_LOG_DEBUG ("GetNInterfaces () : " << m_ipv4->GetNInterfaces ());
   257 //  NS_LOG_DEBUG ("GetNInterfaces () : " << m_ipv4->GetNInterfaces ());
   218 //  DoStart (); // Call DoStart for now, because we are not using Helpers yet
   258 //  DoStart (); // Call DoStart for now, because we are not using Helpers yet
   219 
   259 
   220 
   260 /*
   221 //  simclick_simpacketinfo pinfo;
   261   simclick_simpacketinfo pinfo;
   222 //  simnode_->curtime = *when;
   262   pinfo.id = 2;
   223 //  pinfo.id = 2;
   263   pinfo.fid =2;
   224 //  pinfo.fid =2;
   264 */
   225 //  simclick_click_send(simnode_,ifid_,ptype_,data_,len_,&pinfo);
   265 
   226 
   266   Ptr<Packet> p = Create<Packet> (100);
   227 //  simclick_click_send(m_simNode,1,-1,0,100,&pinfo);
   267 
       
   268   Send (p, (Ipv4Address) "172.16.1.1", (Ipv4Address) "172.16.1.3",2);
       
   269 
       
   270 //  m_simNode->curtime.tv_sec = Simulator::Now ().GetSeconds ();
       
   271 //  simclick_click_send(m_simNode,1,1,buf,p->GetSerializedSize (),&pinfo);
   228 }
   272 }
   229 
   273 
   230 } // namespace ns3
   274 } // namespace ns3
   231 
   275 
   232 #ifdef __cplusplus
   276 #ifdef __cplusplus
   328       {
   372       {
   329         const struct timeval *when = va_arg(val, const struct timeval *);
   373         const struct timeval *when = va_arg(val, const struct timeval *);
   330         double simtime = when->tv_sec + (when->tv_usec / 1.0e6);
   374         double simtime = when->tv_sec + (when->tv_usec / 1.0e6);
   331         double simdelay = simtime - Simulator::Now ().GetSeconds (); 
   375         double simdelay = simtime - Simulator::Now ().GetSeconds (); 
   332 
   376 
   333 
       
   334         Simulator::Schedule (Seconds(simdelay), &Ipv4ClickRouting::HandleScheduleFromClick, clickInstance, when); //Testing
   377         Simulator::Schedule (Seconds(simdelay), &Ipv4ClickRouting::HandleScheduleFromClick, clickInstance, when); //Testing
   335 
   378 
   336 //        clickInstance->HandleScheduleFromClick (simnode,when);
   379 //        clickInstance->HandleScheduleFromClick (simnode,when);
   337         NS_LOG_DEBUG (clickInstance->GetNodeName () << " SIMCLICK_SCHEDULE: "<< simtime << " " << Simulator::Now ().GetSeconds () << " " <<simdelay);
   380         NS_LOG_DEBUG (clickInstance->GetNodeName () << " SIMCLICK_SCHEDULE: "<< simtime << " " << Simulator::Now ().GetSeconds () << " " <<simdelay);
   338 
   381