Last active
February 13, 2017 13:56
-
-
Save olivermt/9784ae731f348db592ae3908e715e99c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Core.SyslogService do | |
use GenServer | |
require Logger | |
def start_link(ref, socket, transport, opts \\ []) do | |
Logger.debug "Trying to start_link" | |
:proc_lib.start_link(__MODULE__, :init, [ref, socket, transport, opts]) | |
end | |
def init(ref, socket, transport, opts \\ []) do | |
Logger.debug "Got start link request" | |
:ok = :proc_lib.init_ack({:ok, self()}) | |
Logger.debug "Start link request acked" | |
Process.send(self(), :accept_ack, []) | |
Logger.debug "Sent message to self to start ranch acking" | |
{:ok, %{ | |
ref: ref, | |
socket: socket, | |
transport: transport | |
}} | |
end | |
def handle_info(:accept_ack, state = %{ref: ref, transport: transport, socket: socket}) do | |
Logger.debug "Accepting ack and setting state to :once" | |
:ok = :ranch.accept_ack(ref) | |
:ok = transport.setopts(socket, active: :once) | |
{:noreply, state} | |
end | |
def handle_info({:tcp, socket, msg}, state = %{transport: transport, socket: socket}) do | |
Logger.debug "Got a tcp message" | |
:ok = transport.setopts(socket, active: :once) | |
parse_line(msg) | |
{:noreply, state} | |
end | |
defp parse_line(line) do | |
Logger.debug "Got a tcp package!" | |
Logger.debug "#{inspect line}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#started with supervisor(Core.SyslogListener, []) from the main Application file | |
defmodule Core.SyslogListener do | |
require Logger | |
@port Application.get_env(:core, :syslog_port, 514) | |
def start_link do | |
opts = [port: @port] | |
Logger.debug "Setting up syslog listener at port #{@port}" | |
{:ok, _} = :ranch.start_listener(:syslog_listener, 100, :ranch_tcp, opts, Core.SyslogService, []) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output:
[debug] module=Core.SyslogListener function=start_link/0 Setting up syslog listener at port 514
Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> [debug] module=Core.SyslogService function=start_link/4 Trying to start_link
iex(1)> [debug] module=Core.SyslogService function=init/4 Got start link request
[debug] module=Core.SyslogService function=init/4 Start link request acked
[debug] module=Core.SyslogService function=init/4 Sent message to self to start ranch acking
iex(1)> [debug] module=Core.SyslogService function=start_link/4 start_link result: {:ok, #PID<0.1092.0>}
iex(1)>