From 0ace0f42d1a810157027c74cdb5910d9656d3f67 Mon Sep 17 00:00:00 2001 From: Daniel Mendel Date: Mon, 15 Apr 2013 12:05:49 -0400 Subject: [PATCH] Adds Webstacklib utilities and @interface macro --- Http/src/Http.jl | 24 +++++++++++------------- Webstacklib/src/Webstacklib.jl | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 Webstacklib/src/Webstacklib.jl diff --git a/Http/src/Http.jl b/Http/src/Http.jl index 7ddad7c..0b5e17d 100644 --- a/Http/src/Http.jl +++ b/Http/src/Http.jl @@ -4,7 +4,7 @@ # module Http -using Httplib +using Webstacklib, Httplib include("RequestParser.jl") export HttpHandler, Server, @@ -92,18 +92,16 @@ end # sockets` is populated. Concrete types of `WebsocketInterface` are required # to define these methods. # -abstract WebsocketInterface -# `is_websocket_handshake` should determine if `req` is a valid websocket -# upgrade request. -# -function is_websocket_handshake(handler::WebsocketInterface, req::Request) - throw("`$(typeof(handler))` does not implement `is_websocket_handshake`.") -end -# `handle` is called when `is_websocket_handshake` returns true, and takes -# full control of the connection. -# -function handle(handler::WebsocketInterface, req::Request, client::Client) - throw("`$(typeof(handler))` does not implement `handle`.") +@interface type WebsocketInterface + # `is_websocket_handshake` should determine if `req` is a valid websocket + # upgrade request. + # + is_websocket_handshake(handler::WebsocketInterface, req::Request) + + # `handle` is called when `is_websocket_handshake` returns true, and takes + # full control of the connection. + # + handle(handler::WebsocketInterface, req::Request, client::Client) end # `Server` types encapsulate an `HttpHandler` and optional diff --git a/Webstacklib/src/Webstacklib.jl b/Webstacklib/src/Webstacklib.jl new file mode 100644 index 0000000..5f2aab5 --- /dev/null +++ b/Webstacklib/src/Webstacklib.jl @@ -0,0 +1,20 @@ +module Webstacklib + export @interface + + macro interface(iexp::Expr) + iexp.head == :type || throw("@interface macro expects a type expression, got $(exp.head)") + name = iexp.args[2] + oexp = :(begin abstract $(esc(name)) end) + for exp in iexp.args[3].args + if exp.head == :call + strexp = string(exp)[3:end-1] + err = string("method ", strexp, " for interface ", name, " not implemented.") + fun = Expr(:function, esc(exp), quote + throw($err) + end) + push!( oexp.args, fun ) + end + end + oexp + end +end