Skip to content
Snippets Groups Projects
traci-object-acceptor.cc 4.38 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-object-acceptor.h"

#include "ns3/assert.h"
#include "ns3/double.h"

namespace ns3 {
namespace traci {

/******************************************************************************
 * AcceptAlways
 ******************************************************************************/

bool
AcceptAlways::AcceptObject (std::string objectId)
{
  return true;
}

/******************************************************************************
 * AcceptIfAll
 ******************************************************************************/

void
AcceptIfAll::AddAcceptor (Ptr<TraCiObjectAcceptor> a)
{
  if (a)
    {
      m_acceptors.push_back (a);
    }
}


bool
AcceptIfAll::AcceptObject (std::string objectId)
{
  if (m_acceptors.empty ())
    {
      // no acceptors, nobody can accept
      return false;
    }

  for (AcceptorList::iterator it = m_acceptors.begin (); it != m_acceptors.end (); ++it)
    {
      if (!(*it)->AcceptObject (objectId))
	{
	  // one entry does not accept
	  return false;
	}
    }
  return true;
}

/******************************************************************************
 * AcceptIfAny
 ******************************************************************************/

void
AcceptIfAny::AddAcceptor (Ptr<TraCiObjectAcceptor> a)
{
  if (a)
    {
      m_acceptors.push_back (a);
    }
}


bool
AcceptIfAny::AcceptObject (std::string objectId)
{
  if (m_acceptors.empty ())
    {
      // no acceptors, nobody can accept
      return false;
    }

  for (AcceptorList::iterator it = m_acceptors.begin (); it != m_acceptors.end (); ++it)
    {
      if ((*it)->AcceptObject (objectId))
	{
	  // one entry does accept
	  return true;
	}
    }
  return false;
}

/******************************************************************************
 * AcceptPartial
 ******************************************************************************/

AcceptPartial::AcceptPartial (double rate)
  : m_rate (rate)
{
  m_rand = CreateObject<UniformRandomVariable> ();
  m_rand->SetAttribute ("Min", DoubleValue (0.0));
  m_rand->SetAttribute ("Max", DoubleValue (1.0));
}

void
AcceptPartial::SetRate (double rate)
{
  m_rate = rate;
}

int64_t
AcceptPartial::AssignStreams (int64_t stream)
{
  NS_ASSERT (m_rand);
  m_rand->SetStream (stream);
  return 1;
}

bool
AcceptPartial::AcceptObject (std::string objectId)
{
  return m_rand->GetValue () < m_rate;
}

/******************************************************************************
 * AcceptByStringList
 ******************************************************************************/

AcceptByStringList::AcceptByStringList ()
{
}

AcceptByStringList::AcceptByStringList (std::string entry)
{
  Add (entry);
}

void
AcceptByStringList::Add (std::string entry)
{
  if (entry.empty ())
    {
      // no need to add empty strings
      return;
    }

  if (entry.at (entry.size () - 1) == '*')
    {
      m_entries.push_back (EntryType (entry.substr (0, entry.size ()-1), true));
    }
  else
    {
      m_entries.push_back (EntryType (entry, false));
    }
}

bool
AcceptByStringList::AcceptObject (std::string objectId)
{
  for (EntryList::const_iterator it = m_entries.begin ();
        it != m_entries.end (); ++it)
    {
      if (it->second)
        {
          // perform prefix match
          if (objectId.compare (0, it->first.size (), it->first) == 0)
            {
              return true;
            }
        }
      else
        {
          // perform string match
          if (objectId.compare (it->first) == 0)
            {
              return true;
            }
        }
    }

  return false;
}

}  // namespace traci
}  // namespace ns3