[953] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r940 - in branches/barnowl_select/owl: . perl/modules/IRC/lib/BarnOwl/Module perl/modules/IRC/lib/BarnOwl/Module/IRC perl/modules/Jabber/lib/BarnOwl/Module perl/modules/Jabber/lib/BarnOwl/Module/Jabber

daemon@ATHENA.MIT.EDU (asedeno@MIT.EDU)
Thu Oct 29 18:11:31 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@nelhage.com
To: dirty-owl-hackers@mit.edu
From: asedeno@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Fri, 15 Feb 2008 00:56:50 -0500 (EST)

Author: asedeno
Date: 2008-02-15 00:56:49 -0500 (Fri, 15 Feb 2008)
New Revision: 940

Added:
   branches/barnowl_select/owl/select.c
Modified:
   branches/barnowl_select/owl/Makefile.in
   branches/barnowl_select/owl/global.c
   branches/barnowl_select/owl/owl.c
   branches/barnowl_select/owl/owl.h
   branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
   branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
   branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm
   branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm
   branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm
   branches/barnowl_select/owl/perlconfig.c
   branches/barnowl_select/owl/perlglue.xs
   branches/barnowl_select/owl/zephyr.c
Log:
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.


Modified: branches/barnowl_select/owl/Makefile.in
===================================================================
--- branches/barnowl_select/owl/Makefile.in	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/Makefile.in	2008-02-15 05:56:49 UTC (rev 940)
@@ -25,7 +25,7 @@
      regex.c history.c view.c dict.c variable.c filterelement.c pair.c \
      keypress.c keymap.c keybinding.c cmd.c context.c zcrypt.c \
      aim.c buddy.c buddylist.c timer.c style.c stylefunc.c errqueue.c \
-     zbuddylist.c muxevents.c popexec.c obarray.c
+     zbuddylist.c muxevents.c popexec.c obarray.c select.c
 OWL_SRC = owl.c
 TESTER_SRC = tester.c
 

Modified: branches/barnowl_select/owl/global.c
===================================================================
--- branches/barnowl_select/owl/global.c	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/global.c	2008-02-15 05:56:49 UTC (rev 940)
@@ -116,6 +116,7 @@
   owl_obarray_init(&(g->obarray));
 
   owl_message_init_fmtext_cache();
+  owl_list_create(&(g->dispatchlist));
 }
 
 void _owl_global_setup_windows(owl_global *g) {
@@ -931,3 +932,8 @@
 {
   return owl_obarray_insert(&(g->obarray), string);
 }
+
+owl_list *owl_global_get_dispatchlist(owl_global *g)
+{
+  return &(g->dispatchlist);
+}

Modified: branches/barnowl_select/owl/owl.c
===================================================================
--- branches/barnowl_select/owl/owl.c	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/owl.c	2008-02-15 05:56:49 UTC (rev 940)
@@ -45,6 +45,7 @@
 #include <time.h>
 #include <sys/param.h>
 #include <sys/types.h>
+#include <sys/time.h>
 #include <termios.h>
 #include <sys/stat.h>
 #include "owl.h"
@@ -59,6 +60,8 @@
 int stderr_replace(void);
 #endif
 
+#define STDIN 0
+
 static const char fileIdent[] = "$Id$";
 
 owl_global g;
@@ -68,7 +71,7 @@
   WINDOW *recwin, *sepwin, *typwin, *msgwin;
   owl_editwin *tw;
   owl_popwin *pw;
-  int j, ret, initialsubs, debug, argcsave, followlast;
+  int ret, initialsubs, debug, argcsave, followlast;
   int newmsgs, nexttimediff;
   struct sigaction sigact;
   char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
@@ -205,11 +208,27 @@
   owl_global_set_startupargs(&g, argcsave, argvsave);
   owl_global_set_haveaim(&g);
 
+  /* prepare stdin dispatch */
+  {
+    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
+    d->fd = STDIN;
+    d->cfunc = &owl_process_input;
+    d->pfunc = NULL;
+    owl_select_add_dispatch(d);
+  }
+  
 #ifdef HAVE_LIBZEPHYR
   /* zephyr init */
   ret=owl_zephyr_initialize();
-  if (!ret)
-      owl_global_set_havezephyr(&g);
+  if (!ret) {
+    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
+    d->fd = ZGetFD();
+    d->cfunc = &owl_zephyr_process_events;
+    d->pfunc = NULL;
+    owl_select_add_dispatch(d);
+    owl_global_set_havezephyr(&g);
+  }
+
 #endif
 
 #if OWL_STDERR_REDIR
@@ -424,18 +443,6 @@
 
     followlast=owl_global_should_followlast(&g);
     
-    /* Do AIM stuff */
-    if (owl_global_is_doaimevents(&g)) {
-      owl_aim_process_events();
-
-      if (owl_global_is_aimloggedin(&g)) {
-	if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
-	  /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
-	  owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
-	}
-      }
-    }
-
     owl_perlconfig_mainloop();
 
     /* little hack */
