Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ev.h>
- #include <amqpcpp.h>
- #include <amqpcpp/libev.h>
- class MyHandler : public AMQP::LibEvHandler
- {
- public:
- MyHandler(struct ev_loop* loop) : AMQP::LibEvHandler(loop) {}
- virtual ~MyHandler() = default;
- 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;
- }
- };
- class MyTimer
- {
- private:
- struct ev_timer _timer;
- AMQP::TcpChannel* _channel;
- std::string _queue;
- static void callback(struct ev_loop *loop, struct ev_timer *timer, int revents)
- {
- MyTimer *self = static_cast<MyTimer*>(timer->data);
- static int i = 2130;
- i++;
- std::string msg(std::string(
- "{"
- "\"creationDate\": null,"
- "\"value\": \"") + std::to_string(i) + std::string("\","
- "\"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");
- self->_channel->publish("scorpio.direct", "SaveSensorDataEvent",
- envelope);
- }
- public:
- MyTimer(struct ev_loop *loop, AMQP::TcpChannel *channel, std::string queue) :
- _channel(channel), _queue(std::move(queue))
- {
- ev_timer_init(&_timer, callback, 0.005, 1.005);
- _timer.data = this;
- ev_timer_start(loop, &_timer);
- }
- virtual ~MyTimer() = default;
- };
- auto main(int argc, char** argv) -> int
- {
- auto *loop = EV_DEFAULT;
- MyHandler handler(loop);
- AMQP::Address address("amqp://guest:guest@192.168.1.94:5672/");
- AMQP::TcpConnection connection(&handler, address);
- AMQP::TcpChannel channel(&connection);
- 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)
- {
- std::cout << "declared queue " << name << std::endl;
- // 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");
- ev_run(loop, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement