source: select.c @ 9c7a701

barnowl_perlaimdebianrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 9c7a701 was 9c7a701, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 13 years ago
Initial select() changes. stdin, zephyr, jabber, and irc are checked and have dispatch functions. aim is hacked in -- not entirely pretty, but documented and isolated. We still want timers.
  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include "owl.h"
2
3static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
4
5/* Returns the index of the dispatch for the file descriptor. */
6int owl_select_find_dispatch(int fd)
7{
8  int i, len;
9  owl_list *dl;
10  owl_dispatch *d;
11 
12  dl = owl_global_get_dispatchlist(&g);
13  len = owl_list_get_size(dl);
14  for(i = 0; i < len; i++) {
15    d = (owl_dispatch*)owl_list_get_element(dl, i);
16    if (d->fd == fd) return i;
17  }
18  return -1;
19}
20
21/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */
22void owl_select_add_dispatch(owl_dispatch *d)
23{
24  int elt;
25  owl_list *dl;
26
27  elt = owl_select_find_dispatch(d->fd);
28  dl = owl_global_get_dispatchlist(&g);
29 
30  if (elt != -1) {  /* If we have a dispatch for this FD */
31    owl_dispatch *d_old;
32    d_old = (owl_dispatch*)owl_list_get_element(dl, elt);
33    /* Ignore if we're adding the same dispatch again.  Otherwise
34       replace the old dispatch. */
35    if (d_old != d) {
36      owl_list_replace_element(dl, elt, d);
37      owl_free(d_old);
38    }
39  }
40  else {
41    owl_list_append_element(dl, d);
42  }
43}
44
45/* Removes an owl_dispatch to the list, based on it's file descriptor. */
46void owl_select_remove_dispatch(int fd)
47{
48  int elt;
49  owl_list *dl;
50
51  elt = owl_select_find_dispatch(fd);
52  dl = owl_global_get_dispatchlist(&g);
53 
54  if (elt != -1) {
55    owl_dispatch *d;
56    d = (owl_dispatch*)owl_list_get_element(dl, elt);
57    owl_list_remove_element(dl, elt);
58    if (d->pfunc) {
59      owl_perlconfig_dispatch_free(d);
60    }
61    owl_free(d);
62  }
63}
64
65int owl_select_dispatch_count()
66{
67  return owl_list_get_size(owl_global_get_dispatchlist(&g));
68}
69
70int owl_select_add_perl_dispatch(int fd, SV *cb)
71{
72  int elt;
73  owl_dispatch *d;
74  elt = owl_select_find_dispatch(fd);
75  if (elt != -1) {
76    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
77    if (d->pfunc == NULL) {
78      /* don't mess with non-perl dispatch functions from here. */
79      return 1;
80    }
81  }
82
83  d = malloc(sizeof(owl_dispatch));
84  d->fd = fd;
85  d->cfunc = NULL;
86  d->pfunc = cb;
87  owl_select_add_dispatch(d);
88  return 0;
89}
90
91int owl_select_remove_perl_dispatch(int fd)
92{
93  int elt;
94  owl_dispatch *d;
95 
96  elt = owl_select_find_dispatch(fd);
97  if (elt != -1) {
98    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
99    if (d->pfunc != NULL) {
100      owl_select_remove_dispatch(fd);
101      return 0;
102    }
103  }
104  return 1;
105}
106
107int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
108{
109  int i, len, max_fd;
110  owl_dispatch *d;
111  owl_list *dl;
112
113  dl = owl_global_get_dispatchlist(&g);
114  FD_ZERO(r);
115  FD_ZERO(e);
116  max_fd = 0;
117  len = owl_select_dispatch_count(g);
118  for(i = 0; i < len; i++) {
119    d = (owl_dispatch*)owl_list_get_element(dl, i);
120    FD_SET(d->fd, r);
121    FD_SET(d->fd, e);
122    if (max_fd < d->fd) max_fd = d->fd;
123  }
124  return max_fd + 1;
125}
126
127void owl_select_dispatch(fd_set *fds, int max_fd)
128{
129  int i, len;
130  owl_dispatch *d;
131  owl_list *dl;
132
133  dl = owl_global_get_dispatchlist(&g);
134  len = owl_select_dispatch_count();
135  for(i = 0; i < len; i++) {
136    d = (owl_dispatch*)owl_list_get_element(dl, i);
137    if (FD_ISSET(d->fd, fds)) {
138      if (d->cfunc != NULL) {
139        (d->cfunc)();
140      }
141      else if (d->pfunc != NULL) {
142        owl_perlconfig_do_dispatch(d);
143      }
144    }
145  }
146}
147
148int owl_select_aim_hack(fd_set *fds)
149{
150  aim_conn_t *cur;
151  aim_session_t *sess;
152  int max_fd;
153
154  FD_ZERO(fds);
155  max_fd = 0;
156  sess = owl_global_get_aimsess(&g);
157  for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) {
158    if (cur->fd != -1) {
159      FD_SET(cur->fd, fds);
160      if (cur->fd > max_fd)
161        max_fd = cur->fd;
162    }
163  }
164  cur = owl_global_get_bosconn(&g);
165  if (cur->fd != -1) {
166    FD_SET(cur->fd, fds);
167    if (cur->fd > max_fd)
168      max_fd = cur->fd;
169  }
170 
171  return max_fd;
172}
173
174void owl_select()
175{
176  int i, max_fd, aim_max_fd, aim_done;
177  fd_set r;
178  fd_set w;
179  fd_set e;
180  fd_set aim_fds;
181  struct timeval timeout;
182
183  timeout.tv_sec = 1;
184  timeout.tv_usec = 0;
185
186  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
187
188  /* AIM HACK:
189   *
190   *  The problem - I'm not sure where to hook into the owl/faim
191   *  interface to keep track of when the AIM socket(s) open and
192   *  close. In particular, the bosconn thing throws me off. So,
193   *  rather than register particular dispatchers for AIM, I look up
194   *  the relevant FDs and add them to select's watch lists, then
195   *  check for them individually before moving on to the other
196   *  dispatchers. --asedeno
197   */
198  aim_done = 1;
199  FD_ZERO(&aim_fds);
200  FD_ZERO(&w);
201  if (owl_global_is_doaimevents(&g)) {
202    aim_done = 0;
203    aim_max_fd = owl_select_aim_hack(&aim_fds);
204    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
205    for(i = 0; i <= aim_max_fd; i++) {
206      if (FD_ISSET(i, &aim_fds)) {
207        FD_SET(i, &r);
208        FD_SET(i, &w); /* Yes, we're checking writable sockets
209                          here. Without it, AIM login is really
210                          slow. */
211        FD_SET(i, &e);
212      }
213    }
214  }
215  /* END AIM HACK */
216 
217  if ( select(max_fd, &r, &w, &e, &timeout) ) {
218    /* Merge fd_sets and clear AIM FDs. */
219    for(i = 0; i <= max_fd; i++) {
220      /* Merge all interesting FDs into one set, since we have a
221         single dispatch per FD. */
222      if (FD_ISSET(i, &r) || FD_ISSET(i, &w) || FD_ISSET(i, &e)) {
223        /* AIM HACK: no separate dispatch, just process here if
224           needed, and only once per run through. */
225        if (!aim_done && FD_ISSET(i, &aim_fds)) {
226          owl_process_aim();
227          aim_done = 1;
228        }
229        else {
230          FD_SET(i, &r);
231        }
232      }
233    }
234
235    /* NOTE: the same dispatch function is called for both exceptional
236       and read ready FDs. */
237    owl_select_dispatch(&r, max_fd);
238  }
239}
Note: See TracBrowser for help on using the repository browser.