Skip to content
Snippets Groups Projects
traci-commands.h 6.77 KiB
Newer Older
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2012 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>
 */
#ifndef TRACI_COMMANDS_H
#define TRACI_COMMANDS_H

#include <string>
#include "traci-command.h"
#include "traci-coding-traits.h"
#include "traci-constants.h"

namespace ns3 {
namespace traci {
namespace command {

/*
 * Control-related commands.
 */

typedef CommandBase<0x00, NullData> GetVersion;
typedef CommandBase<0x7f, NullData> Close;

/*!
 * @brief Data formed of a single value of a known type.
 */
template<typename T>
class SingleValueData {
public:
  SingleValueData () {}
  void SetValue (T v) { m_value = v; }
protected:
  int GetLength () const { return CodingTraits<T>::Size (m_value); }

  bool Serialize (BufferType& buffer) const {
    CodingTraits<T>::Append (buffer, m_value);
    return true;
  }

  T m_value;
};

//! Integer data.
typedef SingleValueData<int> IntegerData;

//! Double data.
typedef SingleValueData<double> DoubleData;

//! Command to run a simulation step.
typedef CommandBase<0x02, DoubleData> SimulationStep;

//! String data.
typedef SingleValueData<std::string> StringData;

//! String list data
typedef SingleValueData<traci::StringList> StringListData;

//! Command to run SUMO - only relevant if the SUMO launcher is used.
typedef CommandBase<0x76, StringListData> RunSumo;

/*!
 * @brief Command to copy a file - only relevance if the SUMO launcher is used.
 *
 * The list is expected to have two entries:
 * 1. name of the file to write
 * 2. data of the file
 */
typedef CommandBase<0x75, StringListData> CopyFile;


/*!
 * @brief Request the value of a single variable.
 */
class VariableRequest : public Command {
public:
  VariableRequest(UByte command, UByte variable, std::string object = "");

  void SetCommand (UByte command)     { m_command = command; }
  void SetVariable (UByte variable)   { m_variable = variable; }
  void SetObject (std::string object) { m_object = object; }

protected:

  virtual int DoGetId () const;
  virtual int DoGetDataLength () const;
  virtual bool DoSerializeData (BufferType& buffer) const;

  UByte m_command;
  UByte m_variable;
  std::string m_object;
};


/*!
 * @brief Sends the value of a single variable.
 */
template<typename VariableType>
class VariablePost : public Command {
public:
  VariablePost(UByte command, UByte variable, VariableType value, std::string object = "")
    : m_command(command), m_variable(variable), m_object(object), m_value(value)
  {}

  void SetCommand (UByte command)     { m_command = command; }
  void SetVariable (UByte variable)   { m_variable = variable; }
  void SetObject (std::string object) { m_object = object; }
  void Setvalue (VariableType value)  { m_value = value; }

protected:

  virtual int DoGetId () const override{ return m_command; }
  virtual int DoGetDataLength () const override{
    return 2 * sizeof(UByte) + CodingTraits<std::string>::Size (m_object) + CodingTraits<VariableType>::Size(m_value);
  }
  virtual bool DoSerializeData (BufferType& buffer) const override{
    // Write type of variable
    CodingTraits<UByte>::Append (buffer, m_variable);
    // Write name of node
    CodingTraits<std::string>::Append (buffer, m_object);
    // Write data type of variable
    CodingTraits<UByte>::Append (buffer, traci::CodingTraits<VariableType>::IDENTIFIER);
    // Write value of variable
    CodingTraits<VariableType>::Append (buffer, m_value);

    return true;
  }

  UByte m_command;
  UByte m_variable;
  std::string m_object;
  VariableType m_value;
};


template<typename PositionType>
class DistanceRequest : public Command
{
public:

  DistanceRequest(PositionType from, PositionType to, bool roadDistance)
    : m_from (from), m_to (to), m_roadDistance (roadDistance)
  {
  }

private:

  int DoGetId () const override
  {
    return libsumo::CMD_GET_SIM_VARIABLE;
  }

  int DoGetDataLength () const override
  {
    return
	1 +
	CodingTraits<std::string>::Size ("") +
	1 +	// compound variable
	CodingTraits<int>::Size (3) + // constant 3
	1 + CodingTraits<PositionType>::Size (m_from) +
	1 + CodingTraits<PositionType>::Size (m_to) +
	1; // distance type
  }

  bool DoSerializeData (BufferType& buffer) const override
  {
    CodingTraits<UByte>::Append (buffer, libsumo::DISTANCE_REQUEST);
    CodingTraits<std::string>::Append (buffer, "");
    CodingTraits<UByte>::Append (buffer, libsumo::TYPE_COMPOUND);
    CodingTraits<int>::Append (buffer, 3);
    CodingTraits<UByte>::Append (buffer, CodingTraits<PositionType>::IDENTIFIER);
    CodingTraits<PositionType>::Append (buffer, m_from);
    CodingTraits<UByte>::Append (buffer, CodingTraits<PositionType>::IDENTIFIER);
    CodingTraits<PositionType>::Append (buffer, m_to);
    CodingTraits<UByte>::Append (buffer, (m_roadDistance) ? 1 : 0);
    return true;
  }

  PositionType m_from;
  PositionType m_to;
  bool m_roadDistance;
};

template<typename FromPositionType, typename ToPositionType>
class PositionConversion : public Command
{
public:

  PositionConversion(FromPositionType from)
    : m_from (from)
  {
  }

private:

  int DoGetId () const override
  {
    return libsumo::CMD_GET_SIM_VARIABLE;
  }

  int DoGetDataLength () const override
  {
    return
	1 +
	CodingTraits<std::string>::Size ("") +
	1 +	// compound variable
	CodingTraits<int>::Size (2) + // constant 2
	1 + CodingTraits<FromPositionType>::Size (m_from) +
	1 + CodingTraits<UByte>::Size (CodingTraits<ToPositionType>::IDENTIFIER);
  }

  bool DoSerializeData (BufferType& buffer) const override
  {
    CodingTraits<UByte>::Append (buffer, libsumo::POSITION_CONVERSION);
    CodingTraits<std::string>::Append (buffer, "");
    CodingTraits<UByte>::Append (buffer, libsumo::TYPE_COMPOUND);
    CodingTraits<int>::Append (buffer, 2);
    CodingTraits<UByte>::Append (buffer, CodingTraits<FromPositionType>::IDENTIFIER);
    CodingTraits<FromPositionType>::Append (buffer, m_from);
    CodingTraits<UByte>::Append (buffer, CodingTraits<UByte>::IDENTIFIER);
    CodingTraits<UByte>::Append (buffer, CodingTraits<ToPositionType>::IDENTIFIER);
    return true;
  }

  FromPositionType m_from;
};


}  // namespace command
}  // namespace traci
}  // namespace ns3

#endif /* TRACI_COMMANDS_H */