added libuv example program to check issue #155
This commit is contained in:
parent
60466cc6bf
commit
276abe4b06
|
|
@ -4,7 +4,7 @@
|
||||||
* Test program to check AMQP functionality based on LibEV
|
* Test program to check AMQP functionality based on LibEV
|
||||||
*
|
*
|
||||||
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
||||||
* @copyright 2015 - 2016 Copernica BV
|
* @copyright 2015 - 2017 Copernica BV
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -14,6 +14,44 @@
|
||||||
#include <amqpcpp.h>
|
#include <amqpcpp.h>
|
||||||
#include <amqpcpp/libev.h>
|
#include <amqpcpp/libev.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom handler
|
||||||
|
*/
|
||||||
|
class MyHandler : public AMQP::LibEvHandler
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Method that is called when a connection error occurs
|
||||||
|
* @param connection
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
virtual void onError(AMQP::TcpConnection *connection, const char *message) override
|
||||||
|
{
|
||||||
|
std::cout << "error: " << message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that is called when the TCP connection ends up in a connected state
|
||||||
|
* @param connection The TCP connection
|
||||||
|
*/
|
||||||
|
virtual void onConnected(AMQP::TcpConnection *connection) override
|
||||||
|
{
|
||||||
|
std::cout << "connected" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @param ev_loop
|
||||||
|
*/
|
||||||
|
MyHandler(struct ev_loop *loop) : AMQP::LibEvHandler(loop) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor
|
||||||
|
*/
|
||||||
|
virtual ~MyHandler() = default;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main program
|
* Main program
|
||||||
* @return int
|
* @return int
|
||||||
|
|
@ -24,7 +62,7 @@ int main()
|
||||||
auto *loop = EV_DEFAULT;
|
auto *loop = EV_DEFAULT;
|
||||||
|
|
||||||
// handler for libev
|
// handler for libev
|
||||||
AMQP::LibEvHandler handler(loop);
|
MyHandler handler(loop);
|
||||||
|
|
||||||
// make a connection
|
// make a connection
|
||||||
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://guest:guest@localhost/"));
|
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://guest:guest@localhost/"));
|
||||||
|
|
@ -37,9 +75,6 @@ int main()
|
||||||
|
|
||||||
// report the name of the temporary queue
|
// report the name of the temporary queue
|
||||||
std::cout << "declared queue " << name << std::endl;
|
std::cout << "declared queue " << name << std::endl;
|
||||||
|
|
||||||
// now we can close the connection
|
|
||||||
connection.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// run the loop
|
// run the loop
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,86 @@
|
||||||
|
/**
|
||||||
|
* LibUV.cpp
|
||||||
|
*
|
||||||
|
* Test program to check AMQP functionality based on LibUV
|
||||||
|
*
|
||||||
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
||||||
|
* @copyright 2015 - 2017 Copernica BV
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dependencies
|
||||||
|
*/
|
||||||
|
#include <uv.h>
|
||||||
|
#include <amqpcpp.h>
|
||||||
|
#include <amqpcpp/libuv.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom handler
|
||||||
|
*/
|
||||||
|
class MyHandler : public AMQP::LibUvHandler
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Method that is called when a connection error occurs
|
||||||
|
* @param connection
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
virtual void onError(AMQP::TcpConnection *connection, const char *message) override
|
||||||
|
{
|
||||||
|
std::cout << "error: " << message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that is called when the TCP connection ends up in a connected state
|
||||||
|
* @param connection The TCP connection
|
||||||
|
*/
|
||||||
|
virtual void onConnected(AMQP::TcpConnection *connection) override
|
||||||
|
{
|
||||||
|
std::cout << "connected" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @param uv_loop
|
||||||
|
*/
|
||||||
|
MyHandler(uv_loop_t *loop) : AMQP::LibUvHandler(loop) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor
|
||||||
|
*/
|
||||||
|
virtual ~MyHandler() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main program
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// access to the event loop
|
||||||
|
auto *loop = uv_default_loop();
|
||||||
|
|
||||||
|
// handler for libev
|
||||||
|
MyHandler handler(loop);
|
||||||
|
|
||||||
|
// make a connection
|
||||||
|
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://guest:guest@localhost/"));
|
||||||
|
|
||||||
|
// we need a channel too
|
||||||
|
AMQP::TcpChannel channel(&connection);
|
||||||
|
|
||||||
|
// create a temporary queue
|
||||||
|
channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
|
||||||
|
|
||||||
|
// report the name of the temporary queue
|
||||||
|
std::cout << "declared queue " << name << std::endl;
|
||||||
|
});
|
||||||
|
|
||||||
|
// run the loop
|
||||||
|
uv_run(loop, UV_RUN_DEFAULT);
|
||||||
|
|
||||||
|
// done
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue