summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/ipc.cpp
blob: 74683573125ae060d791c1417c827750b80f20f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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