some stupid bug

This commit is contained in:
Peter Harpending
2026-09-02 04:44:11 -07:00
parent 17cca5ecfe
commit ede7d8e977
4 changed files with 153 additions and 30 deletions
+41 -20
View File
@@ -5,9 +5,10 @@
% ho from pimp pov
-record(pho,
{pid :: pid(),
ref :: reference(),
name :: string()}).
{pid = none :: none | pid(),
ref = none :: none | reference(),
taken = false :: boolean(),
name = none :: none | string()}).
%% pimp state
-record(ps,
@@ -25,10 +26,9 @@
main([]) ->
io:format("RBX example 4 telnet echo server daemon~n"
"`one' part of the many-to-one relationship~n",
[]),
p_init().
p_init(6969);
main([PortNum]) ->
p_init(list_to_integer(PortNum)).
noise_options() ->
@@ -43,9 +43,8 @@ noise_options() ->
% pimp process startup
p_init() ->
p_init(Port) ->
p_logln("pimp startup (p_init)"),
Port = 6969,
Opts = [binary, {packet, 0}, {active, once}],
case gen_tcp:listen(Port, Opts) of
{ok, LSock} ->
@@ -60,12 +59,12 @@ p_init() ->
p_loop(PS = #ps{hos = Hos}) ->
receive
{ho, _HoPid, taken} ->
NewPS = p_spawn_ho(PS),
{ho, HoPid, taken} ->
NewPS = p_taken(HoPid, PS),
p_loop(NewPS);
{ho, HoPid, sent, Data} ->
HoName = p_ho_name(HoPid, PS),
SendMessage = io_lib:format("~ts :: ~ts\r\n", [HoName, Data]),
SendMessage = io_lib:format("~ts> ~ts\r\n", [HoName, Data]),
ok = p_echo_except(HoPid, HoName, SendMessage, Hos),
p_loop(PS);
Down = {'DOWN', _, _, _, _} ->
@@ -76,6 +75,20 @@ p_loop(PS = #ps{hos = Hos}) ->
p_loop(PS)
end.
p_taken(Pid, PS = #ps{hos = Hos}) ->
Hos_II = mark_taken(Pid, Hos, []),
PS_II = PS#ps{hos = Hos_II},
PS_III = p_spawn_ho(PS_II),
PS_III.
mark_taken(Pid, [Ho = #pho{pid = Pid} | Rest], Acc) ->
NewHo = Ho#pho{taken = true},
lists:reverse(Acc) ++ [NewHo | Rest];
mark_taken(Pid, [WrongHo | Rest], Acc) ->
mark_taken(Pid, Rest, [WrongHo | Acc]);
mark_taken(_, [], Acc) ->
lists:reverse(Acc).
% given pid of ho, get her name
p_ho_name(HoPid, #ps{hos = Hos}) ->
case lists:keyfind(HoPid, #pho.pid, Hos) of
@@ -100,7 +113,8 @@ p_spawn_ho(PS = #ps{lsock = LSock, hos = Hos, ho_names = Names}) ->
% ho down, remove her from roster
p_ho_down({'DOWN', _Ref, process, Pid, Info}, PS = #ps{hos = Hos}) ->
ok = p_logfln("ho down: ~tp; reason: ~tw", [Pid, Info]),
Ho = lists:keyfind(Pid, #pho.pid, Hos),
ok = p_logfln("ho down: ~ts; reason: ~tw", [Ho#pho.name, Info]),
NewHos = lists:keydelete(Pid, #pho.pid, Hos),
NewPS = PS#ps{hos = NewHos},
NewPS.
@@ -110,7 +124,10 @@ p_ho_down({'DOWN', _Ref, process, Pid, Info}, PS = #ps{hos = Hos}) ->
% skip if equal
p_echo_except(SrcPid, SrcName, Data, [#pho{pid = SrcPid} | Rest]) ->
p_echo_except(SrcPid, SrcName, Data, Rest);
p_echo_except(SrcPid, SrcName, Data, [#pho{pid = DstPid} | Rest]) ->
% skip if not taken
p_echo_except(SrcPid, SrcName, Data, [#pho{taken = false} | Rest]) ->
p_echo_except(SrcPid, SrcName, Data, Rest);
p_echo_except(SrcPid, SrcName, Data, [#pho{pid = DstPid, taken = true} | Rest]) ->
DstPid ! {pimp, send, Data},
p_echo_except(SrcPid, SrcName, Data, Rest);
p_echo_except(_, _, _, []) ->
@@ -147,7 +164,8 @@ h_loop(HS = #hs{name = Name, daddy = Daddy, asock = ASock, nsock = NSock}) ->
ok = h_rcvd(Name, Data_II),
Daddy ! {ho, self(), sent, Data_II},
h_loop(HS);
{pimp, send, Data} ->
{pimp, send, Data} = M ->
io:format("~p received ~p~n", [self(), M]),
ok = h_send(Name, NSock, Data),
h_loop(HS);
{tcp_closed, ASock} ->
@@ -160,7 +178,7 @@ h_loop(HS = #hs{name = Name, daddy = Daddy, asock = ASock, nsock = NSock}) ->
% pimp log in bold
p_logln(LogStr) ->
Msg = io_lib:format("!! Daddy :: ~ts~n", [LogStr]),
Msg = io_lib:format("Daddy: ~ts~n", [LogStr]),
PimpChars = ?ANSI_BOLD([Msg]),
io:put_chars(PimpChars).
@@ -170,7 +188,7 @@ p_logfln(LogStr, Args) ->
% ho log dimmed
h_logln(HoName, LogStr) ->
FMsg = io_lib:format("!! ~ts :: ~ts~n", [HoName, LogStr]),
FMsg = io_lib:format("~ts: ~ts~n", [HoName, LogStr]),
HoChars = ?ANSI_DIM([FMsg]),
io:put_chars(HoChars).
@@ -179,12 +197,15 @@ h_logfln(HoName, MsgStr, Args) ->
% normal: john messages
h_rcvd(Name, MsgStr) ->
Msg = io_lib:format("<< ~ts :: ~ts~n", [Name, MsgStr]),
Msg = io_lib:format("~ts< ~ts~n", [Name, string:chomp(MsgStr)]),
io:put_chars(Msg).
h_send(Name, NSock, Msg) ->
ok = io:format(">> ~ts :: ~ts~n", [Name, Msg]),
enoise:send(NSock, Msg).
FMsg = io_lib:format("~ts> ~ts~n", [Name, string:chomp(Msg)]),
HoChars = [?ANSI_DIM(FMsg)],
ok = io:put_chars(HoChars),
Send = unicode:characters_to_binary([string:chomp(Msg), "\r\n"]),
ok = enoise:send(NSock, Send).
init_ho_names() ->