mirror of
https://github.com/pocoproject/poco.git
synced 2025-03-07 07:19:08 +01:00
POCO Redis experiment ...
This commit is contained in:
parent
ee62025cc3
commit
9acd00e25a
127
Redis/include/Poco/Redis/Array.h
Normal file
127
Redis/include/Poco/Redis/Array.h
Normal file
@ -0,0 +1,127 @@
|
||||
//
|
||||
// Array.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Array
|
||||
//
|
||||
// Definition of the Array class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#ifndef Redis_Array_INCLUDED
|
||||
#define Redis_Array_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
#include "Poco/Redis/Redis.h"
|
||||
#include "Poco/Redis/Type.h"
|
||||
#include "Poco/Redis/Exception.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
class Redis_API Array
|
||||
{
|
||||
public:
|
||||
Array();
|
||||
|
||||
Array(const Array& copy);
|
||||
|
||||
virtual ~Array();
|
||||
|
||||
void add(Poco::Int64 value);
|
||||
|
||||
void add(const std::string& value);
|
||||
|
||||
void add(const BulkString& value);
|
||||
|
||||
void add();
|
||||
|
||||
void add(AbstractType::Ptr value);
|
||||
|
||||
void clear();
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
size_t size() const;
|
||||
|
||||
private:
|
||||
|
||||
std::vector<AbstractType::Ptr> _elements;
|
||||
|
||||
friend class Connection;
|
||||
};
|
||||
|
||||
inline void Array::clear()
|
||||
{
|
||||
_elements.clear();
|
||||
}
|
||||
|
||||
inline size_t Array::size() const
|
||||
{
|
||||
return _elements.size();
|
||||
}
|
||||
|
||||
inline void Array::add(AbstractType::Ptr value)
|
||||
{
|
||||
_elements.push_back(value);
|
||||
}
|
||||
|
||||
template<>
|
||||
struct ElementTraits<Array>
|
||||
{
|
||||
enum { TypeId = AbstractType::REDIS_ARRAY };
|
||||
|
||||
static std::string typeName()
|
||||
{
|
||||
return "Array";
|
||||
}
|
||||
|
||||
static std::string toString(const Array& value)
|
||||
{
|
||||
return value.toString();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<> inline
|
||||
void Type<Array>::read(RedisSocket& socket)
|
||||
{
|
||||
std::string line;
|
||||
socket.readLine(line);
|
||||
Int64 length = NumberParser::parse64(line);
|
||||
for(int i = 0; i < length; ++i)
|
||||
{
|
||||
char elementType = socket.get();
|
||||
AbstractType::Ptr t;
|
||||
|
||||
switch(elementType)
|
||||
{
|
||||
case ':': // Integer
|
||||
t = new Type<Int64>();
|
||||
break;
|
||||
case '+' : // Simple String
|
||||
t = new Type<std::string>();
|
||||
break;
|
||||
case '$' : // Bulk String
|
||||
t = new Type<BulkString>();
|
||||
break;
|
||||
}
|
||||
|
||||
if ( t.isNull() ) throw RedisException("Wrong answer received from Redis server");
|
||||
|
||||
t->read(socket);
|
||||
_value.add(t);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
|
||||
#endif // Redis_Array_INCLUDED
|
186
Redis/include/Poco/Redis/Client.h
Normal file
186
Redis/include/Poco/Redis/Client.h
Normal file
@ -0,0 +1,186 @@
|
||||
//
|
||||
// Connection.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Connection
|
||||
//
|
||||
// Definition of the Connection class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef Redis_Connection_INCLUDED
|
||||
#define Redis_Connection_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/Optional.h"
|
||||
|
||||
#include "Poco/Redis/Redis.h"
|
||||
#include "Poco/Redis/Array.h"
|
||||
#include "Poco/Redis/RedisSocket.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
|
||||
class Redis_API Connection
|
||||
/// Represents a connection to a Redis server
|
||||
{
|
||||
public:
|
||||
typedef Poco::SharedPtr<Connection> Ptr;
|
||||
|
||||
Connection();
|
||||
/// Default constructor. Use this when you want to
|
||||
/// connect later on.
|
||||
|
||||
Connection(const std::string& hostAndPort);
|
||||
/// Constructor which connects to the given Redis host/port.
|
||||
/// The host and port must be separated with a colon.
|
||||
|
||||
Connection(const std::string& host, int port);
|
||||
/// Constructor which connects to the given Redis host/port.
|
||||
|
||||
Connection(const Net::SocketAddress& addrs);
|
||||
/// Constructor which connects to the given Redis host/port.
|
||||
|
||||
virtual ~Connection();
|
||||
/// Destructor
|
||||
|
||||
Net::SocketAddress address() const;
|
||||
/// Returns the address of the Redis connection
|
||||
|
||||
void connect(const std::string& hostAndPort);
|
||||
/// Connects to the given Redis server. The host and port must be separated
|
||||
/// with a colon.
|
||||
|
||||
void connect(const std::string& host, int port);
|
||||
/// Connects to the given Redis server.
|
||||
|
||||
void connect(const Net::SocketAddress& addrs);
|
||||
/// Connects to the given Redis server.
|
||||
|
||||
void disconnect();
|
||||
/// Disconnects from the Redis server
|
||||
|
||||
void sendCommand(const Array& command);
|
||||
/// Sends a request to the Redis server
|
||||
|
||||
template<typename T>
|
||||
void sendCommand(const Array& command, T& result)
|
||||
{
|
||||
Type<T> resultType;
|
||||
|
||||
sendCommand(command);
|
||||
|
||||
char type = _socket.get();
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case ':':
|
||||
{
|
||||
if ( resultType.getType() == AbstractType::REDIS_INTEGER )
|
||||
{
|
||||
resultType.read(_socket);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string message;
|
||||
message.append("Expected ");
|
||||
message.append(ElementTraits<T>::typeName());
|
||||
message.append(", got an Integer");
|
||||
throw InvalidArgumentException(message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '+': // Simple String
|
||||
{
|
||||
if ( resultType.getType() == AbstractType::REDIS_SIMPLE_STRING )
|
||||
{
|
||||
resultType.read(_socket);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string message;
|
||||
message.append("Expected ");
|
||||
message.append(ElementTraits<T>::typeName());
|
||||
message.append(", got a Simple String");
|
||||
throw InvalidArgumentException(message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '-' : // Error
|
||||
{
|
||||
std::string error;
|
||||
_socket.readLine(error);
|
||||
throw RedisException(error);
|
||||
}
|
||||
case '$' : // Bulk String
|
||||
{
|
||||
if ( resultType.getType() == AbstractType::REDIS_BULK_STRING )
|
||||
{
|
||||
resultType.read(_socket);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string message;
|
||||
message.append("Expected ");
|
||||
message.append(ElementTraits<T>::typeName());
|
||||
message.append(", got a Bulk String");
|
||||
throw InvalidArgumentException(message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '*' : //Array
|
||||
{
|
||||
if ( resultType.getType() == AbstractType::REDIS_ARRAY )
|
||||
{
|
||||
resultType.read(_socket);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string message;
|
||||
message.append("Expected ");
|
||||
message.append(ElementTraits<T>::typeName());
|
||||
message.append(", got an Array");
|
||||
throw InvalidArgumentException(message);
|
||||
}
|
||||
throw InvalidArgumentException("Expected integer, got an array");
|
||||
}
|
||||
default:
|
||||
throw IOException("Invalid Redis type returned");
|
||||
}
|
||||
|
||||
result = resultType.value();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Connection(const Connection&);
|
||||
Connection& operator = (const Connection&);
|
||||
|
||||
Net::SocketAddress _address;
|
||||
RedisSocket _socket;
|
||||
|
||||
void connect();
|
||||
/// Connects to the Redis server
|
||||
};
|
||||
|
||||
|
||||
inline Net::SocketAddress Connection::address() const
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Redis
|
||||
|
||||
|
||||
#endif //Redis_Connection_INCLUDED
|
32
Redis/include/Poco/Redis/Exception.h
Normal file
32
Redis/include/Poco/Redis/Exception.h
Normal file
@ -0,0 +1,32 @@
|
||||
//
|
||||
// Exception.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Exception
|
||||
//
|
||||
// Definition of the Exception class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#ifndef Redis_Exception_INCLUDED
|
||||
#define Redis_Exception_INCLUDED
|
||||
|
||||
#include "Poco/Redis/Redis.h"
|
||||
#include <typeinfo>
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
POCO_DECLARE_EXCEPTION(Redis_API, RedisException, Exception)
|
||||
|
||||
}}
|
||||
|
||||
#endif // Redis_Exception_INCLUDED
|
66
Redis/include/Poco/Redis/Redis.h
Normal file
66
Redis/include/Poco/Redis/Redis.h
Normal file
@ -0,0 +1,66 @@
|
||||
//
|
||||
// Redis.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Redis
|
||||
//
|
||||
// Basic definitions for the Poco Redis library.
|
||||
// This file must be the first file included by every other Redis
|
||||
// header file.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef RedisRedis_INCLUDED
|
||||
#define RedisRedis_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
|
||||
|
||||
//
|
||||
// The following block is the standard way of creating macros which make exporting
|
||||
// from a DLL simpler. All files within this DLL are compiled with the Redis_EXPORTS
|
||||
// symbol defined on the command line. this symbol should not be defined on any project
|
||||
// that uses this DLL. This way any other project whose source files include this file see
|
||||
// Redis_API functions as being imported from a DLL, wheras this DLL sees symbols
|
||||
// defined with this macro as being exported.
|
||||
//
|
||||
|
||||
|
||||
#if defined(_WIN32) && defined(POCO_DLL)
|
||||
#if defined(Redis_EXPORTS)
|
||||
#define Redis_API __declspec(dllexport)
|
||||
#else
|
||||
#define Redis_API __declspec(dllimport)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(Redis_API)
|
||||
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
|
||||
#define Redis_API __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define Redis_API
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
//
|
||||
// Automatically link Redis library.
|
||||
//
|
||||
#if defined(_MSC_VER)
|
||||
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(Redis_EXPORTS)
|
||||
#pragma comment(lib, "PocoRedis" POCO_LIB_SUFFIX)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#endif // RedisRedis_INCLUDED
|
68
Redis/include/Poco/Redis/RedisSocket.h
Normal file
68
Redis/include/Poco/Redis/RedisSocket.h
Normal file
@ -0,0 +1,68 @@
|
||||
//
|
||||
// RedistSocket.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: RedistSocket
|
||||
//
|
||||
// Definition of the RedistSocket class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#ifndef Redis_RedisSocket_INCLUDED
|
||||
#define Redis_RedisSocket_INCLUDED
|
||||
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/Net/StreamSocket.h"
|
||||
|
||||
#include "Poco/Redis/Redis.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
class Redis_API RedisSocket
|
||||
{
|
||||
public:
|
||||
|
||||
RedisSocket();
|
||||
virtual ~RedisSocket();
|
||||
|
||||
void close();
|
||||
|
||||
void connect(const Net::SocketAddress& addrs);
|
||||
|
||||
int get();
|
||||
|
||||
int peek();
|
||||
|
||||
void read(UInt64 length, std::string& data);
|
||||
|
||||
int write(const char* buffer, std::streamsize length);
|
||||
|
||||
int buffered();
|
||||
|
||||
void refill();
|
||||
|
||||
void readLine(std::string& line);
|
||||
|
||||
private:
|
||||
|
||||
RedisSocket(RedisSocket& copy);
|
||||
RedisSocket& operator = (const RedisSocket&);
|
||||
|
||||
Net::StreamSocket _socket;
|
||||
|
||||
char* _buffer;
|
||||
char* _current;
|
||||
char* _end;
|
||||
};
|
||||
|
||||
} }
|
||||
|
||||
#endif // Redis_RedisSocket_INCLUDED
|
207
Redis/include/Poco/Redis/Type.h
Normal file
207
Redis/include/Poco/Redis/Type.h
Normal file
@ -0,0 +1,207 @@
|
||||
//
|
||||
// Type.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Type
|
||||
//
|
||||
// Definition of the Type class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#ifndef Redis_Type_INCLUDED
|
||||
#define Redis_Type_INCLUDED
|
||||
|
||||
#include "Poco/NumberFormatter.h"
|
||||
#include "Poco/NumberParser.h"
|
||||
#include "Poco/SharedPtr.h"
|
||||
#include "Poco/Optional.h"
|
||||
|
||||
#include "Poco/Redis/Redis.h"
|
||||
#include "Poco/Redis/RedisSocket.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
|
||||
class Redis_API AbstractType
|
||||
{
|
||||
public:
|
||||
|
||||
typedef SharedPtr<AbstractType> Ptr;
|
||||
|
||||
AbstractType();
|
||||
virtual ~AbstractType();
|
||||
|
||||
virtual int getType() const = 0;
|
||||
|
||||
virtual void read(RedisSocket& socket) = 0;
|
||||
|
||||
virtual std::string toString() const = 0;
|
||||
|
||||
enum Types {
|
||||
REDIS_INTEGER,
|
||||
REDIS_SIMPLE_STRING,
|
||||
REDIS_BULK_STRING,
|
||||
REDIS_ARRAY
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ElementTraits
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct ElementTraits<Poco::Int64>
|
||||
{
|
||||
enum { TypeId = AbstractType::REDIS_INTEGER };
|
||||
|
||||
static std::string typeName()
|
||||
{
|
||||
return "Integer";
|
||||
}
|
||||
|
||||
static std::string toString(const Poco::Int64& value)
|
||||
{
|
||||
return ":" + NumberFormatter::format(value) + "\r\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct ElementTraits<std::string>
|
||||
{
|
||||
enum { TypeId = AbstractType::REDIS_SIMPLE_STRING };
|
||||
|
||||
static std::string typeName()
|
||||
{
|
||||
return "Simple String";
|
||||
}
|
||||
|
||||
static std::string toString(const std::string& value)
|
||||
{
|
||||
return "+" + value + "\r\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
typedef Optional<std::string> BulkString;
|
||||
|
||||
|
||||
template<>
|
||||
struct ElementTraits<BulkString>
|
||||
{
|
||||
enum { TypeId = AbstractType::REDIS_BULK_STRING };
|
||||
|
||||
static std::string typeName()
|
||||
{
|
||||
return "Bulk String";
|
||||
}
|
||||
|
||||
static std::string toString(const BulkString& value)
|
||||
{
|
||||
if ( value.isSpecified() ) {
|
||||
std::string s = value.value();
|
||||
return "$" + NumberFormatter::format(s.length()) + "\r\n" + s + "\r\n";
|
||||
}
|
||||
return "$-1\r\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
class Redis_API Type : public AbstractType
|
||||
{
|
||||
public:
|
||||
|
||||
Type()
|
||||
{
|
||||
}
|
||||
|
||||
Type(const T& t) : _value(t)
|
||||
{
|
||||
}
|
||||
|
||||
Type(const Type& copy) : _value(copy._value)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Type()
|
||||
{
|
||||
}
|
||||
|
||||
int getType() const
|
||||
{
|
||||
return ElementTraits<T>::TypeId;
|
||||
}
|
||||
|
||||
void read(RedisSocket& socket)
|
||||
{
|
||||
}
|
||||
|
||||
virtual std::string toString() const {
|
||||
return ElementTraits<T>::toString(_value);
|
||||
}
|
||||
|
||||
T value() const
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T _value;
|
||||
};
|
||||
|
||||
template<> inline
|
||||
void Type<Int64>::read(RedisSocket& socket)
|
||||
{
|
||||
std::string number;
|
||||
socket.readLine(number);
|
||||
_value = NumberParser::parse64(number);
|
||||
}
|
||||
|
||||
template<> inline
|
||||
void Type<std::string>::read(RedisSocket& socket)
|
||||
{
|
||||
std::string s;
|
||||
socket.readLine(s);
|
||||
_value = s;
|
||||
}
|
||||
|
||||
template<> inline
|
||||
void Type<BulkString>::read(RedisSocket& socket)
|
||||
{
|
||||
_value.clear();
|
||||
|
||||
std::string line;
|
||||
socket.readLine(line);
|
||||
int length = NumberParser::parse64(line);
|
||||
if ( length >= 0 )
|
||||
{
|
||||
std::string s;
|
||||
socket.read(length, s);
|
||||
_value.assign(s);
|
||||
socket.readLine(line);
|
||||
}
|
||||
else // -1
|
||||
{
|
||||
socket.readLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
|
||||
#endif // Redis_Type_INCLUDED
|
72
Redis/src/Array.cpp
Normal file
72
Redis/src/Array.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
//
|
||||
// Array.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Array
|
||||
//
|
||||
// Implementation of the Array class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "Poco/Redis/Array.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
Array::Array()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Array::Array(const Array& copy) : _elements(copy._elements)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Array::~Array()
|
||||
{
|
||||
}
|
||||
|
||||
void Array::add(Int64 value)
|
||||
{
|
||||
_elements.push_back(new Type<Int64>(value));
|
||||
}
|
||||
|
||||
void Array::add(const std::string& value)
|
||||
{
|
||||
BulkString rs(value);
|
||||
_elements.push_back(new Type<BulkString>(rs));
|
||||
}
|
||||
|
||||
void Array::add(const BulkString& value)
|
||||
{
|
||||
_elements.push_back(new Type<BulkString>(value));
|
||||
}
|
||||
|
||||
void Array::add()
|
||||
{
|
||||
BulkString value;
|
||||
_elements.push_back(new Type<BulkString>(value));
|
||||
}
|
||||
|
||||
std::string Array::toString() const
|
||||
{
|
||||
std::stringstream result;
|
||||
result << "*" << _elements.size() << "\r\n";
|
||||
for(std::vector<AbstractType::Ptr>::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
{
|
||||
result << (*it)->toString();
|
||||
}
|
||||
return result.str();
|
||||
}
|
||||
|
||||
} }
|
92
Redis/src/Client.cpp
Normal file
92
Redis/src/Client.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
//
|
||||
// Connection.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Connection
|
||||
//
|
||||
// Implementation of the Connection class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#include "Poco/Redis/Client.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
|
||||
Connection::Connection() : _address(), _socket()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection(const std::string& hostAndPort) : _address(hostAndPort), _socket()
|
||||
{
|
||||
connect();
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection(const std::string& host, int port) : _address(host, port), _socket()
|
||||
{
|
||||
connect();
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection(const Net::SocketAddress& addrs) : _address(addrs), _socket()
|
||||
{
|
||||
connect();
|
||||
}
|
||||
|
||||
|
||||
Connection::~Connection()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void Connection::connect()
|
||||
{
|
||||
_socket.connect(_address);
|
||||
}
|
||||
|
||||
|
||||
void Connection::connect(const std::string& hostAndPort)
|
||||
{
|
||||
_address = Net::SocketAddress(hostAndPort);
|
||||
connect();
|
||||
}
|
||||
|
||||
|
||||
void Connection::connect(const std::string& host, int port)
|
||||
{
|
||||
_address = Net::SocketAddress(host, port);
|
||||
connect();
|
||||
}
|
||||
|
||||
|
||||
void Connection::connect(const Net::SocketAddress& addrs)
|
||||
{
|
||||
_address = addrs;
|
||||
connect();
|
||||
}
|
||||
|
||||
|
||||
void Connection::disconnect()
|
||||
{
|
||||
_socket.close();
|
||||
}
|
||||
|
||||
void Connection::sendCommand(const Array& command)
|
||||
{
|
||||
std::string commandStr = command.toString();
|
||||
_socket.write(commandStr.c_str(), commandStr.length());
|
||||
}
|
||||
|
||||
|
||||
} } // Poco::Redis
|
25
Redis/src/Exception.cpp
Normal file
25
Redis/src/Exception.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
//
|
||||
// Exception.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Exception
|
||||
//
|
||||
// Implementation of the Exception class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#include "Poco/Redis/Exception.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
POCO_IMPLEMENT_EXCEPTION(RedisException, Exception, "Redis Exception")
|
||||
|
||||
}}
|
119
Redis/src/RedisSocket.cpp
Normal file
119
Redis/src/RedisSocket.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
//
|
||||
// RedistSocket.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: RedistSocket
|
||||
//
|
||||
// Implementation of the RedistSocket class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#include "Poco/Redis/RedisSocket.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
RedisSocket::RedisSocket() : _socket(), _buffer(0), _current(0), _end(0)
|
||||
{
|
||||
}
|
||||
|
||||
RedisSocket::~RedisSocket()
|
||||
{
|
||||
if ( _buffer ) delete[] _buffer;
|
||||
|
||||
try
|
||||
{
|
||||
_socket.close();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
void RedisSocket::close()
|
||||
{
|
||||
_socket.close();
|
||||
}
|
||||
|
||||
void RedisSocket::connect(const Net::SocketAddress& addrs)
|
||||
{
|
||||
_socket.connect(addrs);
|
||||
}
|
||||
|
||||
int RedisSocket::get()
|
||||
{
|
||||
if ( _current == _end ) refill();
|
||||
|
||||
if ( _current < _end ) return *_current++;
|
||||
|
||||
return std::char_traits<char>::eof();
|
||||
}
|
||||
|
||||
int RedisSocket::peek()
|
||||
{
|
||||
if ( _current == _end ) refill();
|
||||
|
||||
if ( _current < _end ) return *_current;
|
||||
|
||||
return std::char_traits<char>::eof();
|
||||
}
|
||||
|
||||
void RedisSocket::read(UInt64 length, std::string& data)
|
||||
{
|
||||
UInt64 count = 0;
|
||||
data.clear();
|
||||
|
||||
while(count < length)
|
||||
{
|
||||
int c = get();
|
||||
if ( c == -1 ) throw IOException("Invalid EOF");
|
||||
|
||||
data += c;
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
int RedisSocket::write(const char* buffer, std::streamsize length)
|
||||
{
|
||||
_socket.sendBytes(buffer, (int) length);
|
||||
}
|
||||
|
||||
void RedisSocket::refill()
|
||||
{
|
||||
if ( ! _buffer ) _buffer = new char[1024];
|
||||
|
||||
_current = _end = _buffer;
|
||||
int n = _socket.receiveBytes(_buffer, 1024);
|
||||
_end += n;
|
||||
}
|
||||
|
||||
|
||||
void RedisSocket::readLine(std::string& line)
|
||||
{
|
||||
line.clear();
|
||||
|
||||
int c = get();
|
||||
|
||||
while(1)
|
||||
{
|
||||
if ( c == -1 ) throw IOException("Invalid EOF");
|
||||
|
||||
if ( c == '\r' && peek() == '\n' )
|
||||
{
|
||||
get();
|
||||
break;
|
||||
}
|
||||
|
||||
line += c;
|
||||
c = get();
|
||||
}
|
||||
}
|
||||
|
||||
} }
|
32
Redis/src/Type.cpp
Normal file
32
Redis/src/Type.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
//
|
||||
// Type.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: Redis
|
||||
// Package: Redis
|
||||
// Module: Type
|
||||
//
|
||||
// Implementation of the Type class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#include "Poco/Redis/Type.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace Redis {
|
||||
|
||||
|
||||
AbstractType::AbstractType()
|
||||
{
|
||||
}
|
||||
|
||||
AbstractType::~AbstractType()
|
||||
{
|
||||
}
|
||||
|
||||
}}
|
19
Redis/testsuite/src/Driver.cpp
Normal file
19
Redis/testsuite/src/Driver.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// Driver.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Console-based test driver for Poco MongoDB.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TestRunner.h"
|
||||
#include "RedisTestSuite.h"
|
||||
|
||||
|
||||
CppUnitMain(RedisTestSuite)
|
113
Redis/testsuite/src/RedisTest.cpp
Normal file
113
Redis/testsuite/src/RedisTest.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
//
|
||||
// RedisTest.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
#include "Poco/DateTime.h"
|
||||
#include "Poco/ObjectPool.h"
|
||||
|
||||
#include "Poco/Net/NetException.h"
|
||||
|
||||
#include "RedisTest.h"
|
||||
#include "Poco/Redis/Client.h"
|
||||
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
|
||||
using namespace Poco::Redis;
|
||||
|
||||
|
||||
bool RedisTest::_connected = false;
|
||||
Poco::Redis::Connection RedisTest::_redis;
|
||||
|
||||
|
||||
RedisTest::RedisTest(const std::string& name):
|
||||
CppUnit::TestCase("Redis"),
|
||||
_host("localhost"),
|
||||
_port(6379)
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
_redis.connect(_host, _port);
|
||||
_connected = true;
|
||||
std::cout << "Connected to [" << _host << ':' << _port << ']' << std::endl;
|
||||
}
|
||||
catch (Poco::Net::ConnectionRefusedException& e)
|
||||
{
|
||||
std::cout << "Couldn't connect to " << e.message() << ". " << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
RedisTest::~RedisTest()
|
||||
{
|
||||
if (_connected)
|
||||
{
|
||||
_redis.disconnect();
|
||||
_connected = false;
|
||||
std::cout << "Disconnected from [" << _host << ':' << _port << ']' << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void RedisTest::setUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void RedisTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void RedisTest::testEcho()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Array command;
|
||||
command.add("ECHO");
|
||||
command.add("Hello World");
|
||||
|
||||
BulkString result;
|
||||
_redis.sendCommand(command, result);
|
||||
}
|
||||
|
||||
void RedisTest::testPing()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Array command;
|
||||
command.add("PING");
|
||||
|
||||
std::string result;
|
||||
_redis.sendCommand(command, result);
|
||||
}
|
||||
|
||||
CppUnit::Test* RedisTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RedisTest");
|
||||
|
||||
CppUnit_addTest(pSuite, RedisTest, testEcho);
|
||||
CppUnit_addTest(pSuite, RedisTest, testPing);
|
||||
|
||||
return pSuite;
|
||||
}
|
51
Redis/testsuite/src/RedisTest.h
Normal file
51
Redis/testsuite/src/RedisTest.h
Normal file
@ -0,0 +1,51 @@
|
||||
//
|
||||
// RedisTest.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Definition of the RedisTest class.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef RedisTest_INCLUDED
|
||||
#define RedisTest_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Redis/Redis.h"
|
||||
#include "Poco/Redis/Client.h"
|
||||
|
||||
#include "CppUnit/TestCase.h"
|
||||
|
||||
|
||||
class RedisTest: public CppUnit::TestCase
|
||||
{
|
||||
public:
|
||||
RedisTest(const std::string& name);
|
||||
|
||||
|
||||
virtual ~RedisTest();
|
||||
|
||||
void testEcho();
|
||||
void testPing();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
|
||||
std::string _host;
|
||||
unsigned _port;
|
||||
static bool _connected;
|
||||
static Poco::Redis::Connection _redis;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // RedisTest_INCLUDED
|
24
Redis/testsuite/src/RedisTestSuite.cpp
Normal file
24
Redis/testsuite/src/RedisTestSuite.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
//
|
||||
// RedisTestSuite.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "RedisTestSuite.h"
|
||||
#include "RedisTest.h"
|
||||
|
||||
|
||||
CppUnit::Test* RedisTestSuite::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RedisTestSuite");
|
||||
|
||||
pSuite->addTest(RedisTest::suite());
|
||||
|
||||
return pSuite;
|
||||
}
|
29
Redis/testsuite/src/RedisTestSuite.h
Normal file
29
Redis/testsuite/src/RedisTestSuite.h
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// RedisTestSuite.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Definition of the RedisTestSuite class.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef RedisTestSuite_INCLUDED
|
||||
#define RedisTestSuite_INCLUDED
|
||||
|
||||
|
||||
#include "CppUnit/TestSuite.h"
|
||||
|
||||
|
||||
class RedisTestSuite
|
||||
{
|
||||
public:
|
||||
static CppUnit::Test* suite();
|
||||
};
|
||||
|
||||
|
||||
#endif // RedisTestSuite_INCLUDED
|
32
Redis/testsuite/src/WinCEDriver.cpp
Normal file
32
Redis/testsuite/src/WinCEDriver.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
//
|
||||
// WinCEDriver.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Console-based test driver for Windows CE.
|
||||
//
|
||||
// Copyright (c) 2004-2010, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TestRunner.h"
|
||||
#include "RedisTestSuite.h"
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
int _tmain(int argc, wchar_t* argv[])
|
||||
{
|
||||
std::vector<std::string> args;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
char buffer[1024];
|
||||
std::wcstombs(buffer, argv[i], sizeof(buffer));
|
||||
args.push_back(std::string(buffer));
|
||||
}
|
||||
CppUnit::TestRunner runner;
|
||||
runner.addTest("RedisTestSuite", RedisTestSuite::suite());
|
||||
return runner.run(args) ? 0 : 1;
|
||||
}
|
30
Redis/testsuite/src/WinDriver.cpp
Normal file
30
Redis/testsuite/src/WinDriver.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
//
|
||||
// WinDriver.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Windows test driver for Poco MongoDB.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "WinTestRunner/WinTestRunner.h"
|
||||
#include "RedisTestSuite.h"
|
||||
|
||||
|
||||
class TestDriver: public CppUnit::WinTestRunnerApp
|
||||
{
|
||||
void TestMain()
|
||||
{
|
||||
CppUnit::WinTestRunner runner;
|
||||
runner.addTest(RedisTestSuite::suite());
|
||||
runner.run();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TestDriver theDriver;
|
Loading…
x
Reference in New Issue
Block a user