I need an erlang application to read and write on a named pipe.
Opening a named pipe as a file will result in an error with eisdir .
I wrote the following module, but it is fragile and feels wrong in many ways. Also after a while he does not read. Is there any way to make it more ... elegant?
-module(port_forwarder). -export([start/2, forwarder/2]). -include("logger.hrl"). start(From, To)-> spawn(fun() -> forwarder(From, To) end). forwarder(FromFile, ToFile) -> To = open_port({spawn,"/bin/cat > " ++ ToFifo}, [binary, out, eof,{packet, 4}]), From = open_port({spawn,"/bin/cat " ++ FromFifo}, [binary, in, eof, {packet, 4}]), forwarder(From, To, nil). forwarder(From, To, Pid) -> receive {Manager, {command, Bin}} -> ?ERROR("Sending : ~p", [Bin]), To ! {self(), {command, Bin}}, forwarder(From, To, Manager); {From ,{data,Data}} -> Pid ! {self(), {data, Data}}, forwarder(From, To, Pid); E -> ?ERROR("Quitting, first message not understood : ~p", [E]) end.
As you may have noticed, it mimics the port format in what it accepts or returns. I want it to replace the C code, which will read the other ends of the pipes and run from the debugger.