Added a timeout watcher to TCP class - probably isn't working properly yet.
[ebb.git] / ruby_binding / ebb_ext.c
blob448c99feb8cc9e199de83f5dd3724492d7adb3b6
1 /* A ruby binding to the ebb web server
2 * Copyright (c) 2007 Ry Dahl <ry.d4hl@gmail.com>
3 * This software is released under the "MIT License". See README file for details.
4 */
6 #include <ruby.h>
7 #include <ebb.h>
9 static VALUE cServer;
10 static VALUE cClient;
12 /* Variables with an underscore are C-level variables */
14 VALUE client_new(ebb_client*);
16 VALUE server_alloc(VALUE self)
18 ebb_server *_server = ebb_server_new();
19 VALUE server = Qnil;
20 server = Data_Wrap_Struct(cServer, 0, ebb_server_free, _server);
21 return server;
24 void request_cb(ebb_client *_client, void *data)
26 VALUE server = (VALUE)data;
27 VALUE client = client_new(_client);
28 rb_funcall(server, rb_intern("process"), 1, client);
31 VALUE server_start(VALUE server)
33 ebb_server *_server;
34 VALUE host, port;
36 Data_Get_Struct(server, ebb_server, _server);
38 host = rb_iv_get(server, "@host");
39 Check_Type(host, T_STRING);
40 port = rb_iv_get(server, "@port");
41 Check_Type(port, T_FIXNUM);
43 ebb_server_start(_server, StringValuePtr(host), FIX2INT(port), request_cb, (void*)server);
45 return Qnil;
48 VALUE server_stop(VALUE server) {
49 ebb_server *_server;
50 Data_Get_Struct(server, ebb_server, _server);
51 ebb_server_stop(_server);
52 return Qnil;
55 VALUE client_new(ebb_client *_client)
57 return Data_Wrap_Struct(cClient, 0, ebb_client_free, _client);
60 VALUE client_write(VALUE client, VALUE string)
62 ebb_client *_client;
63 int written;
65 Data_Get_Struct(client, ebb_client, _client);
66 written = ebb_client_write(_client, RSTRING_PTR(string), RSTRING_LEN(string));
67 return INT2FIX(written);
70 VALUE client_env(VALUE client)
72 ebb_client *_client;
73 VALUE hash = rb_hash_new();
74 ebb_env_pair *pair;
76 Data_Get_Struct(client, ebb_client, _client);
77 while((pair = g_queue_pop_head(_client->env))) {
78 rb_hash_aset(hash
79 , rb_str_new(pair->field, pair->flen) // use symbol?
80 , rb_str_new(pair->value, pair->vlen)
83 return hash;
86 VALUE client_close(VALUE client)
88 ebb_client *_client;
90 Data_Get_Struct(client, ebb_client, _client);
91 ebb_client_close(_client);
92 return Qnil;
95 void Init_ebb_ext()
97 VALUE mEbb = rb_define_module("Ebb");
98 cServer = rb_define_class_under(mEbb, "Server", rb_cObject);
99 cClient = rb_define_class_under(mEbb, "Client", rb_cObject);
101 rb_define_alloc_func(cServer, server_alloc);
102 rb_define_method(cServer, "_start", server_start, 0);
103 rb_define_method(cServer, "stop", server_stop, 0);
105 rb_define_method(cClient, "write", client_write, 1);
106 rb_define_method(cClient, "env", client_env, 0);
107 rb_define_method(cClient, "close", client_close, 0);