struct ChannelConfig {
struct Error : std::runtime_error {
- Error(const std::string& message) : runtime_error(message) {}
+ explicit Error(const std::string& message) : runtime_error(message) {}
};
- ChannelConfig(std::string address);
+ explicit ChannelConfig(std::string address);
std::string transport;
std::string key;
class PacketGen : public Gen
{
public:
- PacketGen(const std::string& type)
+ explicit PacketGen(const std::string& type)
: m_type(type)
{
m_gen.add("packet", m_type);
void runImpl();
+ bool start();
+
int connect();
int connectTCP();
int connectUNIX();
throw Error("Invalid port value.");
}
+Conn::Conn(const std::string& address, Callback callback, void* data)
+ : m_impl(new Impl(address, callback, data))
+{
+}
+
+Conn::~Conn()
+{
+}
+
+bool Conn::stop()
+{
+ return m_impl->stop();
+}
+
+bool Conn::connected() const
+{
+ return m_impl->connected();
+}
+
+bool Conn::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
+{
+ return m_impl->request(type, userName, password, pairs);
+}
+
Conn::Impl::Impl(const std::string& address, Callback callback, void* data)
: m_config(address),
m_sock(connect()),
m_connected(true)
{
pthread_mutex_init(&m_mutex, NULL);
- int res = pthread_create(&m_thread, NULL, &Conn::Impl::run, this);
- if (res != 0)
- throw Error("Failed to create thread: " + std::string(strerror(errno)));
}
Conn::Impl::~Impl()
bool Conn::Impl::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
{
+ if (!m_running)
+ if (!start())
+ return false;
MapGen map;
for (PAIRS::const_iterator it = pairs.begin(); it != pairs.end(); ++it)
map.add(it->first, new StringGen(it->second));
break;
}
+
if (!m_running)
break;
m_stopped = true;
}
+bool Conn::Impl::start()
+{
+ int res = pthread_create(&m_thread, NULL, &Conn::Impl::run, this);
+ if (res != 0)
+ return false;
+ return true;
+}
+
int Conn::Impl::connect()
{
if (m_config.transport == "tcp")
void Conn::Impl::processPing()
{
- RadLog("Got ping, sending pong.");
sendPong();
}
void Conn::Impl::processPong()
{
- RadLog("Got pong.");
m_lastActivity = time(NULL);
}
void Conn::Impl::processData()
{
RESULT data;
- RadLog("Got data.");
for (PairsParser::Pairs::const_iterator it = m_parser.reply().begin(); it != m_parser.reply().end(); ++it)
data.reply.push_back(std::make_pair(it->first, it->second));
for (PairsParser::Pairs::const_iterator it = m_parser.modify().begin(); it != m_parser.modify().end(); ++it)
bool Conn::Impl::write(void* data, const char* buf, size_t size)
{
- RadLog("Sending JSON:");
std::string json(buf, size);
- RadLog("%s", json.c_str());
+ RadLog("Sending JSON: %s", json.c_str());
Conn::Impl& impl = *static_cast<Conn::Impl*>(data);
while (size > 0)
{