summaryrefslogtreecommitdiff
path: root/x11/ipc/server/ocopserver.cpp
Unidiff
Diffstat (limited to 'x11/ipc/server/ocopserver.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--x11/ipc/server/ocopserver.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/x11/ipc/server/ocopserver.cpp b/x11/ipc/server/ocopserver.cpp
index 992cb8c..3ee38e9 100644
--- a/x11/ipc/server/ocopserver.cpp
+++ b/x11/ipc/server/ocopserver.cpp
@@ -1,310 +1,310 @@
1#include <errno.h> 1#include <errno.h>
2#include <signal.h> 2#include <signal.h>
3#include <stdio.h> 3#include <stdio.h>
4#include <stdlib.h> 4#include <stdlib.h>
5#include <unistd.h> 5#include <unistd.h>
6#include <sys/socket.h> 6#include <sys/socket.h>
7#include <sys/un.h> 7#include <sys/un.h>
8 8
9#include <qcstring.h> 9#include <qcstring.h>
10#include <qtimer.h> 10#include <qtimer.h>
11 11
12#include "ocopserver.h" 12#include "ocopserver.h"
13 13
14OCopServer::OCopServer() 14OCopServer::OCopServer()
15 : QObject() 15 : QObject()
16{ 16{
17 setName( "ocopserver"); 17 setName( "ocopserver");
18 18
19 /* 19 /*
20 * init the server 20 * init the server
21 */ 21 */
22 init(); 22 init();
23 initSocket(); 23 initSocket();
24} 24}
25OCopServer::~OCopServer() { 25OCopServer::~OCopServer() {
26// socket notifiers should be deleted 26// socket notifiers should be deleted
27 close(m_serverfd ); 27 close(m_serverfd );
28} 28}
29void OCopServer::init() { 29void OCopServer::init() {
30 /* 30 /*
31 * we set SIGPIPE to SIG_IGN 31 * we set SIGPIPE to SIG_IGN
32 * to get EPIPE on reads ;) 32 * to get EPIPE on reads ;)
33 */ 33 */
34// qWarning("SIGPIPE to be ignored"); 34// qWarning("SIGPIPE to be ignored");
35 signal(SIGPIPE, SIG_IGN ); 35 signal(SIGPIPE, SIG_IGN );
36 36
37 /* 37 /*
38 * initialize some variables 38 * initialize some variables
39 */ 39 */
40 m_server = 0l; 40 m_server = 0l;
41 m_serverError = 0l; 41 m_serverError = 0l;
42} 42}
43 43
44/** 44/**
45 * here we will init our server 45 * here we will init our server
46 * socket and bind and do the listen 46 * socket and bind and do the listen
47 */ 47 */
48void OCopServer::initSocket() { 48void OCopServer::initSocket() {
49 /* get the home dir */ 49 /* get the home dir */
50 QCString home( getenv("HOME") ); 50 QCString home( getenv("HOME") );
51 QCString path( home + "/.opie.cop"); 51 QCString path( home + "/.opie.cop");
52 52
53 if ( ( m_serverfd = socket( PF_UNIX, SOCK_STREAM, 0 ) ) == -1 ) { 53 if ( ( m_serverfd = socket( PF_UNIX, SOCK_STREAM, 0 ) ) == -1 ) {
54 qWarning("failed to create server socket"); 54 qWarning("failed to create server socket");
55 /* try again later */ 55 /* try again later */
56 QTimer::singleShot( 400, this, SLOT(initSocket() ) ); 56 QTimer::singleShot( 400, this, SLOT(initSocket() ) );
57 return; 57 return;
58 } 58 }
59 qWarning( "unlinking file %s", path.data() ); 59 qWarning( "unlinking file %s", path.data() );
60 60
61 /* unlink previous sockets */ 61 /* unlink previous sockets */
62 unlink( path.data() ); 62 unlink( path.data() );
63 63
64 struct sockaddr_un m_address; 64 struct sockaddr_un m_address;
65 memset(&m_address, 0, sizeof(m_address ) ); 65 memset(&m_address, 0, sizeof(m_address ) );
66 m_address.sun_family = AF_UNIX; /* unix domain socket */ 66 m_address.sun_family = AF_UNIX; /* unix domain socket */
67 strcpy(m_address.sun_path, path.data() ); 67 strcpy(m_address.sun_path, path.data() );
68 m_adrlaenge = sizeof(m_address.sun_family) + strlen(m_address.sun_path ); 68 m_adrlaenge = sizeof(m_address.sun_family) + strlen(m_address.sun_path );
69 69
70 /* cast to make it a (sockadr*) */ 70 /* cast to make it a (sockadr*) */
71 if (bind(m_serverfd, (struct sockaddr*)&m_address, m_adrlaenge ) == -1 ) { 71 if (bind(m_serverfd, (struct sockaddr*)&m_address, m_adrlaenge ) == -1 ) {
72 qWarning("Server could not bind try again"); 72 qWarning("Server could not bind try again");
73 close(m_serverfd); 73 close(m_serverfd);
74 QTimer::singleShot(400, this, SLOT(initSocket() ) ); 74 QTimer::singleShot(400, this, SLOT(initSocket() ) );
75 return; 75 return;
76 } 76 }
77 77
78 /* tell the kernel that we're listening and accepting 78 /* tell the kernel that we're listening and accepting
79 * 5 pending connections */ 79 * 5 pending connections */
80 if (listen(m_serverfd, 5) == -1 ) { 80 if (listen(m_serverfd, 5) == -1 ) {
81 qWarning("could not listen"); 81 qWarning("could not listen");
82 close(m_serverfd ); 82 close(m_serverfd );
83 QTimer::singleShot(400, this, SLOT(initSocket() ) ); 83 QTimer::singleShot(400, this, SLOT(initSocket() ) );
84 return; 84 return;
85 } 85 }
86 86
87 /* 87 /*
88 * now we will create two QSocketNotifier 88 * now we will create two QSocketNotifier
89 * which will us notify on reads 89 * which will us notify on reads
90 * and errors 90 * and errors
91 * we do this because they integrate 91 * we do this because they integrate
92 * nicely into the QApplication eventloop 92 * nicely into the QApplication eventloop
93 */ 93 */
94 m_server = new QSocketNotifier(m_serverfd, QSocketNotifier::Read, this ); 94 m_server = new QSocketNotifier(m_serverfd, QSocketNotifier::Read, this );
95 connect( m_server, SIGNAL(activated(int) ), 95 connect( m_server, SIGNAL(activated(int) ),
96 this, SLOT(newOnServer() ) ); 96 this, SLOT(newOnServer() ) );
97 97
98 m_serverError = new QSocketNotifier( m_serverfd, QSocketNotifier::Exception, this); 98 m_serverError = new QSocketNotifier( m_serverfd, QSocketNotifier::Exception, this);
99 connect(m_serverError, SIGNAL(activated(int) ), 99 connect(m_serverError, SIGNAL(activated(int) ),
100 this, SLOT(errorOnServer() ) ); 100 this, SLOT(errorOnServer() ) );
101 101
102 qWarning("done with registering"); 102 qWarning("done with registering");
103} 103}
104/** 104/**
105 * we got the possibility to read 105 * we got the possibility to read
106 * on the server 106 * on the server
107 * this is mostly due a connect 107 * this is mostly due a connect
108 * on a client side 108 * on a client side
109 * we will accept it 109 * we will accept it
110 * add it to our list 110 * add it to our list
111 */ 111 */
112void OCopServer::newOnServer() { 112void OCopServer::newOnServer() {
113 int fd = accept(); 113 int fd = accept();
114 if ( fd < 0 ) 114 if ( fd < 0 )
115 return; 115 return;
116 116
117 /* 117 /*
118 * we got a successfull new connection 118 * we got a successful new connection
119 * be happy 119 * be happy
120 * set SocketNotifier 120 * set SocketNotifier
121 * connect it 121 * connect it
122 * and a OCOPClient 122 * and a OCOPClient
123 */ 123 */
124// qWarning("Heureka new connection %d", fd ); 124// qWarning("Heureka new connection %d", fd );
125 125
126 126
127 registerClient( fd ); 127 registerClient( fd );
128} 128}
129int OCopServer::accept() { 129int OCopServer::accept() {
130 /* 130 /*
131 * accept it 131 * accept it
132 * the socket is currently blocking IIRC 132 * the socket is currently blocking IIRC
133 */ 133 */
134 return ::accept( m_serverfd, (struct sockaddr*)&m_address, &m_adrlaenge ); 134 return ::accept( m_serverfd, (struct sockaddr*)&m_address, &m_adrlaenge );
135} 135}
136void OCopServer::newOnClient( int fd ) { 136void OCopServer::newOnClient( int fd ) {
137 errno = 0; 137 errno = 0;
138 OCOPHead head; 138 OCOPHead head;
139 memset(&head, 0, sizeof(head) ); 139 memset(&head, 0, sizeof(head) );
140 int rea = ::read(fd, &head, sizeof(head) ); 140 int rea = ::read(fd, &head, sizeof(head) );
141 //qWarning("read %d %d", rea, errno); 141 //qWarning("read %d %d", rea, errno);
142 /* 142 /*
143 * I should get EPIPE but nothing like this happens 143 * I should get EPIPE but nothing like this happens
144 * so if rea == 0 and we were signaled by the notifier 144 * so if rea == 0 and we were signaled by the notifier
145 * we close it and drop the clients... 145 * we close it and drop the clients...
146 */ 146 */
147 if ( rea <= 0 ) { 147 if ( rea <= 0 ) {
148 deregisterClient( fd ); 148 deregisterClient( fd );
149 return; 149 return;
150 } 150 }
151 /* 151 /*
152 * OCOPHead 152 * OCOPHead
153 */ 153 */
154 //qWarning("data %s %d", &bug, rea ); 154 //qWarning("data %s %d", &bug, rea );
155 155
156 /* 156 /*
157 * Check the magic 157 * Check the magic
158 * if chcked read till EOF if magic does not match 158 * if chcked read till EOF if magic does not match
159 * otherwise do read 159 * otherwise do read
160 * channel 160 * channel
161 * func 161 * func
162 * data into mem 162 * data into mem
163 * and then send the OCOPPacket 163 * and then send the OCOPPacket
164 * 164 *
165 */ 165 */
166 if (head.magic == 47 ) { 166 if (head.magic == 47 ) {
167// qWarning("magic match"); 167// qWarning("magic match");
168 QCString channel( head.chlen+1 ); 168 QCString channel( head.chlen+1 );
169 QCString func( head.funclen+1 ); 169 QCString func( head.funclen+1 );
170 QByteArray data ( head.datalen+1 ); 170 QByteArray data ( head.datalen+1 );
171 171
172 /* 172 /*
173 * we do not check for errors 173 * we do not check for errors
174 */ 174 */
175// qWarning("read "); 175// qWarning("read ");
176 int s = read(fd, channel.data(), head.chlen ); 176 int s = read(fd, channel.data(), head.chlen );
177 s = read(fd, func.data(), head.funclen ); 177 s = read(fd, func.data(), head.funclen );
178 s = read(fd, data.data(), head.datalen ); 178 s = read(fd, data.data(), head.datalen );
179// qWarning("read"); 179// qWarning("read");
180 180
181 /* debug output */ 181 /* debug output */
182// qWarning("channel %s %d", channel.data(), head.chlen ); 182// qWarning("channel %s %d", channel.data(), head.chlen );
183// qWarning("func %s %d", func.data(), head.funclen ); 183// qWarning("func %s %d", func.data(), head.funclen );
184 /* debug end */ 184 /* debug end */
185 185
186 /* 186 /*
187 * now that we got the complete body 187 * now that we got the complete body
188 * we need to make a package 188 * we need to make a package
189 * and then we need to send it to clients 189 * and then we need to send it to clients
190 * making a package is done here 190 * making a package is done here
191 * dispatching it not 191 * dispatching it not
192 */ 192 */
193 OCOPPacket packet( head.type, channel, func, data ); 193 OCOPPacket packet( head.type, channel, func, data );
194 dispatch( packet, fd ); 194 dispatch( packet, fd );
195 195
196 }else{ 196 }else{
197// qWarning("magic does not match"); 197// qWarning("magic does not match");
198// qWarning("magic %d", head.magic ); 198// qWarning("magic %d", head.magic );
199 } 199 }
200} 200}
201void OCopServer::registerClient( int fd ) { 201void OCopServer::registerClient( int fd ) {
202 if (m_clients.contains(fd) ) 202 if (m_clients.contains(fd) )
203 return; 203 return;
204 204
205 QSocketNotifier* notify = new QSocketNotifier(fd, QSocketNotifier::Read, this ); 205 QSocketNotifier* notify = new QSocketNotifier(fd, QSocketNotifier::Read, this );
206 connect(notify, SIGNAL(activated(int) ), 206 connect(notify, SIGNAL(activated(int) ),
207 this, SLOT(newOnClient(int) ) ); 207 this, SLOT(newOnClient(int) ) );
208 OCOPClient client; 208 OCOPClient client;
209 client.fd = fd; 209 client.fd = fd;
210 client.notify = notify; 210 client.notify = notify;
211 m_clients.insert( client.fd, client ); 211 m_clients.insert( client.fd, client );
212// qWarning("clients are up to %d", m_clients.count() ); 212// qWarning("clients are up to %d", m_clients.count() );
213}; 213};
214void OCopServer::deregisterClient(int fd ) { 214void OCopServer::deregisterClient(int fd ) {
215 QMap<int, OCOPClient>::Iterator it = m_clients.find( fd ); 215 QMap<int, OCOPClient>::Iterator it = m_clients.find( fd );
216 if (it != m_clients.end() ) { 216 if (it != m_clients.end() ) {
217 /* 217 /*
218 * TIME_ME 218 * TIME_ME
219 * 219 *
220 * now delete from all channels 220 * now delete from all channels
221 * go through all channels 221 * go through all channels
222 * remove the fd from the list 222 * remove the fd from the list
223 * if count becomes 0 remove the channel 223 * if count becomes 0 remove the channel
224 * otherwise replace QArray<int> 224 * otherwise replace QArray<int>
225 */ 225 */
226 QMap<QCString, QValueList<int> >::Iterator it2; 226 QMap<QCString, QValueList<int> >::Iterator it2;
227 repeatIt: 227 repeatIt:
228 for ( it2 = m_channels.begin(); it2 != m_channels.end(); ++it2 ) { 228 for ( it2 = m_channels.begin(); it2 != m_channels.end(); ++it2 ) {
229 /* 229 /*
230 * The channel contains this fd 230 * The channel contains this fd
231 */ 231 */
232// qWarning("Channel %s %d", it2.key().data(), it2.data().count() ); 232// qWarning("Channel %s %d", it2.key().data(), it2.data().count() );
233 if ( it2.data().contains( fd ) ) { 233 if ( it2.data().contains( fd ) ) {
234 qWarning("contains"); 234 qWarning("contains");
235 QValueList<int> array = it2.data(); 235 QValueList<int> array = it2.data();
236 236
237 /* 237 /*
238 * remove channel or just replace 238 * remove channel or just replace
239 */ 239 */
240 if ( array.count() == 1 || array.count() == 0) { 240 if ( array.count() == 1 || array.count() == 0) {
241// qWarning("Invalidate!"); 241// qWarning("Invalidate!");
242 /* is the list now invalidatet? */ 242 /* is the list now invalidatet? */
243 m_channels.remove( it2 ); 243 m_channels.remove( it2 );
244 /* That is the first go to of my life 244 /* That is the first go to of my life
245 * but Iterator remove( Iterator ) 245 * but Iterator remove( Iterator )
246 * does not exist 246 * does not exist
247 * it2 = --it2; 247 * it2 = --it2;
248 * does not work reliable too 248 * does not work reliable too
249 * so the only way is to reiterate :( 249 * so the only way is to reiterate :(
250 */ 250 */
251 goto repeatIt; 251 goto repeatIt;
252 }else{ 252 }else{
253// qWarning("removing count %d %d",fd, array.count() ); 253// qWarning("removing count %d %d",fd, array.count() );
254 QValueList<int>::Iterator it3 = array.find( fd ); 254 QValueList<int>::Iterator it3 = array.find( fd );
255 it3 = array.remove( it3 ); 255 it3 = array.remove( it3 );
256 QCString key = it2.key().copy(); 256 QCString key = it2.key().copy();
257 it2 = m_channels.replace( key, array ); 257 it2 = m_channels.replace( key, array );
258 } 258 }
259 } 259 }
260 } // off all channels 260 } // off all channels
261 OCOPClient client = it.data(); 261 OCOPClient client = it.data();
262 delete client.notify; 262 delete client.notify;
263 m_clients.remove(fd ); 263 m_clients.remove(fd );
264 close(fd ); 264 close(fd );
265 } 265 }
266// qWarning("clients are now at %d", m_clients.count() ); 266// qWarning("clients are now at %d", m_clients.count() );
267} 267}
268/** 268/**
269 * this function will evaluate 269 * this function will evaluate
270 * the package and then do the appropriate thins 270 * the package and then do the appropriate thins
271 */ 271 */
272void OCopServer::dispatch( const OCOPPacket& packet, int sourceFD ) { 272void OCopServer::dispatch( const OCOPPacket& packet, int sourceFD ) {
273// qWarning("packet.type() == %d", packet.type() ); 273// qWarning("packet.type() == %d", packet.type() );
274 switch( packet.type() ) { 274 switch( packet.type() ) {
275 case OCOPPacket::Register: 275 case OCOPPacket::Register:
276 registerClient(sourceFD ); 276 registerClient(sourceFD );
277 break; 277 break;
278 case OCOPPacket::Unregister: 278 case OCOPPacket::Unregister:
279 deregisterClient(sourceFD ); 279 deregisterClient(sourceFD );
280 break; 280 break;
281 case OCOPPacket::Call: 281 case OCOPPacket::Call:
282 call( packet, sourceFD ); 282 call( packet, sourceFD );
283 break; 283 break;
284 /* not implemented */ 284 /* not implemented */
285 case OCOPPacket::Method: 285 case OCOPPacket::Method:
286 break; 286 break;
287 /* nit implemented */ 287 /* nit implemented */
288 case OCOPPacket::Reply: 288 case OCOPPacket::Reply:
289 break; 289 break;
290 case OCOPPacket::RegisterChannel: 290 case OCOPPacket::RegisterChannel:
291 addChannel( packet.channel() , sourceFD ); 291 addChannel( packet.channel() , sourceFD );
292 break; 292 break;
293 case OCOPPacket::UnregisterChannel: 293 case OCOPPacket::UnregisterChannel:
294 delChannel( packet.channel(), sourceFD ); 294 delChannel( packet.channel(), sourceFD );
295 break; 295 break;
296 /* not implemented */ 296 /* not implemented */
297 case OCOPPacket::Return: 297 case OCOPPacket::Return:
298 break; 298 break;
299 /* not implemented :( */ 299 /* not implemented :( */
300 case OCOPPacket::Signal: 300 case OCOPPacket::Signal:
301 break; 301 break;
302 case OCOPPacket::IsRegistered: 302 case OCOPPacket::IsRegistered:
303// qWarning("Server:IsRegistered %s", packet.channel().data() ); 303// qWarning("Server:IsRegistered %s", packet.channel().data() );
304 isRegistered( packet.channel(), sourceFD ); 304 isRegistered( packet.channel(), sourceFD );
305 break; 305 break;
306 }; 306 };
307} 307}
308void OCopServer::errorOnServer() { 308void OCopServer::errorOnServer() {
309 /* 309 /*
310 * something is wrong on the server socket? 310 * something is wrong on the server socket?