1 | /* |
---|
2 | * neercs console-based window manager |
---|
3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
5 | * 2008-2010 Pascal Terjan <pterjan@linuxfr.org> |
---|
6 | * All Rights Reserved |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | #if defined HAVE_CONFIG_H |
---|
16 | # include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <stdio.h> /* BUFSIZ */ |
---|
20 | #include <string.h> /* strncmp() */ |
---|
21 | |
---|
22 | #include "mini-neercs.h" |
---|
23 | #include "mini-socket.h" |
---|
24 | |
---|
25 | static nrx_socket_t *insock, *outsock; |
---|
26 | |
---|
27 | void server_init(void) |
---|
28 | { |
---|
29 | insock = socket_open("/tmp/neercs.sock", 1); |
---|
30 | } |
---|
31 | |
---|
32 | int server_step(void) |
---|
33 | { |
---|
34 | char buf[BUFSIZ]; |
---|
35 | ssize_t bytes; |
---|
36 | int ret; |
---|
37 | |
---|
38 | ret = socket_select(insock, 1000); |
---|
39 | if (ret <= 0) |
---|
40 | return 1; |
---|
41 | |
---|
42 | bytes = socket_read(insock, buf, BUFSIZ); |
---|
43 | if (bytes <= 0) |
---|
44 | return 1; |
---|
45 | |
---|
46 | /* Parse message */ |
---|
47 | if (!strncmp(buf, "CONNECT ", strlen("CONNECT "))) |
---|
48 | { |
---|
49 | outsock = socket_open(buf + strlen("CONNECT "), 0); |
---|
50 | socket_puts(outsock, "OK"); |
---|
51 | } |
---|
52 | else if (!strncmp(buf, "QUIT ", strlen("QUIT "))) |
---|
53 | { |
---|
54 | return 0; |
---|
55 | } |
---|
56 | |
---|
57 | return 1; |
---|
58 | } |
---|
59 | |
---|
60 | void server_fini(void) |
---|
61 | { |
---|
62 | if (insock) |
---|
63 | socket_close(insock); |
---|
64 | if (outsock) |
---|
65 | socket_close(outsock); |
---|
66 | } |
---|
67 | |
---|