我正在尝试创建2个C ++应用程序,一个是发送命令的GUI界面,另一个是调用命令的从站。我在两个应用程序之间设置了一个rabbitmq服务器,以在两个应用程序之间建立异步通信。这是作为发布者的接收者的代码,工作正常。
// main.cpp
#include <vector>
#include <string>
#include <sstream>
#include "cmd_ctl.h"
#include "cmd_handler.h"
#include "uart_handler.h"
#include "receiver.h"
int main() {
CommandInvoker *invoker = new CommandInvoker;
CommandHandler *handler = new CommandHandler;
Receiver *receiver = new Receiver;
// machanism to retreive or to store
while(1) {
std::cout << "Wainting for command...\n";
receiver->revceiveRawCommand("hello_queue");
}
delete invoker;
delete handler;
// receiver.h
#pragma once
#include <string>
#include <SimpleAmqpClient/SimpleAmqpClient.h>
#include "cmd_ctl.h"
class Receiver {
public:
Receiver(const std::string hostname="localhost",
const int port=5672,
const std::string username="guest",
const std::string password="guest",
const std::string virtualHost="/");
~Receiver() {};
void revceiveRawCommand(std::string);
void commandCallback(std::string, CommandInvoker*, CommandHandler*);
inline void setConnection(AmqpClient::Channel::ptr_t connection) { this->connection = connection; };
private:
AmqpClient::Channel::ptr_t connection;
};
// receiver.cpp
#include <iostream>
#include <sstream>
#include <vector>
#include "receiver.h"
Receiver::Receiver(std::string hostname, int port, std::string username, std::string password, std::string virtualHost) {
AmqpClient::Channel::ptr_t conncetion = AmqpClient::Channel::Create(
hostname, port, username, password, virtualHost
);
this->setConnection(connection);
}
void Receiver::revceiveRawCommand(std::string queue) {
try {
this->connection->DeclareQueue(queue, false, true, false, false);
AmqpClient::Envelope::ptr_t envelope;
connection->BasicConsume(queue, "", true, false, false);
while(true) {
connection->BasicConsumeMessage(envelope, 0);
if (envelope) {
std::string message = envelope->Message()->Body();
// this->commandCallback(message);
}
}
} catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
}
void Receiver::commandCallback(std::string message, CommandInvoker* invoker, CommandHandler* handler) {
// Get the message attribute inside a vector
std::istringstream iss(message);
std::vector<std::string> commands;
std::string command;
while(iss>> command) {
commands.push_back(command);
}
// clean the message vector
std::string commandName = commands.front();
commands.erase(commands.begin());
invoker->create_command(handler, commandName, commands);
invoker->executeCommand();
}
我正在为 cpp 使用rabbitmq 库,它依赖于 boost::asio 库来创建两个二进制文件之间的异步通信。
我在主函数的
receiver->receiveCommand("hello_queue");
行中收到 SIGABRT 错误,指示此错误
/usr/include/boost/smart_ptr/shared_ptr.hpp:784: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = AmqpClient::Channel; typename boost::detail::sp_member_access<T>::type = AmqpClient::Channel*]: Assertion px != 0' failed.
fish: Job 1, './DJIN-2' terminated by signal SIGABRT (Abort)