@@ -453,8 +460,6 @@
       }
     }
 
-    owl_zephyr_process_events();
-    
     /* Grab incoming messages. */
     newmsgs=0;
     while(owl_global_messagequeue_pending(&g)) {
@@ -529,40 +534,10 @@
     /* Handle all keypresses.  If no key has been pressed, sleep for a
      * little bit, but otherwise do not.  This lets input be grabbed
      * as quickly as possbile */
-    j=wgetch(typwin);
-    if (j==ERR) {
-      usleep(10000);
-    } else {
-      owl_global_set_lastinputtime(&g, now);
-      /* find and activate the current keymap.
-       * TODO: this should really get fixed by activating
-       * keymaps as we switch between windows... 
-       */
-      if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
-	owl_context_set_popless(owl_global_get_context(&g), 
-				owl_global_get_viewwin(&g));
-	owl_function_activate_keymap("popless");
-      } else if (owl_global_is_typwin_active(&g) 
-		 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
-	/*
-	  owl_context_set_editline(owl_global_get_context(&g), tw);
-	  owl_function_activate_keymap("editline");
-	*/
-      } else if (owl_global_is_typwin_active(&g) 
-		 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
-	owl_context_set_editmulti(owl_global_get_context(&g), tw);
-	owl_function_activate_keymap("editmulti");
-      } else {
-	owl_context_set_recv(owl_global_get_context(&g));
-	owl_function_activate_keymap("recv");
-      }
-      /* now actually handle the keypress */
-      ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
-      if (ret!=0 && ret!=1) {
-	owl_function_makemsg("Unable to handle keypress");
-      }
-    }
 
+    /* select on FDs we know about. */
+    owl_select();
+
     /* Log any error signals */
     {
       siginfo_t si;
@@ -679,6 +654,62 @@
   return 1;
 }
 
+void owl_process_aim()
+{
+  if (owl_global_is_doaimevents(&g)) {
+    owl_aim_process_events();
+    
+    if (owl_global_is_aimloggedin(&g)) {
+      if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
+        /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
+        owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
+      }
+    }
+  }
+}
+
+void owl_process_input()
+{
+  int ret, j;
+  owl_popwin *pw;
+  owl_editwin *tw;
+
+  j = wgetch(owl_global_get_curs_typwin(&g));
+  if (j == ERR) return;
+
+  owl_global_set_lastinputtime(&g, time(NULL));
+  pw=owl_global_get_popwin(&g);
+  tw=owl_global_get_typwin(&g);
+
+  /* find and activate the current keymap.
+   * TODO: this should really get fixed by activating
+   * keymaps as we switch between windows... 
+   */
+  if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
+    owl_context_set_popless(owl_global_get_context(&g), 
+                            owl_global_get_viewwin(&g));
+    owl_function_activate_keymap("popless");
+  } else if (owl_global_is_typwin_active(&g) 
+             && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_ONELINE) {
+    /*
+      owl_context_set_editline(owl_global_get_context(&g), tw);
+      owl_function_activate_keymap("editline");
+    */
+  } else if (owl_global_is_typwin_active(&g) 
+             && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_MULTILINE) {
+    owl_context_set_editmulti(owl_global_get_context(&g), tw);
+    owl_function_activate_keymap("editmulti");
+  } else {
+    owl_context_set_recv(owl_global_get_context(&g));
+    owl_function_activate_keymap("recv");
+  }
+  /* now actually handle the keypress */
+  ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
+  if (ret != 0 && ret != 1) {
+    owl_function_makemsg("Unable to handle keypress");
+  }
+}
+
 void sig_handler(int sig, siginfo_t *si, void *data)
 {
   if (sig==SIGWINCH) {
@@ -693,7 +724,6 @@
   } else if (sig==SIGTERM || sig==SIGHUP) {
     owl_function_quit();
   }
-
 }
 
 void usage()

