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
/* -*- 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;
typedef CommandBase<0x02, DoubleData> SimulationStep;
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
//! 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);
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
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;
};
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
class ChangeLane : public Command
{
public:
ChangeLane(std::string vehicle, Byte lane, double duration)
: m_vehicle (vehicle), m_lane (lane), m_duration (duration)
{
}
private:
int DoGetId () const override
{
return libsumo::CMD_SET_VEHICLE_VARIABLE;
}
int DoGetDataLength () const override
{
return
1 + CodingTraits<std::string>::Size (m_vehicle) +
1 + CodingTraits<int>::Size (2) + // compound variable
2 + // byte type
1 + CodingTraits<double>::Size (m_duration); // double type
}
bool DoSerializeData (BufferType& buffer) const override
{
CodingTraits<UByte>::Append (buffer, libsumo::CMD_CHANGELANE);
CodingTraits<std::string>::Append (buffer, m_vehicle);
CodingTraits<UByte>::Append (buffer, libsumo::TYPE_COMPOUND);
CodingTraits<int>::Append (buffer, 2);
CodingTraits<UByte>::Append (buffer, CodingTraits<Byte>::IDENTIFIER);
CodingTraits<Byte>::Append (buffer, m_lane);
CodingTraits<UByte>::Append (buffer, CodingTraits<double>::IDENTIFIER);
CodingTraits<double>::Append (buffer, m_duration);
return true;
}
std::string m_vehicle;
Byte m_lane;
double m_duration;
};
class SetSpeed : public Command
{
public:
SetSpeed(std::string vehicle, double speed)
: m_vehicle (vehicle), m_speed (speed)
{
}
private:
int DoGetId () const override
{
return libsumo::CMD_SET_VEHICLE_VARIABLE;
}
int DoGetDataLength () const override
{
return
1 + CodingTraits<std::string>::Size (m_vehicle) +
1 + CodingTraits<double>::Size (m_speed);
}
bool DoSerializeData (BufferType& buffer) const override
{
CodingTraits<UByte>::Append (buffer, libsumo::CMD_SPEED);
CodingTraits<std::string>::Append (buffer, m_vehicle);
CodingTraits<UByte>::Append (buffer, CodingTraits<double>::IDENTIFIER);
CodingTraits<double>::Append (buffer, m_speed);
return true;
}
std::string m_vehicle;
double m_speed;
};
class SlowDown : public Command
{
public:
SlowDown(std::string vehicle, double speed, double duration)
: m_vehicle (vehicle), m_speed (speed), m_duration (duration)
{
}
private:
int DoGetId () const override
{
return libsumo::CMD_SET_VEHICLE_VARIABLE;
}
int DoGetDataLength () const override
{
return
1 + CodingTraits<std::string>::Size (m_vehicle) +
1 + CodingTraits<int>::Size (2) + // compound variable
1 + CodingTraits<double>::Size (m_speed) + // double type
1 + CodingTraits<double>::Size (m_duration); // double type
}
bool DoSerializeData (BufferType& buffer) const override
{
CodingTraits<UByte>::Append (buffer, libsumo::CMD_SLOWDOWN);
CodingTraits<std::string>::Append (buffer, m_vehicle);
CodingTraits<UByte>::Append (buffer, libsumo::TYPE_COMPOUND);
CodingTraits<int>::Append (buffer, 2);
CodingTraits<UByte>::Append (buffer, CodingTraits<double>::IDENTIFIER);
CodingTraits<double>::Append (buffer, m_speed);
CodingTraits<UByte>::Append (buffer, CodingTraits<double>::IDENTIFIER);
CodingTraits<double>::Append (buffer, m_duration);
return true;
}
std::string m_vehicle;
double m_speed;
double m_duration;
};
class MoveToXY : public Command
{
public:
MoveToXY(std::string vehicle, std::string edge, int lane, double x, double y, double angle)
: m_vehicle (vehicle), m_edge (edge), m_lane (lane), m_x (x), m_y (y), m_angle (angle)
{
}
private:
int DoGetId () const override
{
return libsumo::CMD_SET_VEHICLE_VARIABLE;
}
int DoGetDataLength () const override
{
return
1 + CodingTraits<std::string>::Size (m_vehicle) +
1 + CodingTraits<int>::Size (5) + // compound variable
1 + CodingTraits<std::string>::Size (m_edge) + // string type
1 + CodingTraits<int>::Size (m_lane) + // int type
1 + CodingTraits<double>::Size (m_x) + // double type
1 + CodingTraits<double>::Size (m_y) + // double type
1 + CodingTraits<double>::Size (m_angle); // double type
}
bool DoSerializeData (BufferType& buffer) const override
{
CodingTraits<UByte>::Append (buffer, libsumo::MOVE_TO_XY);
CodingTraits<std::string>::Append (buffer, m_vehicle);
CodingTraits<UByte>::Append (buffer, libsumo::TYPE_COMPOUND);
CodingTraits<int>::Append (buffer, 5);
CodingTraits<UByte>::Append (buffer, CodingTraits<std::string>::IDENTIFIER);
CodingTraits<std::string>::Append (buffer, m_edge);
CodingTraits<UByte>::Append (buffer, CodingTraits<int>::IDENTIFIER);
CodingTraits<int>::Append (buffer, m_lane);
CodingTraits<UByte>::Append (buffer, CodingTraits<double>::IDENTIFIER);
CodingTraits<double>::Append (buffer, m_x);
CodingTraits<UByte>::Append (buffer, CodingTraits<double>::IDENTIFIER);
CodingTraits<double>::Append (buffer, m_y);
CodingTraits<UByte>::Append (buffer, CodingTraits<double>::IDENTIFIER);
CodingTraits<double>::Append (buffer, m_angle);
return true;
}
std::string m_vehicle;
std::string m_edge;
int m_lane;
double m_x;
double m_y;
double m_angle;
};