hide copy constructor, fixed skipped updates, nicer NetMessage notify/wait, nicer Connection cleanup, removed weird workaround with \r, documentation
This commit is contained in:
+10
-15
@@ -129,22 +129,18 @@ void BaseLoop::start()
|
||||
NetMessage* message = m_netQueue.remove();
|
||||
string data = message->getData();
|
||||
|
||||
time_t since, now;
|
||||
time(&now);
|
||||
|
||||
time_t since, until;
|
||||
time(&until);
|
||||
bool listening = message->isListening(since);
|
||||
if (listening == false)
|
||||
since = until;
|
||||
|
||||
if (data.length() == 0)
|
||||
result = getUpdates(since, now);
|
||||
else {
|
||||
if (data.length() > 0) {
|
||||
data.erase(remove(data.begin(), data.end(), '\r'), data.end());
|
||||
data.erase(remove(data.begin(), data.end(), '\n'), data.end());
|
||||
|
||||
L.log(bas, event, ">>> %s", data.c_str());
|
||||
|
||||
if (data.length() == 0 && listening == true)
|
||||
data = "listen";
|
||||
|
||||
// decode message
|
||||
if (strcasecmp(data.c_str(), "STOP") != 0)
|
||||
result = decodeMessage(data, listening);
|
||||
@@ -154,13 +150,12 @@ void BaseLoop::start()
|
||||
L.log(bas, event, "<<< %s", result.c_str());
|
||||
result += "\n\n";
|
||||
}
|
||||
|
||||
// add help sign for Connection::waitSignal()
|
||||
result += "\r";
|
||||
if (listening == true) {
|
||||
result += getUpdates(since, until);
|
||||
}
|
||||
|
||||
// send result to client
|
||||
message->setResult(result, listening, now);
|
||||
message->sendSignal();
|
||||
message->setResult(result, listening, until);
|
||||
|
||||
// stop daemon
|
||||
if (strcasecmp(data.c_str(), "STOP") == 0)
|
||||
@@ -503,7 +498,7 @@ string BaseLoop::decodeMessage(const string& data, bool& listening)
|
||||
break;
|
||||
}
|
||||
|
||||
bool enabled = !listening;
|
||||
bool enabled = !listening; // TODO switch to argument "stop"
|
||||
listening = enabled;
|
||||
return (enabled ? "listen started" : "listen stopped");
|
||||
}
|
||||
|
||||
@@ -135,15 +135,15 @@ private:
|
||||
/**
|
||||
* @brief Decode and execute client message.
|
||||
* @param data the data string to decode (may be empty).
|
||||
* @param listening true if client is in listening mode.
|
||||
* @param listening set to true when the client is in listening mode.
|
||||
* @return result string to send back to client.
|
||||
*/
|
||||
string decodeMessage(const string& data, bool& listening);
|
||||
|
||||
/**
|
||||
* @brief Get the updates received since the specified time.
|
||||
* @param since the time from which to add the updates.
|
||||
* @param until the time from which to add the updates.
|
||||
* @param since the start time from which to add updates (inclusive).
|
||||
* @param until the end time to which to add updates (exclusive).
|
||||
* @return result string to send back to client.
|
||||
*/
|
||||
string getUpdates(time_t since, time_t until);
|
||||
|
||||
@@ -129,14 +129,8 @@ void Connection::run()
|
||||
|
||||
// wait for result
|
||||
L.log(net, debug, "[%05d] wait for result", getID());
|
||||
message.waitSignal();
|
||||
|
||||
L.log(net, debug, "[%05d] result added", getID());
|
||||
string result = message.getResult();
|
||||
|
||||
// remove help sign for Connection::waitSignal()
|
||||
result.erase(remove(result.begin(), result.end(), '\r'), result.end());
|
||||
|
||||
if (m_socket->isValid() == false)
|
||||
break;
|
||||
|
||||
@@ -146,7 +140,6 @@ void Connection::run()
|
||||
|
||||
}
|
||||
|
||||
delete m_socket;
|
||||
L.log(net, trace, "[%05d] connection closed", getID());
|
||||
}
|
||||
|
||||
@@ -177,7 +170,8 @@ Network::~Network()
|
||||
stop();
|
||||
join();
|
||||
|
||||
delete m_tcpServer;
|
||||
if (m_tcpServer != NULL)
|
||||
delete m_tcpServer;
|
||||
}
|
||||
|
||||
void Network::run()
|
||||
|
||||
+37
-36
@@ -47,7 +47,7 @@ public:
|
||||
* @param listenSince start timestamp of listening update.
|
||||
*/
|
||||
NetMessage(const string data, const bool listening, const time_t listenSince)
|
||||
: m_data(data), m_listening(listening), m_listenSince(listenSince)
|
||||
: m_data(data), m_resultSet(false), m_listening(listening), m_listenSince(listenSince)
|
||||
{
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
@@ -62,11 +62,14 @@ public:
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief copy constructor.
|
||||
* @param src message object for copy.
|
||||
* @brief Hidden copy constructor.
|
||||
* @param src the object to copy from.
|
||||
*/
|
||||
NetMessage(const NetMessage& src) : m_data(src.m_data) {}
|
||||
NetMessage(const NetMessage& src);
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief get the data string.
|
||||
@@ -75,54 +78,51 @@ public:
|
||||
string getData() const { return m_data; }
|
||||
|
||||
/**
|
||||
* @brief get the result string.
|
||||
* @brief Wait for the result being set and return the result string.
|
||||
* @return the result string.
|
||||
*/
|
||||
string getResult() const { return m_result; }
|
||||
|
||||
/**
|
||||
* @brief Set the result string.
|
||||
* @param result the result string.
|
||||
* @param listening whether the client is in listening mode.
|
||||
* @param listenUntil end timestamp of last listening update.
|
||||
*/
|
||||
void setResult(const string result, const bool listening, const time_t listenUntil)
|
||||
{ m_result = result; m_listening = listening; m_listenSince = listenUntil; }
|
||||
|
||||
/**
|
||||
* @brief Return whether the client is in listening mode.
|
||||
* @param listenSince start timestamp of listening update.
|
||||
* @return whether the client is in listen mode.
|
||||
*/
|
||||
bool isListening(time_t& listenSince) { listenSince = m_listenSince; return m_listening; }
|
||||
|
||||
/**
|
||||
* @brief wait on notification.
|
||||
*/
|
||||
void waitSignal()
|
||||
{
|
||||
string getResult() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
|
||||
while (m_result.size() == 0)
|
||||
while (m_resultSet == false)
|
||||
pthread_cond_wait(&m_cond, &m_mutex);
|
||||
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
|
||||
return m_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief send notification.
|
||||
* @brief Set the result string and notify the waiting thread.
|
||||
* @param result the result string.
|
||||
* @param listening whether the client is in listening mode.
|
||||
* @param listenUntil the end time to which to updates were added (exclusive).
|
||||
*/
|
||||
void sendSignal()
|
||||
{
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
void setResult(const string result, const bool listening, const time_t listenUntil)
|
||||
{
|
||||
m_result = result;
|
||||
m_listening = listening;
|
||||
m_listenSince = listenUntil;
|
||||
m_resultSet = true;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return whether the client is in listening mode.
|
||||
* @param listenSince set to the start time from which to add updates (inclusive).
|
||||
* @return whether the client is in listening mode.
|
||||
*/
|
||||
bool isListening(time_t& listenSince) { listenSince = m_listenSince; return m_listening; }
|
||||
|
||||
private:
|
||||
/** the data string */
|
||||
string m_data;
|
||||
|
||||
/** whether the result was already set. */
|
||||
bool m_resultSet;
|
||||
|
||||
/** the result string */
|
||||
string m_result;
|
||||
|
||||
@@ -156,6 +156,7 @@ public:
|
||||
: m_socket(socket), m_netQueue(netQueue), m_listening(false)
|
||||
{ m_id = ++m_ids; }
|
||||
|
||||
virtual ~Connection() { delete m_socket; }
|
||||
/**
|
||||
* @brief endless loop for connection instance.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user