mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-24 00:49:46 +02:00
Use RedisEventArgs
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "Poco/Redis/Redis.h"
|
#include "Poco/Redis/Redis.h"
|
||||||
#include "Poco/Redis/Client.h"
|
#include "Poco/Redis/Client.h"
|
||||||
|
#include "Poco/Redis/RedisEventArgs.h"
|
||||||
#include "Poco/Activity.h"
|
#include "Poco/Activity.h"
|
||||||
|
|
||||||
namespace Poco {
|
namespace Poco {
|
||||||
@@ -37,8 +38,10 @@ class Redis_API AsyncReader
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
BasicEvent<RedisType::Ptr> redisResponse;
|
BasicEvent<RedisEventArgs> redisResponse;
|
||||||
/// Event that is called when a message is received
|
/// Event that is called when a message is received
|
||||||
|
BasicEvent<RedisEventArgs> redisException;
|
||||||
|
/// Event that is called when an error occurred.
|
||||||
|
|
||||||
AsyncReader(Client& client);
|
AsyncReader(Client& client);
|
||||||
/// Constructor.
|
/// Constructor.
|
||||||
|
|||||||
85
Redis/include/Poco/Redis/RedisEventArgs.h
Normal file
85
Redis/include/Poco/Redis/RedisEventArgs.h
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
//
|
||||||
|
// RedisEventArgs.h
|
||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
//
|
||||||
|
// Library: Redis
|
||||||
|
// Package: Redis
|
||||||
|
// Module: RedisEventArgs
|
||||||
|
//
|
||||||
|
// Definition of the RedisEventArgs class.
|
||||||
|
//
|
||||||
|
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||||
|
// and Contributors.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef Redis_RedisEventArgs_INCLUDED
|
||||||
|
#define Redis_RedisEventArgs_INCLUDED
|
||||||
|
|
||||||
|
#include "Poco/Redis/Type.h"
|
||||||
|
|
||||||
|
namespace Poco {
|
||||||
|
namespace Redis {
|
||||||
|
|
||||||
|
class Redis_API RedisEventArgs
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RedisEventArgs(RedisType::Ptr message);
|
||||||
|
/// Constructor
|
||||||
|
|
||||||
|
RedisEventArgs(Exception* e);
|
||||||
|
/// Constructor
|
||||||
|
|
||||||
|
~RedisEventArgs();
|
||||||
|
/// Destructor
|
||||||
|
|
||||||
|
RedisType::Ptr message() const;
|
||||||
|
/// Returns the message retrieved from the Redis server.
|
||||||
|
/// This can be a NULL pointer when this event is about an exception.
|
||||||
|
|
||||||
|
const Exception* exception() const;
|
||||||
|
/// Returns the exception if any, otherwise it returns null pointer
|
||||||
|
|
||||||
|
void stop();
|
||||||
|
/// When called, the AsyncReader will stop.
|
||||||
|
/// Note: The AsyncReader will always stop when this is an exception
|
||||||
|
/// event. Use this for example for pub/sub when there are no
|
||||||
|
/// subcribers anymore ...
|
||||||
|
|
||||||
|
bool isStopped() const;
|
||||||
|
/// Returns true when the AsyncReader will stop
|
||||||
|
|
||||||
|
private:
|
||||||
|
RedisType::Ptr _message;
|
||||||
|
|
||||||
|
Exception* _exception;
|
||||||
|
|
||||||
|
bool _stop;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline RedisType::Ptr RedisEventArgs::message() const
|
||||||
|
{
|
||||||
|
return _message;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const Exception* RedisEventArgs::exception() const
|
||||||
|
{
|
||||||
|
return _exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool RedisEventArgs::isStopped() const
|
||||||
|
{
|
||||||
|
return _stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RedisEventArgs::stop()
|
||||||
|
{
|
||||||
|
_stop = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}} // namespace Poco::Redis
|
||||||
|
|
||||||
|
#endif // Redis_RedisEventArgs_INCLUDED
|
||||||
@@ -40,16 +40,19 @@ void AsyncReader::runActivity()
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
RedisType::Ptr reply = _client.readReply();
|
RedisType::Ptr reply = _client.readReply();
|
||||||
redisResponse.notify(this, reply);
|
|
||||||
|
RedisEventArgs args(reply);
|
||||||
|
redisResponse.notify(this, args);
|
||||||
|
|
||||||
|
if ( args.isStopped() ) stop();
|
||||||
}
|
}
|
||||||
catch(TimeoutException&)
|
catch(Exception& e)
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
catch(Exception &)
|
|
||||||
{
|
{
|
||||||
|
RedisEventArgs args(&e);
|
||||||
|
redisException.notify(this, args);
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
if (!_activity.isStopped()) Thread::trySleep(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
43
Redis/src/RedisEventArgs.cpp
Normal file
43
Redis/src/RedisEventArgs.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
//
|
||||||
|
// RedisEventArgs.cpp
|
||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
//
|
||||||
|
// Library: Redis
|
||||||
|
// Package: Redis
|
||||||
|
// Module: RedisEventArgs
|
||||||
|
//
|
||||||
|
// Implementation of the RedisEventArgs class.
|
||||||
|
//
|
||||||
|
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||||
|
// and Contributors.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Poco/Redis/RedisEventArgs.h"
|
||||||
|
|
||||||
|
namespace Poco {
|
||||||
|
namespace Redis {
|
||||||
|
|
||||||
|
RedisEventArgs::RedisEventArgs(RedisType::Ptr message) :
|
||||||
|
_message(message),
|
||||||
|
_exception(0),
|
||||||
|
_stop(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
RedisEventArgs::RedisEventArgs(Exception* exception) :
|
||||||
|
_message(),
|
||||||
|
_exception(exception ? exception->clone() : 0),
|
||||||
|
_stop(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
RedisEventArgs::~RedisEventArgs()
|
||||||
|
{
|
||||||
|
delete _exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}} // namespace Poco::Redis
|
||||||
Reference in New Issue
Block a user