aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2013-12-07 14:08:25 +0100
committerLoïc Hoguin <[email protected]>2013-12-07 14:08:25 +0100
commit0770e2893f88cd50020a621e4d3b813ff8e6c58f (patch)
treedb8086ef3b68ea205f69b7582334e579ff2dd907 /examples
parentb0c37e853ef0600ca5b86216e427d767b6bc6126 (diff)
downloadranch-0770e2893f88cd50020a621e4d3b813ff8e6c58f.tar.gz
ranch-0770e2893f88cd50020a621e4d3b813ff8e6c58f.tar.bz2
ranch-0770e2893f88cd50020a621e4d3b813ff8e6c58f.zip
Fix small issues and style of the examples
Diffstat (limited to 'examples')
-rw-r--r--examples/tcp_echo/README.md10
-rw-r--r--examples/tcp_echo/rebar.config4
-rw-r--r--examples/tcp_echo/src/echo_protocol.erl5
-rw-r--r--examples/tcp_echo/src/tcp_echo.app.src4
-rw-r--r--examples/tcp_echo/src/tcp_echo.erl12
-rw-r--r--examples/tcp_reverse/README.md18
-rw-r--r--examples/tcp_reverse/src/reverse_protocol.erl72
-rw-r--r--examples/tcp_reverse/src/tcp_reverse.app.src2
-rw-r--r--examples/tcp_reverse/src/tcp_reverse.erl13
-rw-r--r--examples/tcp_reverse/src/tcp_reverse_app.erl2
10 files changed, 68 insertions, 74 deletions
diff --git a/examples/tcp_echo/README.md b/examples/tcp_echo/README.md
index df4c6bd..d65fae5 100644
--- a/examples/tcp_echo/README.md
+++ b/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
```
diff --git a/examples/tcp_echo/rebar.config b/examples/tcp_echo/rebar.config
deleted file mode 100644
index 78300c9..0000000
--- a/examples/tcp_echo/rebar.config
+++ /dev/null
@@ -1,4 +0,0 @@
-{deps, [
- {ranch, ".*",
- {git, "git://github.com/extend/ranch.git", "master"}}
-]}.
diff --git a/examples/tcp_echo/src/echo_protocol.erl b/examples/tcp_echo/src/echo_protocol.erl
index 85ea289..5ed79b3 100644
--- a/examples/tcp_echo/src/echo_protocol.erl
+++ b/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]),
diff --git a/examples/tcp_echo/src/tcp_echo.app.src b/examples/tcp_echo/src/tcp_echo.app.src
index 103fd56..af50890 100644
--- a/examples/tcp_echo/src/tcp_echo.app.src
+++ b/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,
diff --git a/examples/tcp_echo/src/tcp_echo.erl b/examples/tcp_echo/src/tcp_echo.erl
deleted file mode 100644
index 46d31da..0000000
--- a/examples/tcp_echo/src/tcp_echo.erl
+++ /dev/null
@@ -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).
diff --git a/examples/tcp_reverse/README.md b/examples/tcp_reverse/README.md
index c73ae60..6a17772 100644
--- a/examples/tcp_reverse/README.md
+++ b/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
```
diff --git a/examples/tcp_reverse/src/reverse_protocol.erl b/examples/tcp_reverse/src/reverse_protocol.erl
index ea28d4e..6f7c770 100644
--- a/examples/tcp_reverse/src/reverse_protocol.erl
+++ b/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"].
diff --git a/examples/tcp_reverse/src/tcp_reverse.app.src b/examples/tcp_reverse/src/tcp_reverse.app.src
index 900b400..46cfca7 100644
--- a/examples/tcp_reverse/src/tcp_reverse.app.src
+++ b/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]},
diff --git a/examples/tcp_reverse/src/tcp_reverse.erl b/examples/tcp_reverse/src/tcp_reverse.erl
deleted file mode 100644
index e20f170..0000000
--- a/examples/tcp_reverse/src/tcp_reverse.erl
+++ /dev/null
@@ -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).
diff --git a/examples/tcp_reverse/src/tcp_reverse_app.erl b/examples/tcp_reverse/src/tcp_reverse_app.erl
index 49a5349..106e527 100644
--- a/examples/tcp_reverse/src/tcp_reverse_app.erl
+++ b/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) ->