retab
Still setting this computer up...
This commit is contained in:
parent
4d7dcf160c
commit
587fa1710c
24
src/msp.erl
24
src/msp.erl
@ -9,18 +9,18 @@ start() -> application:start(hakuzaru).
|
||||
stop() -> application:stop(hakuzaru).
|
||||
|
||||
start(normal, _Args) ->
|
||||
{ok, Sup} = msp_sup:start_link(),
|
||||
case init:get_plain_arguments() of
|
||||
[_] ->
|
||||
io:format("MSP started as idle process.~n");
|
||||
[_, "test"] ->
|
||||
io:format("Running tests.~n", []),
|
||||
msp_tests:spawn_tests();
|
||||
[_ | CLArgs] ->
|
||||
io:format("Unknown args ~p~n", [CLArgs])
|
||||
end,
|
||||
{ok, Sup}.
|
||||
{ok, Sup} = msp_sup:start_link(),
|
||||
case init:get_plain_arguments() of
|
||||
[_] ->
|
||||
io:format("MSP started as idle process.~n");
|
||||
[_, "test"] ->
|
||||
io:format("Running tests.~n", []),
|
||||
msp_tests:spawn_tests();
|
||||
[_ | CLArgs] ->
|
||||
io:format("Unknown args ~p~n", [CLArgs])
|
||||
end,
|
||||
{ok, Sup}.
|
||||
|
||||
stop(_State) ->
|
||||
ok.
|
||||
ok.
|
||||
|
||||
|
||||
@ -26,10 +26,10 @@
|
||||
-type channel() :: pid().
|
||||
|
||||
-record(s,
|
||||
{socket :: gen_udp:socket(),
|
||||
peer :: endpoint(),
|
||||
side :: 0 | 1,
|
||||
connections = #{} :: #{integer() => channel()}}).
|
||||
{socket :: gen_udp:socket(),
|
||||
peer :: endpoint(),
|
||||
side :: 0 | 1,
|
||||
connections = #{} :: #{integer() => channel()}}).
|
||||
|
||||
-type state() :: none | #s{}.
|
||||
|
||||
@ -39,15 +39,15 @@
|
||||
% socket, and a host that knows you will be talking MSP, then a
|
||||
% new msp_connection can be initialized.
|
||||
begin_msp(Connection, OurSock, TheirIP, TheirPort, OurSide) ->
|
||||
% Transfer the socket to the gen_server. If it is
|
||||
% active then a bunch of messages will be received
|
||||
% at once, but that is fine.
|
||||
case gen_udp:controlling_process(OurSock, Connection) of
|
||||
ok ->
|
||||
gen_server:cast(Connection, {begin_msp, OurSock, {TheirIP, TheirPort}, OurSide});
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
% Transfer the socket to the gen_server. If it is
|
||||
% active then a bunch of messages will be received
|
||||
% at once, but that is fine.
|
||||
case gen_udp:controlling_process(OurSock, Connection) of
|
||||
ok ->
|
||||
gen_server:cast(Connection, {begin_msp, OurSock, {TheirIP, TheirPort}, OurSide});
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
|
||||
%%% gen_server
|
||||
|
||||
@ -70,16 +70,16 @@ handle_call(Unexpected, From, State) ->
|
||||
|
||||
|
||||
handle_cast({begin_msp, Sock, Peer, Side}, none) ->
|
||||
State = do_begin_msp(Sock, Peer, Side),
|
||||
{noreply, State};
|
||||
State = do_begin_msp(Sock, Peer, Side),
|
||||
{noreply, State};
|
||||
handle_cast(Unexpected, State) ->
|
||||
ok = log(warning, "Unexpected cast: ~tp", [Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_info({udp, Sock, IP, Port, Packet}, State = #s{socket = Sock, peer = {IP, Port}}) ->
|
||||
NewState = do_dispatch(State, Packet),
|
||||
{noreply, NewState};
|
||||
NewState = do_dispatch(State, Packet),
|
||||
{noreply, NewState};
|
||||
handle_info(Unexpected, State) ->
|
||||
ok = io:format(warning, "Unexpected info: ~tp", [Unexpected]),
|
||||
{noreply, State}.
|
||||
@ -105,14 +105,14 @@ terminate(_, _) ->
|
||||
%%% Doer Functions
|
||||
|
||||
do_begin_msp(Sock, Peer, Side) ->
|
||||
ok = inet:setopts(Sock, [{active, once}]),
|
||||
State = #s{socket = Sock,
|
||||
peer = Peer,
|
||||
side = Side},
|
||||
State.
|
||||
ok = inet:setopts(Sock, [{active, once}]),
|
||||
State = #s{socket = Sock,
|
||||
peer = Peer,
|
||||
side = Side},
|
||||
State.
|
||||
|
||||
do_dispatch(State = #s{socket = Sock}, Packet) ->
|
||||
io:format("Got data: ~p~n", [Packet]),
|
||||
ok = inet:setopts(Sock, [{active, once}]),
|
||||
State.
|
||||
io:format("Got data: ~p~n", [Packet]),
|
||||
ok = inet:setopts(Sock, [{active, once}]),
|
||||
State.
|
||||
|
||||
|
||||
@ -5,28 +5,28 @@
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]).
|
||||
|
||||
-record(s,
|
||||
{}).
|
||||
{}).
|
||||
|
||||
-type state() :: #s{}.
|
||||
|
||||
start_link() ->
|
||||
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
||||
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
||||
|
||||
-spec init(none) -> {ok, state()}.
|
||||
|
||||
init(none) ->
|
||||
ok = io:format("msp_man starting.~n", []),
|
||||
State = #s{},
|
||||
{ok, State}.
|
||||
ok = io:format("msp_man starting.~n", []),
|
||||
State = #s{},
|
||||
{ok, State}.
|
||||
|
||||
handle_call(_, _, State) ->
|
||||
{reply, ok, State}.
|
||||
{reply, ok, State}.
|
||||
|
||||
handle_cast(_, State) ->
|
||||
{noreply, State}.
|
||||
{noreply, State}.
|
||||
|
||||
handle_info(Unexpected, State) ->
|
||||
ok = io:format("Warning: Unexpected info ~p~n", [Unexpected]),
|
||||
{noreply, State}.
|
||||
ok = io:format("Warning: Unexpected info ~p~n", [Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
|
||||
@ -5,14 +5,14 @@
|
||||
-export([init/1]).
|
||||
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
init([]) ->
|
||||
RestartStrategy = {one_for_one, 0, 60},
|
||||
Children = [{msp_man,
|
||||
{msp_man, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[msp_man]}],
|
||||
{ok, {RestartStrategy, Children}}.
|
||||
RestartStrategy = {one_for_one, 0, 60},
|
||||
Children = [{msp_man,
|
||||
{msp_man, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[msp_man]}],
|
||||
{ok, {RestartStrategy, Children}}.
|
||||
|
||||
@ -2,41 +2,41 @@
|
||||
-export([spawn_tests/0, run_tests_and_halt/0, run_tests_protected/0, run_tests/0]).
|
||||
|
||||
spawn_tests() ->
|
||||
spawn(?MODULE, run_tests_and_halt, []),
|
||||
ok.
|
||||
spawn(?MODULE, run_tests_and_halt, []),
|
||||
ok.
|
||||
|
||||
run_tests_and_halt() ->
|
||||
Result = run_tests_protected(),
|
||||
io:format("Tests returned ~p~n", [Result]),
|
||||
halt().
|
||||
Result = run_tests_protected(),
|
||||
io:format("Tests returned ~p~n", [Result]),
|
||||
halt().
|
||||
|
||||
run_tests_protected() ->
|
||||
try
|
||||
run_tests()
|
||||
catch
|
||||
_:Reason:Stack -> {error, Reason, Stack}
|
||||
end.
|
||||
try
|
||||
run_tests()
|
||||
catch
|
||||
_:Reason:Stack -> {error, Reason, Stack}
|
||||
end.
|
||||
|
||||
run_tests() ->
|
||||
ok = send_test(),
|
||||
ok.
|
||||
ok = send_test(),
|
||||
ok.
|
||||
|
||||
make_connection(OurPort, TheirIP, TheirPort, Side) ->
|
||||
{ok, Sock} = gen_udp:open(OurPort),
|
||||
{ok, Pid} = msp_connection:start_link(),
|
||||
ok = msp_connection:begin_msp(Pid, Sock, TheirIP, TheirPort, Side),
|
||||
{Pid, Sock}.
|
||||
{ok, Sock} = gen_udp:open(OurPort),
|
||||
{ok, Pid} = msp_connection:start_link(),
|
||||
ok = msp_connection:begin_msp(Pid, Sock, TheirIP, TheirPort, Side),
|
||||
{Pid, Sock}.
|
||||
|
||||
|
||||
send_test() ->
|
||||
IP = {127, 0, 0, 1},
|
||||
PortA = 5555,
|
||||
PortB = 6666,
|
||||
{A, SockA} = make_connection(PortA, IP, PortB, 0),
|
||||
{B, SockB} = make_connection(PortB, IP, PortA, 1),
|
||||
gen_udp:send(SockA, {IP, PortB}, <<"message sent from A to B">>),
|
||||
gen_udp:send(SockB, {IP, PortA}, <<"message sent from B to A">>),
|
||||
timer:sleep(10),
|
||||
ok.
|
||||
IP = {127, 0, 0, 1},
|
||||
PortA = 5555,
|
||||
PortB = 6666,
|
||||
{A, SockA} = make_connection(PortA, IP, PortB, 0),
|
||||
{B, SockB} = make_connection(PortB, IP, PortA, 1),
|
||||
gen_udp:send(SockA, {IP, PortB}, <<"message sent from A to B">>),
|
||||
gen_udp:send(SockB, {IP, PortA}, <<"message sent from B to A">>),
|
||||
timer:sleep(10),
|
||||
ok.
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user