aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-08-07 14:03:19 +0200
committerLoïc Hoguin <[email protected]>2012-08-07 14:03:19 +0200
commitbfa353f8e75e06363bea565cc50f8de52090ae95 (patch)
tree317e014411ac24d55df598c3b35eb5f1ca35ce71
parentb186d01367a1a744f6195e071611f97f9cc88f8e (diff)
downloadranch-bfa353f8e75e06363bea565cc50f8de52090ae95.tar.gz
ranch-bfa353f8e75e06363bea565cc50f8de52090ae95.tar.bz2
ranch-bfa353f8e75e06363bea565cc50f8de52090ae95.zip
Add a TCP Echo protocol example
-rw-r--r--examples/tcp_echo/README.md18
-rw-r--r--examples/tcp_echo/rebar.config4
-rw-r--r--examples/tcp_echo/src/echo_protocol.erl21
-rw-r--r--examples/tcp_echo/src/tcp_echo.app.src15
-rw-r--r--examples/tcp_echo/src/tcp_echo.erl12
-rw-r--r--examples/tcp_echo/src/tcp_echo_app.erl19
-rw-r--r--examples/tcp_echo/src/tcp_echo_sup.erl22
-rwxr-xr-xexamples/tcp_echo/start.sh3
8 files changed, 114 insertions, 0 deletions
diff --git a/examples/tcp_echo/README.md b/examples/tcp_echo/README.md
new file mode 100644
index 0000000..ee8a8c8
--- /dev/null
+++ b/examples/tcp_echo/README.md
@@ -0,0 +1,18 @@
+Ranch TCP Echo
+==============
+
+To compile this example you need rebar in your PATH.
+
+Type the following command:
+```
+$ rebar get-deps compile
+```
+
+You can then start the Erlang node with the following command:
+```
+./start.sh
+```
+
+Then start telnet as indicated and type in a few lines. 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/rebar.config b/examples/tcp_echo/rebar.config
new file mode 100644
index 0000000..78300c9
--- /dev/null
+++ b/examples/tcp_echo/rebar.config
@@ -0,0 +1,4 @@
+{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
new file mode 100644
index 0000000..b54e294
--- /dev/null
+++ b/examples/tcp_echo/src/echo_protocol.erl
@@ -0,0 +1,21 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(echo_protocol).
+-export([start_link/4, init/4]).
+
+start_link(ListenerPid, Socket, Transport, Opts) ->
+ Pid = spawn_link(?MODULE, init, [ListenerPid, Socket, Transport, Opts]),
+ {ok, Pid}.
+
+init(ListenerPid, Socket, Transport, _Opts = []) ->
+ ok = ranch:accept_ack(ListenerPid),
+ loop(Socket, Transport).
+
+loop(Socket, Transport) ->
+ case Transport:recv(Socket, 0, 5000) of
+ {ok, Data} ->
+ Transport:send(Socket, Data),
+ loop(Socket, Transport);
+ _ ->
+ ok = Transport:close(Socket)
+ end.
diff --git a/examples/tcp_echo/src/tcp_echo.app.src b/examples/tcp_echo/src/tcp_echo.app.src
new file mode 100644
index 0000000..103fd56
--- /dev/null
+++ b/examples/tcp_echo/src/tcp_echo.app.src
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, tcp_echo, [
+ {description, "Ranch TCP Echo example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ ranch
+ ]},
+ {mod, {tcp_echo_app, []}},
+ {env, []}
+]}.
diff --git a/examples/tcp_echo/src/tcp_echo.erl b/examples/tcp_echo/src/tcp_echo.erl
new file mode 100644
index 0000000..46d31da
--- /dev/null
+++ b/examples/tcp_echo/src/tcp_echo.erl
@@ -0,0 +1,12 @@
+%% 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_echo/src/tcp_echo_app.erl b/examples/tcp_echo/src/tcp_echo_app.erl
new file mode 100644
index 0000000..7fac685
--- /dev/null
+++ b/examples/tcp_echo/src/tcp_echo_app.erl
@@ -0,0 +1,19 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(tcp_echo_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+ {ok, _} = ranch:start_listener(tcp_echo, 1,
+ ranch_tcp, [{port, 5555}], echo_protocol, []),
+ tcp_echo_sup:start_link().
+
+stop(_State) ->
+ ok.
diff --git a/examples/tcp_echo/src/tcp_echo_sup.erl b/examples/tcp_echo/src/tcp_echo_sup.erl
new file mode 100644
index 0000000..8f33593
--- /dev/null
+++ b/examples/tcp_echo/src/tcp_echo_sup.erl
@@ -0,0 +1,22 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(tcp_echo_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}, []}}.
diff --git a/examples/tcp_echo/start.sh b/examples/tcp_echo/start.sh
new file mode 100755
index 0000000..925cf36
--- /dev/null
+++ b/examples/tcp_echo/start.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+erl -pa ebin deps/*/ebin -s tcp_echo \
+ -eval "io:format(\"Run: telnet localhost 5555~n\")."