Page MenuHomePhorge

messaging-system.cpp
No OneTemporary

Size
4 KB
Referenced Files
None
Subscribers
None

messaging-system.cpp

/*******************************************************************************
*
* File: messaging-system.cpp
*
* Contents: A simple example on the MessagingSystem and Agent classes of the
* RoSA Core library.
*
* Copyright 2017
*
* Author: David Juhasz (david.juhasz@tuwien.ac.at)
*
******************************************************************************/
#include "rosa/config/version.h"
#include "rosa/core/Agent.hpp"
#include "rosa/core/MessagingSystem.hpp"
#include "rosa/support/log.h"
#include "rosa/support/terminal_colors.h"
#include <iostream>
using namespace rosa;
using namespace rosa::terminal;
// A dummy wrapper for testing MessagingSystem.
// NOTE: Since we test MessagingSystem directly here, we need to get access to
// its protected members. That we do by imitating to be a decent subclass of
// MessagingSystem, while calling protected member functions on an object of a
// type from which we actually don't inherit.
struct SystemTester : protected MessagingSystem {
template <typename T, typename Fun, typename... Funs>
static AgentHandle createMyAgent(MessagingSystem *S, const std::string &Name,
Fun &&F, Funs &&... Fs) {
return ((SystemTester *)S)
->createAgent<T>(Name, std::move(F), std::move(Fs)...);
}
static void destroyMyAgent(MessagingSystem *S, const AgentHandle &H) {
((SystemTester *)S)->destroyUnit(H.agent());
}
template <typename Type, typename... Types>
static void sendToMyAgent(MessagingSystem *S, const AgentHandle &H,
const Type &T, const Types &... Ts) {
((SystemTester *)S)->send<Type, Types...>(H, T, Ts...);
}
template <typename Type, typename... Types>
static void sendToMyAgent(MessagingSystem *S, const AgentHandle &H, Type &&T,
Types &&... Ts) {
((SystemTester *)S)
->send<Type, Types...>(H, std::move(T), std::move(Ts)...);
}
};
int main(void) {
LOG_INFO_STREAM << library_string() << " -- " << Color::Red
<< "messaging-system example" << Color::Default << std::endl;
std::unique_ptr<MessagingSystem> S = MessagingSystem::createSystem("Sys");
MessagingSystem *SP = S.get();
AgentHandle Agent1 = SystemTester::createMyAgent<Agent>(
SP, "Agent1", Invoker::F<std::string>([](const std::string &M) noexcept {
LOG_INFO("Agent1: " + M);
}));
using PrintAtom = AtomConstant<atom("print")>;
using ForwardAtom = AtomConstant<atom("forward")>;
AgentHandle Agent2 = SystemTester::createMyAgent<Agent>(
SP, "Agent2",
Invoker::F<PrintAtom, uint8_t>([](PrintAtom, uint8_t N) noexcept {
LOG_INFO("Agent2: " + std::to_string(N));
}),
Invoker::F<ForwardAtom, uint8_t>([&Agent1](ForwardAtom,
uint8_t N) noexcept {
if (Agent1) {
Agent1.send(std::to_string(N));
} else {
LOG_INFO("Agent2 cannot forward: Agent1 is not valid");
}
}));
LOG_INFO_STREAM << std::endl
<< "Agent1 is valid: " << bool(Agent1) << std::endl
<< "Agent2 is valid: " << bool(Agent2) << std::endl;
LOG_INFO("Sending a print-message to Agent2...");
SystemTester::sendToMyAgent<PrintAtom, uint8_t>(SP, Agent2, PrintAtom::Value,
42);
LOG_INFO("Sending a forward-message to Agent2...");
SystemTester::sendToMyAgent<ForwardAtom, uint8_t>(SP, Agent2,
ForwardAtom::Value, 42);
LOG_INFO("Sending an unexpected message to Agent2...");
SystemTester::sendToMyAgent<unit_t>(SP, Agent2, unit);
SystemTester::destroyMyAgent(SP, Agent1);
LOG_INFO_STREAM << std::endl
<< "Agent1 is valid: " << bool(Agent1) << std::endl
<< "Agent2 is valid: " << bool(Agent2) << std::endl;
LOG_INFO("Sending a forward-message to Agent2...");
SystemTester::sendToMyAgent<ForwardAtom, uint8_t>(SP, Agent2,
ForwardAtom::Value, 42);
SystemTester::destroyMyAgent(SP, Agent2);
return 0;
}

File Metadata

Mime Type
text/x-c++
Expires
Sun, Mar 1, 10:24 PM (13 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
284353
Default Alt Text
messaging-system.cpp (4 KB)

Event Timeline