aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2013-12-07 13:53:00 +0100
committerLoïc Hoguin <[email protected]>2013-12-07 13:53:00 +0100
commitb0c37e853ef0600ca5b86216e427d767b6bc6126 (patch)
treef25ccf1af5d97df4665aa7bc3477592f7f59e3f2
parent7194df4568e66c1f2fee86816ace3308ec9eb302 (diff)
parent4e5e04f66869d2a34b8d32232e1bbb0e813befba (diff)
downloadranch-b0c37e853ef0600ca5b86216e427d767b6bc6126.tar.gz
ranch-b0c37e853ef0600ca5b86216e427d767b6bc6126.tar.bz2
ranch-b0c37e853ef0600ca5b86216e427d767b6bc6126.zip
Merge branch 'tcp_echo_example_updates' of git://github.com/joshrotenberg/ranch
-rw-r--r--examples/tcp_echo/Makefile6
-rw-r--r--examples/tcp_echo/README.md23
-rw-r--r--examples/tcp_echo/relx.config2
-rwxr-xr-xexamples/tcp_echo/start.sh3
-rw-r--r--examples/tcp_reverse/Makefile6
-rw-r--r--examples/tcp_reverse/README.md29
-rw-r--r--examples/tcp_reverse/relx.config2
-rw-r--r--examples/tcp_reverse/src/reverse_protocol.erl61
-rw-r--r--examples/tcp_reverse/src/tcp_reverse.app.src15
-rw-r--r--examples/tcp_reverse/src/tcp_reverse.erl13
-rw-r--r--examples/tcp_reverse/src/tcp_reverse_app.erl19
-rw-r--r--examples/tcp_reverse/src/tcp_reverse_sup.erl22
12 files changed, 189 insertions, 12 deletions
diff --git a/examples/tcp_echo/Makefile b/examples/tcp_echo/Makefile
new file mode 100644
index 0000000..712ae19
--- /dev/null
+++ b/examples/tcp_echo/Makefile
@@ -0,0 +1,6 @@
+PROJECT = tcp_echo
+
+DEPS = ranch
+dep_ranch = pkg://ranch 0.9.0
+
+include ../../erlang.mk
diff --git a/examples/tcp_echo/README.md b/examples/tcp_echo/README.md
index ee8a8c8..df4c6bd 100644
--- a/examples/tcp_echo/README.md
+++ b/examples/tcp_echo/README.md
@@ -1,18 +1,23 @@
Ranch TCP Echo
==============
-To compile this example you need rebar in your PATH.
-
-Type the following command:
-```
-$ rebar get-deps compile
+To build the example:
+``` bash
+$ make
```
-You can then start the Erlang node with the following command:
+To start the release in the foreground:
+
+``` bash
+$ ./_rel/bin/tcp_echo_example console
```
-./start.sh
+
+Then start a telnet session to port 5555:
+``` bash
+$ telnet localhost 5555
```
-Then start telnet as indicated and type in a few lines. Be
-aware that there is a timeout of 5 seconds without receiving
+Type in a few words and see them echoed back.
+
+Be aware that there is a timeout of 5 seconds without receiving
data before the example server disconnects your session.
diff --git a/examples/tcp_echo/relx.config b/examples/tcp_echo/relx.config
new file mode 100644
index 0000000..a850b71
--- /dev/null
+++ b/examples/tcp_echo/relx.config
@@ -0,0 +1,2 @@
+{release, {tcp_echo_example, "1"}, [tcp_echo]}.
+{extended_start_script, true}.
diff --git a/examples/tcp_echo/start.sh b/examples/tcp_echo/start.sh
deleted file mode 100755
index 925cf36..0000000
--- a/examples/tcp_echo/start.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-erl -pa ebin deps/*/ebin -s tcp_echo \
- -eval "io:format(\"Run: telnet localhost 5555~n\")."
diff --git a/examples/tcp_reverse/Makefile b/examples/tcp_reverse/Makefile
new file mode 100644
index 0000000..f56d051
--- /dev/null
+++ b/examples/tcp_reverse/Makefile
@@ -0,0 +1,6 @@
+PROJECT = tcp_reverse
+
+DEPS = ranch
+dep_ranch = pkg://ranch 0.9.0
+
+include ../../erlang.mk
diff --git a/examples/tcp_reverse/README.md b/examples/tcp_reverse/README.md
new file mode 100644
index 0000000..c73ae60
--- /dev/null
+++ b/examples/tcp_reverse/README.md
@@ -0,0 +1,29 @@
+Ranch TCP Reverse
+=================
+
+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:
+
+http://ninenines.eu/docs/en/ranch/HEAD/guide/protocols/#using_gen_server
+
+To build the example:
+``` bash
+$ make
+```
+
+To start the release in the foreground:
+
+``` bash
+$ ./_rel/bin/tcp_reverse_example console
+```
+
+Then start a telnet session to port 5555:
+``` bash
+$ telnet localhost 5555
+```
+
+Type in a few words and see them reversed! Amazing!
+
+Be aware that there is a timeout of 5 seconds without receiving
+data before the example server disconnects your session.
diff --git a/examples/tcp_reverse/relx.config b/examples/tcp_reverse/relx.config
new file mode 100644
index 0000000..2a83916
--- /dev/null
+++ b/examples/tcp_reverse/relx.config
@@ -0,0 +1,2 @@
+{release, {tcp_reverse_example, "1"}, [tcp_reverse]}.
+{extended_start_script, true}.
diff --git a/examples/tcp_reverse/src/reverse_protocol.erl b/examples/tcp_reverse/src/reverse_protocol.erl
new file mode 100644
index 0000000..ea28d4e
--- /dev/null
+++ b/examples/tcp_reverse/src/reverse_protocol.erl
@@ -0,0 +1,61 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(reverse_protocol).
+
+-behaviour(gen_server).
+-behaviour(ranch_protocol).
+
+-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]).
+
+-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"].
+
+start_link(Ref, Socket, Transport, Opts) ->
+ proc_lib:start_link(?MODULE, init, [Ref, Socket, Transport, Opts]).
+
+init(Args) ->
+ {ok, Args}.
+
+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};
+handle_info({tcp_closed, _Socket}, State) ->
+ {stop, normal, State};
+handle_info({tcp_error, _, Reason}, State) ->
+ {stop, Reason, State};
+handle_info(timeout, State) ->
+ {stop, normal, State};
+handle_info(_Info, State) ->
+ {stop, normal, State}.
+
+handle_call(_Request, _From, State) ->
+ {reply, ok, State}.
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
diff --git a/examples/tcp_reverse/src/tcp_reverse.app.src b/examples/tcp_reverse/src/tcp_reverse.app.src
new file mode 100644
index 0000000..900b400
--- /dev/null
+++ b/examples/tcp_reverse/src/tcp_reverse.app.src
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, tcp_reverse, [
+ {description, "Ranch TCP Reverse example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, [tcp_reverse_sup]},
+ {applications, [
+ kernel,
+ stdlib,
+ ranch
+ ]},
+ {mod, {tcp_reverse_app, []}},
+ {env, []}
+]}.
diff --git a/examples/tcp_reverse/src/tcp_reverse.erl b/examples/tcp_reverse/src/tcp_reverse.erl
new file mode 100644
index 0000000..e20f170
--- /dev/null
+++ b/examples/tcp_reverse/src/tcp_reverse.erl
@@ -0,0 +1,13 @@
+%% 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
new file mode 100644
index 0000000..49a5349
--- /dev/null
+++ b/examples/tcp_reverse/src/tcp_reverse_app.erl
@@ -0,0 +1,19 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(tcp_reverse_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+ {ok, _} = ranch:start_listener(tcp_reverse, 10,
+ ranch_tcp, [{port, 5555}], reverse_protocol, []),
+ tcp_reverse_sup:start_link().
+
+stop(_State) ->
+ ok.
diff --git a/examples/tcp_reverse/src/tcp_reverse_sup.erl b/examples/tcp_reverse/src/tcp_reverse_sup.erl
new file mode 100644
index 0000000..4264d18
--- /dev/null
+++ b/examples/tcp_reverse/src/tcp_reverse_sup.erl
@@ -0,0 +1,22 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(tcp_reverse_sup).
+-behaviour(supervisor).
+
+%% API.
+-export([start_link/0]).
+
+%% supervisor.
+-export([init/1]).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% supervisor.
+
+init([]) ->
+ {ok, {{one_for_one, 10, 10}, []}}.