aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorjosh rotenberg <[email protected]>2013-11-17 15:47:35 -0800
committerjosh rotenberg <[email protected]>2013-12-04 14:44:53 -0800
commit88209ea02ea71fd8c028e76f9bf7aedbf0dfdd55 (patch)
tree3139b2d57c6552d1b38d1792ce4a644bf8590628 /examples
parent6a5015fedd0f85388144de7b8a6ed7d883da5e1a (diff)
downloadranch-88209ea02ea71fd8c028e76f9bf7aedbf0dfdd55.tar.gz
ranch-88209ea02ea71fd8c028e76f9bf7aedbf0dfdd55.tar.bz2
ranch-88209ea02ea71fd8c028e76f9bf7aedbf0dfdd55.zip
Add example using gen_server
Diffstat (limited to 'examples')
-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
8 files changed, 167 insertions, 0 deletions
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}, []}}.