Created
December 18, 2016 06:17
-
-
Save jeremytregunna/1f0476dec71ee8ea0886ec0c35ac61c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Junction.Receiver do | |
| require Logger | |
| use GenServer | |
| alias Junction.Types.Log | |
| def start_link(opts \\ []) do | |
| GenServer.start_link(__MODULE__, :ok, opts) | |
| end | |
| def parse_packet(data) do | |
| << | |
| key :: bytes-size(16), | |
| length :: little-unsigned-integer-size(32), | |
| message :: binary | |
| >> = data | |
| message = %Log{ | |
| key: UUID.binary_to_string!(key), | |
| length: length, | |
| message: message | |
| } | |
| end | |
| def init(:ok) do | |
| {:ok, _socket} = :gen_udp.open(11110) | |
| end | |
| def handle_info({:udp, _socket, _ip, _port, raw}, state) do | |
| Logger.info "Raw: " <> inspect(raw) | |
| message = parse_packet(raw) | |
| Logger.info "Received a secret message! " <> inspect(message) | |
| {:noreply, state} | |
| end | |
| def handle_info({_, _socket}, state) do | |
| {:noreply, state} | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment