Initial idea
This commit is contained in:
125
anscloud-common/include/anscloud/common/i_message_broker.h
Normal file
125
anscloud-common/include/anscloud/common/i_message_broker.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#pragma once
|
||||
//=============================================================================
|
||||
// IMessageBroker - Abstract interface for message broker operations
|
||||
//
|
||||
// Your existing RabbitMQ C++ API implements this interface.
|
||||
// The SDK (DeviceAgent / GatewayAgent) depends only on this abstraction,
|
||||
// not on any concrete AMQP library.
|
||||
//=============================================================================
|
||||
|
||||
#ifndef ANSCLOUD_COMMON_I_MESSAGE_BROKER_H
|
||||
#define ANSCLOUD_COMMON_I_MESSAGE_BROKER_H
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
|
||||
namespace anscloud {
|
||||
|
||||
// Forward declare
|
||||
struct BrokerConfig;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Message received from broker
|
||||
//-----------------------------------------------------------------------------
|
||||
struct BrokerMessage {
|
||||
std::string exchange;
|
||||
std::string routing_key;
|
||||
std::string body; // JSON payload
|
||||
std::string correlation_id; // For RPC matching
|
||||
std::string reply_to; // Reply queue/routing key
|
||||
std::string message_id;
|
||||
std::string content_type; // "application/json"
|
||||
uint64_t delivery_tag = 0;
|
||||
bool redelivered = false;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Publish options
|
||||
//-----------------------------------------------------------------------------
|
||||
struct PublishOptions {
|
||||
std::string correlation_id;
|
||||
std::string reply_to;
|
||||
std::string message_id;
|
||||
std::string content_type = "application/json";
|
||||
bool persistent = true;
|
||||
int expiration_ms = 0; // 0 = no expiry (TTL)
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Callback types
|
||||
//-----------------------------------------------------------------------------
|
||||
using MessageCallback = std::function<void(const BrokerMessage& msg)>;
|
||||
using ConnectionCallback = std::function<void(bool connected)>;
|
||||
using ErrorCallback = std::function<void(const std::string& error)>;
|
||||
|
||||
//=============================================================================
|
||||
// IMessageBroker - Pure virtual interface
|
||||
//
|
||||
// Your existing RabbitMQ C++ API creates a class that inherits from this:
|
||||
//
|
||||
// class MyRabbitMQBroker : public anscloud::IMessageBroker {
|
||||
// // ... implement all pure virtual methods using your existing API
|
||||
// };
|
||||
//
|
||||
//=============================================================================
|
||||
class IMessageBroker {
|
||||
public:
|
||||
virtual ~IMessageBroker() = default;
|
||||
|
||||
//--- Connection lifecycle ------------------------------------------------
|
||||
virtual bool connect(const std::string& host, int port,
|
||||
const std::string& username, const std::string& password,
|
||||
const std::string& vhost) = 0;
|
||||
virtual void disconnect() = 0;
|
||||
virtual bool is_connected() const = 0;
|
||||
|
||||
//--- Exchange & Queue declarations (idempotent) --------------------------
|
||||
virtual bool declare_exchange(const std::string& name,
|
||||
const std::string& type = "topic",
|
||||
bool durable = true) = 0;
|
||||
|
||||
virtual bool declare_queue(const std::string& name,
|
||||
bool durable = true,
|
||||
bool exclusive = false,
|
||||
bool auto_delete = false) = 0;
|
||||
|
||||
virtual bool bind_queue(const std::string& queue,
|
||||
const std::string& exchange,
|
||||
const std::string& routing_key) = 0;
|
||||
|
||||
//--- Publishing ----------------------------------------------------------
|
||||
virtual bool publish(const std::string& exchange,
|
||||
const std::string& routing_key,
|
||||
const std::string& body,
|
||||
const PublishOptions& options = {}) = 0;
|
||||
|
||||
//--- Consuming -----------------------------------------------------------
|
||||
// Start consuming from a queue. Messages delivered via callback.
|
||||
// Returns a consumer tag (string) that can be used to cancel.
|
||||
virtual std::string start_consuming(const std::string& queue,
|
||||
MessageCallback callback,
|
||||
bool auto_ack = true) = 0;
|
||||
|
||||
// Cancel a consumer by tag
|
||||
virtual void cancel_consumer(const std::string& consumer_tag) = 0;
|
||||
|
||||
// Process pending messages (call from event loop).
|
||||
// timeout_ms: max time to wait for messages. 0 = non-blocking poll.
|
||||
// Returns number of messages processed.
|
||||
virtual int process_messages(int timeout_ms = 100) = 0;
|
||||
|
||||
//--- Callbacks -----------------------------------------------------------
|
||||
virtual void on_connected(ConnectionCallback cb) = 0;
|
||||
virtual void on_error(ErrorCallback cb) = 0;
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Factory function type - your application provides this
|
||||
//=============================================================================
|
||||
using BrokerFactory = std::function<std::unique_ptr<IMessageBroker>()>;
|
||||
|
||||
} // namespace anscloud
|
||||
|
||||
#endif // ANSCLOUD_COMMON_I_MESSAGE_BROKER_H
|
||||
Reference in New Issue
Block a user