Modified: branches/barnowl_select/owl/owl.h
===================================================================
--- branches/barnowl_select/owl/owl.h	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/owl.h	2008-02-15 05:56:49 UTC (rev 940)
@@ -537,6 +537,12 @@
   owl_list strings;
 } owl_obarray;
 
+typedef struct _owl_dispatch {
+  int fd;           /* FD to watch for dispatch. */
+  void (*cfunc)();  /* C function to dispatch to. */
+  SV *pfunc;        /* Perl function to dispatch to. */
+} owl_dispatch;
+
 typedef struct _owl_global {
   owl_mainwin mw;
   owl_popwin pw;
@@ -607,6 +613,7 @@
   owl_timer zephyr_buddycheck_timer;
   struct termios startup_tio;
   owl_obarray obarray;
+  owl_list dispatchlist;
 } owl_global;
 
 /* globals */

Modified: branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
===================================================================
--- branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm	2008-02-15 05:56:49 UTC (rev 940)
@@ -68,6 +68,12 @@
     return $self;
 }
 
+sub getSocket
+{
+    my $self = shift;
+    return $self->conn->socket;
+}
+
 ################################################################################
 ############################### IRC callbacks ##################################
 ################################################################################
@@ -167,7 +173,7 @@
 sub on_disconnect {
     my $self = shift;
     delete $BarnOwl::Module::IRC::ircnets{$self->alias};
-
+    BarnOwl::remove_dispatch($self->{FD});
     BarnOwl::admin_message('IRC',
                            "[" . $self->alias . "] Disconnected from server");
 }

Modified: branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
===================================================================
--- branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	2008-02-15 05:56:49 UTC (rev 940)
@@ -68,7 +68,15 @@
     }
 }
 
