Skip to content
Snippets Groups Projects
traci-node-lifecycle.cc 3.46 KiB
Newer Older
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2016 Fraunhofer ESK
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author:Karsten Roscher <karsten.roscher@esk.fraunhofer.de>
 */
#include "traci-node-lifecycle.h"

#include <utility>
#include "ns3/simulator.h"
#include "ns3/log.h"

#include "ns3/node-lifecycle-manager.h"

NS_LOG_COMPONENT_DEFINE ("TraCiNodeLifecyclePolicy");

namespace ns3
{

TraCiNodeLifecyclePolicy::TraCiNodeLifecyclePolicy()
  : m_earliestActivationTime (Seconds (0))
{

}

TraCiNodeLifecyclePolicy::~TraCiNodeLifecyclePolicy()
{
  // cancel pending events - just to be sure
  for (ActivationMap::iterator it = m_waitingNodes.begin ();
      it != m_waitingNodes.end (); ++it)
    {
      it->second.Cancel ();
    }
}

void
TraCiNodeLifecyclePolicy::SetRandomActivationDelay (Ptr<RandomVariableStream> rand)
{
  m_randomDelay = rand;
}

void
TraCiNodeLifecyclePolicy::SetEarliestActivationTime (Time startTime)
{
  m_earliestActivationTime = startTime;
}

void
TraCiNodeLifecyclePolicy::HandleActivateNode (Ptr<Node> node, Ptr<traci::Vehicle> vehicle)
{
  // get current time
  Time now = Simulator::Now ();

  if (m_randomDelay)
    {
      // calculate time to wait in case we are before the activation time
      // will be 0 if now >= earliest time
      Time timeToWait = std::max (m_earliestActivationTime, now) - now;

      // schedule for later
      EventId e = Simulator::Schedule (timeToWait + Seconds (m_randomDelay->GetValue ()),
			   &TraCiNodeLifecyclePolicy::HandleDelayedActivation, this, node);

      m_waitingNodes.insert (std::make_pair (node, e));
    }
  else
    {
      if (now >= m_earliestActivationTime)
	{
	  // activate right away
	  ActivateNode (node);
	}
      else
	{
	  // schedule for the earliest activation time
	  EventId e = Simulator::Schedule (m_earliestActivationTime - now,
			       &TraCiNodeLifecyclePolicy::HandleDelayedActivation, this, node);

	  m_waitingNodes.insert (std::make_pair (node, e));
	}
    }
}

void
TraCiNodeLifecyclePolicy::HandleShutdownNode (Ptr<Node> node)
{
  ActivationMap::iterator it = m_waitingNodes.find (node);
  if (it != m_waitingNodes.end ())
    {
      NS_LOG_LOGIC ("Node to shut down is still waiting for activation");

      // cancel pending event to avoid activation later
      it->second.Cancel ();

      // remove entry from map
      m_waitingNodes.erase (it);
    }

  // deactivate node - does not hurt even if it was not activated yet
  DeactivateNode (node);
}

void
TraCiNodeLifecyclePolicy::HandleDelayedActivation (Ptr<Node> node)
{
  ActivationMap::iterator it = m_waitingNodes.find (node);
  if (it != m_waitingNodes.end ())
    {
      NS_LOG_LOGIC ("Removing entry from waiting list");
      m_waitingNodes.erase (it);
    }
  else
    {
      NS_LOG_WARN ("Handling delayed activation but map entry is missing");
    }

  ActivateNode (node);
}

}  // namespace ns3