SIGABRT error CPP receiving data from rabbitmq server

53 views Asked by At

I'm trying to create 2 c++ application, one is a gui interface that sends commands and the other is a slave which invoke commands. I set a rabbitmq server between the 2 app to establish an asynchronous communication between both apps. This the code of the receiver as the publisher works fine.

// 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();


}

I'm using a rabbitmq library for cpp which depends on the boost::asio library to create an asynchronous communication between two binaries.

I'm getting the error of SIGABRT in the receiver->receiveCommand("hello_queue"); line in the main function indicating this error

  /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)
0

There are 0 answers