/*************************************************************************** * * * 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 #include #include #include #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(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(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