From 3ec36b7986c893701dd4e68c50eb680c63c7bfd0 Mon Sep 17 00:00:00 2001 From: "J. Hamatoma" Date: Sun, 15 Dec 2013 23:10:49 +0100 Subject: [PATCH] dayly work --- rplcore/rplconfig.cpp | 229 ++++++++++++++++++++-------------------- rplcore/rpllogger.cpp | 6 +- rplnet/rpltcpclient.cpp | 16 ++- rplnet/rpltcpclient.hpp | 11 +- rplnet/rpltcppeer.cpp | 18 +++- rplnet/rpltcppeer.hpp | 4 +- rplnet/rpltcpserver.cpp | 1 + 7 files changed, 161 insertions(+), 124 deletions(-) diff --git a/rplcore/rplconfig.cpp b/rplcore/rplconfig.cpp index d5a3c27..3ab4699 100644 --- a/rplcore/rplconfig.cpp +++ b/rplcore/rplconfig.cpp @@ -21,14 +21,13 @@ * VALUE is a string */ -enum Locations{ - LOC_WRITE_1 = RPL_FIRST_OF(RPLMODULE_CONFIG), - LOC_WRITE_2, - LOC_READ_1, - LOC_READ_2, +enum Locations { + LOC_WRITE_1 = RPL_FIRST_OF(RPLMODULE_CONFIG), + LOC_WRITE_2, + LOC_READ_1, + LOC_READ_2, }; - /** * Constructor. * @@ -39,43 +38,41 @@ enum Locations{ * @param logger NULL or a logger */ RplConfig::RplConfig(const char* file, bool readOnly, RplLogger* logger) : - m_file(file), - m_lineList(), - m_readOnly(readOnly), - m_logger(logger), - m_ownLogger(logger == NULL) -{ - if (logger == NULL){ - initLogger(); - } - if (file != NULL) - read(file); + m_file(file), + m_lineList(), + m_readOnly(readOnly), + m_logger(logger), + m_ownLogger(logger == NULL){ + if (logger == NULL){ + initLogger(); + } + if (file != NULL) + read(file); } - /** * Destructor. * * Frees the resources. */ RplConfig::~RplConfig(){ - if (m_ownLogger) - delete m_logger; - m_logger = NULL; + if (m_ownLogger) + delete m_logger; + m_logger = NULL; } /** * Inititializes a logger. */ void RplConfig::initLogger(){ - m_logger = new RplLogger(); - RplMemoryAppender* appender = new RplMemoryAppender(); - appender->setAutoDelete(true); - m_logger->addAppender(appender); - - RplStreamAppender* appender2 = new RplStreamAppender(stdout); - appender2->setAutoDelete(true); - m_logger->addAppender(appender2); + m_logger = new RplLogger(); + RplMemoryAppender* appender = new RplMemoryAppender(); + appender->setAutoDelete(true); + m_logger->addAppender(appender); + + RplStreamAppender* appender2 = new RplStreamAppender(stdout); + appender2->setAutoDelete(true); + m_logger->addAppender(appender2); } /** @@ -87,11 +84,11 @@ void RplConfig::initLogger(){ * otherwise: the value assigned to key */ int RplConfig::asInt(const char* key, int defaultValue) const{ - int rc = defaultValue; - if (contains(key)){ - rc = atoi((*this)[key]); - } - return rc; + int rc = defaultValue; + if (contains(key)){ + rc = atoi((*this)[key]); + } + return rc; } /** @@ -103,13 +100,14 @@ int RplConfig::asInt(const char* key, int defaultValue) const{ * otherwise: the value assigned to key */ bool RplConfig::asBool(const char* key, bool defaultValue) const{ - bool rc = defaultValue; - if (contains(key)){ - QByteArray value = (*this)[key].toLower(); - rc = value == "1" || value == "y" || value == "yes" || value == "t" || value == "true"; - } - - return rc; + bool rc = defaultValue; + if (contains(key)){ + QByteArray value = (*this)[key].toLower(); + rc = value == "1" || value == "y" || value == "yes" || value == "t" + || value == "true"; + } + + return rc; } /** @@ -120,11 +118,11 @@ bool RplConfig::asBool(const char* key, bool defaultValue) const{ * @return defaultValue: key does not exist */ QByteArray RplConfig::asString(const char* key, const char* defaultValue){ - QByteArray rc = defaultValue; - if (contains(key)){ - rc = (*this)[key]; - } - return rc; + QByteArray rc = defaultValue; + if (contains(key)){ + rc = (*this)[key]; + } + return rc; } /** @@ -135,33 +133,33 @@ QByteArray RplConfig::asString(const char* key, const char* defaultValue){ * false: error occurred */ bool RplConfig::read(const char* file){ - bool rc = true; - m_lineList.reserve(1024); - FILE* fp = fopen(file, "r"); - if (fp == NULL){ - m_logger->logv(LOG_ERROR, LOC_READ_1, "cannot read: %s", file); - rc = false; - } else{ - char line[64000]; - char* separator; - int lineNo = 0; - while(fgets(line, sizeof line, fp) != NULL){ - lineNo++; - m_lineList.append(line); - if(isalnum(line[0]) && (separator = strchr(line, '=')) != NULL){ - QByteArray key(line, separator - line); - QByteArray value(separator + 1); - key = key.trimmed(); - value = value.trimmed(); - if (contains(key)) - m_logger->logv(LOG_WARNING, LOC_READ_2, "defined more than once: %s-%d: %s", - file, lineNo, line); - else - insert(key, value); - } - } - } - return rc; + bool rc = true; + m_lineList.reserve(1024); + FILE* fp = fopen(file, "r"); + if (fp == NULL){ + m_logger->logv(LOG_ERROR, LOC_READ_1, "cannot read: %s", file); + rc = false; + }else{ + char line[64000]; + char* separator; + int lineNo = 0; + while (fgets(line, sizeof line, fp) != NULL){ + lineNo++; + m_lineList.append(line); + if (isalnum(line[0]) && (separator = strchr(line, '=')) != NULL){ + QByteArray key(line, separator - line); + QByteArray value(separator + 1); + key = key.trimmed(); + value = value.trimmed(); + if (contains(key)) + m_logger->logv(LOG_WARNING, LOC_READ_2, + "defined more than once: %s-%d: %s", file, lineNo, line); + else + insert(key, value); + } + } + } + return rc; } /** @@ -172,13 +170,13 @@ bool RplConfig::read(const char* file){ * false: error occurred */ bool RplConfig::write(const char* file){ - bool rc = false; - if (m_readOnly) - m_logger->log(LOG_ERROR, LOC_WRITE_1, "cannot write: (readonly"); - else{ - m_logger->log(LOG_ERROR, LOC_WRITE_2, "not implemented: write()"); - } - return rc; + bool rc = false; + if (m_readOnly) + m_logger->log(LOG_ERROR, LOC_WRITE_1, "cannot write: (readonly"); + else{ + m_logger->log(LOG_ERROR, LOC_WRITE_2, "not implemented: write()"); + } + return rc; } // ------------------ @@ -190,48 +188,49 @@ bool RplConfig::write(const char* file){ /** * @brief Unit test of RplConfig. */ -class TestRplConfig : public RplTest -{ +class TestRplConfig: public RplTest { public: - TestRplConfig() : RplTest("RplConfig"){} + TestRplConfig() : + RplTest("RplConfig"){ + } public: - void testBasic(){ - QByteArray fn = getTempFile("test.data", "config"); - RplString::write( fn, "#comment\na=1\nb.1==x\n#=\nB=zzz"); - RplConfig config(fn.constData()); - checkE(3, config.size()); - checkE("1", config["a"]); - checkE("=x", config["b.1"]); - checkE("zzz", config["B"]); - } - void testAsX(){ - QByteArray fn = getTempFile("test.data", "config"); - RplString::write( fn, "i=123\nb=1\nb2=true\nb3=yes\ns=abc"); - RplConfig config(fn.constData()); - checkE(5, config.size()); - checkE(123, config.asInt("i", -1)); - checkE(-1, config.asInt("I", -1)); - checkT(config.asBool("b", false)); - checkT(config.asBool("b2", false)); - checkT(config.asBool("b3", false)); - checkT(config.asBool("-", true)); - checkF(config.asBool("-", false)); - checkE("abc", config.asString("s", "x")); - checkE("x", config.asString("S", "x")); - } - - virtual void doIt(){ - testAsX(); - testBasic(); - - } + void testBasic(){ + QByteArray fn = getTempFile("test.data", "config"); + RplString::write(fn, "#comment\na=1\nb.1==x\n#=\nB=zzz"); + RplConfig config(fn.constData()); + checkE(3, config.size()); + checkE("1", config["a"]); + checkE("=x", config["b.1"]); + checkE("zzz", config["B"]); + } + void testAsX(){ + QByteArray fn = getTempFile("test.data", "config"); + RplString::write(fn, "i=123\nb=1\nb2=true\nb3=yes\ns=abc"); + RplConfig config(fn.constData()); + checkE(5, config.size()); + checkE(123, config.asInt("i", -1)); + checkE(-1, config.asInt("I", -1)); + checkT(config.asBool("b", false)); + checkT(config.asBool("b2", false)); + checkT(config.asBool("b3", false)); + checkT(config.asBool("-", true)); + checkF(config.asBool("-", false)); + checkE("abc", config.asString("s", "x")); + checkE("x", config.asString("S", "x")); + } + + virtual void doIt(){ + testAsX(); + testBasic(); + + } }; #endif void testRplConfig(){ #ifdef RPL_TEST - TestRplConfig test; - test.run(); + TestRplConfig test; + test.run(); #endif } diff --git a/rplcore/rpllogger.cpp b/rplcore/rpllogger.cpp index d3181aa..02ccc6e 100644 --- a/rplcore/rpllogger.cpp +++ b/rplcore/rpllogger.cpp @@ -191,10 +191,12 @@ char RplLogger::getPrefixOfLevel(RplLoggerLevel level) const { */ bool RplLogger::isActive(RplLoggerLevel level) const{ bool rc = false; - for (size_t ix = 0; ! rc && ix < m_countAppenders; ix++){ + for (size_t ix = 0; ix < m_countAppenders; ix++){ RplAppender* appender = m_appenders[ix]; - if (appender->isActive(level)) + if (appender->isActive(level)){ rc = true; + break; + } } return rc; } diff --git a/rplnet/rpltcpclient.cpp b/rplnet/rpltcpclient.cpp index adbb54b..167fc21 100644 --- a/rplnet/rpltcpclient.cpp +++ b/rplnet/rpltcpclient.cpp @@ -9,6 +9,7 @@ enum { LOC_1 = RPL_FIRST_OF(RPLMODULE_TCPCLIENT), // 701 + LOC_HANDLE_ERROR_1, }; /** @class RplTcpClient rpltcpclient.hpp "rplnet/rpltcpclient.hpp" @@ -28,7 +29,8 @@ enum { */ RplTcpClient::RplTcpClient(const char* ip, int port, QThread* thread, RplTerminator* terminator, RplLogger* logger) : - m_peer(new RplTcpPeer(ip, port, thread, terminator, logger)) + m_peer(new RplTcpPeer(ip, port, thread, terminator, logger)), + m_logger(logger) { if (ip != NULL && port != 0) setRemoteAddress(ip, port); @@ -49,6 +51,8 @@ void RplTcpClient::setRemoteAddress(const char* ip, int port){ m_peer->setSocket(NULL); else{ socket = new QTcpSocket(); + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), + this, SLOT(handleError(QAbstractSocket::SocketError))); m_peer->setSocket(socket); socket->connectToHost(QString(ip), port); } @@ -62,6 +66,16 @@ RplTcpPeer* RplTcpClient::getPeer() const{ return m_peer; } +/** + * @brief Handles a network error. + * + * @param socketError the error code + */ +void RplTcpClient::handleError(QAbstractSocket::SocketError socketError){ + m_logger->logv(LOG_ERROR, LOC_HANDLE_ERROR_1, "Network error %d", + socketError); +} + /** @class RplClientThread rpltcpclient.hpp "rplnet/rpltcpclient.hpp" * * @brief Implements a thread usable for a tcp client. diff --git a/rplnet/rpltcpclient.hpp b/rplnet/rpltcpclient.hpp index 0f11817..1421072 100644 --- a/rplnet/rpltcpclient.hpp +++ b/rplnet/rpltcpclient.hpp @@ -8,8 +8,14 @@ #ifndef RPLTCPCLIENT_HPP #define RPLTCPCLIENT_HPP -class RplTcpClient +#ifndef RPLNET_HPP +#include "rplnet.hpp" +#endif + +class RplTcpPeer; +class RplTcpClient : public QObject { + Q_OBJECT public: RplTcpClient(const char* ip, int port, QThread* thread, RplTerminator* terminator, RplLogger* logger = NULL); @@ -23,8 +29,11 @@ public: RplTcpPeer* getPeer() const; private: void setRemoteAddress(const char* ip, int port); +public slots: + void handleError(QAbstractSocket::SocketError socketError); private: RplTcpPeer* m_peer; + RplLogger* m_logger; }; class RplClientThread : public QThread { diff --git a/rplnet/rpltcppeer.cpp b/rplnet/rpltcppeer.cpp index e1e3edb..fcfbe20 100644 --- a/rplnet/rpltcppeer.cpp +++ b/rplnet/rpltcppeer.cpp @@ -12,11 +12,12 @@ enum { LOC_READ_BYTES_1, LOC_READ_BYTES_2, LOC_READ_BYTES_3, + LOC_HANDLE_ERROR_1, }; static int s_dummy = 0; -/** @class RplTcpNode rpltcpnode.hpp "rplnet/rpltcpnode.hpp" +/** @class rpltcppeer rpltcppeer.hpp "rplnet/rpltcppeer.hpp" * * @brief Implement the common things for TCP server and client. * @@ -30,7 +31,7 @@ static int s_dummy = 0; *
FLAGS [SALT] COMMAND SIZE
  * 
*