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