author | ulf69 <ulf69> | 2004-09-15 17:53:22 (UTC) |
---|---|---|
committer | ulf69 <ulf69> | 2004-09-15 17:53:22 (UTC) |
commit | d3925ba5bd25224bc4a60d3d6a107c464994a1ea (patch) (side-by-side diff) | |
tree | 60f69da1d2b79ee3081e7ef5c09a46470ca6eda0 /pwmanager/pwmanager/ipc.cpp | |
parent | ce83a3479d23b9e8a59c745ccd0a0b14f64ef4e8 (diff) | |
download | kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.zip kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.tar.gz kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.tar.bz2 |
initial revision
-rw-r--r-- | pwmanager/pwmanager/ipc.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/ipc.cpp b/pwmanager/pwmanager/ipc.cpp new file mode 100644 index 0000000..7468357 --- a/dev/null +++ b/pwmanager/pwmanager/ipc.cpp @@ -0,0 +1,152 @@ +/*************************************************************************** + * * + * copyright (C) 2004 by Michael Buesch * + * email: mbuesch@freenet.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License version 2 * + * as published by the Free Software Foundation. * + * * + ***************************************************************************/ + +/*************************************************************************** + * copyright (C) 2004 by Ulf Schenk + * This file is originaly based on version 1.0.1 of pwmanager + * and was modified to run on embedded devices that run microkde + * + * $Id$ + **************************************************************************/ + +#include "ipc.h" +#include "pwmexception.h" + +#include <qsocketnotifier.h> + +#include <sys/types.h> +#include <sys/socket.h> +#include <stdio.h> + +#define END_OF_LINE '\n' +#define INIT_LINEBUF_LEN 64 /* byte */ + + +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"); + } + rdBufSize = INIT_LINEBUF_LEN; + rdBuf = static_cast<char *>(malloc(rdBufSize)); + if (!rdBuf) { + close(sock[0]); + close(sock[1]); + throw PwMException(PwMException::EX_GENERIC, + "Ipc: OOM"); + } + stream = fdopen(sock[0], "r"); + if (!stream) { + close(sock[0]); + close(sock[1]); + free(rdBuf); + throw PwMException(PwMException::EX_GENERIC, + "Ipc: fdopen() failed"); + } +#else + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sock)) { + qDebug("Ipc: socketpair() failed"); + } + rdBufSize = INIT_LINEBUF_LEN; + rdBuf = (char *)(malloc(rdBufSize)); + if (!rdBuf) { + close(sock[0]); + close(sock[1]); + qDebug("Ipc: OOM"); + } + stream = fdopen(sock[0], "r"); + if (!stream) { + close(sock[0]); + close(sock[1]); + free(rdBuf); + qDebug("Ipc: fdopen() failed"); + } +#endif + + notifier = new QSocketNotifier(sock[0], QSocketNotifier::Read); + connect(notifier, SIGNAL(activated(int)), + this, SLOT(receiveData(int))); + host = true; +} + +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) { + throw PwMException(PwMException::EX_GENERIC, + "Ipc: OOM"); + } + sock[0] = ipc->sock[1]; + sock[1] = ipc->sock[0]; + stream = fdopen(sock[0], "r"); + if (!stream) { + free(rdBuf); + throw PwMException(PwMException::EX_GENERIC, + "Ipc: fdopen() failed"); + } +#else + rdBufSize = INIT_LINEBUF_LEN; + rdBuf = (char *)(malloc(rdBufSize)); + if (!rdBuf) { + qDebug("Ipc: OOM"); + } + sock[0] = ipc->sock[1]; + sock[1] = ipc->sock[0]; + 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; +} + +Ipc::~Ipc() +{ + delete_ifnot_null(notifier); + if (rdBuf) + free(rdBuf); + if (stream) + fclose(stream); + 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); + if (likely(rd > 0)) { + emit lineAvailable(rdBuf, rd); + } +} + +#ifndef PWM_EMBEDDED +#include "ipc.moc" +#endif |