-sub mainloop_hook {
+#sub mainloop_hook {
+#    return unless defined $irc;
+#    eval {
+#        $irc->do_one_loop();
+#    };
+#    return;
+#}
+
+sub OwlProcess {
     return unless defined $irc;
     eval {
         $irc->do_one_loop();
@@ -76,6 +84,7 @@
     return;
 }
 
+
 sub register_handlers {
     if(!$irc) {
         $irc = Net::IRC->new;
@@ -108,7 +117,7 @@
 
 $BarnOwl::Hooks::startup->add(\&startup);
 $BarnOwl::Hooks::shutdown->add(\&shutdown);
-$BarnOwl::Hooks::mainLoop->add(\&mainloop_hook);
+#$BarnOwl::Hooks::mainLoop->add(\&mainloop_hook);
 
 ################################################################################
 ######################## Owl command handlers ##################################
@@ -164,6 +173,9 @@
     if ($conn->conn->connected) {
         BarnOwl::admin_message("IRC", "Connected to $alias as $nick");
         $ircnets{$alias} = $conn;
+        my $fd = $conn->getSocket()->fileno();
+        BarnOwl::add_dispatch($fd, \&OwlProcess);
+        $conn->{FD} = $fd;
     } else {
         die("IRC::Connection->connect failed: $!");
     }

Modified: branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm
===================================================================
--- branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm	2008-02-15 05:56:49 UTC (rev 940)
@@ -109,6 +109,45 @@
 }
 
 
+=head2 getSID
+
+Returns the StreamID for this connection.
+
+=cut
+
+sub getStreamID {
+    my $self = shift;
+    return $self->{SESSION}->{id} || "";
+}
+
+=head2 getSocket
+
+Returns the IO::Socket for this connection.
+
+=cut
+
+sub getSocket {
+    my $self = shift;
+    my $sid = getStreamID($self);
+    return $self->{STREAM}->{SIDS}->{$sid}->{sock} || -1;
+}
+
+=head2 OwlProcess
+
+Non-blocking connection processing. For use in a select loop.
+
+=cut
+
+sub OwlProcess {
+    my $self = shift;
+    my $status = $self->Process(0);
+    if ( !defined($status) ) {
+        my $jid = $self->{SESSION}->{FULLJID};
+        BarnOwl::error("Jabber account $jid disconnected!");
+        do_logout($jid);
+    }
+}
+
 =head1 SEE ALSO
 
 L<Net::Jabber::Client>, L<BarnOwl::Module::Jabber>

Modified: branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm
===================================================================
--- branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm	2008-02-15 05:56:49 UTC (rev 940)
@@ -36,6 +36,7 @@
     my $jidStr = shift;
     return 0 unless exists $self->{$jidStr};
 
+    BarnOwl::remove_dispatch($self->{$jidStr}->{Client}->getSocket()->fileno());
     $self->{$jidStr}->{Client}->Disconnect()
       if $self->{$jidStr}->{Client};
     delete $self->{$jidStr};
@@ -50,7 +51,7 @@
     return 0 unless exists $self->{$oldJidStr};
     return 0 if $oldJidStr eq $newJidStr;
 
-    $self->{$newJidStr} = $self->{$oldJidStr}; 
+    $self->{$newJidStr} = $self->{$oldJidStr};
     delete $self->{$oldJidStr};
     return 1;
 }

Modified: branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm
===================================================================
--- branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm	2008-02-15 05:56:49 UTC (rev 940)
@@ -115,15 +115,9 @@
             $conn->removeConnection($jid);
             BarnOwl::error("Connection for $jid undefined -- error in reload?");
         }
-
-        my $status = $client->Process(0);
-        if ( !defined($status) ) {
-            BarnOwl::error("Jabber account $jid disconnected!");
-            do_logout($jid);
-        }
         if ($::shutdown) {
             do_logout($jid);
-            return;
+            next;
         }
         if ($vars{status_changed}) {
             my $p = new Net::Jabber::Presence;
@@ -386,6 +380,9 @@
 		my $fullJid = $client->{SESSION}->{FULLJID} || $jidStr;
 		$conn->renameConnection($jidStr, $fullJid);
                 queue_admin_msg("Connected to jabber as $fullJid");
+                # The remove_dispatch() method is called from the
+                # ConnectionManager's removeConnection() method.
+                BarnOwl::add_dispatch($client->getSocket()->fileno(), sub { $client->OwlProcess() });
             }
         }
     }

Modified: branches/barnowl_select/owl/perlconfig.c
===================================================================
--- branches/barnowl_select/owl/perlconfig.c	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/perlconfig.c	2008-02-15 05:56:49 UTC (rev 940)
@@ -484,6 +484,11 @@
   SvREFCNT_dec(cmd->cmd_perl);
 }
 
+void owl_perlconfig_dispatch_free(owl_dispatch *d)
+{
+  SvREFCNT_dec(d->pfunc);
+}
+
 void owl_perlconfig_edit_callback(owl_editwin *e)
 {
   SV *cb = (SV*)(e->cbdata);
@@ -527,3 +532,28 @@
   }
   return;
 }
+
+void owl_perlconfig_do_dispatch(owl_dispatch *d)
+{
+  SV *cb = d->pfunc;
+  unsigned int n_a;
+  dSP;
+  if(cb == NULL) {
+    owl_function_error("Perl callback is NULL!");
+  }
+
+  ENTER;
+  SAVETMPS;
+
+  PUSHMARK(SP);
+  PUTBACK;
+  
+  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
+
+  if(SvTRUE(ERRSV)) {
+    owl_function_error("%s", SvPV(ERRSV, n_a));
+  }
+
+  FREETMPS;
+  LEAVE;
+}

Modified: branches/barnowl_select/owl/perlglue.xs
===================================================================
--- branches/barnowl_select/owl/perlglue.xs	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/perlglue.xs	2008-02-15 05:56:49 UTC (rev 940)
@@ -341,3 +341,17 @@
 				      summ,
 				      desc,
 				      ival);
