-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathTraCIDemo11p.cc
119 lines (105 loc) · 3.97 KB
/
TraCIDemo11p.cc
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
//
// Copyright (C) 2006-2011 Christoph Sommer <christoph.sommer@uibk.ac.at>
//
// Documentation for these modules is at http://veins.car2x.org/
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// 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
//
#include "veins/modules/application/traci/TraCIDemo11p.h"
#include "veins/modules/application/traci/TraCIDemo11pMessage_m.h"
using namespace veins;
Define_Module(veins::TraCIDemo11p);
void TraCIDemo11p::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
if (stage == 0) {
sentMessage = false;
lastDroveAt = simTime();
currentSubscribedServiceId = -1;
}
}
void TraCIDemo11p::onWSA(DemoServiceAdvertisment* wsa)
{
if (currentSubscribedServiceId == -1) {
mac->changeServiceChannel(static_cast<Channel>(wsa->getTargetChannel()));
currentSubscribedServiceId = wsa->getPsid();
if (currentOfferedServiceId != wsa->getPsid()) {
stopService();
startService(static_cast<Channel>(wsa->getTargetChannel()), wsa->getPsid(), "Mirrored Traffic Service");
}
}
}
void TraCIDemo11p::onWSM(BaseFrame1609_4* frame)
{
TraCIDemo11pMessage* wsm = check_and_cast<TraCIDemo11pMessage*>(frame);
findHost()->getDisplayString().setTagArg("i", 1, "green");
if (mobility->getRoadId()[0] != ':') traciVehicle->changeRoute(wsm->getDemoData(), 9999);
if (!sentMessage) {
sentMessage = true;
// repeat the received traffic update once in 2 seconds plus some random delay
wsm->setSenderAddress(myId);
wsm->setSerial(3);
scheduleAt(simTime() + 2 + uniform(0.01, 0.2), wsm->dup());
}
}
void TraCIDemo11p::handleSelfMsg(cMessage* msg)
{
if (TraCIDemo11pMessage* wsm = dynamic_cast<TraCIDemo11pMessage*>(msg)) {
// send this message on the service channel until the counter is 3 or higher.
// this code only runs when channel switching is enabled
sendDown(wsm->dup());
wsm->setSerial(wsm->getSerial() + 1);
if (wsm->getSerial() >= 3) {
// stop service advertisements
stopService();
delete (wsm);
}
else {
scheduleAt(simTime() + 1, wsm);
}
}
else {
DemoBaseApplLayer::handleSelfMsg(msg);
}
}
void TraCIDemo11p::handlePositionUpdate(cObject* obj)
{
DemoBaseApplLayer::handlePositionUpdate(obj);
// stopped for for at least 10s?
if (mobility->getSpeed() < 1) {
if (simTime() - lastDroveAt >= 10 && sentMessage == false) {
findHost()->getDisplayString().setTagArg("i", 1, "red");
sentMessage = true;
TraCIDemo11pMessage* wsm = new TraCIDemo11pMessage();
populateWSM(wsm);
wsm->setDemoData(mobility->getRoadId().c_str());
// host is standing still due to crash
if (dataOnSch) {
startService(Channel::sch2, 42, "Traffic Information Service");
// started service and server advertising, schedule message to self to send later
scheduleAt(computeAsynchronousSendingTime(1, ChannelType::service), wsm);
}
else {
// send right away on CCH, because channel switching is disabled
sendDown(wsm);
}
}
}
else {
lastDroveAt = simTime();
}
}