Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -*- 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;
//! Command to run a simulation step.
typedef CommandBase<0x02, IntegerData> 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 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, DISTANCE_REQUEST);
CodingTraits<std::string>::Append (buffer, "");
CodingTraits<UByte>::Append (buffer, 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 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, POSITION_CONVERSION);
CodingTraits<std::string>::Append (buffer, "");
CodingTraits<UByte>::Append (buffer, 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 */