-rw-r--r-- | pwmanager/pwmanager/ipc.cpp | 80 |
1 files changed, 72 insertions, 8 deletions
diff --git a/pwmanager/pwmanager/ipc.cpp b/pwmanager/pwmanager/ipc.cpp index 7468357..b1d2c68 100644 --- a/pwmanager/pwmanager/ipc.cpp +++ b/pwmanager/pwmanager/ipc.cpp @@ -22,20 +22,25 @@ #include <qsocketnotifier.h> -#include <sys/types.h> #include <sys/socket.h> + +#ifndef PWM_EMBEDDED +#include <sys/types.h> #include <stdio.h> +#else +#include <qsocket.h> +#endif #define END_OF_LINE '\n' #define INIT_LINEBUF_LEN 64 /* byte */ +#ifndef PWM_EMBEDDED Ipc::Ipc() : stream (0) , notifier (0) , rdBuf (0) { -#ifndef PWM_EMBEDDED if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sock)) { throw PwMException(PwMException::EX_GENERIC, "Ipc: socketpair() failed"); @@ -56,10 +61,24 @@ Ipc::Ipc() throw PwMException(PwMException::EX_GENERIC, "Ipc: fdopen() failed"); } + + notifier = new QSocketNotifier(sock[0], QSocketNotifier::Read); + connect(notifier, SIGNAL(activated(int)), + this, SLOT(receiveData(int))); + host = true; +} #else +Ipc::Ipc() + : notifier (0) + , rdBuf (0) +{ if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sock)) { qDebug("Ipc: socketpair() failed"); } + + QSocket* qsock = new QSocket(); + qsock->setSocket(sock[0]); + rdBufSize = INIT_LINEBUF_LEN; rdBuf = (char *)(malloc(rdBufSize)); if (!rdBuf) { @@ -67,6 +86,11 @@ Ipc::Ipc() close(sock[1]); qDebug("Ipc: OOM"); } + + qsock = new QSocket(); + qsock->setSocket(sock[0]); + + /*US stream = fdopen(sock[0], "r"); if (!stream) { close(sock[0]); @@ -74,7 +98,7 @@ Ipc::Ipc() free(rdBuf); qDebug("Ipc: fdopen() failed"); } -#endif + */ notifier = new QSocketNotifier(sock[0], QSocketNotifier::Read); connect(notifier, SIGNAL(activated(int)), @@ -82,12 +106,16 @@ Ipc::Ipc() host = true; } +#endif + + +#ifndef PWM_EMBEDDED + Ipc::Ipc(const Ipc *ipc) : stream (0) , notifier (0) , rdBuf (0) { -#ifndef PWM_EMBEDDED rdBufSize = INIT_LINEBUF_LEN; rdBuf = static_cast<char *>(malloc(rdBufSize)); if (!rdBuf) { @@ -102,7 +130,19 @@ Ipc::Ipc(const Ipc *ipc) throw PwMException(PwMException::EX_GENERIC, "Ipc: fdopen() failed"); } + + notifier = new QSocketNotifier(sock[0], QSocketNotifier::Read); + connect(notifier, SIGNAL(activated(int)), + this, SLOT(receiveData(int))); + host = false; +} + #else + +Ipc::Ipc(const Ipc *ipc) + : notifier (0) + , rdBuf (0) +{ rdBufSize = INIT_LINEBUF_LEN; rdBuf = (char *)(malloc(rdBufSize)); if (!rdBuf) { @@ -110,41 +150,65 @@ Ipc::Ipc(const Ipc *ipc) } sock[0] = ipc->sock[1]; sock[1] = ipc->sock[0]; + + qSock = new QSocket(); + qSock->setSocket(sock[0]); + + /*US stream = fdopen(sock[0], "r"); if (!stream) { free(rdBuf); qDebug("Ipc: fdopen() failed"); } -#endif + */ + notifier = new QSocketNotifier(sock[0], QSocketNotifier::Read); connect(notifier, SIGNAL(activated(int)), this, SLOT(receiveData(int))); host = false; } +#endif + Ipc::~Ipc() { +#ifdef PWM_EMBEDDED + delete qSock; +#endif delete_ifnot_null(notifier); if (rdBuf) free(rdBuf); +#ifndef PWM_EMBEDDED if (stream) fclose(stream); +#endif if (host) { close(sock[0]); close(sock[1]); } + } void Ipc::receiveData(int s) { - ssize_t rd; - PWM_ASSERT(s == sock[0]); PARAM_UNUSED(s); - rd = getline(&rdBuf, &rdBufSize, stream); +#ifndef PWM_EMBEDDED + ssize_t rd; + + rd = ::getline(&rdBuf, &rdBufSize, stream); if (likely(rd > 0)) { emit lineAvailable(rdBuf, rd); } +#else + int rd; + rd = qSock->readLine(rdBuf, rdBufSize); + if (rd > 0) { + emit lineAvailable(rdBuf, rd); + } +#endif + qDebug("void Ipc::receiveData(int s) has to be implemented."); + } #ifndef PWM_EMBEDDED |