+
+void
+add_dispatch(fd, cb)
+	int fd
+	SV * cb
+	CODE:
+        SvREFCNT_inc(cb);
+	owl_select_add_perl_dispatch(fd, cb);
+
+void
+remove_dispatch(fd)
+	int fd
+	CODE:
+	owl_select_remove_perl_dispatch(fd);

Added: branches/barnowl_select/owl/select.c
===================================================================
--- branches/barnowl_select/owl/select.c	                        (rev 0)
+++ branches/barnowl_select/owl/select.c	2008-02-15 05:56:49 UTC (rev 940)
@@ -0,0 +1,239 @@
+#include "owl.h"
+
+static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
+
+/* Returns the index of the dispatch for the file descriptor. */
+int owl_select_find_dispatch(int fd)
+{
+  int i, len;
+  owl_list *dl;
+  owl_dispatch *d;
+  
+  dl = owl_global_get_dispatchlist(&g);
+  len = owl_list_get_size(dl);
+  for(i = 0; i < len; i++) {
+    d = (owl_dispatch*)owl_list_get_element(dl, i);
+    if (d->fd == fd) return i;
+  }
+  return -1;
+}
+
+/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */
+void owl_select_add_dispatch(owl_dispatch *d)
+{
+  int elt;
+  owl_list *dl;
+
+  elt = owl_select_find_dispatch(d->fd);
+  dl = owl_global_get_dispatchlist(&g);
+  
+  if (elt != -1) {  /* If we have a dispatch for this FD */
+    owl_dispatch *d_old;
+    d_old = (owl_dispatch*)owl_list_get_element(dl, elt);
+    /* Ignore if we're adding the same dispatch again.  Otherwise
+       replace the old dispatch. */
+    if (d_old != d) {
+      owl_list_replace_element(dl, elt, d);
+      owl_free(d_old);
+    }
+  }
+  else {
+    owl_list_append_element(dl, d);
+  }
+}
+
+/* Removes an owl_dispatch to the list, based on it's file descriptor. */
+void owl_select_remove_dispatch(int fd)
+{
+  int elt;
+  owl_list *dl;
+
+  elt = owl_select_find_dispatch(fd);
+  dl = owl_global_get_dispatchlist(&g);
+  
+  if (elt != -1) {
+    owl_dispatch *d;
+    d = (owl_dispatch*)owl_list_get_element(dl, elt);
+    owl_list_remove_element(dl, elt);
+    if (d->pfunc) {
+      owl_perlconfig_dispatch_free(d);
+    }
+    owl_free(d);
+  }
+}
+
+int owl_select_dispatch_count()
+{
+  return owl_list_get_size(owl_global_get_dispatchlist(&g));
+}
+
+int owl_select_add_perl_dispatch(int fd, SV *cb)
+{
+  int elt;
+  owl_dispatch *d;
+  elt = owl_select_find_dispatch(fd);
+  if (elt != -1) {
+    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
+    if (d->pfunc == NULL) {
+      /* don't mess with non-perl dispatch functions from here. */
+      return 1;
+    }
+  }
+
+  d = malloc(sizeof(owl_dispatch));
+  d->fd = fd;
+  d->cfunc = NULL;
+  d->pfunc = cb;
+  owl_select_add_dispatch(d);
+  return 0;
+}
+
+int owl_select_remove_perl_dispatch(int fd)
+{
+  int elt;
+  owl_dispatch *d;
+  
+  elt = owl_select_find_dispatch(fd);
+  if (elt != -1) {
+    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
+    if (d->pfunc != NULL) {
+      owl_select_remove_dispatch(fd);
+      return 0;
+    }
+  }
+  return 1;
+}
+
+int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
+{
+  int i, len, max_fd;
+  owl_dispatch *d;
+  owl_list *dl;
+
+  dl = owl_global_get_dispatchlist(&g);
+  FD_ZERO(r);
+  FD_ZERO(e);
+  max_fd = 0;
+  len = owl_select_dispatch_count(g);
+  for(i = 0; i < len; i++) {
+    d = (owl_dispatch*)owl_list_get_element(dl, i);
+    FD_SET(d->fd, r);
+    FD_SET(d->fd, e);
+    if (max_fd < d->fd) max_fd = d->fd;
+  }
+  return max_fd + 1;
+}
+
+void owl_select_dispatch(fd_set *fds, int max_fd)
+{
+  int i, len;
+  owl_dispatch *d;
+  owl_list *dl;
+
+  dl = owl_global_get_dispatchlist(&g);
+  len = owl_select_dispatch_count();
+  for(i = 0; i < len; i++) {
+    d = (owl_dispatch*)owl_list_get_element(dl, i);
+    if (FD_ISSET(d->fd, fds)) {
+      if (d->cfunc != NULL) {
+        (d->cfunc)();
+      }
+      else if (d->pfunc != NULL) {
+        owl_perlconfig_do_dispatch(d);
+      }
+    }
+  }
+}
+
+int owl_select_aim_hack(fd_set *fds)
+{
+  aim_conn_t *cur;
+  aim_session_t *sess;
+  int max_fd;
+
+  FD_ZERO(fds);
+  max_fd = 0;
+  sess = owl_global_get_aimsess(&g);
+  for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) {
+    if (cur->fd != -1) {
+      FD_SET(cur->fd, fds);
+      if (cur->fd > max_fd)
+        max_fd = cur->fd;
+    }
+  }
+  cur = owl_global_get_bosconn(&g);
+  if (cur->fd != -1) {
+    FD_SET(cur->fd, fds);
+    if (cur->fd > max_fd)
+      max_fd = cur->fd;
+  }
+  
+  return max_fd;
+}
+
+void owl_select()
+{
+  int i, max_fd, aim_max_fd, aim_done;
+  fd_set r;
+  fd_set w;
+  fd_set e;
+  fd_set aim_fds;
+  struct timeval timeout;
+
+  timeout.tv_sec = 1;
+  timeout.tv_usec = 0;
+
+  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
+
+  /* AIM HACK: 
+   *
+   *  The problem - I'm not sure where to hook into the owl/faim
+   *  interface to keep track of when the AIM socket(s) open and
+   *  close. In particular, the bosconn thing throws me off. So,
+   *  rather than register particular dispatchers for AIM, I look up
+   *  the relevant FDs and add them to select's watch lists, then
+   *  check for them individually before moving on to the other
+   *  dispatchers. --asedeno
+   */
+  aim_done = 1;
+  FD_ZERO(&aim_fds);
+  FD_ZERO(&w);
+  if (owl_global_is_doaimevents(&g)) {
+    aim_done = 0;
+    aim_max_fd = owl_select_aim_hack(&aim_fds);
+    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
+    for(i = 0; i <= aim_max_fd; i++) {
+      if (FD_ISSET(i, &aim_fds)) {
+        FD_SET(i, &r);
+        FD_SET(i, &w); /* Yes, we're checking writable sockets
+                          here. Without it, AIM login is really
+                          slow. */
+        FD_SET(i, &e);
+      }
+    }
+  }
+  /* END AIM HACK */
+  
+  if ( select(max_fd, &r, &w, &e, &timeout) ) {
+    /* Merge fd_sets and clear AIM FDs. */
+    for(i = 0; i <= max_fd; i++) {
+      /* Merge all interesting FDs into one set, since we have a
+         single dispatch per FD. */
+      if (FD_ISSET(i, &r) || FD_ISSET(i, &w) || FD_ISSET(i, &e)) {
+        /* AIM HACK: no separate dispatch, just process here if
+           needed, and only once per run through. */
+        if (!aim_done && FD_ISSET(i, &aim_fds)) {
+          owl_process_aim();
+          aim_done = 1;
+        }
+        else {
+          FD_SET(i, &r);
+        }
+      }
+    }
+
+    /* NOTE: the same dispatch function is called for both exceptional
+       and read ready FDs. */
+    owl_select_dispatch(&r, max_fd);
+  }
+}

Modified: branches/barnowl_select/owl/zephyr.c
===================================================================
--- branches/barnowl_select/owl/zephyr.c	2008-02-15 05:53:16 UTC (rev 939)
+++ branches/barnowl_select/owl/zephyr.c	2008-02-15 05:56:49 UTC (rev 940)
@@ -29,7 +29,6 @@
   return(0);
 }
 
-
 int owl_zephyr_shutdown()
 {
 #ifdef HAVE_LIBZEPHYR


home help back first fref pref prev next nref lref last post