chiark / gitweb /
tidying and docs
[disorder] / disobedience / client.c
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 */
24 struct 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.  */
33 static 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. */
44 static 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 */
52 static 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 */
71 static 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 */
81 static 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. */
110 static void gtkclient_comms_error(void attribute((unused)) *u,
111                                   const char *msg) {
112   D(("gtkclient_comms_error %s", msg));
113   gtk_label_set_text(GTK_LABEL(report_label), msg);
114 }
115
116 /* Report a protocol error.  It will not be retried.  We offer a callback to
117  * the submitter of the original command and if none is supplied we pop up an
118  * error box. */
119 static void gtkclient_protocol_error(void attribute((unused)) *u,
120                                      void *v,
121                                      int code,
122                                      const char *msg) {
123   struct callbackdata *cbd = v;
124
125   D(("gtkclient_protocol_error %s", msg));
126   if(cbd && cbd->onerror)
127     cbd->onerror(cbd, code, msg);
128   else
129     popup_protocol_error(code, msg);
130 }
131
132 static void gtkclient_report(void attribute((unused)) *u,
133                              const char *msg) {
134   if(!msg)
135     /* We're idle - clear the report line */
136     gtk_label_set_text(GTK_LABEL(report_label), "");
137 }
138
139 void popup_protocol_error(int attribute((unused)) code,
140                           const char *msg) {
141   gtk_label_set_text(GTK_LABEL(report_label), msg);
142   popup_error(msg);
143 }
144
145 /* Table of eclient callbacks */
146 static const disorder_eclient_callbacks gtkclient_callbacks = {
147   gtkclient_comms_error,
148   gtkclient_protocol_error,
149   gtkclient_poll,
150   gtkclient_report
151 };
152
153 /* Create an eclient using the GLib main loop */
154 disorder_eclient *gtkclient(void) {
155   GSource *source;
156   struct eclient_source *esource;
157
158   D(("gtkclient"));
159   source = g_source_new((GSourceFuncs *)&sourcefuncs,
160                         sizeof (struct eclient_source));
161   esource = (struct eclient_source *)source;
162   esource->pollfd.fd = -1;
163   esource->client = disorder_eclient_new(&gtkclient_callbacks, source);
164   if(!esource->client) {
165     g_source_destroy(source);
166     return 0;
167   }
168   g_source_attach(source, 0);
169   return esource->client;
170 }
171
172 /*
173 Local Variables:
174 c-basic-offset:2
175 comment-column:40
176 fill-column:79
177 indent-tabs-mode:nil
178 End:
179 */