Permalink
Browse files

first commit

  • Loading branch information...
1 parent 6abc906 commit bac4aa4fc96f6dfc4f9de671c6cea1a3946584fd @ronalfei committed Mar 3, 2014
View
@@ -0,0 +1,3 @@
+*.dump
+*.log
+*.beam
View
@@ -0,0 +1,16 @@
+# Feel free to use, reuse and abuse the code in this file.
+
+all: app
+
+app: get-deps
+ @./rebar compile
+ ctags -R ./
+
+get-deps:
+ @./rebar get-deps
+
+clean:
+ @./rebar clean
+ rm -f erl_crash.dump
+
+dist-clean: clean
View
Binary file not shown.
View
Binary file not shown.
View
@@ -0,0 +1,8 @@
+{application,websocket,
+ [{description,"Cowboy websocket example."},
+ {vsn,"1"},
+ {modules,[reloader,ses,websocket_app,websocket_sup,ws_handler]},
+ {registered,[websocket_sup]},
+ {applications,[kernel,stdlib,cowboy]},
+ {mod,{websocket_app,[]}},
+ {env,[]}]}.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
BIN rebar
Binary file not shown.
View
@@ -0,0 +1,11 @@
+%% -*- erlang -*-
+{erl_opts, [{src_dirs,["src"]}]}.
+{deps, [
+ {lager, ".*", {git, "git://github.com/basho/lager.git", "master"}},
+ {riakc, ".*", {git, "git://github.com/basho/riak-erlang-client.git", {tag,"1.4.2"}}},
+ {riakpool, ".*", {git, "git://github.com/dweldon/riakpool.git", "master"}},
+ {cowboy, ".*", {git, "git://github.com/extend/cowboy.git", {tag, "0.9.0"}}}
+
+]}.
+{cover_enabled, true}.
+{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
View
@@ -0,0 +1,163 @@
+
+%% @copyright 2007 Mochi Media, Inc.
+%% @author Matthew Dempsky <matthew@mochimedia.com>
+%%
+%% @doc Erlang module for automatically reloading modified modules
+%% during development.
+
+-module(reloader).
+-author("Matthew Dempsky <matthew@mochimedia.com>").
+
+-include_lib("kernel/include/file.hrl").
+
+-behaviour(gen_server).
+-export([start/0, start_link/0]).
+-export([stop/0]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+-export([all_changed/0]).
+-export([is_changed/1]).
+-export([reload_modules/1]).
+-record(state, {last, tref}).
+
+%% External API
+
+%% @spec start() -> ServerRet
+%% @doc Start the reloader.
+start() ->
+ gen_server:start({local, ?MODULE}, ?MODULE, [], []).
+
+%% @spec start_link() -> ServerRet
+%% @doc Start the reloader.
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+%% @spec stop() -> ok
+%% @doc Stop the reloader.
+stop() ->
+ gen_server:call(?MODULE, stop).
+
+%% gen_server callbacks
+
+%% @spec init([]) -> {ok, State}
+%% @doc gen_server init, opens the server in an initial state.
+init([]) ->
+ {ok, TRef} = timer:send_interval(timer:seconds(1), doit),
+ {ok, #state{last = stamp(), tref = TRef}}.
+
+%% @spec handle_call(Args, From, State) -> tuple()
+%% @doc gen_server callback.
+handle_call(stop, _From, State) ->
+ {stop, shutdown, stopped, State};
+handle_call(_Req, _From, State) ->
+ {reply, {error, badrequest}, State}.
+
+%% @spec handle_cast(Cast, State) -> tuple()
+%% @doc gen_server callback.
+handle_cast(_Req, State) ->
+ {noreply, State}.
+
+%% @spec handle_info(Info, State) -> tuple()
+%% @doc gen_server callback.
+handle_info(doit, State) ->
+ Now = stamp(),
+ _ = doit(State#state.last, Now),
+ {noreply, State#state{last = Now}};
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+%% @spec terminate(Reason, State) -> ok
+%% @doc gen_server termination callback.
+terminate(_Reason, State) ->
+ {ok, cancel} = timer:cancel(State#state.tref),
+ ok.
+
+
+%% @spec code_change(_OldVsn, State, _Extra) -> State
+%% @doc gen_server code_change callback (trivial).
+code_change(_Vsn, State, _Extra) ->
+ {ok, State}.
+
+%% @spec reload_modules([atom()]) -> [{module, atom()} | {error, term()}]
+%% @doc code:purge/1 and code:load_file/1 the given list of modules in order,
+%% return the results of code:load_file/1.
+reload_modules(Modules) ->
+ [begin code:purge(M), code:load_file(M) end || M <- Modules].
+
+%% @spec all_changed() -> [atom()]
+%% @doc Return a list of beam modules that have changed.
+all_changed() ->
+ [M || {M, Fn} <- code:all_loaded(), is_list(Fn), is_changed(M)].
+
+%% @spec is_changed(atom()) -> boolean()
+%% @doc true if the loaded module is a beam with a vsn attribute
+%% and does not match the on-disk beam file, returns false otherwise.
+is_changed(M) ->
+ try
+ module_vsn(M:module_info()) =/= module_vsn(code:get_object_code(M))
+ catch _:_ ->
+ false
+ end.
+
+%% Internal API
+
+module_vsn({M, Beam, _Fn}) ->
+ {ok, {M, Vsn}} = beam_lib:version(Beam),
+ Vsn;
+module_vsn(L) when is_list(L) ->
+ {_, Attrs} = lists:keyfind(attributes, 1, L),
+ {_, Vsn} = lists:keyfind(vsn, 1, Attrs),
+ Vsn.
+
+doit(From, To) ->
+ [case file:read_file_info(Filename) of
+ {ok, #file_info{mtime = Mtime}} when Mtime >= From, Mtime < To ->
+ reload(Module);
+ {ok, _} ->
+ unmodified;
+ {error, enoent} ->
+ %% The Erlang compiler deletes existing .beam files if
+ %% recompiling fails. Maybe it's worth spitting out a
+ %% warning here, but I'd want to limit it to just once.
+ gone;
+ {error, Reason} ->
+ io:format("Error reading ~s's file info: ~p~n",
+ [Filename, Reason]),
+ error
+ end || {Module, Filename} <- code:all_loaded(), is_list(Filename)].
+
+reload(Module) ->
+ io:format("Reloading ~p ...", [Module]),
+ code:purge(Module),
+ case code:load_file(Module) of
+ {module, Module} ->
+ io:format(" ok.~n"),
+ case erlang:function_exported(Module, test, 0) of
+ true ->
+ io:format(" - Calling ~p:test() ...", [Module]),
+ case catch Module:test() of
+ ok ->
+ io:format(" ok.~n"),
+ reload;
+ Reason ->
+ io:format(" fail: ~p.~n", [Reason]),
+ reload_but_test_failed
+ end;
+ false ->
+ reload
+ end;
+ {error, Reason} ->
+ io:format(" fail: ~p.~n", [Reason]),
+ error
+ end.
+
+
+stamp() ->
+ erlang:localtime().
+
+%%
+%% Tests
+%%
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+-endif.
+
View
@@ -0,0 +1,25 @@
+-include("ses.hrl").
+-module(ses).
+-export([start/0, start_link/0, test/0]).
+-export([stop/0]).
+
+start()->
+ lager:start(),
+ application:start(ranch),
+lager:info("ranch ok"),
+ application:start(cowlib),
+lager:info("cowlib ok"),
+ application:start(crypto),
+lager:info("crypto ok"),
+ application:start(cowboy),
+lager:info("cowboy ok"),
+ application:start(websocket).
+
+start_link()->
+ application:start(websocket).
+
+stop()->
+ application:stop(websocket).
+
+test() ->
+ lager:info("asdfasdf").
View
@@ -0,0 +1,7 @@
+-compile([{parse_transform, lager_transform}]).
+
+-define(LOG_LEVEL, debug). % log levle will be lager's levels [debug,info,notice,warning,error,critical,alert,emergency, none]
+
+
+-define(HOSTNAME, ess_util:hostname()).
+
View
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, websocket, [
+ {description, "Cowboy websocket example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, [websocket_sup]},
+ {applications, [
+ kernel,
+ stdlib,
+ cowboy
+ ]},
+ {mod, {websocket_app, []}},
+ {env, []}
+]}.
View
@@ -0,0 +1,25 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(websocket_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+start(_Type, _Args) ->
+ Dispatch = cowboy_router:compile([
+ {'_', [
+ {"/", cowboy_static, {priv_file, websocket, "index.html"}},
+ {"/websocket", ws_handler, []},
+ {"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}}
+ ]}
+ ]),
+ {ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
+ [{env, [{dispatch, Dispatch}]}]),
+ websocket_sup:start_link().
+
+stop(_State) ->
+ ok.
View
@@ -0,0 +1,23 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(websocket_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}}.
View
@@ -0,0 +1,29 @@
+-module(ws_handler).
+-behaviour(cowboy_websocket_handler).
+
+-export([init/3]).
+-export([websocket_init/3]).
+-export([websocket_handle/3]).
+-export([websocket_info/3]).
+-export([websocket_terminate/3]).
+
+init({tcp, http}, _Req, _Opts) ->
+ {upgrade, protocol, cowboy_websocket}.
+
+websocket_init(_TransportName, Req, _Opts) ->
+ erlang:start_timer(1000, self(), <<"Hello!">>),
+ {ok, Req, undefined_state}.
+
+websocket_handle({text, Msg}, Req, State) ->
+ {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
+websocket_handle(_Data, Req, State) ->
+ {ok, Req, State}.
+
+websocket_info({timeout, _Ref, Msg}, Req, State) ->
+ erlang:start_timer(1000, self(), <<"How' you doin'?">>),
+ {reply, {text, Msg}, Req, State};
+websocket_info(_Info, Req, State) ->
+ {ok, Req, State}.
+
+websocket_terminate(_Reason, _Req, _State) ->
+ ok.
View
@@ -0,0 +1,11 @@
+#!/bin/sh
+erl -name ses@10.100.1.83 -pa ebin -pa deps/*/ebin -s ses -s reloader \
+ +K true \
+ +A 128 \
+ -env ERL_MAX_PORTS 64000 \
+ -env ERL_FULLSWEEP_AFTER 0 \
+ -smp enable \
+ +zdbbl 32768 \
+ -setcookie ses \
+ -eval "io:format(\"* Eventsource: http://localhost:8080/~n~n~n\"). "
+ %-detached
View
@@ -0,0 +1,12 @@
+#!/bin/sh
+NAME="ess"
+screen -dmS $NAME
+screen -S $NAME -X screen erl -name ess@10.100.1.83 -pa ebin -pa deps/*/ebin -s ess -s reloader \
+ +K true \
+ +A 128 \
+ -env ERL_MAX_PORTS 64000 \
+ -env ERL_FULLSWEEP_AFTER 0 \
+ -smp enable \
+ +zdbbl 32768 \
+ -setcookie ess \
+ -eval "io:format(\"* Eventsource: http://localhost:8181/~n~n~n\"). "
Oops, something went wrong.

0 comments on commit bac4aa4

Please sign in to comment.