From 89670887aa9d323aff55557e3187add3a121ca66 Mon Sep 17 00:00:00 2001
From: Hoop77 <p.badenhoop@gmx.de>
Date: Wed, 4 Apr 2018 14:05:44 +0200
Subject: [PATCH] request doesn't contain a dstVehicle anymore

---
 .../PlatoonProtocolLib/FollowerVehicle.h      | 10 +----
 .../PlatoonProtocolLib/LeaderVehicle.h        |  2 +-
 .../PlatoonProtocolLib/PlatoonMessage.h       |  5 +--
 .../src/FollowerVehicle.cpp                   | 39 ++++---------------
 .../PlatoonProtocolLib/src/LeaderVehicle.cpp  |  5 +--
 .../PlatoonProtocolLib/test/FollowerTest.cpp  |  4 --
 6 files changed, 13 insertions(+), 52 deletions(-)

diff --git a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/FollowerVehicle.h b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/FollowerVehicle.h
index ebf559a3..b07e6a20 100644
--- a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/FollowerVehicle.h
+++ b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/FollowerVehicle.h
@@ -38,8 +38,6 @@ public:
     void setOnPlatoonConfigUpdatedCallback(Callback callback)
     { onPlatoonConfigUpdatedCallback = callback; }
 
-    void addVehicleToRequest(VehicleId vehicleId);
-
 protected:
     void doCreatePlatoon() override;
 
@@ -53,20 +51,16 @@ protected:
 
 private:
     static constexpr networking::time::Duration RESPONSE_TIMEOUT = std::chrono::milliseconds(3000);
-    static constexpr networking::time::Duration HEARTBEAT_INTERVAL = std::chrono::milliseconds(50);
+    static constexpr networking::time::Duration HEARTBEAT_INTERVAL = std::chrono::milliseconds(200);
     static constexpr networking::time::Duration BROADCAST_TIMEOUT = std::chrono::milliseconds(500);
 
     VehicleId leader;
-    std::vector<VehicleId> vehiclesToRequest;
-    std::vector<VehicleId>::iterator vehiclesToRequestIter;
     networking::time::Timer::Ptr responseTimer;
     networking::time::Timer::Ptr broadcastTimer;
     networking::time::Timer::Ptr heartbeatTimer;
     Callback onPlatoonConfigUpdatedCallback;
 
-    void sendPlatoonCreateRequestToNextVehicle();
-
-    void sendPlatoonCreateRequest(VehicleId vehicleId);
+    void sendPlatoonCreateRequest();
 
     void receivePlatoonCreateRequest(const PlatoonMessage & request);
 
diff --git a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/LeaderVehicle.h b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/LeaderVehicle.h
index c45b828f..d0a48363 100644
--- a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/LeaderVehicle.h
+++ b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/LeaderVehicle.h
@@ -52,7 +52,7 @@ protected:
 
 private:
     static constexpr networking::time::Duration HEARTBEAT_TIMEOUT = std::chrono::milliseconds(1000);
-    static constexpr networking::time::Duration BROADCAST_INTERVAL = std::chrono::milliseconds(10);
+    static constexpr networking::time::Duration BROADCAST_INTERVAL = std::chrono::milliseconds(50);
 
     struct Follower
     {
diff --git a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h
index 6fe9270a..cbcd60a9 100644
--- a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h
+++ b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h
@@ -72,9 +72,8 @@ struct PlatoonMessage
           , followers(followers)
     {}
 
-    static PlatoonMessage followerRequest(VehicleId srcVehicle,
-                                          VehicleId dstVehicle)
-    { return PlatoonMessage(messageTypes::FV_REQUEST, srcVehicle, dstVehicle, 0, 0, 0, {}); }
+    static PlatoonMessage followerRequest(VehicleId srcVehicle)
+    { return PlatoonMessage(messageTypes::FV_REQUEST, srcVehicle, 0, 0, 0, 0, {}); }
 
     static PlatoonMessage rejectResponse(VehicleId srcVehicle,
                                          VehicleId dstVehicle)
diff --git a/modules/catkin_ws/src/PlatoonProtocolLib/src/FollowerVehicle.cpp b/modules/catkin_ws/src/PlatoonProtocolLib/src/FollowerVehicle.cpp
index d32c35b7..78f1027e 100644
--- a/modules/catkin_ws/src/PlatoonProtocolLib/src/FollowerVehicle.cpp
+++ b/modules/catkin_ws/src/PlatoonProtocolLib/src/FollowerVehicle.cpp
@@ -47,11 +47,7 @@ void FollowerVehicle::doCreatePlatoon()
 
     Vehicle::doCreatePlatoon();
 
-    // We cycle through every available endpoint and try to make a create-platoon-request.
-    // Note that we'll be actually starting with the first element in the vector
-    // by setting the iterator to end() due to utils::cycle().
-    vehiclesToRequestIter = vehiclesToRequest.end();
-    sendPlatoonCreateRequestToNextVehicle();
+    sendPlatoonCreateRequest();
 }
 
 void FollowerVehicle::doRunPlatoon()
@@ -99,15 +95,12 @@ void FollowerVehicle::receiveMessage(const PlatoonMessage & message)
     }
 }
 
