Skip to content

Instantly share code, notes, and snippets.

@pareeohnos
Created July 5, 2016 10:13
Show Gist options
  • Select an option

  • Save pareeohnos/91622f980636c758cc86f5538e151da5 to your computer and use it in GitHub Desktop.

Select an option

Save pareeohnos/91622f980636c758cc86f5538e151da5 to your computer and use it in GitHub Desktop.
defmodule MyServer do
def ack(conn, frame) do
GenServer.cast(conn, { :acknowledge, frame })
end
def register_callback(conn, destination, callback) do
GenServer.call(conn, { :register_callback, destination, callback })
end
def handle_cast({ :acknowledge, frame }, %{ sock: sock } = state) do
ack_frame = FB.build_frame("ACK", %{
id: frame.headers["message-id"]
})
FH.send_frame(sock, ack_frame)
{ :noreply, state }
end
def handle_info({ :tcp, _, data}, %{ sock: sock, callbacks: callbacks } = state) do
frame = FH.parse_frame(data)
destination = frame.headers["destination"]
Dict.get(callbacks, destination, [])
|> Enum.map(fn(func) -> func.(frame) end)
:inet.setopts(sock, active: :once)
{ :noreply, state }
end
end
test = fn (msg)->
IO.puts "HELLO"
MyServer.ack(conn, msg)
end
MyServer.register_callback conn, "/queue/testing", test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment