chiark / gitweb /
more control state fiddling, starts nicely when server down now
[disorder] / disobedience / client.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2006 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21#include "disobedience.h"
22
23/* GSource subclass for disorder_eclient */
24struct eclient_source {
25 GSource gsource;
26 disorder_eclient *client;
27 time_t last_poll;
28 GPollFD pollfd;
29};
30
31/* Called before FDs are polled to choose a timeout. We ask for a 3s
32 * timeout and every 10s or so we force a dispatch. */
33static gboolean gtkclient_prepare(GSource *source,
34 gint *timeout) {
35 const struct eclient_source *esource = (struct eclient_source *)source;
36 D(("gtkclient_prepare"));
37 if(time(0) > esource->last_poll + 10)
38 return TRUE; /* timed out */
39 *timeout = 3000/*milliseconds*/;
40 return FALSE; /* please poll */
41}
42
43/* Check whether we're ready to dispatch. */
44static gboolean gtkclient_check(GSource *source) {
45 const struct eclient_source *esource = (struct eclient_source *)source;
46 D(("gtkclient_check fd=%d events=%x revents=%x",
47 esource->pollfd.fd, esource->pollfd.events, esource->pollfd.revents));
48 return esource->pollfd.fd != -1 && esource->pollfd.revents != 0;
49}
50
51/* Dispatch */
52static gboolean gtkclient_dispatch(GSource *source,
53 GSourceFunc attribute((unused)) callback,
54 gpointer attribute((unused)) user_data) {
55 struct eclient_source *esource = (struct eclient_source *)source;
56 unsigned revents, mode;
57
58 D(("gtkclient_dispatch"));
59 revents = esource->pollfd.revents & esource->pollfd.events;
60 mode = 0;
61 if(revents & (G_IO_IN|G_IO_HUP|G_IO_ERR))
62 mode |= DISORDER_POLL_READ;
63 if(revents & (G_IO_OUT|G_IO_HUP|G_IO_ERR))
64 mode |= DISORDER_POLL_WRITE;
65 time(&esource->last_poll);
66 disorder_eclient_polled(esource->client, mode);
67 return TRUE; /* ??? not documented */
68}
69
70/* Table of callbacks for GSource subclass */
71static const GSourceFuncs sourcefuncs = {
72 gtkclient_prepare,
73 gtkclient_check,
74 gtkclient_dispatch,
75 0,
76 0,
77 0,
78};
79
80/* Tell the mainloop what we need */
81static void gtkclient_poll(void *u,
82 disorder_eclient attribute((unused)) *c,
83 int fd, unsigned mode) {
84 struct eclient_source *esource = u;
85 GSource *source = u;
86
87 D(("gtkclient_poll fd=%d mode=%x", fd, mode));
88 /* deconfigure the source if currently configured */
89 if(esource->pollfd.fd != -1) {
90 D(("calling g_source_remove_poll"));
91 g_source_remove_poll(source, &esource->pollfd);
92 esource->pollfd.fd = -1;
93 esource->pollfd.events = 0;
94 }
95 /* install new settings */
96 if(fd != -1 && mode) {
97 esource->pollfd.fd = fd;
98 esource->pollfd.events = 0;
99 if(mode & DISORDER_POLL_READ)
100 esource->pollfd.events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
101 if(mode & DISORDER_POLL_WRITE)
102 esource->pollfd.events |= G_IO_OUT | G_IO_ERR;
103 /* reconfigure the source */
104 D(("calling g_source_add_poll"));
105 g_source_add_poll(source, &esource->pollfd);
106 }
107}
108
109/* Report a communication-level error. It will be automatically retried. */
110static void gtkclient_comms_error(void attribute((unused)) *u,
111 const char *msg) {
112 D(("gtkclient_comms_error %s", msg));
e836b1aa 113 menu_update(-1);
460b9539 114 gtk_label_set_text(GTK_LABEL(report_label), msg);
115}
116
117/* Report a protocol error. It will not be retried. We offer a callback to
118 * the submitter of the original command and if none is supplied we pop up an
119 * error box. */
120static void gtkclient_protocol_error(void attribute((unused)) *u,
121 void *v,
122 int code,
123 const char *msg) {
124 struct callbackdata *cbd = v;
125
126 D(("gtkclient_protocol_error %s", msg));
127 if(cbd && cbd->onerror)
128 cbd->onerror(cbd, code, msg);
129 else
130 popup_protocol_error(code, msg);
131}
132
133static void gtkclient_report(void attribute((unused)) *u,
134 const char *msg) {
135 if(!msg)
136 /* We're idle - clear the report line */
137 gtk_label_set_text(GTK_LABEL(report_label), "");
e836b1aa 138 menu_update(-1);
460b9539 139}
140
141void popup_protocol_error(int attribute((unused)) code,
142 const char *msg) {
143 gtk_label_set_text(GTK_LABEL(report_label), msg);
144 popup_error(msg);
145}
146
147/* Table of eclient callbacks */
148static const disorder_eclient_callbacks gtkclient_callbacks = {
149 gtkclient_comms_error,
150 gtkclient_protocol_error,
151 gtkclient_poll,
152 gtkclient_report
153};
154
155/* Create an eclient using the GLib main loop */
156disorder_eclient *gtkclient(void) {
157 GSource *source;
158 struct eclient_source *esource;
159
160 D(("gtkclient"));
161 source = g_source_new((GSourceFuncs *)&sourcefuncs,
162 sizeof (struct eclient_source));
163 esource = (struct eclient_source *)source;
164 esource->pollfd.fd = -1;
165 esource->client = disorder_eclient_new(&gtkclient_callbacks, source);
1c0d78bd
RK
166 if(!esource->client) {
167 g_source_destroy(source);
168 return 0;
169 }
460b9539 170 g_source_attach(source, 0);
171 return esource->client;
172}
173
174/*
175Local Variables:
176c-basic-offset:2
177comment-column:40
178fill-column:79
179indent-tabs-mode:nil
180End:
181*/