You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by wi...@apache.org on 2020/01/13 19:31:34 UTC

[couchdb-mochiweb] 05/08: begin restructure

This is an automated email from the ASF dual-hosted git repository.

willholley pushed a commit to tag active-passive
in repository https://gitbox.apache.org/repos/asf/couchdb-mochiweb.git

commit 26f9c01753411ef2905b272aa43a17bdf9dc64a8
Author: Richard Jones <rj...@metabrew.com>
AuthorDate: Sat Nov 20 15:45:30 2010 +0000

    begin restructure
---
 .../{websockets.erl => websockets_active.erl}      | 31 +++++-----------------
 .../{websockets.erl => websockets_passive.erl}     | 31 +++++-----------------
 src/mochiweb_http.erl                              | 17 +++++++++++-
 3 files changed, 30 insertions(+), 49 deletions(-)

diff --git a/examples/websockets/websockets.erl b/examples/websockets/websockets_active.erl
similarity index 63%
copy from examples/websockets/websockets.erl
copy to examples/websockets/websockets_active.erl
index ebd8fc6..48a0ad6 100644
--- a/examples/websockets/websockets.erl
+++ b/examples/websockets/websockets_active.erl
@@ -1,16 +1,19 @@
--module(websockets).
+-module(websockets_active).
 -author('author <rj...@metabrew.com>').
 
--export([start/0, start/1, stop/0, loop/2, wsloop_active/1]).
+-export([start/0, start/1, stop/0, loop/2, wsloop_active/1, wsloop/1]).
 
 start() -> start([{port, 8080}, {docroot, "."}]).
 
 start(Options) ->
     {DocRoot, Options1} = get_option(docroot, Options),
     Loop = fun (Req) -> ?MODULE:loop(Req, DocRoot) end,
+    % websockets options:
+    WsOpts = [ {active, true},
+               {loop,   {?MODULE, wsloop_active}} ],
     mochiweb_http:start([{name, ?MODULE}, 
-                         {loop, Loop}, 
-                         {wsloop, {?MODULE, wsloop_active}} | Options1]).
+                         {loop, Loop},
+                         {websockets_opts, WsOpts} | Options1]).
 
 stop() ->
     mochiweb_http:stop(?MODULE).
@@ -35,26 +38,6 @@ wsloop_active0(Pid) ->
     end,
     wsloop_active0(Pid).
 
-wsloop(Ws) ->
-    io:format("Websocket request, path: ~p~n", [Ws:get(path)]),
-    case Ws:get_data() of
-        closed ->  ok;
-        closing -> ok;
-        timeout -> timeout;
-        
-        % older websockets spec which is in the wild, messages are framed with
-        % 0x00...0xFF
-        {legacy_frame, Body} ->
-            Ws:send(["YOU SENT US LEGACY FRAME: ", Body]),
-            wsloop(Ws);
-    
-        % current spec, each message has a 0xFF/<64bit length> header
-        % and must contain utf8 bytestream
-        {utf8_frame, Body} ->
-            Ws:send(["YOU SENT US MODERN FRAME: ", Body]),
-            wsloop(Ws)
-    end.
-
 loop(Req, DocRoot) ->
     "/" ++ Path = Req:get(path),
     case Req:get(method) of
diff --git a/examples/websockets/websockets.erl b/examples/websockets/websockets_passive.erl
similarity index 66%
rename from examples/websockets/websockets.erl
rename to examples/websockets/websockets_passive.erl
index ebd8fc6..979ad71 100644
--- a/examples/websockets/websockets.erl
+++ b/examples/websockets/websockets_passive.erl
@@ -1,40 +1,23 @@
--module(websockets).
+-module(websockets_passive).
 -author('author <rj...@metabrew.com>').
 
--export([start/0, start/1, stop/0, loop/2, wsloop_active/1]).
+-export([start/0, start/1, stop/0, loop/2, wsloop/1]).
 
 start() -> start([{port, 8080}, {docroot, "."}]).
 
 start(Options) ->
     {DocRoot, Options1} = get_option(docroot, Options),
     Loop = fun (Req) -> ?MODULE:loop(Req, DocRoot) end,
+    % websockets options:
+    WsOpts = [ {active, false},
+               {loop,   {?MODULE, wsloop}} ],
     mochiweb_http:start([{name, ?MODULE}, 
-                         {loop, Loop}, 
-                         {wsloop, {?MODULE, wsloop_active}} | Options1]).
+                         {loop, Loop},
+                         {websockets_opts, WsOpts} | Options1]). 
 
 stop() ->
     mochiweb_http:stop(?MODULE).
 
-wsloop_active(Pid) ->
-    mochiweb_websocket_delegate:send(Pid, "WELCOME MSG!"),
-    wsloop_active0(Pid).
-
-wsloop_active0(Pid) ->
-    receive
-        closed ->
-            io:format("client api got closed~n",[]),
-            ok;
-        {error, _Reason} ->
-            ok;
-        % {legacy_frame, M} or {utf8_frame, M}
-        {_, X} ->
-            Msg = io_lib:format("SRVER_GOT: ~p", [X]),
-            mochiweb_websocket_delegate:send(Pid, Msg)
-    after 10000 ->
-            mochiweb_websocket_delegate:send(Pid, "IDLE!")
-    end,
-    wsloop_active0(Pid).
-
 wsloop(Ws) ->
     io:format("Websocket request, path: ~p~n", [Ws:get(path)]),
     case Ws:get_data() of
diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl
index 69255d4..fcec33f 100644
--- a/src/mochiweb_http.erl
+++ b/src/mochiweb_http.erl
@@ -17,8 +17,23 @@
 -define(DEFAULTS, [{name, ?MODULE},
                    {port, 8888}]).
 
+% client loop holds fun/info on how to hand off request to client code
+-record(clientloop, {http_loop, websocket_loop, websocket_active}). 
+
 parse_options(Options) ->
     HttpLoop = proplists:get_value(loop, Options),
+    case proplists:get_value(websocket_opts, Options) of
+        WsProps when is_list(WsProps) ->
+            WsLoop   = proplists:get_value(loop, WsProps),
+            WsActive = proplists:get_value(active, WsProps, false);
+        _ ->
+            WsLoop   = undefined,
+            WsActive = undefined
+    end,
+    ClientLoop = #clientloop{http_loop        = HttpLoop,
+                             websocket_loop   = WsLoop,
+                             websocket_active = WsActive},
+                
     WsLoop   = proplists:get_value(wsloop, Options),
     Loop = fun (S) ->
                    ?MODULE:loop(S, {HttpLoop, WsLoop})
@@ -136,7 +151,7 @@ headers(Socket, Request, Headers, {WwwLoop, WsLoop} = Body, HeaderCount) ->
                     io:format("notmal -> ws~n",[]),
                     {_, {abs_path,Path}, _} = Request,
                     ok = websocket_init(Socket, Path, H),
-                    Active = true,
+                    Active = false,
                     case Active of
                         true ->
                             {ok, WSPid} = mochiweb_websocket_delegate:start_link(Path, H, self()),