Browse Source

Fix small issues and style of the examples

Loïc Hoguin 11 years ago
parent
commit
0770e2893f

+ 7 - 3
examples/tcp_echo/README.md

@@ -1,7 +1,10 @@
-Ranch TCP Echo
-==============
+Ranch TCP echo example
+======================
+
+To try this example, you need GNU `make` and `git` in your PATH.
+
+To build the example, run the following command:
 
-To build the example:
 ``` bash
 $ make
 ```
@@ -13,6 +16,7 @@ $ ./_rel/bin/tcp_echo_example console
 ```
 
 Then start a telnet session to port 5555:
+
 ``` bash
 $ telnet localhost 5555
 ```

+ 0 - 4
examples/tcp_echo/rebar.config

@@ -1,4 +0,0 @@
-{deps, [
-	{ranch, ".*",
-		{git, "git://github.com/extend/ranch.git", "master"}}
-]}.

+ 4 - 1
examples/tcp_echo/src/echo_protocol.erl

@@ -1,7 +1,10 @@
 %% Feel free to use, reuse and abuse the code in this file.
 
 -module(echo_protocol).
--export([start_link/4, init/4]).
+-behaviour(ranch_protocol).
+
+-export([start_link/4]).
+-export([init/4]).
 
 start_link(Ref, Socket, Transport, Opts) ->
 	Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),

+ 2 - 2
examples/tcp_echo/src/tcp_echo.app.src

@@ -1,10 +1,10 @@
 %% Feel free to use, reuse and abuse the code in this file.
 
 {application, tcp_echo, [
-	{description, "Ranch TCP Echo example."},
+	{description, "Ranch TCP echo example."},
 	{vsn, "1"},
 	{modules, []},
-	{registered, []},
+	{registered, [tcp_echo_sup]},
 	{applications, [
 		kernel,
 		stdlib,

+ 0 - 12
examples/tcp_echo/src/tcp_echo.erl

@@ -1,12 +0,0 @@
-%% Feel free to use, reuse and abuse the code in this file.
-
--module(tcp_echo).
-
-%% API.
--export([start/0]).
-
-%% API.
-
-start() ->
-	ok = application:start(ranch),
-	ok = application:start(tcp_echo).

+ 11 - 7
examples/tcp_reverse/README.md

@@ -1,13 +1,16 @@
-Ranch TCP Reverse
-=================
+Ranch TCP reverse example
+=========================
 
-This example uses a gen_server to handle a protocol to revese input. See
-reverse_protocol.erl for the implementation, and documentation at
-the following URL:
+This example uses a `gen_server` to handle a protocol to revese input.
+See `reverse_protocol.erl` for the implementation. Documentation about
+this topic can be found in the guide:
 
-http://ninenines.eu/docs/en/ranch/HEAD/guide/protocols/#using_gen_server
+  http://ninenines.eu/docs/en/ranch/HEAD/guide/protocols/#using_gen_server
+
+To try this example, you need GNU `make` and `git` in your PATH.
+
+To build the example, run the following command:
 
-To build the example:
 ``` bash
 $ make
 ```
@@ -19,6 +22,7 @@ $ ./_rel/bin/tcp_reverse_example console
 ```
 
 Then start a telnet session to port 5555:
+
 ``` bash
 $ telnet localhost 5555
 ```

+ 42 - 30
examples/tcp_reverse/src/reverse_protocol.erl

@@ -1,61 +1,73 @@
 %% Feel free to use, reuse and abuse the code in this file.
 
 -module(reverse_protocol).
-
 -behaviour(gen_server).
 -behaviour(ranch_protocol).
 
+%% API.
 -export([start_link/4]).
 
--export([init/1, init/4, handle_call/3, handle_cast/2, handle_info/2,
-         terminate/2, code_change/3]).
-
--export([reverse_binary/1]).
+%% gen_server.
+-export([init/1]).
+-export([init/4]).
+-export([handle_call/3]).
+-export([handle_cast/2]).
+-export([handle_info/2]).
+-export([terminate/2]).
+-export([code_change/3]).
 
 -define(TIMEOUT, 5000).
 
 -record(state, {socket, transport}).
 
