Skip to content

Commit

Permalink
Merge pull request #33 from cabol/1.x.x
Browse files Browse the repository at this point in the history
Fixed `proplist_to_record` macro, added point_to_point example, fixed version to 1.0.0.
  • Loading branch information
cabol committed Jan 9, 2016
2 parents 2c6144a + b568803 commit bf1739e
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ See [examples](./examples).

$ make doc

> **Note:** Once you run previous command, a new folder `doc` is created, and you'll have a pretty nice HTML documentation.

## Change Log

Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ ErlBus Examples

* [pubsub](./pubsub):
basic example to show the use of Pub/Sub pattern.
* [point_to_point](./point_to_point):
basic example to show how to implement a point-to-point pattern.
* [chat](./chat):
basic chat example using websockets.
22 changes: 22 additions & 0 deletions examples/point_to_point/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
REBAR = ../../rebar3

BUILD_PATH = ./_build/default/lib/*/ebin

CONFIG ?= config/sys.config

.PHONY: all compile clean distclean run

all: compile

compile:
$(REBAR) compile

clean:
$(REBAR) clean

distclean: clean
$(REBAR) clean --all
rm -rf _build *.lock

run: all
erl -pa $(BUILD_PATH) -s point_to_point -config ${CONFIG}
16 changes: 16 additions & 0 deletions examples/point_to_point/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Point-To-Point example
======================

To try this example, you need GNU `make` and `git` in your PATH.

To build the example, run the following command:

``` bash
$ make
```

To start the example:

``` bash
$ make run
```
14 changes: 14 additions & 0 deletions examples/point_to_point/config/sys.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
%% EBus
{ebus,
[
{pubsub,
[
{adapter, ebus_ps_pg2},
{pool_size, 5},
{name, ebus_ps}
]
}
]
}
].
10 changes: 10 additions & 0 deletions examples/point_to_point/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%% Erlang compiler options
{erl_opts, [debug_info, fail_on_warning]}.

%% Whether to enable coverage reporting. Default is `false'
{cover_enabled, true}.

%% Dependencies
{deps, [
{erlbus, {git, "https://github.com/cabol/erlbus.git", {branch, "master"}}}
]}.
19 changes: 19 additions & 0 deletions examples/point_to_point/src/point_to_point.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%% Feel free to use, reuse and abuse the code in this file.

{application, point_to_point,
[
{description, "ErlBus Point-To-Point example."},
{vsn, "1"},
{modules, []},
{registered, []},
{applications,
[
kernel,
stdlib,
ebus
]
},
{mod, {point_to_point, []}},
{env, []}
]
}.
51 changes: 51 additions & 0 deletions examples/point_to_point/src/point_to_point.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
%% Feel free to use, reuse and abuse the code in this file.

-module(point_to_point).

-behaviour(application).

%% API.
-export([start/0, start/2]).
-export([stop/0, stop/1]).

-define(TOPIC, <<"P2P_TOPIC">>).

%% API.

start() ->
application:ensure_all_started(point_to_point).

start(_Type, _Args) ->
P = spawn_link(fun() -> dispatcher(?TOPIC) end),
lists:foreach(fun(N) -> subscriber(?TOPIC, N) end, lists:seq(1, 3)),
timer:sleep(1 * 60 * 1000),
exit(P, kill),
teardown_ebus().

stop() ->
application:stop(point_to_point).

stop(_State) ->
ok.

%% Internals

dispatcher(Topic) ->
timer:sleep(500),
Msg = #{topic => Topic, payload => os:timestamp()},
io:format("Dispatch: ~p~n", [Msg]),
ebus:dispatch(Topic, Msg),
dispatcher(Topic).

subscriber(Topic, N) ->
Callback = fun(Ctx, Msg) ->
io:format(
"\e[36m---> [PID: ~p][CTX: ~p][MSG: ~p]~n\e[0m",
[self(), Ctx, Msg]
)
end,
{Handler, _} = ebus_process:spawn_handler(Callback, N, [monitor]),
ebus:sub(Handler, Topic).

teardown_ebus() ->
application:stop(ebus).
2 changes: 1 addition & 1 deletion examples/pubsub/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ distclean: clean
rm -rf _build *.lock

run: all
erl -pa $(BUILD_PATH) -s ebus -s pubsub -config ${CONFIG}
erl -pa $(BUILD_PATH) -s pubsub -config ${CONFIG}
14 changes: 10 additions & 4 deletions include/ebus.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@
[Tag | Values] = tuple_to_list(Tuple),
Defaults = lists:zip(Fields, Values),
F = fun({K, V}) ->
case lists:keyfind(K, 1, Proplist) of
{_K, V0} -> V0;
_ -> V
case lists:keyfind(K, 1, Proplist) of
{K, V0} ->
V0;
_ ->
BinK = atom_to_binary(K, utf8),
case lists:keyfind(BinK, 1, Proplist) of
{BinK, V1} -> V1;
_ -> V
end
end,
end
end,
L = lists:map(F, Defaults),
list_to_tuple([Tag | L])
end
Expand Down
2 changes: 1 addition & 1 deletion src/ebus.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, ebus,
[
{description, "Erlang Message Event Bus"},
{vsn, "0.2.0"},
{vsn, "1.0.0"},
{id, "git"},
{modules, []},
{registered, []},
Expand Down

0 comments on commit bf1739e

Please sign in to comment.