mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
SDK: Add Topology::AsyncSetProperties
Co-Author: Alexey Rybalchenko <alexryba@gmail.com>
This commit is contained in:
committed by
Dennis Klein
parent
25658370fa
commit
1c8ad03f3c
@@ -27,6 +27,8 @@ std::string ErrorCategory::message(int ev) const
|
||||
return "async operation canceled";
|
||||
case ErrorCode::DeviceChangeStateFailed:
|
||||
return "failed to change state of a fairmq device";
|
||||
case ErrorCode::DeviceSetPropertiesFailed:
|
||||
return "failed to set fairmq device properties";
|
||||
default:
|
||||
return "(unrecognized error)";
|
||||
}
|
||||
|
@@ -37,7 +37,8 @@ enum class ErrorCode
|
||||
OperationInProgress = 10,
|
||||
OperationTimeout,
|
||||
OperationCanceled,
|
||||
DeviceChangeStateFailed
|
||||
DeviceChangeStateFailed,
|
||||
DeviceSetPropertiesFailed
|
||||
};
|
||||
|
||||
std::error_code MakeErrorCode(ErrorCode);
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include <fairmq/sdk/Error.h>
|
||||
#include <fairmq/States.h>
|
||||
#include <fairmq/tools/Semaphore.h>
|
||||
#include <fairmq/tools/Unique.h>
|
||||
|
||||
#include <fairlogger/Logger.h>
|
||||
|
||||
@@ -35,6 +36,7 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <ostream>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@@ -45,6 +47,7 @@ namespace fair {
|
||||
namespace mq {
|
||||
namespace sdk {
|
||||
|
||||
using DeviceId = std::string;
|
||||
using DeviceState = fair::mq::State;
|
||||
using DeviceTransition = fair::mq::Transition;
|
||||
|
||||
@@ -70,6 +73,9 @@ struct DeviceStatus
|
||||
DDSCollection::Id collectionId;
|
||||
};
|
||||
|
||||
using DeviceProperty = std::pair<std::string, std::string>; /// pair := (key, value)
|
||||
using DeviceProperties = std::vector<DeviceProperty>;
|
||||
|
||||
using TopologyState = std::vector<DeviceStatus>;
|
||||
using TopologyStateIndex = std::unordered_map<DDSTask::Id, int>; // task id -> index in the data vector
|
||||
using TopologyStateByTask = std::unordered_map<DDSTask::Id, DeviceStatus>;
|
||||
@@ -168,20 +174,21 @@ class BasicTopology : public AsioBase<Executor, Allocator>
|
||||
Cmds inCmds;
|
||||
inCmds.Deserialize(msg);
|
||||
// LOG(debug) << "Received " << inCmds.Size() << " command(s) with total size of " << msg.length() << " bytes: ";
|
||||
|
||||
for (const auto& cmd : inCmds) {
|
||||
// LOG(debug) << " > " << cmd->GetType();
|
||||
switch (cmd->GetType()) {
|
||||
case Type::state_change: {
|
||||
DDSTask::Id taskId(static_cast<StateChange&>(*cmd).GetTaskId());
|
||||
auto _cmd = static_cast<StateChange&>(*cmd);
|
||||
DDSTask::Id taskId(_cmd.GetTaskId());
|
||||
fDDSSession.UpdateChannelToTaskAssociation(senderId, taskId);
|
||||
if (static_cast<StateChange&>(*cmd).GetCurrentState() == DeviceState::Exiting) {
|
||||
if (_cmd.GetCurrentState() == DeviceState::Exiting) {
|
||||
Cmds outCmds;
|
||||
outCmds.Add<StateChangeExitingReceived>();
|
||||
fDDSSession.SendCommand(outCmds.Serialize(), senderId);
|
||||
}
|
||||
UpdateStateEntry(taskId, static_cast<StateChange&>(*cmd).GetCurrentState());
|
||||
}
|
||||
break;
|
||||
UpdateStateEntry(taskId, _cmd.GetCurrentState());
|
||||
} break;
|
||||
case Type::state_change_subscription:
|
||||
if (static_cast<StateChangeSubscription&>(*cmd).GetResult() != Result::Ok) {
|
||||
LOG(error) << "State change subscription failed for " << static_cast<StateChangeSubscription&>(*cmd).GetDeviceId();
|
||||
@@ -203,6 +210,10 @@ class BasicTopology : public AsioBase<Executor, Allocator>
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Type::properties_set: {
|
||||
HandleCmd(static_cast<cmd::PropertiesSet&>(*cmd));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
LOG(warn) << "Unexpected/unknown command received: " << cmd->GetType();
|
||||
LOG(warn) << "Origin: " << senderId;
|
||||
@@ -320,55 +331,53 @@ class BasicTopology : public AsioBase<Executor, Allocator>
|
||||
/// }
|
||||
/// @endcode
|
||||
template<typename CompletionToken>
|
||||
auto AsyncChangeState(TopologyTransition transition,
|
||||
auto AsyncChangeState(const TopologyTransition transition,
|
||||
Duration timeout,
|
||||
CompletionToken&& token)
|
||||
{
|
||||
return asio::async_initiate<CompletionToken, ChangeStateCompletionSignature>(
|
||||
[&](auto handler) {
|
||||
std::lock_guard<std::mutex> lk(fMtx);
|
||||
return asio::async_initiate<CompletionToken, ChangeStateCompletionSignature>([&](auto handler) {
|
||||
std::lock_guard<std::mutex> lk(fMtx);
|
||||
|
||||
if (fChangeStateOp.IsCompleted()) {
|
||||
fChangeStateOp = ChangeStateOp(AsioBase<Executor, Allocator>::GetExecutor(),
|
||||
AsioBase<Executor, Allocator>::GetAllocator(),
|
||||
std::move(handler));
|
||||
fChangeStateTarget = expectedState.at(transition);
|
||||
ResetTransitionedCount(fChangeStateTarget);
|
||||
cmd::Cmds cmds(cmd::make<cmd::ChangeState>(transition));
|
||||
fDDSSession.SendCommand(cmds.Serialize());
|
||||
if (timeout > std::chrono::milliseconds(0)) {
|
||||
fChangeStateOpTimer.expires_after(timeout);
|
||||
fChangeStateOpTimer.async_wait([&](std::error_code ec) {
|
||||
if (!ec) {
|
||||
std::lock_guard<std::mutex> lk2(fMtx);
|
||||
fChangeStateOp.Timeout(fStateData);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// TODO refactor to hide boiler plate
|
||||
auto ex2(asio::get_associated_executor(handler, AsioBase<Executor, Allocator>::GetExecutor()));
|
||||
auto alloc2(asio::get_associated_allocator(handler, AsioBase<Executor, Allocator>::GetAllocator()));
|
||||
auto state(GetCurrentStateUnsafe());
|
||||
|
||||
ex2.post(
|
||||
[h = std::move(handler), s = std::move(state)]() mutable {
|
||||
try {
|
||||
h(MakeErrorCode(ErrorCode::OperationInProgress), s);
|
||||
} catch (const std::exception& e) {
|
||||
LOG(error) << "Uncaught exception in completion handler: " << e.what();
|
||||
} catch (...) {
|
||||
LOG(error) << "Unknown uncaught exception in completion handler.";
|
||||
}
|
||||
},
|
||||
alloc2);
|
||||
if (fChangeStateOp.IsCompleted()) {
|
||||
fChangeStateOp = ChangeStateOp(AsioBase<Executor, Allocator>::GetExecutor(),
|
||||
AsioBase<Executor, Allocator>::GetAllocator(),
|
||||
std::move(handler));
|
||||
fChangeStateTarget = expectedState.at(transition);
|
||||
ResetTransitionedCount(fChangeStateTarget);
|
||||
cmd::Cmds cmds(cmd::make<cmd::ChangeState>(transition));
|
||||
fDDSSession.SendCommand(cmds.Serialize());
|
||||
if (timeout > std::chrono::milliseconds(0)) {
|
||||
fChangeStateOpTimer.expires_after(timeout);
|
||||
fChangeStateOpTimer.async_wait([&](std::error_code ec) {
|
||||
if (!ec) {
|
||||
std::lock_guard<std::mutex> lk2(fMtx);
|
||||
fChangeStateOp.Timeout(fStateData);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
token);
|
||||
} else {
|
||||
// TODO refactor to hide boiler plate
|
||||
auto ex2(asio::get_associated_executor(handler, AsioBase<Executor, Allocator>::GetExecutor()));
|
||||
auto alloc2(asio::get_associated_allocator(handler, AsioBase<Executor, Allocator>::GetAllocator()));
|
||||
auto state(GetCurrentStateUnsafe());
|
||||
|
||||
ex2.post([h = std::move(handler), s = std::move(state)]() mutable {
|
||||
try {
|
||||
h(MakeErrorCode(ErrorCode::OperationInProgress), s);
|
||||
} catch (const std::exception& e) {
|
||||
LOG(error) << "Uncaught exception in completion handler: " << e.what();
|
||||
} catch (...) {
|
||||
LOG(error) << "Unknown uncaught exception in completion handler.";
|
||||
}
|
||||
},
|
||||
alloc2);
|
||||
}
|
||||
},
|
||||
token);
|
||||
}
|
||||
|
||||
template<typename CompletionToken>
|
||||
auto AsyncChangeState(TopologyTransition transition, CompletionToken&& token)
|
||||
auto AsyncChangeState(const TopologyTransition transition, CompletionToken&& token)
|
||||
{
|
||||
return AsyncChangeState(transition, Duration(0), std::move(token));
|
||||
}
|
||||
@@ -378,7 +387,7 @@ class BasicTopology : public AsioBase<Executor, Allocator>
|
||||
/// @param timeout Timeout in milliseconds, 0 means no timeout
|
||||
/// @tparam CompletionToken Asio completion token type
|
||||
/// @throws std::system_error
|
||||
auto ChangeState(TopologyTransition transition, Duration timeout = Duration(0))
|
||||
auto ChangeState(const TopologyTransition transition, Duration timeout = Duration(0))
|
||||
-> std::pair<std::error_code, TopologyState>
|
||||
{
|
||||
tools::SharedSemaphore blocker;
|
||||
@@ -406,6 +415,146 @@ class BasicTopology : public AsioBase<Executor, Allocator>
|
||||
|
||||
auto StateEqualsTo(DeviceState state) const -> bool { return sdk::StateEqualsTo(GetCurrentState(), state); }
|
||||
|
||||
using FailedDevices = std::set<DeviceId>;
|
||||
using SetPropertiesCompletionSignature = void(std::error_code, FailedDevices);
|
||||
|
||||
private:
|
||||
struct SetPropertiesOp
|
||||
{
|
||||
using Id = std::size_t;
|
||||
using SetCount = unsigned int;
|
||||
|
||||
template<typename Handler>
|
||||
SetPropertiesOp(Id id,
|
||||
SetCount expectedCount,
|
||||
Duration timeout,
|
||||
std::mutex& mutex,
|
||||
Executor const & ex,
|
||||
Allocator const & alloc,
|
||||
Handler&& handler)
|
||||
: fId(id)
|
||||
, fOp(ex, alloc, std::move(handler))
|
||||
, fTimer(ex)
|
||||
, fCount(0)
|
||||
, fExpectedCount(expectedCount)
|
||||
, fFailedDevices(alloc)
|
||||
, fMtx(mutex)
|
||||
{
|
||||
if (timeout > std::chrono::milliseconds(0)) {
|
||||
fTimer.expires_after(timeout);
|
||||
fTimer.async_wait([&](std::error_code ec) {
|
||||
if (!ec) {
|
||||
std::lock_guard<std::mutex> lk(fMtx);
|
||||
fOp.Timeout(fFailedDevices);
|
||||
}
|
||||
});
|
||||
}
|
||||
// LOG(debug) << "SetProperties " << fId << " with expected count of " << fExpectedCount << " started.";
|
||||
}
|
||||
SetPropertiesOp() = delete;
|
||||
SetPropertiesOp(const SetPropertiesOp&) = delete;
|
||||
SetPropertiesOp& operator=(const SetPropertiesOp&) = delete;
|
||||
SetPropertiesOp(SetPropertiesOp&&) = default;
|
||||
SetPropertiesOp& operator=(SetPropertiesOp&&) = default;
|
||||
~SetPropertiesOp() = default;
|
||||
|
||||
auto Update(const std::string& deviceId, cmd::Result result) -> void
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(fMtx);
|
||||
if (cmd::Result::Ok != result) {
|
||||
fFailedDevices.insert(deviceId);
|
||||
}
|
||||
++fCount;
|
||||
TryCompletion();
|
||||
}
|
||||
|
||||
private:
|
||||
Id const fId;
|
||||
AsioAsyncOp<Executor, Allocator, SetPropertiesCompletionSignature> fOp;
|
||||
asio::steady_timer fTimer;
|
||||
SetCount fCount;
|
||||
SetCount const fExpectedCount;
|
||||
FailedDevices fFailedDevices;
|
||||
std::mutex& fMtx;
|
||||
|
||||
/// precondition: fMtx is locked.
|
||||
auto TryCompletion() -> void
|
||||
{
|
||||
if (!fOp.IsCompleted() && fCount == fExpectedCount) {
|
||||
fTimer.cancel();
|
||||
if (fFailedDevices.size() > 0) {
|
||||
fOp.Complete(MakeErrorCode(ErrorCode::DeviceSetPropertiesFailed), fFailedDevices);
|
||||
} else {
|
||||
fOp.Complete(fFailedDevices);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
auto HandleCmd(cmd::PropertiesSet const& cmd) -> void
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(fMtx);
|
||||
try {
|
||||
auto& op(fSetPropertiesOps.at(cmd.GetRequestId()));
|
||||
lk.unlock();
|
||||
op.Update(cmd.GetDeviceId(), cmd.GetResult());
|
||||
} catch (std::out_of_range& e) {
|
||||
LOG(debug) << "SetProperties operation (request id: " << cmd.GetRequestId()
|
||||
<< ") not found (probably completed or timed out), "
|
||||
<< "discarding reply of device " << cmd.GetDeviceId();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
template<typename CompletionToken>
|
||||
auto AsyncSetProperties(const DeviceProperties& props,
|
||||
Duration timeout,
|
||||
CompletionToken&& token)
|
||||
{
|
||||
return asio::async_initiate<CompletionToken, SetPropertiesCompletionSignature>(
|
||||
[&](auto handler) {
|
||||
typename SetPropertiesOp::Id const id(tools::UuidHash());
|
||||
|
||||
std::lock_guard<std::mutex> lk(fMtx);
|
||||
fSetPropertiesOps.emplace(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(id),
|
||||
std::forward_as_tuple(id,
|
||||
fStateData.size(),
|
||||
timeout,
|
||||
fMtx,
|
||||
AsioBase<Executor, Allocator>::GetExecutor(),
|
||||
AsioBase<Executor, Allocator>::GetAllocator(),
|
||||
std::move(handler)));
|
||||
|
||||
cmd::Cmds const cmds(cmd::make<cmd::SetProperties>(id, props));
|
||||
fDDSSession.SendCommand(cmds.Serialize());
|
||||
},
|
||||
token);
|
||||
}
|
||||
|
||||
template<typename CompletionToken>
|
||||
auto AsyncSetProperties(DeviceProperties const & properties, CompletionToken&& token)
|
||||
{
|
||||
return AsyncSetProperties(properties, Duration(0), std::move(token));
|
||||
}
|
||||
|
||||
auto SetProperties(DeviceProperties const& properties, Duration timeout = Duration(0))
|
||||
-> std::pair<std::error_code, FailedDevices>
|
||||
{
|
||||
tools::SharedSemaphore blocker;
|
||||
std::error_code ec;
|
||||
FailedDevices failed;
|
||||
AsyncSetProperties(
|
||||
properties, timeout, [&, blocker](std::error_code _ec, FailedDevices _failed) mutable {
|
||||
ec = _ec;
|
||||
failed = _failed;
|
||||
blocker.Signal();
|
||||
});
|
||||
blocker.Wait();
|
||||
return {ec, failed};
|
||||
}
|
||||
|
||||
private:
|
||||
using TransitionedCount = unsigned int;
|
||||
|
||||
@@ -421,6 +570,8 @@ class BasicTopology : public AsioBase<Executor, Allocator>
|
||||
DeviceState fChangeStateTarget;
|
||||
TransitionedCount fTransitionedCount;
|
||||
|
||||
std::unordered_map<typename SetPropertiesOp::Id, SetPropertiesOp> fSetPropertiesOps;
|
||||
|
||||
auto makeTopologyState() -> void
|
||||
{
|
||||
fStateData.reserve(fDDSTopo.GetTasks().size());
|
||||
|
@@ -46,7 +46,7 @@ array<string, 2> resultNames =
|
||||
}
|
||||
};
|
||||
|
||||
array<string, 17> typeNames =
|
||||
array<string, 18> typeNames =
|
||||
{
|
||||
{
|
||||
"CheckState",
|
||||
@@ -57,6 +57,7 @@ array<string, 17> typeNames =
|
||||
"SubscribeToStateChange",
|
||||
"UnsubscribeFromStateChange",
|
||||
"StateChangeExitingReceived",
|
||||
"SetProperties",
|
||||
|
||||
"CurrentState",
|
||||
"TransitionStatus",
|
||||
@@ -66,7 +67,8 @@ array<string, 17> typeNames =
|
||||
"Heartbeat",
|
||||
"StateChangeSubscription",
|
||||
"StateChangeUnsubscription",
|
||||
"StateChange"
|
||||
"StateChange",
|
||||
"PropertiesSet"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -148,7 +150,7 @@ array<sdk::cmd::FBTransition, 12> mqTransitionToFBTransition =
|
||||
}
|
||||
};
|
||||
|
||||
array<FBCmd, 17> typeToFBCmd =
|
||||
array<FBCmd, 19> typeToFBCmd =
|
||||
{
|
||||
{
|
||||
FBCmd::FBCmd_check_state,
|
||||
@@ -159,6 +161,7 @@ array<FBCmd, 17> typeToFBCmd =
|
||||
FBCmd::FBCmd_subscribe_to_state_change,
|
||||
FBCmd::FBCmd_unsubscribe_from_state_change,
|
||||
FBCmd::FBCmd_state_change_exiting_received,
|
||||
FBCmd::FBCmd_set_properties,
|
||||
FBCmd::FBCmd_current_state,
|
||||
FBCmd::FBCmd_transition_status,
|
||||
FBCmd::FBCmd_config,
|
||||
@@ -167,11 +170,12 @@ array<FBCmd, 17> typeToFBCmd =
|
||||
FBCmd::FBCmd_heartbeat,
|
||||
FBCmd::FBCmd_state_change_subscription,
|
||||
FBCmd::FBCmd_state_change_unsubscription,
|
||||
FBCmd::FBCmd_state_change
|
||||
FBCmd::FBCmd_state_change,
|
||||
FBCmd::FBCmd_properties_set
|
||||
}
|
||||
};
|
||||
|
||||
array<Type, 17> fbCmdToType =
|
||||
array<Type, 19> fbCmdToType =
|
||||
{
|
||||
{
|
||||
Type::check_state,
|
||||
@@ -182,6 +186,7 @@ array<Type, 17> fbCmdToType =
|
||||
Type::subscribe_to_state_change,
|
||||
Type::unsubscribe_from_state_change,
|
||||
Type::state_change_exiting_received,
|
||||
Type::set_properties,
|
||||
Type::current_state,
|
||||
Type::transition_status,
|
||||
Type::config,
|
||||
@@ -190,7 +195,8 @@ array<Type, 17> fbCmdToType =
|
||||
Type::heartbeat,
|
||||
Type::state_change_subscription,
|
||||
Type::state_change_unsubscription,
|
||||
Type::state_change
|
||||
Type::state_change,
|
||||
Type::properties_set
|
||||
}
|
||||
};
|
||||
|
||||
@@ -249,41 +255,60 @@ string Cmds::Serialize(const Format type) const
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
}
|
||||
break;
|
||||
case Type::set_properties: {
|
||||
auto _cmd = static_cast<SetProperties&>(*cmd);
|
||||
std::vector<flatbuffers::Offset<FBProperty>> propsVector;
|
||||
for (auto const& e : _cmd.GetProps()) {
|
||||
auto const key(fbb.CreateString(e.first));
|
||||
auto const val(fbb.CreateString(e.second));
|
||||
propsVector.push_back(CreateFBProperty(fbb, key, val));
|
||||
}
|
||||
auto props = fbb.CreateVector(propsVector);
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_request_id(_cmd.GetRequestId());
|
||||
cmdBuilder->add_properties(props);
|
||||
}
|
||||
break;
|
||||
case Type::current_state: {
|
||||
auto deviceId = fbb.CreateString(static_cast<CurrentState&>(*cmd).GetDeviceId());
|
||||
auto _cmd = static_cast<CurrentState&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_current_state(GetFBState(static_cast<CurrentState&>(*cmd).GetCurrentState()));
|
||||
cmdBuilder->add_current_state(GetFBState(_cmd.GetCurrentState()));
|
||||
}
|
||||
break;
|
||||
case Type::transition_status: {
|
||||
auto deviceId = fbb.CreateString(static_cast<TransitionStatus&>(*cmd).GetDeviceId());
|
||||
auto _cmd = static_cast<TransitionStatus&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_result(GetFBResult(static_cast<TransitionStatus&>(*cmd).GetResult()));
|
||||
cmdBuilder->add_transition(GetFBTransition(static_cast<TransitionStatus&>(*cmd).GetTransition()));
|
||||
cmdBuilder->add_result(GetFBResult(_cmd.GetResult()));
|
||||
cmdBuilder->add_transition(GetFBTransition(_cmd.GetTransition()));
|
||||
}
|
||||
break;
|
||||
case Type::config: {
|
||||
auto deviceId = fbb.CreateString(static_cast<Config&>(*cmd).GetDeviceId());
|
||||
auto config = fbb.CreateString(static_cast<Config&>(*cmd).GetConfig());
|
||||
auto _cmd = static_cast<Config&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
auto config = fbb.CreateString(_cmd.GetConfig());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_config_string(config);
|
||||
}
|
||||
break;
|
||||
case Type::heartbeat_subscription: {
|
||||
auto deviceId = fbb.CreateString(static_cast<HeartbeatSubscription&>(*cmd).GetDeviceId());
|
||||
auto _cmd = static_cast<HeartbeatSubscription&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_result(GetFBResult(static_cast<HeartbeatSubscription&>(*cmd).GetResult()));
|
||||
cmdBuilder->add_result(GetFBResult(_cmd.GetResult()));
|
||||
}
|
||||
break;
|
||||
case Type::heartbeat_unsubscription: {
|
||||
auto deviceId = fbb.CreateString(static_cast<HeartbeatUnsubscription&>(*cmd).GetDeviceId());
|
||||
auto _cmd = static_cast<HeartbeatUnsubscription&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_result(GetFBResult(static_cast<HeartbeatUnsubscription&>(*cmd).GetResult()));
|
||||
cmdBuilder->add_result(GetFBResult(_cmd.GetResult()));
|
||||
}
|
||||
break;
|
||||
case Type::heartbeat: {
|
||||
@@ -293,26 +318,38 @@ string Cmds::Serialize(const Format type) const
|
||||
}
|
||||
break;
|
||||
case Type::state_change_subscription: {
|
||||
auto deviceId = fbb.CreateString(static_cast<StateChangeSubscription&>(*cmd).GetDeviceId());
|
||||
auto _cmd = static_cast<StateChangeSubscription&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_result(GetFBResult(static_cast<StateChangeSubscription&>(*cmd).GetResult()));
|
||||
cmdBuilder->add_result(GetFBResult(_cmd.GetResult()));
|
||||
}
|
||||
break;
|
||||
case Type::state_change_unsubscription: {
|
||||
auto deviceId = fbb.CreateString(static_cast<StateChangeUnsubscription&>(*cmd).GetDeviceId());
|
||||
auto _cmd = static_cast<StateChangeUnsubscription&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_result(GetFBResult(static_cast<StateChangeUnsubscription&>(*cmd).GetResult()));
|
||||
cmdBuilder->add_result(GetFBResult(_cmd.GetResult()));
|
||||
}
|
||||
break;
|
||||
case Type::state_change: {
|
||||
auto deviceId = fbb.CreateString(static_cast<StateChange&>(*cmd).GetDeviceId());
|
||||
auto _cmd = static_cast<StateChange&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_task_id(static_cast<StateChange&>(*cmd).GetTaskId());
|
||||
cmdBuilder->add_last_state(GetFBState(static_cast<StateChange&>(*cmd).GetLastState()));
|
||||
cmdBuilder->add_current_state(GetFBState(static_cast<StateChange&>(*cmd).GetCurrentState()));
|
||||
cmdBuilder->add_task_id(_cmd.GetTaskId());
|
||||
cmdBuilder->add_last_state(GetFBState(_cmd.GetLastState()));
|
||||
cmdBuilder->add_current_state(GetFBState(_cmd.GetCurrentState()));
|
||||
}
|
||||
break;
|
||||
case Type::properties_set: {
|
||||
auto _cmd = static_cast<PropertiesSet&>(*cmd);
|
||||
auto deviceId = fbb.CreateString(_cmd.GetDeviceId());
|
||||
cmdBuilder = tools::make_unique<FBCommandBuilder>(fbb);
|
||||
cmdBuilder->add_device_id(deviceId);
|
||||
cmdBuilder->add_request_id(_cmd.GetRequestId());
|
||||
cmdBuilder->add_result(GetFBResult(_cmd.GetResult()));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -391,6 +428,14 @@ void Cmds::Deserialize(const string& str, const Format type)
|
||||
case FBCmd_state_change_exiting_received:
|
||||
fCmds.emplace_back(make<StateChangeExitingReceived>());
|
||||
break;
|
||||
case FBCmd_set_properties: {
|
||||
std::vector<std::pair<std::string, std::string>> properties;
|
||||
auto props = cmdPtr.properties();
|
||||
for (unsigned int j = 0; j < props->size(); ++j) {
|
||||
properties.emplace_back(props->Get(j)->key()->str(), props->Get(j)->value()->str());
|
||||
}
|
||||
fCmds.emplace_back(make<SetProperties>(cmdPtr.request_id(), properties));
|
||||
} break;
|
||||
case FBCmd_current_state:
|
||||
fCmds.emplace_back(make<CurrentState>(cmdPtr.device_id()->str(), GetMQState(cmdPtr.current_state())));
|
||||
break;
|
||||
@@ -418,6 +463,9 @@ void Cmds::Deserialize(const string& str, const Format type)
|
||||
case FBCmd_state_change:
|
||||
fCmds.emplace_back(make<StateChange>(cmdPtr.device_id()->str(), cmdPtr.task_id(), GetMQState(cmdPtr.last_state()), GetMQState(cmdPtr.current_state())));
|
||||
break;
|
||||
case FBCmd_properties_set:
|
||||
fCmds.emplace_back(make<PropertiesSet>(cmdPtr.device_id()->str(), cmdPtr.request_id(), GetResult(cmdPtr.result())));
|
||||
break;
|
||||
default:
|
||||
throw CommandFormatError("unrecognized command type given to fair::mq::cmd::Cmds::Deserialize()");
|
||||
break;
|
||||
|
@@ -47,6 +47,7 @@ enum class Type : int
|
||||
subscribe_to_state_change, // args: { }
|
||||
unsubscribe_from_state_change, // args: { }
|
||||
state_change_exiting_received, // args: { }
|
||||
set_properties, // args: { request_id, properties }
|
||||
|
||||
current_state, // args: { device_id, current_state }
|
||||
transition_status, // args: { device_id, Result, transition }
|
||||
@@ -56,12 +57,14 @@ enum class Type : int
|
||||
heartbeat, // args: { device_id }
|
||||
state_change_subscription, // args: { device_id, Result }
|
||||
state_change_unsubscription, // args: { device_id, Result }
|
||||
state_change // args: { device_id, task_id, last_state, current_state }
|
||||
state_change, // args: { device_id, task_id, last_state, current_state }
|
||||
properties_set // args: { device_id, request_id, Result }
|
||||
};
|
||||
|
||||
struct Cmd
|
||||
{
|
||||
explicit Cmd(const Type type) : fType(type) {}
|
||||
virtual ~Cmd() = default;
|
||||
|
||||
Type GetType() const { return fType; }
|
||||
|
||||
@@ -118,6 +121,24 @@ struct StateChangeExitingReceived : Cmd
|
||||
explicit StateChangeExitingReceived() : Cmd(Type::state_change_exiting_received) {}
|
||||
};
|
||||
|
||||
struct SetProperties : Cmd
|
||||
{
|
||||
SetProperties(std::size_t request_id, std::vector<std::pair<std::string, std::string>> properties)
|
||||
: Cmd(Type::set_properties)
|
||||
, fRequestId(request_id)
|
||||
, fProperties(std::move(properties))
|
||||
{}
|
||||
|
||||
auto GetRequestId() const -> std::size_t { return fRequestId; }
|
||||
auto SetRequestId(std::size_t requestId) -> void { fRequestId = requestId; }
|
||||
auto GetProps() const -> std::vector<std::pair<std::string, std::string>> { return fProperties; }
|
||||
auto SetProps(std::vector<std::pair<std::string, std::string>> properties) -> void { fProperties = std::move(properties); }
|
||||
|
||||
private:
|
||||
std::size_t fRequestId;
|
||||
std::vector<std::pair<std::string, std::string>> fProperties;
|
||||
};
|
||||
|
||||
struct CurrentState : Cmd
|
||||
{
|
||||
explicit CurrentState(const std::string& id, State currentState)
|
||||
@@ -288,6 +309,27 @@ struct StateChange : Cmd
|
||||
fair::mq::State fCurrentState;
|
||||
};
|
||||
|
||||
struct PropertiesSet : Cmd {
|
||||
PropertiesSet(std::string deviceId, std::size_t requestId, Result result)
|
||||
: Cmd(Type::properties_set)
|
||||
, fDeviceId(std::move(deviceId))
|
||||
, fRequestId(requestId)
|
||||
, fResult(result)
|
||||
{}
|
||||
|
||||
auto GetDeviceId() const -> std::string { return fDeviceId; }
|
||||
auto SetDeviceId(std::string deviceId) -> void { fDeviceId = std::move(deviceId); }
|
||||
auto GetRequestId() const -> std::size_t { return fRequestId; }
|
||||
auto SetRequestId(std::size_t requestId) -> void { fRequestId = requestId; }
|
||||
auto GetResult() const -> Result { return fResult; }
|
||||
auto SetResult(Result result) -> void { fResult = result; }
|
||||
|
||||
private:
|
||||
std::string fDeviceId;
|
||||
std::size_t fRequestId;
|
||||
Result fResult;
|
||||
};
|
||||
|
||||
template<typename C, typename... Args>
|
||||
std::unique_ptr<Cmd> make(Args&&... args)
|
||||
{
|
||||
@@ -307,7 +349,6 @@ struct Cmds
|
||||
Unpack(std::forward<std::unique_ptr<Cmd>&&>(first), std::forward<Rest>(rest)...);
|
||||
}
|
||||
|
||||
|
||||
void Add(std::unique_ptr<Cmd>&& cmd) { fCmds.emplace_back(std::move(cmd)); }
|
||||
|
||||
template<typename C, typename... Args>
|
||||
|
@@ -38,6 +38,11 @@ enum FBTransition:byte {
|
||||
ErrorFound
|
||||
}
|
||||
|
||||
table FBProperty {
|
||||
key:string;
|
||||
value:string;
|
||||
}
|
||||
|
||||
enum FBCmd:byte {
|
||||
check_state, // args: { }
|
||||
change_state, // args: { transition }
|
||||
@@ -47,6 +52,7 @@ enum FBCmd:byte {
|
||||
subscribe_to_state_change, // args: { }
|
||||
unsubscribe_from_state_change, // args: { }
|
||||
state_change_exiting_received, // args: { }
|
||||
set_properties, // args: { key, value }
|
||||
|
||||
current_state, // args: { device_id, current_state }
|
||||
transition_status, // args: { device_id, Result, transition }
|
||||
@@ -56,13 +62,15 @@ enum FBCmd:byte {
|
||||
heartbeat, // args: { device_id }
|
||||
state_change_subscription, // args: { device_id, Result }
|
||||
state_change_unsubscription, // args: { device_id, Result }
|
||||
state_change // args: { device_id, task_id, last_state, current_state }
|
||||
state_change, // args: { device_id, task_id, last_state, current_state }
|
||||
properties_set // args: { device_id, request_id, Result }
|
||||
}
|
||||
|
||||
table FBCommand {
|
||||
command_id:FBCmd;
|
||||
device_id:string;
|
||||
task_id:uint64;
|
||||
request_id:uint64;
|
||||
state:FBState;
|
||||
transition:FBTransition;
|
||||
result:FBResult;
|
||||
@@ -70,6 +78,7 @@ table FBCommand {
|
||||
last_state:FBState;
|
||||
current_state:FBState;
|
||||
debug:string;
|
||||
properties:[FBProperty];
|
||||
}
|
||||
|
||||
table FBCommands {
|
||||
|
Reference in New Issue
Block a user