-reverse_binary(B) when is_binary(B) ->
-   [list_to_binary(
-       lists:reverse(
-           binary_to_list(binary:part(B, {0, byte_size(B)-2})))),
-    "\r\n"].
+%% API.
 
 start_link(Ref, Socket, Transport, Opts) ->
-    proc_lib:start_link(?MODULE, init, [Ref, Socket, Transport, Opts]).
+	proc_lib:start_link(?MODULE, init, [Ref, Socket, Transport, Opts]).
 
-init(Args) ->
-    {ok, Args}.
+%% gen_server.
+
+%% This function is never called. We only define it so that
+%% we can use the -behaviour(gen_server) attribute.
+init([]) -> {ok, undefined}.
 
 init(Ref, Socket, Transport, _Opts = []) ->
-    ok = proc_lib:init_ack({ok, self()}),
-    ok = ranch:accept_ack(Ref),
-    ok = Transport:setopts(Socket, [{active, once}]),
-    gen_server:enter_loop(?MODULE,[], {state, Socket, Transport},?TIMEOUT).
-
-handle_info({tcp, Socket, Data}, #state{transport = Transport} = State) ->
-    inet:setopts(State#state.socket, [{active, once}]),
-    Transport:send(Socket, reverse_binary(Data)),
-    {noreply, State, ?TIMEOUT};
+	ok = proc_lib:init_ack({ok, self()}),
+	ok = ranch:accept_ack(Ref),
+	ok = Transport:setopts(Socket, [{active, once}]),
+	gen_server:enter_loop(?MODULE, [],
+		#state{socket=Socket, transport=Transport},
+		?TIMEOUT).
+
+handle_info({tcp, Socket, Data}, State=#state{
+		socket=Socket, transport=Transport}) ->
+	Transport:setopts(Socket, [{active, once}]),
+	Transport:send(Socket, reverse_binary(Data)),
+	{noreply, State, ?TIMEOUT};
 handle_info({tcp_closed, _Socket}, State) ->
-    {stop, normal, State};
+	{stop, normal, State};
 handle_info({tcp_error, _, Reason}, State) ->
-    {stop, Reason, State};
+	{stop, Reason, State};
 handle_info(timeout, State) ->
-    {stop, normal, State};
+	{stop, normal, State};
 handle_info(_Info, State) ->
-    {stop, normal, State}.
+	{stop, normal, State}.
 
 handle_call(_Request, _From, State) ->
-    {reply, ok, State}.
+	{reply, ok, State}.
 
 handle_cast(_Msg, State) ->
-    {noreply, State}.
+	{noreply, State}.
 
 terminate(_Reason, _State) ->
-    ok.
+	ok.
 
 code_change(_OldVsn, State, _Extra) ->
-    {ok, State}.
+	{ok, State}.
+
+%% Internal.
 
+reverse_binary(B) when is_binary(B) ->
+	[list_to_binary(lists:reverse(binary_to_list(
+		binary:part(B, {0, byte_size(B)-2})
+	))), "\r\n"].

+ 1 - 1
examples/tcp_reverse/src/tcp_reverse.app.src

@@ -1,7 +1,7 @@
 %% Feel free to use, reuse and abuse the code in this file.
 
 {application, tcp_reverse, [
-	{description, "Ranch TCP Reverse example."},
+	{description, "Ranch TCP reverse example."},
 	{vsn, "1"},
 	{modules, []},
 	{registered, [tcp_reverse_sup]},

+ 0 - 13
examples/tcp_reverse/src/tcp_reverse.erl

@@ -1,13 +0,0 @@
-%% Feel free to use, reuse and abuse the code in this file.
-
--module(tcp_reverse).
-
-%% API.
--export([start/0]).
-
-%% API.
-
-start() ->
-    io:format("starting ranch and tcp_reverse~n"),
-	ok = application:start(ranch),
-	ok = application:start(tcp_reverse).

+ 1 - 1
examples/tcp_reverse/src/tcp_reverse_app.erl

@@ -12,7 +12,7 @@
 
 start(_Type, _Args) ->
     {ok, _} = ranch:start_listener(tcp_reverse, 10,
-				   ranch_tcp, [{port, 5555}], reverse_protocol, []),
+		ranch_tcp, [{port, 5555}], reverse_protocol, []),
     tcp_reverse_sup:start_link().
 
 stop(_State) ->