ry / ebb fork watch download tarball
public this repo is viewable by everyone
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
None of the Ruby library methods work as they should, so I will try 
another more hacky technique of attaching an idle watcher.
Ryan Dahl (author)
2 months ago
commit  49fe3951f6168ca7cd3f828080aa67aaffeef187
tree    0b593d43ede75b1cdf5660b637dcdeb6a10a07cb
parent  0b0e45780854a7db3a8f0c1700976ddda41b3e40
...
3
4
5
 
 
6
7
8
...
38
39
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
42
43
44
45
 
46
47
48
...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
 
 
 
76
77
78
79
80
81
82
 
 
 
83
84
85
...
267
268
269
270
271
272
273
274
 
275
276
277
...
3
4
5
6
7
8
9
10
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
...
71
72
73
 
 
 
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
76
77
78
79
80
81
82
83
 
 
84
85
86
87
88
89
...
271
272
273
 
 
 
 
274
275
276
277
278
0
@@ -3,6 +3,8 @@
0
  * License. See README file for details.
0
  */
0
 #include <ruby.h>
0
+#include <rubyio.h>
0
+#include <rubysig.h>
0
 #include <assert.h>
0
 #include <fcntl.h>
0
 #include <ebb.h>
0
@@ -38,11 +40,27 @@ struct ev_idle idle_watcher;
0
 # define RSTRING_LEN(s) (RSTRING(s)->len)
0
 #endif
0
 
0
+void attach_idle_watcher()
0
+{
0
+ if(!ev_is_active(&idle_watcher)) {
0
+ ev_idle_start (loop, &idle_watcher);
0
+ printf("attach!\n");
0
+ }
0
+}
0
+
0
+
0
+void detach_idle_watcher()
0
+{
0
+ ev_idle_stop(loop, &idle_watcher);
0
+ printf("detach!\n");
0
+}
0
+
0
 void request_cb(ebb_client *client, void *data)
0
 {
0
   VALUE waiting_clients = (VALUE)data;
0
   VALUE rb_client = Data_Wrap_Struct(cClient, 0, 0, client);
0
   rb_ary_push(waiting_clients, rb_client);
0
+ attach_idle_watcher();
0
 }
0
 
0
 VALUE server_listen_on_port(VALUE _, VALUE port)
0
@@ -53,33 +71,19 @@ VALUE server_listen_on_port(VALUE _, VALUE port)
0
 }
0
 
0
 static void
0
-oneshot_timeout (struct ev_loop *loop, struct ev_timer *w, int revents) {;}
0
-
0
-static void
0
 idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) {
0
- int i;
0
- int running_clients = FALSE;
0
- for(i = 0; i < EBB_MAX_CLIENTS; i++)
0
- if(server->clients[i].open) {
0
- running_clients = TRUE;
0
- break;
0
- }
0
- if(!running_clients) {
0
- fd_set fds;
0
- struct timeval tv = { tv_sec: 1, tv_usec: 0 };
0
- FD_ZERO(&fds);
0
- FD_SET(server->fd, &fds);
0
- /* sit in ruby thread select for a second when there are no connections */
0
- // printf("enter select()\n");
0
- rb_thread_select(server->fd+1, &fds, &fds, &fds, &tv);
0
- // printf("leave select()\n");
0
+ if(rb_thread_alone()) {
0
+ detach_idle_watcher();
0
+ } else {
0
+ rb_thread_schedule();
0
   }
0
 }
0
 
0
 VALUE server_process_connections(VALUE _)
0
 {
0
- ev_loop(loop, EVLOOP_NONBLOCK);
0
-
0
+ TRAP_BEG;
0
+ ev_loop(loop, EVLOOP_ONESHOT);
0
+ TRAP_END;
0
   return Qnil;
0
 }
0
 
0
@@ -267,11 +271,8 @@ void Init_ebb_ext()
0
   
0
   /* initialize ebb_server */
0
   loop = ev_default_loop (0);
0
- ev_idle_init (&idle_watcher, idle_cb);
0
- ev_idle_start (loop, &idle_watcher);
0
-
0
-
0
   
0
+ ev_idle_init (&idle_watcher, idle_cb);
0
   server = ebb_server_alloc();
0
   VALUE waiting_clients = rb_ary_new();
0
   rb_iv_set(mFFI, "@waiting_clients", waiting_clients);

Comments

    No one has commented yet.