mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Refactor MessageFilter and MessageCallback
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include "communication/include/command.h"
|
||||
#include "communication/include/network.h"
|
||||
#include "communication/include/packet.h"
|
||||
#include "communication/message/callback/include/messagecallback.h"
|
||||
#include "communication/message/include/serialnumbermessage.h"
|
||||
#include "communication/include/packetizer.h"
|
||||
#include "communication/include/messagedecoder.h"
|
||||
#include <memory>
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#ifndef __MESSAGECALLBACK_H_
|
||||
#define __MESSAGECALLBACK_H_
|
||||
|
||||
#include "communication/message/include/message.h"
|
||||
#include "communication/include/messagefilter.h"
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
class MessageCallback {
|
||||
public:
|
||||
typedef std::function< void( std::shared_ptr<Message> ) > fn_messageCallback;
|
||||
|
||||
MessageCallback(fn_messageCallback cb, std::shared_ptr<MessageFilter> f) : callback(cb), filter(f) {}
|
||||
MessageCallback(fn_messageCallback cb, MessageFilter f = MessageFilter()) : callback(cb), filter(std::make_shared<MessageFilter>(f)) {}
|
||||
|
||||
// Allow the filter to be placed first if the user wants (maybe in the case of a lambda)
|
||||
MessageCallback(MessageFilter f, fn_messageCallback cb) { MessageCallback(cb, f); }
|
||||
|
||||
virtual bool callIfMatch(const std::shared_ptr<Message>& message) const {
|
||||
bool ret = filter->match(message);
|
||||
if(ret)
|
||||
callback(message);
|
||||
return ret;
|
||||
}
|
||||
const MessageFilter& getFilter() const { return *filter; }
|
||||
const fn_messageCallback& getCallback() const { return callback; }
|
||||
|
||||
protected:
|
||||
fn_messageCallback callback;
|
||||
std::shared_ptr<MessageFilter> filter;
|
||||
};
|
||||
|
||||
class CANMessageCallback : public MessageCallback {
|
||||
public:
|
||||
CANMessageCallback(fn_messageCallback cb, CANMessageFilter f = CANMessageFilter()) : MessageCallback(cb, std::make_shared<CANMessageFilter>(f)) {}
|
||||
|
||||
// Allow the filter to be placed first if the user wants (maybe in the case of a lambda)
|
||||
CANMessageCallback(CANMessageFilter f, fn_messageCallback cb) : MessageCallback(cb, std::make_shared<CANMessageFilter>(f)) {}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,72 +0,0 @@
|
||||
#ifndef __MESSAGEFILTER_H_
|
||||
#define __MESSAGEFILTER_H_
|
||||
|
||||
#include "communication/include/network.h"
|
||||
#include "communication/message/include/message.h"
|
||||
#include "communication/message/include/canmessage.h"
|
||||
#include <memory>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
class MessageFilter {
|
||||
public:
|
||||
MessageFilter() : matchAny(true) {}
|
||||
MessageFilter(Network::Type type) : type(type) {}
|
||||
MessageFilter(Network::NetID netid) : netid(netid) {}
|
||||
virtual ~MessageFilter() {}
|
||||
|
||||
virtual bool match(const std::shared_ptr<Message>& message) const {
|
||||
if(matchAny)
|
||||
return true;
|
||||
if(!matchType(message->network.getType()))
|
||||
return false;
|
||||
if(!matchNetID(message->network.getNetID()))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool matchAny = false;
|
||||
|
||||
Network::Type type = Network::Type::Invalid; // Matching a type of invalid will match any
|
||||
bool matchType(Network::Type mtype) const {
|
||||
if(type == Network::Type::Invalid)
|
||||
return true;
|
||||
return type == mtype;
|
||||
}
|
||||
|
||||
Network::NetID netid = Network::NetID::Invalid; // Matching a netid of invalid will match any
|
||||
bool matchNetID(Network::NetID mnetid) const {
|
||||
if(netid == Network::NetID::Invalid)
|
||||
return true;
|
||||
return netid == mnetid;
|
||||
}
|
||||
};
|
||||
|
||||
class CANMessageFilter : public MessageFilter {
|
||||
public:
|
||||
CANMessageFilter() : MessageFilter(Network::Type::CAN), arbid(INVALID_ARBID) {}
|
||||
CANMessageFilter(uint32_t arbid) : MessageFilter(Network::Type::CAN), arbid(arbid) {}
|
||||
|
||||
bool match(const std::shared_ptr<Message>& message) const {
|
||||
if(!MessageFilter::match(message))
|
||||
return false;
|
||||
const auto canMessage = std::dynamic_pointer_cast<CANMessage>(message);
|
||||
if(canMessage == nullptr || !matchArbID(canMessage->arbid))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr uint32_t INVALID_ARBID = 0xffffffff;
|
||||
uint32_t arbid;
|
||||
bool matchArbID(uint32_t marbid) const {
|
||||
if(arbid == INVALID_ARBID)
|
||||
return true;
|
||||
return arbid == marbid;
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user