Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283%% Local launcher ports and socket relay. The driver remains the only owner%% of protocol state; this module only forwards framed bytes and exit status.-module(albedo_python_launch).-export([cold/1, relay/3, relay_loop/3]).
-define(GONE, 3).
%% Owns a local kernel's socket for Owner and speaks as a bridge port would:%% {self(), {data, Frame}} and {self(), {exit_status, Status}} go out,%% {Owner, {command, Frame}} and {Owner, close} come in. Starting a kernel%% waits until it listens, without a bridge process.relay(Owner, Socket, Start) -> Watch = monitor(process, Owner), Status = Start(), Connected = Status =:= 0 andalso gen_tcp:connect({local, Socket}, 0, [local, binary, {packet, 4}, {active, true}]), case Connected of {ok, Connection} -> relay_loop(Owner, Watch, Connection); false -> Owner ! {self(), {exit_status, Status}}; {error, Gone} when Gone =:= enoent; Gone =:= econnrefused; Gone =:= enotdir -> Owner ! {self(), {exit_status, ?GONE}}; {error, _} -> Owner ! {self(), {exit_status, 1}} end.
relay_loop(Owner, Watch, Connection) -> receive {tcp, Connection, Data} -> Owner ! {self(), {data, Data}}, relay_loop(Owner, Watch, Connection); {Owner, {command, Data}} -> _ = gen_tcp:send(Connection, Data), relay_loop(Owner, Watch, Connection); {tcp_closed, Connection} -> Owner ! {self(), {exit_status, 0}}; {tcp_error, Connection, _} -> Owner ! {self(), {exit_status, 0}}; {Owner, close} -> gen_tcp:close(Connection); {'DOWN', Watch, process, Owner, _} -> gen_tcp:close(Connection) after albedo_clock:delay(100) -> erlang:hibernate(?MODULE, relay_loop, [Owner, Watch, Connection]) end.
%% Starts the kernel itself, the token as its one line of stdin. The kernel%% closes its stdout once it listens (or exits); 0 then means "try the socket".%% No exit_status: with it, erts holds the eof until the kernel exits.cold({Python, Args, Cwd, Token}) -> Executable = albedo_process_name:python(Python, "kernel"), try open_port( {spawn_executable, binary_to_list(Executable)}, [ binary, eof, use_stdio, hide, {args, [binary_to_list(A) || A <- Args]}, {cd, binary_to_list(Cwd)}, {env, albedo_python:clean_environment()} ] ) of Port -> port_command(Port, [Token, $\n]), launched(Port) catch _:_ -> 1 end.
launched(Port) -> receive {Port, eof} -> catch port_close(Port), 0; {Port, {data, _}} -> launched(Port) end.