Skip to content
This repository was archived by the owner on Aug 15, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c334659
Update Lager dependency with the newest version
ethrbh Dec 26, 2016
53648bc
Eliminate compilation warnings, by comment the unused variables
ethrbh Dec 26, 2016
2ca16f9
Erwa can works with Cowboy-2.x version
ethrbh Dec 26, 2016
c203015
Use Cowboy master branch by default
ethrbh Dec 26, 2016
59bf775
Update .app.src file to be sync with Makefile
ethrbh Dec 26, 2016
5d51818
Update for able to work with Cowboy-2.x version
ethrbh Dec 26, 2016
2f129dd
Remove debug printouts
ethrbh Dec 26, 2016
727082f
Update Cowboy dependency
ethrbh Jul 3, 2017
34f2359
Update dependencies list of ERWA
ethrbh Jul 4, 2017
723bf5c
Bug fix the previously made typo fault
ethrbh Jul 4, 2017
db8ace6
Update for Erlang/OTP-20.0
ethrbh Jul 26, 2017
2f61a54
Update to use lager dep from its original repo
ethrbh Nov 4, 2020
ebab7af
Merge pull request #1 from ethrbh/use_lager_from_erlang-lager
ethrbh Nov 5, 2020
e73c42d
Update Lager dependency with the newest version
ethrbh Dec 26, 2016
c76889d
Eliminate compilation warnings, by comment the unused variables
ethrbh Dec 26, 2016
ef4dda0
Erwa can works with Cowboy-2.x version
ethrbh Dec 26, 2016
5006028
Use Cowboy master branch by default
ethrbh Dec 26, 2016
fce9124
Update .app.src file to be sync with Makefile
ethrbh Dec 26, 2016
6987818
Update for able to work with Cowboy-2.x version
ethrbh Dec 26, 2016
fd56907
Remove debug printouts
ethrbh Dec 26, 2016
90b6ac6
Update Cowboy dependency
ethrbh Jul 3, 2017
e621a2a
Update dependencies list of ERWA
ethrbh Jul 4, 2017
3a17811
Bug fix the previously made typo fault
ethrbh Jul 4, 2017
76cdc87
Update for Erlang/OTP-20.0
ethrbh Jul 26, 2017
e0e68cd
Update to use lager dep from its original repo
ethrbh Nov 4, 2020
a74b4af
Merge pull request #2 from ethrbh/sync_with_original_master_branch_fr…
ethrbh Nov 5, 2020
fe4bb77
Update to use wamper from ethrbh
ethrbh Nov 5, 2020
66df5d5
Merge pull request #3 from ethrbh/dev_branch
ethrbh Nov 5, 2020
8447fb2
Update to use erlang-lager/lager 3.9.2
ethrbh Jan 6, 2022
c35398d
Merge pull request #4 from ethrbh/dev_branch
ethrbh Jan 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ CT_OPTS = -cover test/cover.spec
ERLC_OPTS = +debug_info


DEPS = cowboy ranch wamper lager
dep_cowboy = git https://github.com/ninenines/cowboy.git 1.1.x
dep_wamper = git https://github.com/bwegh/wamper master
dep_lager = git https://github.com/basho/lager 3.0.2
DEPS = cowboy wamper lager
%%dep_cowboy = git https://github.com/ninenines/cowboy master
dep_cowboy_commit = master
dep_wamper = git https://github.com/ethrbh/wamper master
dep_lager = git https://github.com/erlang-lager/lager 3.9.2



TEST_DEPS = awre
dep_awre = git https://github.com/bwegh/awre master
dep_awre = git https://github.com/ethrbh/awre master


include erlang.mk
5 changes: 3 additions & 2 deletions examples/simple_router/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PROJECT = simple_router
ERLC_OPTS = +debug_info

DEPS = erwa
dep_erwa = git https://github.com/bwegh/erwa master
DEPS = erwa elogger
dep_erwa = git https://github.com/ethrbh/erwa master
dep_elogger = git https://github.com/bwegh/elogger master


include ../../erlang.mk
2 changes: 2 additions & 0 deletions examples/simple_router/simple_router.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

COMPILE_FIRST +=
12 changes: 11 additions & 1 deletion examples/simple_router/src/simple_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ init([]) ->
{"/static/[...]", cowboy_static, {priv_dir, simple_router, "static"}}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],[{env, [{dispatch, Dispatch}]}]),

