use spaces instead of tab
This commit is contained in:
+11
-11
@@ -18,8 +18,8 @@
|
||||
|
||||
#include "clock.h"
|
||||
#ifdef __MACH__
|
||||
# include <mach/clock.h>
|
||||
# include <mach/mach.h>
|
||||
# include <mach/clock.h>
|
||||
# include <mach/mach.h>
|
||||
#endif
|
||||
|
||||
#ifdef __MACH__
|
||||
@@ -29,15 +29,15 @@ static clock_serv_t clockServ;
|
||||
|
||||
void clockGettime(struct timespec* t) {
|
||||
#ifdef __MACH__
|
||||
if (!clockInitialized) {
|
||||
clockInitialized = true;
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &clockServ);
|
||||
}
|
||||
mach_timespec_t mts;
|
||||
clock_get_time(clockServ, &mts);
|
||||
t->tv_sec = mts.tv_sec;
|
||||
t->tv_nsec = mts.tv_nsec;
|
||||
if (!clockInitialized) {
|
||||
clockInitialized = true;
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &clockServ);
|
||||
}
|
||||
mach_timespec_t mts;
|
||||
clock_get_time(clockServ, &mts);
|
||||
t->tv_sec = mts.tv_sec;
|
||||
t->tv_nsec = mts.tv_nsec;
|
||||
#else
|
||||
clock_gettime(CLOCK_REALTIME, t);
|
||||
clock_gettime(CLOCK_REALTIME, t);
|
||||
#endif
|
||||
}
|
||||
|
||||
+101
-101
@@ -27,23 +27,23 @@
|
||||
|
||||
/** the name of each @a LogFacility. */
|
||||
static const char *facilityNames[] = {
|
||||
"main",
|
||||
"network",
|
||||
"bus",
|
||||
"update",
|
||||
"other",
|
||||
"all",
|
||||
NULL
|
||||
"main",
|
||||
"network",
|
||||
"bus",
|
||||
"update",
|
||||
"other",
|
||||
"all",
|
||||
NULL
|
||||
};
|
||||
|
||||
/** the name of each @a LogLevel. */
|
||||
static const char* levelNames[] = {
|
||||
"none",
|
||||
"error",
|
||||
"notice",
|
||||
"info",
|
||||
"debug",
|
||||
NULL
|
||||
"none",
|
||||
"error",
|
||||
"notice",
|
||||
"info",
|
||||
"debug",
|
||||
NULL
|
||||
};
|
||||
|
||||
/** the bit combination of currently active log facilities (1 << @a LogFacility). */
|
||||
@@ -56,119 +56,119 @@ static LogLevel s_logLevel = ll_notice;
|
||||
static FILE* s_logFile = stdout;
|
||||
|
||||
bool setLogFacilities(const char* facilities) {
|
||||
char *input = strdup(facilities);
|
||||
char *opt = reinterpret_cast<char*>(input), *value = NULL;
|
||||
int newFacilites = 0;
|
||||
while (*opt) {
|
||||
int val = getsubopt(&opt, (char *const *)facilityNames, &value);
|
||||
if (val < 0 || val > lf_COUNT || value) {
|
||||
free(input);
|
||||
return false;
|
||||
}
|
||||
if (val == lf_COUNT) {
|
||||
newFacilites = LF_ALL;
|
||||
} else {
|
||||
newFacilites |= 1 << val;
|
||||
}
|
||||
}
|
||||
//s_lastFacilities = newFacilites;
|
||||
s_logFacilites = newFacilites;
|
||||
free(input);
|
||||
return true;
|
||||
char *input = strdup(facilities);
|
||||
char *opt = reinterpret_cast<char*>(input), *value = NULL;
|
||||
int newFacilites = 0;
|
||||
while (*opt) {
|
||||
int val = getsubopt(&opt, (char *const *)facilityNames, &value);
|
||||
if (val < 0 || val > lf_COUNT || value) {
|
||||
free(input);
|
||||
return false;
|
||||
}
|
||||
if (val == lf_COUNT) {
|
||||
newFacilites = LF_ALL;
|
||||
} else {
|
||||
newFacilites |= 1 << val;
|
||||
}
|
||||
}
|
||||
//s_lastFacilities = newFacilites;
|
||||
s_logFacilites = newFacilites;
|
||||
free(input);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool getLogFacilities(char* buffer) {
|
||||
if (s_logFacilites == LF_ALL) {
|
||||
return snprintf(buffer, 48, "%s", facilityNames[lf_COUNT]) != 0;
|
||||
}
|
||||
*buffer = 0; // for strcat to work
|
||||
bool found = false;
|
||||
size_t len = 0;
|
||||
for (int val = 0; val < lf_COUNT; val++) {
|
||||
if (s_logFacilites&(1 << val)) {
|
||||
if (found) {
|
||||
len += snprintf(buffer+len, 48-len, ",");
|
||||
}
|
||||
found = true;
|
||||
len += snprintf(buffer+len, 48-len, "%s", facilityNames[val]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
if (s_logFacilites == LF_ALL) {
|
||||
return snprintf(buffer, 48, "%s", facilityNames[lf_COUNT]) != 0;
|
||||
}
|
||||
*buffer = 0; // for strcat to work
|
||||
bool found = false;
|
||||
size_t len = 0;
|
||||
for (int val = 0; val < lf_COUNT; val++) {
|
||||
if (s_logFacilites&(1 << val)) {
|
||||
if (found) {
|
||||
len += snprintf(buffer+len, 48-len, ",");
|
||||
}
|
||||
found = true;
|
||||
len += snprintf(buffer+len, 48-len, "%s", facilityNames[val]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool setLogLevel(const char* level) {
|
||||
char *input = strdup(level);
|
||||
char *opt = reinterpret_cast<char*>(input), *value = NULL;
|
||||
int newLevel = 0;
|
||||
if (*opt) {
|
||||
int val = getsubopt(&opt, (char *const *)levelNames, &value);
|
||||
if (val < 0 || val >= ll_COUNT || value || *opt) {
|
||||
free(input);
|
||||
return false;
|
||||
}
|
||||
newLevel = val;
|
||||
}
|
||||
s_logLevel = (LogLevel)newLevel;
|
||||
free(input);
|
||||
return true;
|
||||
char *input = strdup(level);
|
||||
char *opt = reinterpret_cast<char*>(input), *value = NULL;
|
||||
int newLevel = 0;
|
||||
if (*opt) {
|
||||
int val = getsubopt(&opt, (char *const *)levelNames, &value);
|
||||
if (val < 0 || val >= ll_COUNT || value || *opt) {
|
||||
free(input);
|
||||
return false;
|
||||
}
|
||||
newLevel = val;
|
||||
}
|
||||
s_logLevel = (LogLevel)newLevel;
|
||||
free(input);
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* getLogLevel() {
|
||||
return levelNames[s_logLevel];
|
||||
return levelNames[s_logLevel];
|
||||
}
|
||||
|
||||
bool setLogFile(const char* filename) {
|
||||
FILE* newFile = fopen(filename, "a");
|
||||
if (newFile == NULL) {
|
||||
return false;
|
||||
}
|
||||
closeLogFile();
|
||||
s_logFile = newFile;
|
||||
return true;
|
||||
FILE* newFile = fopen(filename, "a");
|
||||
if (newFile == NULL) {
|
||||
return false;
|
||||
}
|
||||
closeLogFile();
|
||||
s_logFile = newFile;
|
||||
return true;
|
||||
}
|
||||
|
||||
void closeLogFile() {
|
||||
if (s_logFile != NULL) {
|
||||
if (s_logFile != stdout) {
|
||||
fclose(s_logFile);
|
||||
}
|
||||
s_logFile = NULL;
|
||||
}
|
||||
if (s_logFile != NULL) {
|
||||
if (s_logFile != stdout) {
|
||||
fclose(s_logFile);
|
||||
}
|
||||
s_logFile = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool needsLog(const LogFacility facility, const LogLevel level) {
|
||||
return ((s_logFacilites & (1 << facility)) != 0)
|
||||
&& (s_logLevel >= level);
|
||||
return ((s_logFacilites & (1 << facility)) != 0)
|
||||
&& (s_logLevel >= level);
|
||||
}
|
||||
|
||||
void logWrite(const char* facility, const char* level, const char* message, va_list ap) {
|
||||
struct timespec ts;
|
||||
struct tm td;
|
||||
clockGettime(&ts);
|
||||
localtime_r(&ts.tv_sec, &td);
|
||||
char* buf;
|
||||
if (vasprintf(&buf, message, ap) >= 0 && buf) {
|
||||
fprintf(s_logFile, "%04d-%02d-%02d %02d:%02d:%02d.%03ld [%s %s] %s\n",
|
||||
td.tm_year+1900, td.tm_mon+1, td.tm_mday,
|
||||
td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000,
|
||||
facility, level, buf);
|
||||
fflush(s_logFile);
|
||||
}
|
||||
if (buf) {
|
||||
free(buf);
|
||||
}
|
||||
struct timespec ts;
|
||||
struct tm td;
|
||||
clockGettime(&ts);
|
||||
localtime_r(&ts.tv_sec, &td);
|
||||
char* buf;
|
||||
if (vasprintf(&buf, message, ap) >= 0 && buf) {
|
||||
fprintf(s_logFile, "%04d-%02d-%02d %02d:%02d:%02d.%03ld [%s %s] %s\n",
|
||||
td.tm_year+1900, td.tm_mon+1, td.tm_mday,
|
||||
td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000,
|
||||
facility, level, buf);
|
||||
fflush(s_logFile);
|
||||
}
|
||||
if (buf) {
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facilityNames[facility], levelNames[level], message, ap);
|
||||
va_end(ap);
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facilityNames[facility], levelNames[level], message, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void logWrite(const char* facility, const LogLevel level, const char* message, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facility, levelNames[level], message, ap);
|
||||
va_end(ap);
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
logWrite(facility, levelNames[level], message, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
+12
-12
@@ -23,12 +23,12 @@
|
||||
|
||||
/** the available log facilities. */
|
||||
enum LogFacility {
|
||||
lf_main = 0, //!< main loop
|
||||
lf_network, //!< network related
|
||||
lf_bus, //!< eBUS related
|
||||
lf_update, //!< updates found while listening to the bus
|
||||
lf_other, //!< all other log facilities
|
||||
lf_COUNT = 5 //!< number of available log facilities
|
||||
lf_main = 0, //!< main loop
|
||||
lf_network, //!< network related
|
||||
lf_bus, //!< eBUS related
|
||||
lf_update, //!< updates found while listening to the bus
|
||||
lf_other, //!< all other log facilities
|
||||
lf_COUNT = 5 //!< number of available log facilities
|
||||
};
|
||||
|
||||
/** macro for enabling all log facilities. */
|
||||
@@ -36,12 +36,12 @@ enum LogFacility {
|
||||
|
||||
/** the available log levels. */
|
||||
enum LogLevel {
|
||||
ll_none = 0, //!< no level at all
|
||||
ll_error, //!< error message
|
||||
ll_notice, //!< important message
|
||||
ll_info, //!< informational message
|
||||
ll_debug, //!< debugging message (normally suppressed)
|
||||
ll_COUNT = 5 //!< number of available log levels
|
||||
ll_none = 0, //!< no level at all
|
||||
ll_error, //!< error message
|
||||
ll_notice, //!< important message
|
||||
ll_info, //!< informational message
|
||||
ll_debug, //!< debugging message (normally suppressed)
|
||||
ll_COUNT = 5 //!< number of available log levels
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+32
-32
@@ -28,45 +28,45 @@
|
||||
* class to notify other thread per pipe.
|
||||
*/
|
||||
class Notify {
|
||||
public:
|
||||
/**
|
||||
* constructs a new instance and do notifying.
|
||||
*/
|
||||
Notify() {
|
||||
int pipefd[2];
|
||||
int ret = pipe(pipefd);
|
||||
public:
|
||||
/**
|
||||
* constructs a new instance and do notifying.
|
||||
*/
|
||||
Notify() {
|
||||
int pipefd[2];
|
||||
int ret = pipe(pipefd);
|
||||
|
||||
if (ret == 0) {
|
||||
m_recvfd = pipefd[0];
|
||||
m_sendfd = pipefd[1];
|
||||
if (ret == 0) {
|
||||
m_recvfd = pipefd[0];
|
||||
m_sendfd = pipefd[1];
|
||||
|
||||
fcntl(m_sendfd, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
}
|
||||
fcntl(m_sendfd, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* destructor.
|
||||
*/
|
||||
~Notify() { close(m_sendfd); close(m_recvfd); }
|
||||
/**
|
||||
* destructor.
|
||||
*/
|
||||
~Notify() { close(m_sendfd); close(m_recvfd); }
|
||||
|
||||
/**
|
||||
* file descriptor to watch for notify event.
|
||||
* @return the notification value.
|
||||
*/
|
||||
int notifyFD() { return m_recvfd; }
|
||||
/**
|
||||
* file descriptor to watch for notify event.
|
||||
* @return the notification value.
|
||||
*/
|
||||
int notifyFD() { return m_recvfd; }
|
||||
|
||||
/**
|
||||
* write notify event to file descriptor.
|
||||
* @return result of writing notification.
|
||||
*/
|
||||
int notify() const { return write(m_sendfd, "1", 1); }
|
||||
/**
|
||||
* write notify event to file descriptor.
|
||||
* @return result of writing notification.
|
||||
*/
|
||||
int notify() const { return write(m_sendfd, "1", 1); }
|
||||
|
||||
private:
|
||||
/** file descriptor to watch */
|
||||
int m_recvfd;
|
||||
private:
|
||||
/** file descriptor to watch */
|
||||
int m_recvfd;
|
||||
|
||||
/** file descriptor to notify */
|
||||
int m_sendfd;
|
||||
/** file descriptor to notify */
|
||||
int m_sendfd;
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_NOTIFY_H_
|
||||
|
||||
+104
-104
@@ -34,122 +34,122 @@ using std::list;
|
||||
*/
|
||||
template <typename T>
|
||||
class Queue {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Queue() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Queue() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
~Queue() {
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
~Queue() {
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
/**
|
||||
* Hidden copy constructor.
|
||||
* @param src the object to copy from.
|
||||
*/
|
||||
Queue(const Queue& src);
|
||||
private:
|
||||
/**
|
||||
* Hidden copy constructor.
|
||||
* @param src the object to copy from.
|
||||
*/
|
||||
Queue(const Queue& src);
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* Add an item to the end of queue.
|
||||
* @param item the item to add.
|
||||
*/
|
||||
void push(T item) {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
m_queue.push_back(item);
|
||||
pthread_cond_broadcast(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* Add an item to the end of queue.
|
||||
* @param item the item to add.
|
||||
*/
|
||||
void push(T item) {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
m_queue.push_back(item);
|
||||
pthread_cond_broadcast(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the first item from the queue optionally waiting for the queue being non-empty.
|
||||
* @param timeout the maximum time in seconds to wait for the queue being filled, or 0 for no wait.
|
||||
* @return the item, or NULL if no item is available within the specified time.
|
||||
*/
|
||||
T pop(int timeout = 0) {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (timeout > 0) {
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += timeout;
|
||||
while (m_queue.empty()) {
|
||||
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t) == ETIMEDOUT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
m_queue.pop_front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
/**
|
||||
* Remove the first item from the queue optionally waiting for the queue being non-empty.
|
||||
* @param timeout the maximum time in seconds to wait for the queue being filled, or 0 for no wait.
|
||||
* @return the item, or NULL if no item is available within the specified time.
|
||||
*/
|
||||
T pop(int timeout = 0) {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (timeout > 0) {
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += timeout;
|
||||
while (m_queue.empty()) {
|
||||
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t) == ETIMEDOUT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
m_queue.pop_front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified item from the queue optionally waiting for it to appear in the queue.
|
||||
* @param item the item to remove and optionally wait for.
|
||||
* @param wait true to wait for the item to appear in the queue.
|
||||
* @return whether the item was removed.
|
||||
*/
|
||||
bool remove(T item, bool wait = false) {
|
||||
bool ret = false;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
do {
|
||||
size_t oldSize = m_queue.size();
|
||||
if (oldSize > 0) {
|
||||
m_queue.remove(item);
|
||||
if (m_queue.size() != oldSize) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_cond_wait(&m_cond, &m_mutex);
|
||||
} while (wait);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Remove the specified item from the queue optionally waiting for it to appear in the queue.
|
||||
* @param item the item to remove and optionally wait for.
|
||||
* @param wait true to wait for the item to appear in the queue.
|
||||
* @return whether the item was removed.
|
||||
*/
|
||||
bool remove(T item, bool wait = false) {
|
||||
bool ret = false;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
do {
|
||||
size_t oldSize = m_queue.size();
|
||||
if (oldSize > 0) {
|
||||
m_queue.remove(item);
|
||||
if (m_queue.size() != oldSize) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_cond_wait(&m_cond, &m_mutex);
|
||||
} while (wait);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first item in the queue without removing it.
|
||||
* @return the item, or NULL if no item is available.
|
||||
*/
|
||||
T peek() {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
/**
|
||||
* Return the first item in the queue without removing it.
|
||||
* @return the item, or NULL if no item is available.
|
||||
*/
|
||||
T peek() {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
/** the queue itself */
|
||||
list<T> m_queue;
|
||||
private:
|
||||
/** the queue itself */
|
||||
list<T> m_queue;
|
||||
|
||||
/** mutex variable for exclusive lock */
|
||||
pthread_mutex_t m_mutex;
|
||||
/** mutex variable for exclusive lock */
|
||||
pthread_mutex_t m_mutex;
|
||||
|
||||
/** condition variable for exclusive lock */
|
||||
pthread_cond_t m_cond;
|
||||
/** condition variable for exclusive lock */
|
||||
pthread_cond_t m_cond;
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_QUEUE_H_
|
||||
|
||||
@@ -30,59 +30,59 @@
|
||||
using std::streamsize;
|
||||
|
||||
RotateFile::~RotateFile() {
|
||||
if (m_stream) {
|
||||
fclose(m_stream);
|
||||
m_stream = NULL;
|
||||
}
|
||||
if (m_stream) {
|
||||
fclose(m_stream);
|
||||
m_stream = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool RotateFile::setEnabled(bool enabled) {
|
||||
if (enabled == m_enabled) {
|
||||
return false;
|
||||
}
|
||||
m_enabled = enabled;
|
||||
if (m_stream) {
|
||||
fclose(m_stream);
|
||||
m_stream = NULL;
|
||||
}
|
||||
if (enabled) {
|
||||
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
|
||||
m_fileSize = 0;
|
||||
}
|
||||
return true;
|
||||
if (enabled == m_enabled) {
|
||||
return false;
|
||||
}
|
||||
m_enabled = enabled;
|
||||
if (m_stream) {
|
||||
fclose(m_stream);
|
||||
m_stream = NULL;
|
||||
}
|
||||
if (enabled) {
|
||||
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
|
||||
m_fileSize = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void RotateFile::write(unsigned char* value, unsigned int size, bool received) {
|
||||
if (!m_enabled || !m_stream) {
|
||||
return;
|
||||
}
|
||||
if (m_textMode) {
|
||||
struct timespec ts;
|
||||
struct tm td;
|
||||
clockGettime(&ts);
|
||||
localtime_r(&ts.tv_sec, &td);
|
||||
fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c",
|
||||
td.tm_year+1900, td.tm_mon+1, td.tm_mday,
|
||||
td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000,
|
||||
received ? '<' : '>');
|
||||
for (unsigned int pos = 0; pos < size; pos++) {
|
||||
fprintf(m_stream, "%2.2x ", value[pos]);
|
||||
}
|
||||
fprintf(m_stream, "\n");
|
||||
m_fileSize += 25+3*size+1;
|
||||
} else {
|
||||
fwrite(value, (streamsize)size, 1, m_stream);
|
||||
m_fileSize += size;
|
||||
}
|
||||
if ((m_fileSize%1024) == 0) {
|
||||
fflush(m_stream);
|
||||
}
|
||||
if (m_fileSize >= m_maxSize * 1024LL) {
|
||||
string oldfile = string(m_fileName)+".old";
|
||||
if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) {
|
||||
fclose(m_stream);
|
||||
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
|
||||
m_fileSize = 0;
|
||||
}
|
||||
}
|
||||
if (!m_enabled || !m_stream) {
|
||||
return;
|
||||
}
|
||||
if (m_textMode) {
|
||||
struct timespec ts;
|
||||
struct tm td;
|
||||
clockGettime(&ts);
|
||||
localtime_r(&ts.tv_sec, &td);
|
||||
fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c",
|
||||
td.tm_year+1900, td.tm_mon+1, td.tm_mday,
|
||||
td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000,
|
||||
received ? '<' : '>');
|
||||
for (unsigned int pos = 0; pos < size; pos++) {
|
||||
fprintf(m_stream, "%2.2x ", value[pos]);
|
||||
}
|
||||
fprintf(m_stream, "\n");
|
||||
m_fileSize += 25+3*size+1;
|
||||
} else {
|
||||
fwrite(value, (streamsize)size, 1, m_stream);
|
||||
m_fileSize += size;
|
||||
}
|
||||
if ((m_fileSize%1024) == 0) {
|
||||
fflush(m_stream);
|
||||
}
|
||||
if (m_fileSize >= m_maxSize * 1024LL) {
|
||||
string oldfile = string(m_fileName)+".old";
|
||||
if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) {
|
||||
fclose(m_stream);
|
||||
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
|
||||
m_fileSize = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+44
-44
@@ -35,61 +35,61 @@ using std::string;
|
||||
* Helper class for writing to a rotating file with maximum size.
|
||||
*/
|
||||
class RotateFile {
|
||||
public:
|
||||
/**
|
||||
* Construct a new instance.
|
||||
* @param fileName the name of the file write to.
|
||||
* @param maxSize the maximum size of the file to write to.
|
||||
* @param textMode whether to write each byte with prefixed timestamp and direction as text.
|
||||
*/
|
||||
RotateFile(const string fileName, const unsigned int maxSize, const bool textMode = false)
|
||||
: m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {}
|
||||
public:
|
||||
/**
|
||||
* Construct a new instance.
|
||||
* @param fileName the name of the file write to.
|
||||
* @param maxSize the maximum size of the file to write to.
|
||||
* @param textMode whether to write each byte with prefixed timestamp and direction as text.
|
||||
*/
|
||||
RotateFile(const string fileName, const unsigned int maxSize, const bool textMode = false)
|
||||
: m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
virtual ~RotateFile();
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
virtual ~RotateFile();
|
||||
|
||||
/**
|
||||
* Enable or disable writing to the file.
|
||||
* @param enabled @p true to enable writing to the file, @p false to disable it.
|
||||
* @return @p true when the state was changed, @p false otherwise.
|
||||
*/
|
||||
bool setEnabled(bool enabled = true);
|
||||
/**
|
||||
* Enable or disable writing to the file.
|
||||
* @param enabled @p true to enable writing to the file, @p false to disable it.
|
||||
* @return @p true when the state was changed, @p false otherwise.
|
||||
*/
|
||||
bool setEnabled(bool enabled = true);
|
||||
|
||||
/**
|
||||
* Return whether writing to the file is enabled.
|
||||
* @return whether writing to the file is enabled.
|
||||
*/
|
||||
bool isEnabled() { return m_enabled; }
|
||||
/**
|
||||
* Return whether writing to the file is enabled.
|
||||
* @return whether writing to the file is enabled.
|
||||
*/
|
||||
bool isEnabled() { return m_enabled; }
|
||||
|
||||
/**
|
||||
* Write a number of bytes to the stream.
|
||||
* @param value the pointer to the bytes to write.
|
||||
* @param size the number of bytes to write.
|
||||
* @param received @a true on reception, @a false on sending (only relevant in text mode).
|
||||
*/
|
||||
void write(unsigned char* value, unsigned int size, bool received = true);
|
||||
/**
|
||||
* Write a number of bytes to the stream.
|
||||
* @param value the pointer to the bytes to write.
|
||||
* @param size the number of bytes to write.
|
||||
* @param received @a true on reception, @a false on sending (only relevant in text mode).
|
||||
*/
|
||||
void write(unsigned char* value, unsigned int size, bool received = true);
|
||||
|
||||
|
||||
private:
|
||||
/** whether writing to the file is enabled. */
|
||||
bool m_enabled;
|
||||
private:
|
||||
/** whether writing to the file is enabled. */
|
||||
bool m_enabled;
|
||||
|
||||
/** the name of the file write to. */
|
||||
const string m_fileName;
|
||||
/** the name of the file write to. */
|
||||
const string m_fileName;
|
||||
|
||||
/** the maximum size of @a m_file, or 0 for infinite. */
|
||||
const unsigned int m_maxSize;
|
||||
/** the maximum size of @a m_file, or 0 for infinite. */
|
||||
const unsigned int m_maxSize;
|
||||
|
||||
/** whether to write each byte with prefixed timestamp and direction as text. */
|
||||
const bool m_textMode;
|
||||
/** whether to write each byte with prefixed timestamp and direction as text. */
|
||||
const bool m_textMode;
|
||||
|
||||
/** the @a FILE to writing to. */
|
||||
FILE* m_stream;
|
||||
/** the @a FILE to writing to. */
|
||||
FILE* m_stream;
|
||||
|
||||
/** the number of bytes already written to the @a m_file. */
|
||||
uint64_t m_fileSize;
|
||||
/** the number of bytes already written to the @a m_file. */
|
||||
uint64_t m_fileSize;
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_ROTATEFILE_H_
|
||||
|
||||
+68
-68
@@ -24,98 +24,98 @@
|
||||
#include <cstdlib>
|
||||
|
||||
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd) {
|
||||
char ip[17];
|
||||
inet_ntop(AF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, (socklen_t)sizeof(ip)-1);
|
||||
m_ip = ip;
|
||||
m_port = (uint16_t)ntohs(address->sin_port);
|
||||
char ip[17];
|
||||
inet_ntop(AF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, (socklen_t)sizeof(ip)-1);
|
||||
m_ip = ip;
|
||||
m_port = (uint16_t)ntohs(address->sin_port);
|
||||
}
|
||||
|
||||
bool TCPSocket::isValid() {
|
||||
return fcntl(m_sfd, F_GETFL) != -1;
|
||||
return fcntl(m_sfd, F_GETFL) != -1;
|
||||
}
|
||||
|
||||
|
||||
TCPSocket* TCPClient::connect(const string& server, const uint16_t& port) {
|
||||
struct sockaddr_in address;
|
||||
int ret;
|
||||
struct sockaddr_in address;
|
||||
int ret;
|
||||
|
||||
memset(reinterpret_cast<char*>(&address), 0, sizeof(address));
|
||||
memset(reinterpret_cast<char*>(&address), 0, sizeof(address));
|
||||
|
||||
if (inet_addr(server.c_str()) == INADDR_NONE) {
|
||||
struct hostent* he;
|
||||
if (inet_addr(server.c_str()) == INADDR_NONE) {
|
||||
struct hostent* he;
|
||||
|
||||
he = gethostbyname(server.c_str());
|
||||
if (he == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memcpy(&address.sin_addr, he->h_addr_list[0], he->h_length);
|
||||
} else {
|
||||
ret = inet_aton(server.c_str(), &address.sin_addr);
|
||||
if (ret == 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
he = gethostbyname(server.c_str());
|
||||
if (he == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memcpy(&address.sin_addr, he->h_addr_list[0], he->h_length);
|
||||
} else {
|
||||
ret = inet_aton(server.c_str(), &address.sin_addr);
|
||||
if (ret == 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons(port);
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons(port);
|
||||
|
||||
int sfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sfd < 0) {
|
||||
return NULL;
|
||||
}
|
||||
ret = ::connect(sfd, (struct sockaddr*) &address, sizeof(address));
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return new TCPSocket(sfd, &address);
|
||||
int sfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sfd < 0) {
|
||||
return NULL;
|
||||
}
|
||||
ret = ::connect(sfd, (struct sockaddr*) &address, sizeof(address));
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return new TCPSocket(sfd, &address);
|
||||
}
|
||||
|
||||
|
||||
int TCPServer::start() {
|
||||
if (m_listening) {
|
||||
return 0;
|
||||
}
|
||||
m_lfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in address;
|
||||
if (m_listening) {
|
||||
return 0;
|
||||
}
|
||||
m_lfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in address;
|
||||
|
||||
memset(&address, 0, sizeof(address));
|
||||
memset(&address, 0, sizeof(address));
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons(m_port);
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons(m_port);
|
||||
|
||||
if (m_address.size() > 0) {
|
||||
inet_pton(AF_INET, m_address.c_str(), &(address.sin_addr));
|
||||
} else {
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
int optval = 1;
|
||||
setsockopt(m_lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
||||
if (m_address.size() > 0) {
|
||||
inet_pton(AF_INET, m_address.c_str(), &(address.sin_addr));
|
||||
} else {
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
int optval = 1;
|
||||
setsockopt(m_lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
||||
|
||||
int result = bind(m_lfd, (struct sockaddr*) &address, sizeof(address));
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
result = listen(m_lfd, 5);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
m_listening = true;
|
||||
return result;
|
||||
int result = bind(m_lfd, (struct sockaddr*) &address, sizeof(address));
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
result = listen(m_lfd, 5);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
m_listening = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
TCPSocket* TCPServer::newSocket() {
|
||||
if (!m_listening) {
|
||||
return NULL;
|
||||
}
|
||||
struct sockaddr_in address;
|
||||
socklen_t len = sizeof(address);
|
||||
if (!m_listening) {
|
||||
return NULL;
|
||||
}
|
||||
struct sockaddr_in address;
|
||||
socklen_t len = sizeof(address);
|
||||
|
||||
memset(&address, 0, sizeof(address));
|
||||
memset(&address, 0, sizeof(address));
|
||||
|
||||
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
|
||||
if (sfd < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return new TCPSocket(sfd, &address);
|
||||
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
|
||||
if (sfd < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return new TCPSocket(sfd, &address);
|
||||
}
|
||||
|
||||
|
||||
+100
-100
@@ -38,140 +38,140 @@ using std::string;
|
||||
* class for low level tcp socket operations. (open, close, send, receive).
|
||||
*/
|
||||
class TCPSocket {
|
||||
public:
|
||||
/** grant access for friend class TCPClient */
|
||||
friend class TCPClient;
|
||||
public:
|
||||
/** grant access for friend class TCPClient */
|
||||
friend class TCPClient;
|
||||
|
||||
/** grant access for friend class TCPServer */
|
||||
friend class TCPServer;
|
||||
/** grant access for friend class TCPServer */
|
||||
friend class TCPServer;
|
||||
|
||||
/**
|
||||
* destructor.
|
||||
*/
|
||||
~TCPSocket() { close(m_sfd); }
|
||||
/**
|
||||
* destructor.
|
||||
*/
|
||||
~TCPSocket() { close(m_sfd); }
|
||||
|
||||
/**
|
||||
* write bytes to opened file descriptor.
|
||||
* @param buffer data to send.
|
||||
* @param len number of bytes to send.
|
||||
* @return number of written bytes or -1 if an error has occured.
|
||||
*/
|
||||
ssize_t send(const char* buffer, size_t len) { return ::send(m_sfd, buffer, len, MSG_NOSIGNAL); }
|
||||
/**
|
||||
* write bytes to opened file descriptor.
|
||||
* @param buffer data to send.
|
||||
* @param len number of bytes to send.
|
||||
* @return number of written bytes or -1 if an error has occured.
|
||||
*/
|
||||
ssize_t send(const char* buffer, size_t len) { return ::send(m_sfd, buffer, len, MSG_NOSIGNAL); }
|
||||
|
||||
/**
|
||||
* read bytes from opened file descriptor.
|
||||
* @param buffer for received bytes.
|
||||
* @param len size of the receive buffer.
|
||||
* @return number of read bytes or -1 if an error has occured.
|
||||
*/
|
||||
ssize_t recv(char* buffer, size_t len) { return ::recv(m_sfd, buffer, len, 0); }
|
||||
/**
|
||||
* read bytes from opened file descriptor.
|
||||
* @param buffer for received bytes.
|
||||
* @param len size of the receive buffer.
|
||||
* @return number of read bytes or -1 if an error has occured.
|
||||
*/
|
||||
ssize_t recv(char* buffer, size_t len) { return ::recv(m_sfd, buffer, len, 0); }
|
||||
|
||||
/**
|
||||
* returns the tcp port.
|
||||
* @return the tcp port.
|
||||
*/
|
||||
uint16_t getPort() const { return m_port; }
|
||||
/**
|
||||
* returns the tcp port.
|
||||
* @return the tcp port.
|
||||
*/
|
||||
uint16_t getPort() const { return m_port; }
|
||||
|
||||
/**
|
||||
* returns the ip address.
|
||||
* @return the ip address.
|
||||
*/
|
||||
string getIP() const { return m_ip; }
|
||||
/**
|
||||
* returns the ip address.
|
||||
* @return the ip address.
|
||||
*/
|
||||
string getIP() const { return m_ip; }
|
||||
|
||||
/**
|
||||
* returns the file descriptor.
|
||||
* @return the file descriptor.
|
||||
*/
|
||||
int getFD() const { return m_sfd; }
|
||||
/**
|
||||
* returns the file descriptor.
|
||||
* @return the file descriptor.
|
||||
*/
|
||||
int getFD() const { return m_sfd; }
|
||||
|
||||
/**
|
||||
* returns status of file descriptor.
|
||||
* @return true if file descriptor is valid.
|
||||
*/
|
||||
bool isValid();
|
||||
/**
|
||||
* returns status of file descriptor.
|
||||
* @return true if file descriptor is valid.
|
||||
*/
|
||||
bool isValid();
|
||||
|
||||
|
||||
private:
|
||||
/** file descriptor from tcp socket */
|
||||
int m_sfd;
|
||||
private:
|
||||
/** file descriptor from tcp socket */
|
||||
int m_sfd;
|
||||
|
||||
/** port of tcp socket */
|
||||
uint16_t m_port;
|
||||
/** port of tcp socket */
|
||||
uint16_t m_port;
|
||||
|
||||
/** ip address of tcp socket */
|
||||
string m_ip;
|
||||
/** ip address of tcp socket */
|
||||
string m_ip;
|
||||
|
||||
/**
|
||||
* private constructor, limited access only for friend classes.
|
||||
* @param sfd the file desctriptor of tcp socket.
|
||||
* @param address struct which holds the ip address.
|
||||
*/
|
||||
TCPSocket(int sfd, struct sockaddr_in* address);
|
||||
/**
|
||||
* private constructor, limited access only for friend classes.
|
||||
* @param sfd the file desctriptor of tcp socket.
|
||||
* @param address struct which holds the ip address.
|
||||
*/
|
||||
TCPSocket(int sfd, struct sockaddr_in* address);
|
||||
};
|
||||
|
||||
/**
|
||||
* class to initiate a tcp socket connection to a listening server.
|
||||
*/
|
||||
class TCPClient {
|
||||
public:
|
||||
/**
|
||||
* initiate a tcp socket connection to a listening server.
|
||||
* @param server the server name or ip address to connect.
|
||||
* @param port the tcp port.
|
||||
* @return pointer to an opened tcp socket.
|
||||
*/
|
||||
TCPSocket* connect(const string& server, const uint16_t& port);
|
||||
public:
|
||||
/**
|
||||
* initiate a tcp socket connection to a listening server.
|
||||
* @param server the server name or ip address to connect.
|
||||
* @param port the tcp port.
|
||||
* @return pointer to an opened tcp socket.
|
||||
*/
|
||||
TCPSocket* connect(const string& server, const uint16_t& port);
|
||||
};
|
||||
|
||||
/**
|
||||
* class for a tcp based network server.
|
||||
*/
|
||||
class TCPServer {
|
||||
public:
|
||||
/**
|
||||
* creates a new instance of a listening tcp server.
|
||||
* @param port the tcp port.
|
||||
* @param address the ip address.
|
||||
*/
|
||||
TCPServer(const uint16_t port, const string address)
|
||||
: m_lfd(0), m_port(port), m_address(address), m_listening(false) {}
|
||||
public:
|
||||
/**
|
||||
* creates a new instance of a listening tcp server.
|
||||
* @param port the tcp port.
|
||||
* @param address the ip address.
|
||||
*/
|
||||
TCPServer(const uint16_t port, const string address)
|
||||
: m_lfd(0), m_port(port), m_address(address), m_listening(false) {}
|
||||
|
||||
/**
|
||||
* destructor.
|
||||
*/
|
||||
~TCPServer() { if (m_lfd > 0) {close(m_lfd);} }
|
||||
/**
|
||||
* destructor.
|
||||
*/
|
||||
~TCPServer() { if (m_lfd > 0) {close(m_lfd);} }
|
||||
|
||||
/**
|
||||
* start listening of tcp socket.
|
||||
* @return result of low level functions.
|
||||
*/
|
||||
int start();
|
||||
/**
|
||||
* start listening of tcp socket.
|
||||
* @return result of low level functions.
|
||||
*/
|
||||
int start();
|
||||
|
||||
/**
|
||||
* accept an incoming tcp connection and create a local tcp socket for communication.
|
||||
* @return pointer to an opened tcp socket.
|
||||
*/
|
||||
TCPSocket* newSocket();
|
||||
/**
|
||||
* accept an incoming tcp connection and create a local tcp socket for communication.
|
||||
* @return pointer to an opened tcp socket.
|
||||
*/
|
||||
TCPSocket* newSocket();
|
||||
|
||||
/**
|
||||
* returns the file descriptor.
|
||||
* @return the file descriptor.
|
||||
*/
|
||||
int getFD() const { return m_lfd; }
|
||||
/**
|
||||
* returns the file descriptor.
|
||||
* @return the file descriptor.
|
||||
*/
|
||||
int getFD() const { return m_lfd; }
|
||||
|
||||
|
||||
private:
|
||||
/** file descriptor from listening tcp socket */
|
||||
int m_lfd;
|
||||
private:
|
||||
/** file descriptor from listening tcp socket */
|
||||
int m_lfd;
|
||||
|
||||
/** listening tcp port */
|
||||
uint16_t m_port;
|
||||
/** listening tcp port */
|
||||
uint16_t m_port;
|
||||
|
||||
/** listening tcp socket ip address */
|
||||
string m_address;
|
||||
/** listening tcp socket ip address */
|
||||
string m_address;
|
||||
|
||||
/** true if object is already listening */
|
||||
bool m_listening;
|
||||
/** true if object is already listening */
|
||||
bool m_listening;
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_TCPSOCKET_H_
|
||||
|
||||
+46
-46
@@ -17,88 +17,88 @@
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "thread.h"
|
||||
#include "clock.h"
|
||||
|
||||
void* Thread::runThread(void* arg) {
|
||||
reinterpret_cast<Thread*>(arg)->enter();
|
||||
return NULL;
|
||||
reinterpret_cast<Thread*>(arg)->enter();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Thread::~Thread() {
|
||||
if (m_started) {
|
||||
pthread_cancel(m_threadid);
|
||||
pthread_detach(m_threadid);
|
||||
}
|
||||
if (m_started) {
|
||||
pthread_cancel(m_threadid);
|
||||
pthread_detach(m_threadid);
|
||||
}
|
||||
}
|
||||
|
||||
bool Thread::start(const char* name) {
|
||||
int result = pthread_create(&m_threadid, NULL, runThread, this);
|
||||
if (result == 0) {
|
||||
int result = pthread_create(&m_threadid, NULL, runThread, this);
|
||||
if (result == 0) {
|
||||
#ifdef HAVE_PTHREAD_SETNAME_NP
|
||||
#ifndef __MACH__
|
||||
pthread_setname_np(m_threadid, name);
|
||||
pthread_setname_np(m_threadid, name);
|
||||
#endif
|
||||
#endif
|
||||
m_started = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
m_started = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Thread::join() {
|
||||
int result = -1;
|
||||
if (m_started) {
|
||||
m_stopped = true;
|
||||
result = pthread_join(m_threadid, NULL);
|
||||
if (result == 0) {
|
||||
m_started = false;
|
||||
}
|
||||
}
|
||||
return result == 0;
|
||||
int result = -1;
|
||||
if (m_started) {
|
||||
m_stopped = true;
|
||||
result = pthread_join(m_threadid, NULL);
|
||||
if (result == 0) {
|
||||
m_started = false;
|
||||
}
|
||||
}
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
void Thread::enter() {
|
||||
m_running = true;
|
||||
run();
|
||||
m_running = false;
|
||||
m_running = true;
|
||||
run();
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
|
||||
WaitThread::WaitThread()
|
||||
: Thread() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
: Thread() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
}
|
||||
|
||||
WaitThread::~WaitThread() {
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
|
||||
void WaitThread::stop() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
Thread::stop();
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
Thread::stop();
|
||||
}
|
||||
|
||||
bool WaitThread::join() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return Thread::join();
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_signal(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return Thread::join();
|
||||
}
|
||||
|
||||
bool WaitThread::Wait(int seconds) {
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += seconds;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_timedwait(&m_cond, &m_mutex, &t);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return isRunning();
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += seconds;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
pthread_cond_timedwait(&m_cond, &m_mutex, &t);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return isRunning();
|
||||
}
|
||||
|
||||
+82
-82
@@ -27,79 +27,79 @@
|
||||
* wrapper class for pthread.
|
||||
*/
|
||||
class Thread {
|
||||
public:
|
||||
/**
|
||||
* constructor.
|
||||
*/
|
||||
Thread() : m_threadid(0), m_started(false), m_running(false), m_stopped(false) {}
|
||||
public:
|
||||
/**
|
||||
* constructor.
|
||||
*/
|
||||
Thread() : m_threadid(0), m_started(false), m_running(false), m_stopped(false) {}
|
||||
|
||||
/**
|
||||
* virtual destructor.
|
||||
*/
|
||||
virtual ~Thread();
|
||||
/**
|
||||
* virtual destructor.
|
||||
*/
|
||||
virtual ~Thread();
|
||||
|
||||
/**
|
||||
* Thread entry helper for pthread_create.
|
||||
* @param arg pointer to the @a Thread.
|
||||
* @return NULL.
|
||||
*/
|
||||
static void* runThread(void* arg);
|
||||
/**
|
||||
* Thread entry helper for pthread_create.
|
||||
* @param arg pointer to the @a Thread.
|
||||
* @return NULL.
|
||||
*/
|
||||
static void* runThread(void* arg);
|
||||
|
||||
/**
|
||||
* Return whether this @a Thread is still running and not yet stopped.
|
||||
* @return true if this @a Thread is till running and not yet stopped.
|
||||
*/
|
||||
virtual bool isRunning() { return m_running && !m_stopped; }
|
||||
/**
|
||||
* Return whether this @a Thread is still running and not yet stopped.
|
||||
* @return true if this @a Thread is till running and not yet stopped.
|
||||
*/
|
||||
virtual bool isRunning() { return m_running && !m_stopped; }
|
||||
|
||||
/**
|
||||
* Create the native thread and set its name.
|
||||
* @param name the thread name to show in the process list.
|
||||
* @return whether the thread was started.
|
||||
*/
|
||||
virtual bool start(const char* name);
|
||||
/**
|
||||
* Create the native thread and set its name.
|
||||
* @param name the thread name to show in the process list.
|
||||
* @return whether the thread was started.
|
||||
*/
|
||||
virtual bool start(const char* name);
|
||||
|
||||
/**
|
||||
* Notify the thread that it shall stop.
|
||||
*/
|
||||
virtual void stop() { m_stopped = true; }
|
||||
/**
|
||||
* Notify the thread that it shall stop.
|
||||
*/
|
||||
virtual void stop() { m_stopped = true; }
|
||||
|
||||
/**
|
||||
* Join the thread.
|
||||
* @return whether the thread was joined.
|
||||
*/
|
||||
virtual bool join();
|
||||
/**
|
||||
* Join the thread.
|
||||
* @return whether the thread was joined.
|
||||
*/
|
||||
virtual bool join();
|
||||
|
||||
/**
|
||||
* Get the thread id.
|
||||
* @return the thread id.
|
||||
*/
|
||||
pthread_t self() { return m_threadid; }
|
||||
/**
|
||||
* Get the thread id.
|
||||
* @return the thread id.
|
||||
*/
|
||||
pthread_t self() { return m_threadid; }
|
||||
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Thread entry method to be overridden by derived class.
|
||||
*/
|
||||
virtual void run() = 0;
|
||||
protected:
|
||||
/**
|
||||
* Thread entry method to be overridden by derived class.
|
||||
*/
|
||||
virtual void run() = 0;
|
||||
|
||||
|
||||
private:
|
||||
/**
|
||||
* Enter the Thread loop by calling run().
|
||||
*/
|
||||
void enter();
|
||||
private:
|
||||
/**
|
||||
* Enter the Thread loop by calling run().
|
||||
*/
|
||||
void enter();
|
||||
|
||||
/** own thread id */
|
||||
pthread_t m_threadid;
|
||||
/** own thread id */
|
||||
pthread_t m_threadid;
|
||||
|
||||
/** Whether the thread was started. */
|
||||
bool m_started;
|
||||
/** Whether the thread was started. */
|
||||
bool m_started;
|
||||
|
||||
/** Whether the thread is still running (i.e. in @a run() ). */
|
||||
bool m_running;
|
||||
/** Whether the thread is still running (i.e. in @a run() ). */
|
||||
bool m_running;
|
||||
|
||||
/** Whether the thread was stopped by @a stop() or @a join(). */
|
||||
bool m_stopped;
|
||||
/** Whether the thread was stopped by @a stop() or @a join(). */
|
||||
bool m_stopped;
|
||||
};
|
||||
|
||||
|
||||
@@ -107,37 +107,37 @@ class Thread {
|
||||
* A @a Thread that can be waited on.
|
||||
*/
|
||||
class WaitThread : public Thread {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
WaitThread();
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
WaitThread();
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
virtual ~WaitThread();
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
virtual ~WaitThread();
|
||||
|
||||
// @copydoc
|
||||
virtual void stop();
|
||||
// @copydoc
|
||||
virtual void stop();
|
||||
|
||||
// @copydoc
|
||||
virtual bool join();
|
||||
// @copydoc
|
||||
virtual bool join();
|
||||
|
||||
/**
|
||||
* Wait for the specified amount of time.
|
||||
* @param seconds the number of seconds to wait.
|
||||
* @return true if this @a WaitThread is still running and not yet stopped.
|
||||
*/
|
||||
bool Wait(int seconds);
|
||||
/**
|
||||
* Wait for the specified amount of time.
|
||||
* @param seconds the number of seconds to wait.
|
||||
* @return true if this @a WaitThread is still running and not yet stopped.
|
||||
*/
|
||||
bool Wait(int seconds);
|
||||
|
||||
|
||||
private:
|
||||
/** the mutex for waiting. */
|
||||
pthread_mutex_t m_mutex;
|
||||
private:
|
||||
/** the mutex for waiting. */
|
||||
pthread_mutex_t m_mutex;
|
||||
|
||||
/** the condition for waiting. */
|
||||
pthread_cond_t m_cond;
|
||||
/** the condition for waiting. */
|
||||
pthread_cond_t m_cond;
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_THREAD_H_
|
||||
|
||||
Reference in New Issue
Block a user