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