{ok, CowboyVsn} = application:get_key(cowboy,vsn),
case string:str(CowboyVsn, "1.") of
1 ->
%% This is for cowboy-1.x
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],[{env, [{dispatch, Dispatch}]}]);
_->
%% This is for cowboy-2.x or newer
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}],#{env => #{dispatch => Dispatch}})
end,

{ok,_} = ranch:start_listener(erwa_tcp, 5, ranch_tcp, [{port,5555}], erwa_in_tcp, []),
{ok, {{one_for_one, 10, 10}, []}}.
4 changes: 3 additions & 1 deletion src/erwa.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
wamper,
mnesia,
lager,
crypto
crypto,
cowboy,
ranch
]},
{mod, {erwa_app, []}},
{env, []}
Expand Down
5 changes: 3 additions & 2 deletions src/erwa_broker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ realm_to_db_name(Realm) ->
binary_to_atom(<< Prefix/binary, Realm/binary >>, utf8 ).

gen_id() ->
crypto:rand_uniform(0,9007199254740993).

%%crypto:rand_uniform(0,9007199254740993).
rand:uniform(9007199254740993).

publish_metaevent(Event, TopicUri, SessionId, SecondArg, Realm) ->
case binary:part(TopicUri,{0,5}) == <<"wamp.">> of
true ->
Expand Down
6 changes: 4 additions & 2 deletions src/erwa_dealer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ get_callee([H|_],single,_) ->
H;
get_callee(Callees,random,_) ->
Len = length(Callees),
lists:nth(crypto:rand_uniform(1,Len+1),Callees);
%%lists:nth(crypto:rand_uniform(1,Len+1),Callees);
lists:nth(rand:uniform(Len+1),Callees);
get_callee(Callees,roundrobin,NextIn) ->
Next = case NextIn > length(Callees) of
true ->
Expand Down Expand Up @@ -381,7 +382,8 @@ realm_to_table_name(Realm) ->


gen_id() ->
crypto:rand_uniform(0,9007199254740993).
%%crypto:rand_uniform(0,9007199254740993).
rand:uniform(9007199254740993).

publish_metaevent(Event,Args) ->
#{uri := ProcedureUri, id := ProcId, session := SessionId, realm := Realm} = Args,
Expand Down
84 changes: 61 additions & 23 deletions src/erwa_in_ws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
% cowboy 1.x
-export([init/3]).
-export([websocket_init/3]).
-export([websocket_handle/3]).
-export([websocket_info/3]).

%cowboy 2.x
-export([init/2]).
-export([websocket_init/1]).
-export([websocket_handle/2]).
-export([websocket_info/2]).

% both cowboy versions
-export([websocket_handle/3]).
-export([websocket_info/3]).
-export([terminate/3]).

-define(TIMEOUT,60000).
Expand All @@ -60,7 +63,9 @@
routing = undefined
}).

% for cowboy 1.x
%% ********************************************************
%% for cowboy 1.x
%% ********************************************************
init(_, Req, _ ) ->
{upgrade, protocol, cowboy_websocket, Req, []}.

Expand All @@ -80,25 +85,6 @@ websocket_init( _Type, Req, _Opts) ->
{shutdown,Req1}
end.

