Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | None | 0 0
  1. /**
  2.  *  LibEV.cpp
  3.  *
  4.  *  Test program to check AMQP functionality based on LibEV
  5.  *
  6.  *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
  7.  *  @copyright 2015 - 2018 Copernica BV
  8.  */
  9.  
  10. /**
  11.  *  Dependencies
  12.  */
  13. #include <ev.h>
  14. #include <amqpcpp.h>
  15. #include <amqpcpp/libev.h>
  16.  
  17.  
  18. /**
  19.  *  Custom handler
  20.  */
  21. class MyHandler : public AMQP::LibEvHandler
  22. {
  23. private:
  24.     virtual void onError(AMQP::TcpConnection *connection, const char *message) override
  25.     {
  26.         std::cout << "error: " << message << std::endl;
  27.     }
  28.  
  29.     virtual void onConnected(AMQP::TcpConnection *connection) override
  30.     {
  31.         std::cout << "connected" << std::endl;
  32.     }
  33.  
  34.     virtual void onReady(AMQP::TcpConnection *connection) override
  35.     {
  36.         std::cout << "ready" << std::endl;
  37.     }
  38.  
  39.     virtual void onClosed(AMQP::TcpConnection *connection) override
  40.     {
  41.         std::cout << "closed" << std::endl;
  42.     }
  43.  
  44.     virtual void onDetached(AMQP::TcpConnection *connection) override
  45.     {
  46.         std::cout << "detached" << std::endl;
  47.     }
  48.    
  49.    
  50. public:
  51.     /**
  52.      *  Constructor
  53.      *  @param  ev_loop
  54.      */
  55.     MyHandler(struct ev_loop *loop) : AMQP::LibEvHandler(loop) {}
  56.  
  57.     /**
  58.      *  Destructor
  59.      */
  60.     virtual ~MyHandler() = default;
  61. };
  62.  
  63. /**
  64.  *  Class that runs a timer
  65.  */
  66. class MyTimer
  67. {
  68. private:
  69.     /**
  70.      *  The actual watcher structure
  71.      *  @var struct ev_io
  72.      */
  73.     struct ev_timer _timer;
  74.  
  75.     /**
  76.      *  Pointer towards the AMQP channel
  77.      *  @var AMQP::TcpChannel
  78.      */
  79.     AMQP::TcpChannel *_channel;
  80.  
  81.     /**
  82.      *  Name of the queue
  83.      *  @var std::string
  84.      */
  85.     std::string _queue;
  86.  
  87.  
  88.     /**
  89.      *  Callback method that is called by libev when the timer expires
  90.      *  @param  loop        The loop in which the event was triggered
  91.      *  @param  timer       Internal timer object
  92.      *  @param  revents     The events that triggered this call
  93.      */
  94.     static void callback(struct ev_loop *loop, struct ev_timer *timer, int revents)
  95.     {
  96.         // retrieve the this pointer
  97.         MyTimer *self = static_cast<MyTimer*>(timer->data);
  98.  
  99.         std::string msg(
  100.             "{"
  101.                 "\"creationDate\": null,"
  102.                 "\"value\": \"2137\","
  103.                 "\"sensorKey\" : \"boguc\","
  104.                 "\"time\": \"2019-12-07T19:31:59.370Z\""
  105.             "}"
  106.         );
  107.         AMQP::Envelope envelope(msg.c_str(), sizeof(char)*msg.length());
  108.         envelope.setDeliveryMode(uint8_t(1));
  109.         envelope.setExpiration("5000");
  110.         envelope.setContentType("application/json");
  111.         envelope.setContentEncoding("UTF8");
  112.         std::cout << self->_queue << std::endl;
  113.         self->_channel->publish("scorpio.direct", "SaveSensorDataEvent",
  114.             envelope);
  115.     }
  116.  
  117. public:
  118.     /**
  119.      *  Constructor
  120.      *  @param  loop
  121.      *  @param  channel
  122.      *  @param  queue
  123.      */
  124.     MyTimer(struct ev_loop *loop, AMQP::TcpChannel *channel, std::string queue) :
  125.         _channel(channel), _queue(std::move(queue))
  126.     {
  127.         // initialize the libev structure
  128.         ev_timer_init(&_timer, callback, 0.005, 1.005);
  129.  
  130.         // this object is the data
  131.         _timer.data = this;
  132.  
  133.         // and start it
  134.         ev_timer_start(loop, &_timer);
  135.     }
  136.    
  137.     void send()
  138.     {
  139.         _channel->publish("scorpio.direct", "FiluToSzmata", "FILUTOSZMAATA");
  140.     }
  141.  
  142.     /**
  143.      *  Destructor
  144.      */
  145.     virtual ~MyTimer()
  146.     {
  147.         // @todo to be implemented
  148.     }
  149. };
  150.  
  151.  
  152. /**
  153.  *  Main program
  154.  *  @return int
  155.  */
  156. int main()
  157. {
  158.     // access to the event loop
  159.     auto *loop = EV_DEFAULT;
  160.  
  161.     // handler for libev
  162.     MyHandler handler(loop);
  163.  
  164.     // make a connection
  165.     AMQP::Address address("amqp://guest:guest@192.168.1.94:5672/");
  166.     AMQP::TcpConnection connection(&handler, address);
  167.  
  168.     // we need a channel too
  169.     AMQP::TcpChannel channel(&connection);
  170.  
  171.     // create a temporary queue
  172.     channel.declareExchange("scorpio.direct", AMQP::direct);
  173.     channel.declareQueue("scorpio.rover.jetson").onSuccess([&connection, &channel, loop](const std::string &name,
  174.         uint32_t messagecount, uint32_t consumercount) {
  175.        
  176.         // report the name of the temporary queue
  177.         std::cout << "declared queue " << name << std::endl;
  178.        
  179.         // close the channel
  180.         //channel.close().onSuccess([&connection, &channel]() {
  181.         //    
  182.         //    // report that channel was closed
  183.         //    std::cout << "channel closed" << std::endl;
  184.         //    
  185.         //    // close the connection
  186.         //    connection.close();
  187.         //});
  188.        
  189.         // construct a timer that is going to publish stuff
  190.         auto *timer = new MyTimer(loop, &channel, name);
  191.        
  192.         //connection.close();
  193.     });
  194.     channel.bindQueue("scorpio.direct", "scorpio.rover.jetson", "FiluToSzmata");
  195.  
  196.     // run the loop
  197.     ev_run(loop, 0);
  198.  
  199.     // done
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement