All I want it to do is emulate the following netcat TCP command, which is working properly from my command line:
$ echo "|c country_US" | nc 10.247.4.104 26542
0.500000 <-- response
To accomplish this I have an Elixir module that uses GenServer and :gen_tcp as follows (it has a few extra IO.puts
and IO.inspect
calls so I can watch progress):
defmodule VowpalWabbex do
use GenServer
def predict(pid, client_data) do
GenServer.call(pid, {:predict, client_data})
end
@initial_state %{socket: nil}
def start_link do
GenServer.start_link(__MODULE__, @initial_state)
end
def init(state) do
IO.puts "VowpalWabbex - init..."
opts = [:binary, :inet, active: false, packet: :line]
{:ok, socket} = :gen_tcp.connect({10, 247, 4, 104}, 26542, opts)
IO.inspect socket
{:ok, %{state | socket: socket}}
end
def handle_call({:predict_adx_inflation, client_data}, _from, %{socket: socket} = state) do
IO.inspect client_data
IO.inspect socket
:ok = :gen_tcp.send(socket, client_data)
IO.puts :ok
{:ok, msg} = :gen_tcp.recv(socket, 0)
{:reply, msg, state}
end
end
I would use this module from within IEX as follows:
$ iex -S mix
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Compiling 1 file (.ex)
iex(1)> {:ok, pid} = VowpalWabbex.start_link
VowpalWabbex - init...
#Port<0.6774>
{:ok, #PID<0.238.0>}
iex(2)> VowpalWabbex.predict(pid, "|c country_US")
"|c country_US"
#Port<0.6774>
ok
** (exit) exited in: GenServer.call(#PID<0.238.0>, {:predict, "data"}, 5000)
** (EXIT) time out
So apparently I get an āokā response when I send the data, but then the I never get a response and instead I time out in a couple of seconds.
Am I doing something wrong in how Iām trying to receive the response?
Thanks much!