init( Req, _Opts) ->
% need to check for the wamp.2.json or wamp.2.msgpack
Protocols = cowboy_req:parse_header(?SUBPROTHEADER, Req),
lager:debug("protocols are ~p~n",[Protocols]),
case find_supported_protocol(Protocols) of
{Enc,WsEncoding,Header} ->
Req1 = cowboy_req:set_resp_header(?SUBPROTHEADER,Header,Req),
%% Peer = cowboy_req:peer(Req1),
Routing = erwa_routing:init(),
%% Routing1 = erwa_routing:set_peer(Peer,Routing),
%% Routing2 = erwa_routing:set_source(websocket,Routing1),
{cowboy_websocket,Req1,#state{enc=Enc,ws_enc=WsEncoding,routing=Routing}};
_ ->
% unsupported
{shutdown,Req}
end.



websocket_handle({WsEnc, Data}, Req, #state{ws_enc=WsEnc,enc=Enc,buffer=Buffer,routing=Routing}=State) ->
{MList,NewBuffer} = wamper_protocol:deserialize(<<Buffer/binary, Data/binary>>,Enc),
{ok,OutFrames,NewRouting} = handle_messages(MList,[],Routing,State),
Expand Down Expand Up @@ -128,10 +114,62 @@ websocket_info({erwa,Msg}, Req, #state{routing=Routing,ws_enc=WsEnc,enc=Enc}=Sta
websocket_info(_Data, Req, State) ->
{ok,Req,State}.

%% ********************************************************
%% for cowboy 2.x
%% ********************************************************
init(Req, _State) ->
% need to check for the wamp.2.json or wamp.2.msgpack
Protocols = cowboy_req:parse_header(?SUBPROTHEADER, Req),
lager:debug("protocols are ~p~n",[Protocols]),
case find_supported_protocol(Protocols) of
{Enc, WsEncoding, Header} ->
Req1 = cowboy_req:set_resp_header(?SUBPROTHEADER,Header,Req),
{cowboy_websocket, Req1, #state{enc = Enc,
ws_enc = WsEncoding
}};
_ ->
% unsupported
{shutdown,Req}
end.

websocket_init(State) ->
Routing = erwa_routing:init(),
{ok, State#state{routing = Routing}}.

websocket_handle({WsEnc, Data}, #state{ws_enc=WsEnc,enc=Enc,buffer=Buffer,routing=Routing}=State) ->
{MList,NewBuffer} = wamper_protocol:deserialize(<<Buffer/binary, Data/binary>>,Enc),
{ok,OutFrames,NewRouting} = handle_messages(MList,[],Routing,State),
{reply, OutFrames, State#state{buffer=NewBuffer,routing=NewRouting}};
websocket_handle(Data, State) ->
erlang:error(unsupported,[Data, State]),
{ok, State}.

websocket_info(erwa_stop, State) ->
{stop, State};
websocket_info({erwa,Msg}, #state{routing=Routing,ws_enc=WsEnc,enc=Enc}=State) when is_tuple(Msg)->
Encode = fun(M) ->
{WsEnc,wamper_protocol:serialize(M,Enc)}
end,
case erwa_routing:handle_info(Msg, Routing) of
{ok, NewRouting} ->
{ok, State#state{routing=NewRouting}};
{send, OutMsg, NewRouting} ->
{reply, Encode(OutMsg), State#state{routing=NewRouting}};
{send_stop, OutMsg, NewRouting} ->
self() ! erwa_stop,
{reply, Encode(OutMsg), State#state{routing=NewRouting}};
{stop, NewRouting} ->
{stop, State#state{routing=NewRouting}}
end;
websocket_info(_Data, State) ->
{ok, State}.

%% ********************************************************
%% for all cowboy versions
%% ********************************************************
terminate(_Reason, _Req, _State) ->
ok.


handle_messages([],ToSend,Routing,_State) ->
{ok,lists:reverse(ToSend),Routing};
handle_messages([Msg|Tail],ToSend,Routing,#state{ws_enc=WsEnc,enc=Enc}=State) ->
Expand Down
4 changes: 2 additions & 2 deletions src/erwa_invocation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ realm_to_tablename(Realm) ->
binary_to_atom(<< Prefix/binary, Realm/binary >>, utf8 ).

gen_id() ->
crypto:rand_uniform(0,9007199254740993).

%%crypto:rand_uniform(0,9007199254740993).
rand:uniform(9007199254740993).

%%
%% ********** UNIT TESTING *************************
Expand Down
3 changes: 2 additions & 1 deletion src/erwa_publications.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ get_pub_id() ->


new_pub_id() ->
ID = crypto:rand_uniform(0,9007199254740993),
%%ID = crypto:rand_uniform(0,9007199254740993),
ID = rand:uniform(9007199254740993),
Data = #erwa_pub_rec{id=ID},
Trans = fun() ->
case mnesia:read({erwa_pub_rec, ID}) of
Expand Down
3 changes: 2 additions & 1 deletion src/erwa_sess_man.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ create_session() ->
Res.

ensure_unique_id() ->
ID = crypto:rand_uniform(0,9007199254740993),
%%ID = crypto:rand_uniform(0,9007199254740993),
ID = rand:uniform(9007199254740993),
case mnesia:read({erwa_session_record,ID}) of
[] -> ID;
_ -> ensure_unique_id()
Expand Down
2 changes: 1 addition & 1 deletion src/erwa_user_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ start_link() ->


-spec allow_anonymous(Realm :: binary(), Transport::atom()) -> true | false.
allow_anonymous(Realm,Transport) ->
allow_anonymous(_Realm,_Transport) ->
%% case can_join(anonymous, Realm, Transport) of
%% {true,_} -> true;
%% _ -> false
Expand Down