-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demos: Use application base only for comm. demos; Add Orchestration d…
…emos
- Loading branch information
Showing
25 changed files
with
600 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <iostream> | ||
|
||
#include "silkit/SilKit.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
if (argc != 2) | ||
{ | ||
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] << " <ParticipantName>" << std::endl; | ||
return -1; | ||
} | ||
std::string participantName(argv[1]); | ||
|
||
try | ||
{ | ||
// Setup participant, lifecycle, time synchronization and logging | ||
const std::string registryUri = "silkit://localhost:8500"; | ||
const std::string configString = R"({"Logging":{"Sinks":[{"Type":"Stdout","Level":"Info"}]}})"; | ||
auto participantConfiguration = SilKit::Config::ParticipantConfigurationFromString(configString); | ||
|
||
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri); | ||
auto logger = participant->GetLogger(); | ||
|
||
auto* lifecycleService = | ||
participant->CreateLifecycleService({SilKit::Services::Orchestration::OperationMode::Autonomous}); | ||
|
||
// Start the worker thread and wait for the go from the system monitor | ||
auto unleashWorkerThreadPromise = std::promise<void>(); | ||
auto unleashWorkerThreadFuture = unleashWorkerThreadPromise.get_future(); | ||
auto systemMonitor = participant->CreateSystemMonitor(); | ||
systemMonitor->AddParticipantStatusHandler( | ||
[&unleashWorkerThreadPromise, participantName, | ||
logger](const SilKit::Services::Orchestration::ParticipantStatus& participantStatus) { | ||
if (participantStatus.participantName == participantName && participantStatus.state | ||
== SilKit::Services::Orchestration::ParticipantState::Running) | ||
{ | ||
logger->Info("My state is now 'Running'."); | ||
unleashWorkerThreadPromise.set_value(); | ||
} | ||
}); | ||
|
||
// Start the worker thread and wait for the go from the system monitor | ||
auto workerThread = std::thread([&unleashWorkerThreadFuture, lifecycleService, logger]() { | ||
|
||
unleashWorkerThreadFuture.wait(); | ||
for (int i = 0; i < 10; ++i) | ||
{ | ||
std::this_thread::sleep_for(1s); | ||
logger->Info("Simulation stop in " + std::to_string(10-i)); | ||
}; | ||
|
||
logger->Info("Stopping just me."); | ||
lifecycleService->Stop("Stopping just me"); | ||
}); | ||
|
||
// Start and wait until lifecycleService->Stop | ||
logger->Info("Start the participant lifecycle."); | ||
auto finalStateFuture = lifecycleService->StartLifecycle(); | ||
finalStateFuture.get(); | ||
|
||
// Clean up the worker thread | ||
if (workerThread.joinable()) | ||
{ | ||
workerThread.join(); | ||
} | ||
|
||
} | ||
catch (const std::exception& error) | ||
{ | ||
std::cerr << "Something went wrong: " << error.what() << std::endl; | ||
return -3; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
make_silkit_demo(SilKitDemoAutonomous Autonomous.cpp) | ||
make_silkit_demo(SilKitDemoCoordinated Coordinated.cpp) | ||
make_silkit_demo(SilKitDemoSimStep SimStep.cpp) | ||
make_silkit_demo(SilKitDemoSimStepAsync SimStepAsync.cpp) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <iostream> | ||
|
||
#include "silkit/SilKit.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
if (argc != 2) | ||
{ | ||
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] << " <ParticipantName>" << std::endl; | ||
return -1; | ||
} | ||
std::string participantName(argv[1]); | ||
|
||
try | ||
{ | ||
// Setup participant, lifecycle, time synchronization and logging | ||
const std::string registryUri = "silkit://localhost:8500"; | ||
const std::string configString = R"({"Logging":{"Sinks":[{"Type":"Stdout","Level":"Info"}]}})"; | ||
auto participantConfiguration = SilKit::Config::ParticipantConfigurationFromString(configString); | ||
|
||
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri); | ||
auto logger = participant->GetLogger(); | ||
|
||
auto* lifecycleService = | ||
participant->CreateLifecycleService({SilKit::Services::Orchestration::OperationMode::Coordinated}); | ||
|
||
// Start the worker thread and wait for the go from the system monitor | ||
auto unleashWorkerThreadPromise = std::promise<void>(); | ||
auto unleashWorkerThreadFuture = unleashWorkerThreadPromise.get_future(); | ||
auto systemMonitor = participant->CreateSystemMonitor(); | ||
systemMonitor->AddParticipantStatusHandler( | ||
[&unleashWorkerThreadPromise, participantName, | ||
logger](const SilKit::Services::Orchestration::ParticipantStatus& participantStatus) { | ||
if (participantStatus.participantName == participantName | ||
&& participantStatus.state == SilKit::Services::Orchestration::ParticipantState::Running) | ||
{ | ||
logger->Info("The sil-kit-system-controller started the simulation. My state is now 'Running'."); | ||
unleashWorkerThreadPromise.set_value(); | ||
} | ||
}); | ||
|
||
// Start the worker thread and wait for the go from the system monitor | ||
std::atomic<bool> workerThreadDone = false; | ||
auto workerThread = std::thread([&unleashWorkerThreadFuture, &workerThreadDone, lifecycleService, logger]() { | ||
unleashWorkerThreadFuture.wait(); | ||
while (!workerThreadDone) | ||
{ | ||
std::this_thread::sleep_for(1s); | ||
logger->Info("Simulation running. Stop via CTRL-C in the sil-kit-system-controller."); | ||
}; | ||
|
||
}); | ||
|
||
// Start and wait until the sil-kit-system-controller is stopped | ||
logger->Info( | ||
"Start the participant lifecycle and wait for the sil-kit-system-controller to start the simulation."); | ||
auto finalStateFuture = lifecycleService->StartLifecycle(); | ||
finalStateFuture.get(); | ||
|
||
// Clean up the worker thread | ||
workerThreadDone = true; | ||
|
||
if (workerThread.joinable()) | ||
{ | ||
workerThread.join(); | ||
} | ||
} | ||
catch (const std::exception& error) | ||
{ | ||
std::cerr << "Something went wrong: " << error.what() << std::endl; | ||
return -3; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <iostream> | ||
|
||
#include "silkit/SilKit.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp) | ||
{ | ||
out << std::chrono::duration_cast<std::chrono::milliseconds>(timestamp).count() << "ms"; | ||
return out; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
if (argc != 2) | ||
{ | ||
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] << " <ParticipantName>" << std::endl; | ||
return -1; | ||
} | ||
std::string participantName(argv[1]); | ||
|
||
try | ||
{ | ||
// Setup participant, lifecycle, time synchronization and logging | ||
const std::string registryUri = "silkit://localhost:8500"; | ||
const std::string configString = R"({"Logging":{"Sinks":[{"Type":"Stdout","Level":"Info"}]}})"; | ||
auto participantConfiguration = SilKit::Config::ParticipantConfigurationFromString(configString); | ||
|
||
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri); | ||
auto logger = participant->GetLogger(); | ||
|
||
auto* lifecycleService = | ||
participant->CreateLifecycleService({SilKit::Services::Orchestration::OperationMode::Coordinated}); | ||
|
||
auto* timeSyncService = lifecycleService->CreateTimeSyncService(); | ||
|
||
// Simulation steps | ||
const auto stepSize = 1ms; | ||
timeSyncService->SetSimulationStepHandler( | ||
[logger](std::chrono::nanoseconds now, std::chrono::nanoseconds duration) { | ||
std::stringstream ss; | ||
ss << "--------- Simulation step T=" << now << " ---------"; | ||
logger->Info(ss.str()); | ||
}, stepSize); | ||
|
||
auto finalStateFuture = lifecycleService->StartLifecycle(); | ||
finalStateFuture.get(); | ||
} | ||
catch (const std::exception& error) | ||
{ | ||
std::cerr << "Something went wrong: " << error.what() << std::endl; | ||
return -3; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <iostream> | ||
|
||
#include "silkit/SilKit.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp) | ||
{ | ||
out << std::chrono::duration_cast<std::chrono::milliseconds>(timestamp).count() << "ms"; | ||
return out; | ||
} | ||
|
||
std::mutex mx; | ||
bool doStep = false; | ||
std::condition_variable cv; | ||
std::atomic<bool> asyncThreadDone = false; | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
if (argc != 2) | ||
{ | ||
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] << " <ParticipantName>" << std::endl; | ||
return -1; | ||
} | ||
std::string participantName(argv[1]); | ||
|
||
try | ||
{ | ||
// Setup participant, lifecycle, time synchronization and logging | ||
const std::string registryUri = "silkit://localhost:8500"; | ||
const std::string configString = R"({"Logging":{"Sinks":[{"Type":"Stdout","Level":"Info"}]}})"; | ||
auto participantConfiguration = SilKit::Config::ParticipantConfigurationFromString(configString); | ||
|
||
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri); | ||
auto logger = participant->GetLogger(); | ||
|
||
auto* lifecycleService = | ||
participant->CreateLifecycleService({SilKit::Services::Orchestration::OperationMode::Coordinated}); | ||
|
||
auto* timeSyncService = lifecycleService->CreateTimeSyncService(); | ||
|
||
// 1. The simulation step gets called by the SIL Kit Time Synchronization algorithm. | ||
// 2. The simulation step handler unlocks a step in the asyncThread with a condition variable. | ||
// 3. The asyncThread performs some asynchronous operation. | ||
// 4. The asyncThread completes the simulation step. | ||
|
||
// Simulation steps | ||
const auto stepSize = 1ms; | ||
timeSyncService->SetSimulationStepHandlerAsync( | ||
[logger](std::chrono::nanoseconds now, std::chrono::nanoseconds duration) { | ||
std::stringstream ss; | ||
ss << "--------- Simulation step T=" << now << " ---------"; | ||
logger->Info(ss.str()); | ||
|
||
std::unique_lock<decltype(mx)> lock(mx); | ||
doStep = true; | ||
cv.notify_one(); | ||
|
||
}, stepSize); | ||
|
||
auto asyncThread = std::thread([timeSyncService, logger]() { | ||
while (!asyncThreadDone) | ||
{ | ||
std::unique_lock<decltype(mx)> lock(mx); | ||
cv.wait(lock, [] { return doStep; }); | ||
doStep = false; | ||
|
||
logger->Info("Asynchronous operation in the simulation step:"); | ||
logger->Info(" Sending a message to another participant..."); | ||
std::this_thread::sleep_for(0.5s); | ||
logger->Info(" Awaiting the reply of another participant..."); | ||
std::this_thread::sleep_for(0.5s); | ||
logger->Info(" Calling a REST API..."); | ||
std::this_thread::sleep_for(0.5s); | ||
logger->Info("Done."); | ||
|
||
timeSyncService->CompleteSimulationStep(); | ||
} | ||
}); | ||
|
||
auto finalStateFuture = lifecycleService->StartLifecycle(); | ||
finalStateFuture.get(); | ||
|
||
asyncThreadDone = true; | ||
if (asyncThread.joinable()) | ||
{ | ||
asyncThread.join(); | ||
} | ||
|
||
} | ||
catch (const std::exception& error) | ||
{ | ||
std::cerr << "Something went wrong: " << error.what() << std::endl; | ||
return -3; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.