Porting Tornado to D
The other day I started porting the Tornado web server to D. I just got the IO Loop part ported. I’m planning on using this in my web framework Rootin Tootin.
You can check out the code on Launchpad: http://launchpad.net/dornado. Next I will add proper Epoll and the HTTP part. But in the meantime, you can try it out with this demo code snippet:
private import tango.net.device.Socket; private import tango.io.model.IConduit; private import tango.net.InternetAddress; private import tango.io.Stdout; public import ioloop; class HelloServer { private ServerSocket sock; private char[1024] buffer; public void handle_connection(Socket connection, char[] address) { connection.read(buffer); connection.write("hello world!"); connection.shutdown(); connection.detach(); } public void connection_ready(ServerSocket sock, ISelectable.Handle fd, uint events) { while(true) { Socket connection = sock.accept(); connection.socket.blocking(false); handle_connection(connection, ""); } } public void call_connection_ready(ISelectable.Handle fd, uint events) { connection_ready(sock, fd, events); } public void start() { int port = 3000; int max_waiting_clients = 128; bool reuse_address = true; this.sock = new ServerSocket(new InternetAddress("0.0.0.0", port), max_waiting_clients, reuse_address); this.sock.socket.blocking(false); auto io_loop = IOLoop.instance(); auto callback = &this.call_connection_ready; io_loop.add_handler(this.sock.fileHandle, callback, io_loop.READ); Stdout("http://localhost:3000").newline.flush; io_loop.start(sock); } } public void main() { IOLoop.use_epoll = false; auto server = new HelloServer(); server.start(); }
You can compile this with:
ldc helloworld.d ../ioloop.d ../language_helper.d