diff --git a/x03-echo/echod.erl b/x03-echo/echod.erl index c438788..116a6d9 100644 --- a/x03-echo/echod.erl +++ b/x03-echo/echod.erl @@ -244,6 +244,5 @@ init_ho_names() -> "Delight", "Capri", "Trixie", "Cinnamon"]. - recycle([N | Ns]) -> {N, Ns ++ [N]}. diff --git a/x04-noisy-echo/nechoc b/x04-noisy-echo/nechoc new file mode 100755 index 0000000..d721f4f --- /dev/null +++ b/x04-noisy-echo/nechoc @@ -0,0 +1,36 @@ +#!/usr/bin/env escript + +-mode(compile). + +% john state +-record(js, + {tsock = none :: gen_tcp:socket(), + nsock = none :: enoise:noise_socket()}). + + +main([]) -> + TcpOpts = [binary, {packet, 0}, {active, once}], + {ok, TSock} = gen_tcp:connect("localhost", 6969, TcpOpts), + ok = inet:setopts(TSock, [{active, once}]), + {ok, NSock, _HandshakeState} = enoise:connect(TSock, noise_options()), + j_loop(#js{tsock = TSock, nsock = NSock}). + +noise_options() -> + % totally safe crypto + KP = enoise_keypair:new(dh25519), + [{noise, "Noise_NN_25519_ChaChaPoly_BLAKE2b"}, + {e, KP}]. + + +j_loop(JS = #js{nsock = NSock}) -> + receive + {noise, NSock, Bytes} -> + ok = io:format("< ~tp~n", [Bytes]), + j_loop(JS); + Unknown -> + ok = io:format("~tp unknown message, killing self: ~tp~n", + [self(), Unknown]), + exit(normal) + end. + + diff --git a/x04-noisy-echo/nechoc.erl b/x04-noisy-echo/nechoc.erl deleted file mode 100644 index 5c99506..0000000 --- a/x04-noisy-echo/nechoc.erl +++ /dev/null @@ -1,30 +0,0 @@ -% @doc echo server client --module(nechoc). - --export([main/0]). - -main() -> - {ok, TcpSocket} = gen_tcp:connect("localhost", 6969, [binary, {packet, 0}]), - ok = inet:setopts(Socket, [{active, once}]), - {ok, NoiseSock, _HandshakeState} = enoise:connect(TcpSocket, noise_options()), - loop(Socket). - -noise_options() -> - % totally safe crypto - KP = enoise_keypair:new(dh25519), - [{noise, "Noise_NN_25519_ChaChaPoly_BLAKE2b"}, - {e, KP}]. - - -j_loop(NSock) -> - ok = inet:setopts(NSock, [{active, once}]), - receive - {tcp, Socket, Bytes} -> - ok = io:format("< ~tp~n", [Bytes]), - loop(Socket); - {tcp_closed, Socket} -> - ok = io:format("connection terminated~n", []), - exit(normal) - end. - - diff --git a/x04-noisy-echo/nechod.erl b/x04-noisy-echo/nechod old mode 100644 new mode 100755 similarity index 75% rename from x04-noisy-echo/nechod.erl rename to x04-noisy-echo/nechod index dcc02ca..4c8be2e --- a/x04-noisy-echo/nechod.erl +++ b/x04-noisy-echo/nechod @@ -1,65 +1,7 @@ -%% @doc -%% super simple telnet echo server daemon -%% -%% no noise yet -%% -%% pimps and hos model (from trash talk template): -%% -%% ``` -%% t_sup -%% | -%% t_clients -%% | -%% +--- t_client_man pimp -%% | -%% +--- t_client_sup bottom bitch -%% | -%% +--- <0.1.0> (t_client) ho -%% +--- <0.2.0> (t_client) ho -%% +--- <0.3.0> (t_client) ho -%% ''' -%% -%% each ho: -%% -%% -%% -%% ``` -%% start(ListenSocket) -> -%% t_client_sup:start_acceptor(ListenSocket). -%% -%% start_link(ListenSocket) -> -%% proc_lib:start_link(?MODULE, init, [self(), ListenSocket]). -%% -%% init(Parent, ListenSocket) -> -%% ok = io:format("~p Listening.~n", [self()]), -%% Debug = sys:debug_options([]), -%% ok = proc_lib:init_ack(Parent, {ok, self()}), -%% listen(Parent, Debug, ListenSocket). -%% -%% listen(Parent, Debug, ListenSocket) -> -%% case gen_tcp:accept(ListenSocket) of -%% {ok, Socket} -> -%% {ok, _} = start(ListenSocket), -%% {ok, Peer} = inet:peername(Socket), -%% ok = io:format("~p Connection accepted from: ~p~n", [self(), Peer]), -%% ok = t_client_man:enroll(), -%% State = #s{socket = Socket}, -%% loop(Parent, Debug, State); -%% {error, closed} -> -%% ok = io:format("~p Retiring: Listen socket closed.~n", [self()]), -%% exit(normal) -%% end. -%% ''' -%% -%% @end --module(echod). +#!/usr/bin/env escript +-mode(compile). -include("./ansi.hrl"). --export([main/0]). - % ho from pimp pov -record(pho, @@ -78,10 +20,11 @@ -record(hs, {name = none :: string(), daddy = none :: pid(), + asock = none :: gen_tcp:socket(), nsock = none :: enoise:noise_socket()}). -main() -> +main([]) -> io:format("RBX example 4 telnet echo server daemon~n" "`one' part of the many-to-one relationship~n", []), @@ -174,37 +117,38 @@ p_echo_except(_, _, _, []) -> ok. %% runs in ho context -h_init(LSock, HS = #hs{name = Name, daddy = Daddy, nsock = none}) -> +h_init(LSock, HS = #hs{name = Name, daddy = Daddy, asock = none, nsock = none}) -> h_logln(Name, "h_init"), % this is key: accept needs to run in ho context, otherwise she % doesn't own the acceptor socket % % blocks until client connects - case enoise:accept(LSock, noise_options()) of - {ok, NSock, _} -> + case gen_tcp:accept(LSock) of + {ok, ASock} -> h_logln(Name, "taken"), % tell daddy to spawn the next ho Daddy ! {ho, self(), taken}, + {ok, NSock, _} = enoise:accept(ASock, noise_options()), Welcome = io_lib:format("HI! I'm ~ts. What's your name?\r\n", [Name]), ok = h_send(Name, NSock, Welcome), - HS_II = HS#hs{nsock = ASock}, + HS_II = HS#hs{asock = ASock, nsock = NSock}, h_loop(HS_II); {error, closed} -> h_logln(Name, "didn't get taken") end. -h_loop(HS = #hs{name = Name, daddy = Daddy, nsock = ASock}) -> +h_loop(HS = #hs{name = Name, daddy = Daddy, asock = ASock, nsock = NSock}) -> ok = inet:setopts(ASock, [{active, once}]), receive - {tcp, ASock, Data} -> + {noise, NSock, Data} -> Data_II = string:chomp(Data), ok = h_rcvd(Name, Data_II), Daddy ! {ho, self(), sent, Data_II}, h_loop(HS); {pimp, send, Data} -> - ok = h_send(Name, ASock, Data), + ok = h_send(Name, NSock, Data), h_loop(HS); {tcp_closed, ASock} -> exit(its_fine); @@ -238,9 +182,9 @@ h_rcvd(Name, MsgStr) -> Msg = io_lib:format("<< ~ts :: ~ts~n", [Name, MsgStr]), io:put_chars(Msg). -h_send(Name, Socket, Msg) -> +h_send(Name, NSock, Msg) -> ok = io:format(">> ~ts :: ~ts~n", [Name, Msg]), - enoise:send(Socket, Msg). + enoise:send(NSock, Msg). init_ho_names() -> diff --git a/x04-noisy-echo/scratch.erl b/x04-noisy-echo/scratch.txt similarity index 100% rename from x04-noisy-echo/scratch.erl rename to x04-noisy-echo/scratch.txt