Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * LibEV.cpp
- *
- * Test program to check AMQP functionality based on LibEV
- *
- * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
- * @copyright 2015 - 2018 Copernica BV
- */
- /**
- * Dependencies
- */
- #include <ev.h>
- #include <amqpcpp.h>
- #include <amqpcpp/libev.h>
- /**
- * Custom handler
- */
- class MyHandler : public AMQP::LibEvHandler
- {
- private:
- virtual void onError(AMQP::TcpConnection *connection, const char *message) override
- {
- std::cout << "error: " << message << std::endl;
- }
- virtual void onConnected(AMQP::TcpConnection *connection) override
- {
- std::cout << "connected" << std::endl;
- }
- virtual void onReady(AMQP::TcpConnection *connection) override
- {
- std::cout << "ready" << std::endl;
- }
- virtual void onClosed(AMQP::TcpConnection *connection) override
- {
- std::cout << "closed" << std::endl;
- }
- virtual void onDetached(AMQP::TcpConnection *connection) override
- {
- std::cout << "detached" << std::endl;
- }
- public:
- /**
- * Constructor
- * @param ev_loop
- */
- MyHandler(struct ev_loop *loop) : AMQP::LibEvHandler(loop) {}
- /**
- * Destructor
- */
- virtual ~MyHandler() = default;
- };
- /**
- * Class that runs a timer
- */
- class MyTimer
- {
- private:
- /**
- * The actual watcher structure
- * @var struct ev_io
- */
- struct ev_timer _timer;
- /**
- * Pointer towards the AMQP channel
- * @var AMQP::TcpChannel
- */
- AMQP::TcpChannel *_channel;
- /**
- * Name of the queue
- * @var std::string
- */
- std::string _queue;
- /**
- * Callback method that is called by libev when the timer expires
- * @param loop The loop in which the event was triggered
- * @param timer Internal timer object
- * @param revents The events that triggered this call
- */
- static void callback(struct ev_loop *loop, struct ev_timer *timer, int revents)
- {
- // retrieve the this pointer
- MyTimer *self = static_cast<MyTimer*>(timer->data);
- std::string msg(
- "{"
- "\"creationDate\": null,"
- "\"value\": \"2137\","
- "\"sensorKey\" : \"boguc\","
- "\"time\": \"2019-12-07T19:31:59.370Z\""
- "}"
- );
- AMQP::Envelope envelope(msg.c_str(), sizeof(char)*msg.length());
- envelope.setDeliveryMode(uint8_t(1));
- envelope.setExpiration("5000");
- envelope.setContentType("application/json");
- envelope.setContentEncoding("UTF8");
- std::cout << self->_queue << std::endl;
- self->_channel->publish("scorpio.direct", "SaveSensorDataEvent",
- envelope);
- }
- public:
- /**
- * Constructor
- * @param loop
- * @param channel
- * @param queue
- */
- MyTimer(struct ev_loop *loop, AMQP::TcpChannel *channel, std::string queue) :
- _channel(channel), _queue(std::move(queue))
- {
- // initialize the libev structure
- ev_timer_init(&_timer, callback, 0.005, 1.005);
- // this object is the data
- _timer.data = this;
- // and start it
- ev_timer_start(loop, &_timer);
- }
- void send()
- {
- _channel->publish("scorpio.direct", "FiluToSzmata", "FILUTOSZMAATA");
- }
- /**
- * Destructor
- */
- virtual ~MyTimer()
- {
- // @todo to be implemented
- }
- };
- /**
- * Main program
- * @return int
- */
- int main()
- {
- // access to the event loop
- auto *loop = EV_DEFAULT;
- // handler for libev
- MyHandler handler(loop);
- // make a connection
- AMQP::Address address("amqp://guest:guest@192.168.1.94:5672/");
- AMQP::TcpConnection connection(&handler, address);
- // we need a channel too
- AMQP::TcpChannel channel(&connection);
- // create a temporary queue
- channel.declareExchange("scorpio.direct", AMQP::direct);
- channel.declareQueue("scorpio.rover.jetson").onSuccess([&connection, &channel, loop](const std::string &name,
- uint32_t messagecount, uint32_t consumercount) {
- // report the name of the temporary queue
- std::cout << "declared queue " << name << std::endl;
- // close the channel
- //channel.close().onSuccess([&connection, &channel]() {
- //
- // // report that channel was closed
- // std::cout << "channel closed" << std::endl;
- //
- // // close the connection
- // connection.close();
- //});
- // construct a timer that is going to publish stuff
- auto *timer = new MyTimer(loop, &channel, name);
- //connection.close();
- });
- channel.bindQueue("scorpio.direct", "scorpio.rover.jetson", "FiluToSzmata");
- // run the loop
- ev_run(loop, 0);
- // done
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement