-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 @@ | |||
1 | /*************************************************************************** | ||
2 | * * | ||
3 | * copyright (C) 2004 by Michael Buesch * | ||
4 | * email: mbuesch@freenet.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License version 2 * | ||
8 | * as published by the Free Software Foundation. * | ||
9 | * * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | /*************************************************************************** | ||
13 | * copyright (C) 2004 by Ulf Schenk | ||
14 | * This file is originaly based on version 1.0.1 of pwmanager | ||
15 | * and was modified to run on embedded devices that run microkde | ||
16 | * | ||
17 | * $Id$ | ||
18 | **************************************************************************/ | ||
19 | |||
20 | #include "ipc.h" | ||
21 | #include "pwmexception.h" | ||
22 | |||
23 | #include <qsocketnotifier.h> | ||
24 | |||
25 | #include <sys/types.h> | ||
26 | #include <sys/socket.h> | ||
27 | #include <stdio.h> | ||
28 | |||
29 | #define END_OF_LINE '\n' | ||
30 | #define INIT_LINEBUF_LEN64 /* byte */ | ||
31 | |||
32 | |||
33 | Ipc::Ipc() | ||
34 | : stream (0) | ||
35 | , notifier (0) | ||
36 | , rdBuf (0) | ||
37 | { | ||
38 | #ifndef PWM_EMBEDDED | ||
39 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sock)) { | ||
40 | throw PwMException(PwMException::EX_GENERIC, | ||
41 | "Ipc: socketpair() failed"); | ||
42 | } | ||
43 | rdBufSize = INIT_LINEBUF_LEN; | ||
44 | rdBuf = static_cast<char *>(malloc(rdBufSize)); | ||
45 | if (!rdBuf) { | ||
46 | close(sock[0]); | ||
47 | close(sock[1]); | ||
48 | throw PwMException(PwMException::EX_GENERIC, | ||
49 | "Ipc: OOM"); | ||
50 | } | ||
51 | stream = fdopen(sock[0], "r"); | ||
52 | if (!stream) { | ||
53 | close(sock[0]); | ||
54 | close(sock[1]); | ||
55 | free(rdBuf); | ||
56 | throw PwMException(PwMException::EX_GENERIC, | ||
57 | "Ipc: fdopen() failed"); | ||
58 | } | ||
59 | #else | ||
60 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sock)) { | ||
61 | qDebug("Ipc: socketpair() failed"); | ||
62 | } | ||
63 | rdBufSize = INIT_LINEBUF_LEN; | ||
64 | rdBuf = (char *)(malloc(rdBufSize)); | ||
65 | if (!rdBuf) { | ||
66 | close(sock[0]); | ||
67 | close(sock[1]); | ||
68 | qDebug("Ipc: OOM"); | ||
69 | } | ||
70 | stream = fdopen(sock[0], "r"); | ||
71 | if (!stream) { | ||
72 | close(sock[0]); | ||
73 | close(sock[1]); | ||
74 | free(rdBuf); | ||
75 | qDebug("Ipc: fdopen() failed"); | ||
76 | } | ||
77 | #endif | ||
78 | |||
79 | notifier = new QSocketNotifier(sock[0], QSocketNotifier::Read); | ||
80 | connect(notifier, SIGNAL(activated(int)), | ||
81 | this, SLOT(receiveData(int))); | ||
82 | host = true; | ||
83 | } | ||
84 | |||
85 | Ipc::Ipc(const Ipc *ipc) | ||
86 | : stream (0) | ||
87 | , notifier (0) | ||
88 | , rdBuf (0) | ||
89 | { | ||
90 | #ifndef PWM_EMBEDDED | ||
91 | rdBufSize = INIT_LINEBUF_LEN; | ||
92 | rdBuf = static_cast<char *>(malloc(rdBufSize)); | ||
93 | if (!rdBuf) { | ||
94 | throw PwMException(PwMException::EX_GENERIC, | ||
95 | "Ipc: OOM"); | ||
96 | } | ||
97 | sock[0] = ipc->sock[1]; | ||
98 | sock[1] = ipc->sock[0]; | ||
99 | stream = fdopen(sock[0], "r"); | ||
100 | if (!stream) { | ||
101 | free(rdBuf); | ||
102 | throw PwMException(PwMException::EX_GENERIC, | ||
103 | "Ipc: fdopen() failed"); | ||
104 | } | ||
105 | #else | ||
106 | rdBufSize = INIT_LINEBUF_LEN; | ||
107 | rdBuf = (char *)(malloc(rdBufSize)); | ||
108 | if (!rdBuf) { | ||
109 | qDebug("Ipc: OOM"); | ||
110 | } | ||
111 | sock[0] = ipc->sock[1]; | ||
112 | sock[1] = ipc->sock[0]; | ||
113 | stream = fdopen(sock[0], "r"); | ||
114 | if (!stream) { | ||
115 | free(rdBuf); | ||
116 | qDebug("Ipc: fdopen() failed"); | ||
117 | } | ||
118 | #endif | ||
119 | notifier = new QSocketNotifier(sock[0], QSocketNotifier::Read); | ||
120 | connect(notifier, SIGNAL(activated(int)), | ||
121 | this, SLOT(receiveData(int))); | ||
122 | host = false; | ||
123 | } | ||
124 | |||
125 | Ipc::~Ipc() | ||
126 | { | ||
127 | delete_ifnot_null(notifier); | ||
128 | if (rdBuf) | ||
129 | free(rdBuf); | ||
130 | if (stream) | ||
131 | fclose(stream); | ||
132 | if (host) { | ||
133 | close(sock[0]); | ||
134 | close(sock[1]); | ||
135 | } | ||
136 | } | ||
137 | |||
138 | void Ipc::receiveData(int s) | ||
139 | { | ||
140 | ssize_t rd; | ||
141 | |||
142 | PWM_ASSERT(s == sock[0]); | ||
143 | PARAM_UNUSED(s); | ||
144 | rd = getline(&rdBuf, &rdBufSize, stream); | ||
145 | if (likely(rd > 0)) { | ||
146 | emit lineAvailable(rdBuf, rd); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | #ifndef PWM_EMBEDDED | ||
151 | #include "ipc.moc" | ||
152 | #endif | ||