diff --git a/Makefile b/Makefile index b7aab34..dad806e 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/examples/simple_router/Makefile b/examples/simple_router/Makefile index 3a3ed3a..805064b 100644 --- a/examples/simple_router/Makefile +++ b/examples/simple_router/Makefile @@ -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 diff --git a/examples/simple_router/simple_router.d b/examples/simple_router/simple_router.d index e69de29..004e985 100644 --- a/examples/simple_router/simple_router.d +++ b/examples/simple_router/simple_router.d @@ -0,0 +1,2 @@ + +COMPILE_FIRST += diff --git a/examples/simple_router/src/simple_router.erl b/examples/simple_router/src/simple_router.erl index a9c1e16..8c8e381 100644 --- a/examples/simple_router/src/simple_router.erl +++ b/examples/simple_router/src/simple_router.erl @@ -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}, []}}. diff --git a/src/erwa.app.src b/src/erwa.app.src index f2165dd..2332473 100644 --- a/src/erwa.app.src +++ b/src/erwa.app.src @@ -35,7 +35,9 @@ wamper, mnesia, lager, - crypto + crypto, + cowboy, + ranch ]}, {mod, {erwa_app, []}}, {env, []} diff --git a/src/erwa_broker.erl b/src/erwa_broker.erl index d170a66..5a63b97 100644 --- a/src/erwa_broker.erl +++ b/src/erwa_broker.erl @@ -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 -> diff --git a/src/erwa_dealer.erl b/src/erwa_dealer.erl index 1cc9b72..d80bf3a 100644 --- a/src/erwa_dealer.erl +++ b/src/erwa_dealer.erl @@ -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 -> @@ -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, diff --git a/src/erwa_in_ws.erl b/src/erwa_in_ws.erl index 071f391..c13cc2f 100644 --- a/src/erwa_in_ws.erl +++ b/src/erwa_in_ws.erl @@ -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). @@ -60,7 +63,9 @@ routing = undefined }). -% for cowboy 1.x +%% ******************************************************** +%% for cowboy 1.x +%% ******************************************************** init(_, Req, _ ) -> {upgrade, protocol, cowboy_websocket, Req, []}. @@ -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(<>,Enc), {ok,OutFrames,NewRouting} = handle_messages(MList,[],Routing,State), @@ -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(<>,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) -> diff --git a/src/erwa_invocation.erl b/src/erwa_invocation.erl index 256e57c..7fadbd9 100644 --- a/src/erwa_invocation.erl +++ b/src/erwa_invocation.erl @@ -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 ************************* diff --git a/src/erwa_publications.erl b/src/erwa_publications.erl index 5556ba1..f522416 100644 --- a/src/erwa_publications.erl +++ b/src/erwa_publications.erl @@ -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 diff --git a/src/erwa_sess_man.erl b/src/erwa_sess_man.erl index 373aac7..6bf9eca 100644 --- a/src/erwa_sess_man.erl +++ b/src/erwa_sess_man.erl @@ -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() diff --git a/src/erwa_user_db.erl b/src/erwa_user_db.erl index 3d996be..9715650 100644 --- a/src/erwa_user_db.erl +++ b/src/erwa_user_db.erl @@ -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