From 111c6f238797216889c50a3891d26516ad455fd5 Mon Sep 17 00:00:00 2001 From: Eldon Stegall Date: Sat, 14 Sep 2013 19:59:36 -0400 Subject: [PATCH] Chat example --- examples/chat/README.md | 19 ++++++++++ examples/chat/rebar.config | 4 ++ examples/chat/src/chat.app.src | 15 ++++++++ examples/chat/src/chat.erl | 27 ++++++++++++++ examples/chat/src/chat_app.erl | 28 ++++++++++++++ examples/chat/src/chat_sup.erl | 23 ++++++++++++ examples/chat/src/stream_handler.erl | 27 ++++++++++++++ examples/chat/src/subscriber.erl | 21 +++++++++++ examples/chat/src/toppage_handler.erl | 54 +++++++++++++++++++++++++++ examples/chat/start.sh | 3 ++ 10 files changed, 221 insertions(+) create mode 100644 examples/chat/README.md create mode 100644 examples/chat/rebar.config create mode 100644 examples/chat/src/chat.app.src create mode 100644 examples/chat/src/chat.erl create mode 100644 examples/chat/src/chat_app.erl create mode 100644 examples/chat/src/chat_sup.erl create mode 100644 examples/chat/src/stream_handler.erl create mode 100644 examples/chat/src/subscriber.erl create mode 100644 examples/chat/src/toppage_handler.erl create mode 100755 examples/chat/start.sh diff --git a/examples/chat/README.md b/examples/chat/README.md new file mode 100644 index 0000000..321dd23 --- /dev/null +++ b/examples/chat/README.md @@ -0,0 +1,19 @@ +Bullet Chat +============ + +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 point your browser to the indicated URL. + +You can interrupt temporarily the node to check that Bullet +properly reconnects when something happens. diff --git a/examples/chat/rebar.config b/examples/chat/rebar.config new file mode 100644 index 0000000..a254494 --- /dev/null +++ b/examples/chat/rebar.config @@ -0,0 +1,4 @@ +{deps, [ + {bullet, ".*", + {git, "git://github.com/extend/bullet.git", "master"}} +]}. diff --git a/examples/chat/src/chat.app.src b/examples/chat/src/chat.app.src new file mode 100644 index 0000000..3198e8d --- /dev/null +++ b/examples/chat/src/chat.app.src @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +{application, chat, [ + {description, "Bullet chat example."}, + {vsn, "1"}, + {modules, []}, + {registered, []}, + {applications, [ + kernel, + stdlib, + cowboy + ]}, + {mod, {chat_app, []}}, + {env, []} +]}. diff --git a/examples/chat/src/chat.erl b/examples/chat/src/chat.erl new file mode 100644 index 0000000..27dbf6d --- /dev/null +++ b/examples/chat/src/chat.erl @@ -0,0 +1,27 @@ +%% Copyright (c) 2012, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(chat). + +%% API. +-export([start/0]). + +%% API. + +start() -> + ok = application:start(crypto), + ok = application:start(ranch), + ok = application:start(cowlib), + ok = application:start(cowboy), + ok = application:start(chat). diff --git a/examples/chat/src/chat_app.erl b/examples/chat/src/chat_app.erl new file mode 100644 index 0000000..5757314 --- /dev/null +++ b/examples/chat/src/chat_app.erl @@ -0,0 +1,28 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @private +-module(chat_app). +-behaviour(application). + +%% API. +-export([start/2]). +-export([stop/1]). + +%% API. + +start(_Type, _Args) -> + Dispatch = cowboy_router:compile([ + {'_', [ + {"/", toppage_handler, []}, + {"/bullet", bullet_handler, [{handler, stream_handler}]}, + {"/static/[...]", cowboy_static, {priv_dir, bullet, []}} + ]} + ]), + {ok, _} = cowboy:start_http(http, 100, + [{port, 8080}], [{env, [{dispatch, Dispatch}]}] + ), + register(chatty, spawn(subscriber,doit,[[]])), + chat_sup:start_link(). + +stop(_State) -> + ok. diff --git a/examples/chat/src/chat_sup.erl b/examples/chat/src/chat_sup.erl new file mode 100644 index 0000000..2f0aa9f --- /dev/null +++ b/examples/chat/src/chat_sup.erl @@ -0,0 +1,23 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @private +-module(chat_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([]) -> + Procs = [], + {ok, {{one_for_one, 10, 10}, Procs}}. diff --git a/examples/chat/src/stream_handler.erl b/examples/chat/src/stream_handler.erl new file mode 100644 index 0000000..c12fa05 --- /dev/null +++ b/examples/chat/src/stream_handler.erl @@ -0,0 +1,27 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Stream handler for chatting. +-module(stream_handler). + +-export([init/4]). +-export([stream/3]). +-export([info/3]). +-export([terminate/2]). + +init(_Transport, Req, _Opts, _Active) -> + whereis(chatty) ! {add, self()}, + {ok, Req, self()}. + +stream(<<"ping: ", Name/binary>>, Req, State) -> + io:format("ping ~p received~n", [Name]), + {reply, <<"pong">>, Req, State}; + +stream(Message, Req, State) -> + whereis(chatty) ! { send, Message }, + {ok, Req, State}. + +info(Info, Req, State) -> + {reply, Info, Req, State}. + +terminate(_Req, _) -> + ok. diff --git a/examples/chat/src/subscriber.erl b/examples/chat/src/subscriber.erl new file mode 100644 index 0000000..fe3d557 --- /dev/null +++ b/examples/chat/src/subscriber.erl @@ -0,0 +1,21 @@ +-module(subscriber). + +-compile(export_all). + +doit(Subscribers) -> + receive + {add, Pid} -> + io:format("add pid ~p ~n", [Pid]), + NewSubscribers = [Pid| Subscribers], + doit(NewSubscribers); + {send, Message } -> + io:format("Sending message ~p to ~p ~n", [Message, Subscribers]), + lists:map( fun (Pid) -> Pid ! Message end, Subscribers), + doit(Subscribers); + {who, Sender} -> + io:format("Responsing with subscribers ~p to pid ~p", [Subscribers, Sender]), + Sender ! Subscribers, + doit(Subscribers); + terminate -> + ok + end. diff --git a/examples/chat/src/toppage_handler.erl b/examples/chat/src/toppage_handler.erl new file mode 100644 index 0000000..bfffdd5 --- /dev/null +++ b/examples/chat/src/toppage_handler.erl @@ -0,0 +1,54 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Main page of the chat application. +-module(toppage_handler). + +-export([init/3]). +-export([handle/2]). +-export([terminate/3]). + +init(_Transport, Req, []) -> + {ok, Req, undefined}. + +handle(Req, State) -> + Body = <<" + + + + + Bullet Chat + + + + + + +

Chats!

+ + + +">>, + {ok, Req2} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], + Body, Req), + {ok, Req2, State}. + +terminate(_Reason, _Req, _State) -> + ok. diff --git a/examples/chat/start.sh b/examples/chat/start.sh new file mode 100755 index 0000000..c8c8738 --- /dev/null +++ b/examples/chat/start.sh @@ -0,0 +1,3 @@ +#!/bin/sh +erl -pa ebin deps/*/ebin -s chat \ + -eval "io:format(\"Point your browser at http://localhost:8080~n\")."