-void FollowerVehicle::sendPlatoonCreateRequestToNextVehicle()
+void FollowerVehicle::sendPlatoonCreateRequest()
 {
-    if (state != State::CREATING_PLATOON || vehiclesToRequest.empty())
+    if (state != State::CREATING_PLATOON)
         return;
 
-    utils::cycle(vehiclesToRequestIter, vehiclesToRequest);
-
-    auto vehicleToRequest = *vehiclesToRequestIter;
-    sendPlatoonCreateRequest(vehicleToRequest);
+    sendMessage(PlatoonMessage::followerRequest(myInfo.vehicleId), 3s);
 
     auto self = shared_from_this();
     // Create a new one to make sure we're never getting a busy exception.
@@ -115,13 +108,7 @@ void FollowerVehicle::sendPlatoonCreateRequestToNextVehicle()
     responseTimer->startTimeout(
         RESPONSE_TIMEOUT,
         [self]
-        { self->sendPlatoonCreateRequestToNextVehicle(); });
-}
-
-void FollowerVehicle::sendPlatoonCreateRequest(VehicleId vehicleToRequest)
-{
-    // We'll send a request indicating that we want to be the follower.
-    sendMessage(PlatoonMessage::followerRequest(myInfo.vehicleId, vehicleToRequest), 3s);
+        { self->sendPlatoonCreateRequest(); });
 }
 
 void FollowerVehicle::receivePlatoonCreateRequest(const PlatoonMessage & request)
@@ -188,8 +175,7 @@ void FollowerVehicle::receiveBroadcast(const PlatoonMessage & broadcast)
 void FollowerVehicle::receiveResponse(const PlatoonMessage & response)
 {
     if (state != State::CREATING_PLATOON ||
-        response.dstVehicle != myInfo.vehicleId ||
-        response.srcVehicle != *vehiclesToRequestIter)
+        response.dstVehicle != myInfo.vehicleId)
         return;
 
     responseTimer->stop();
@@ -202,7 +188,7 @@ void FollowerVehicle::receiveResponse(const PlatoonMessage & response)
     }
     else
     {
-        sendPlatoonCreateRequestToNextVehicle();
+        sendPlatoonCreateRequest();
     }
 }
 
@@ -211,17 +197,6 @@ void FollowerVehicle::sendLeavePlatoonMessage()
     sendMessage(PlatoonMessage::leaveMessage(myInfo.vehicleId, leader, platoonId), HEARTBEAT_INTERVAL);
 }
 
-void FollowerVehicle::addVehicleToRequest(VehicleId vehicleId)
-{
-    if (vehicleId == 0)
-        throw std::invalid_argument{"Invalid vehicle ID '0'"};
-
-    auto self = shared_from_this();
-    net.callLater(
-        [self, vehicleId]
-        { self->vehiclesToRequest.push_back(vehicleId); });
-}
-
 FollowerVehicle::Ptr FollowerVehicle::shared_from_this()
 {
     return std::static_pointer_cast<FollowerVehicle>(Vehicle::shared_from_this());
diff --git a/modules/catkin_ws/src/PlatoonProtocolLib/src/LeaderVehicle.cpp b/modules/catkin_ws/src/PlatoonProtocolLib/src/LeaderVehicle.cpp
index 0a6afeae..582686e5 100644
--- a/modules/catkin_ws/src/PlatoonProtocolLib/src/LeaderVehicle.cpp
+++ b/modules/catkin_ws/src/PlatoonProtocolLib/src/LeaderVehicle.cpp
@@ -75,10 +75,6 @@ void LeaderVehicle::doLeavePlatoon()
 
 void LeaderVehicle::receiveMessage(const PlatoonMessage & message)
 {
-    // Each message must be directed to us.
-    if (message.dstVehicle != myInfo.vehicleId)
-        return;
-
     switch (message.messageType)
     {
         case messageTypes::FV_HEARTBEAT:
@@ -223,6 +219,7 @@ void LeaderVehicle::removeAllFollowers()
 bool LeaderVehicle::checkFollowerMessage(const PlatoonMessage & message) const
 {
     return state == State::RUNNING_PLATOON &&
+           message.dstVehicle == myInfo.vehicleId &&
            findFollower(message.srcVehicle) &&
            message.platoonId == platoonId;
 }
diff --git a/modules/catkin_ws/src/PlatoonProtocolLib/test/FollowerTest.cpp b/modules/catkin_ws/src/PlatoonProtocolLib/test/FollowerTest.cpp
index 63f5980a..06c38c0f 100644
--- a/modules/catkin_ws/src/PlatoonProtocolLib/test/FollowerTest.cpp
+++ b/modules/catkin_ws/src/PlatoonProtocolLib/test/FollowerTest.cpp
@@ -34,10 +34,6 @@ int main(int argc, char ** argv)
                       << "\nPS=" << config.platoonSpeed << "\n";
         });
 
-    auto numVehiclesToRequest = argc - 3;
-    for (std::size_t i = 0; i < numVehiclesToRequest; i++)
-        follower->addVehicleToRequest(static_cast<VehicleId>(atoi(argv[i + 3])));
-
     follower->createPlatoon();
 
     while (1)
-- 
GitLab