%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 31. 8月 2023 13:48
%%%-------------------------------------------------------------------
-module(iot_udp_handler).
-author("aresei").

%% API
-export([start_link/2, loop/2]).

start_link(Transport, Peer) ->
    {ok, spawn_link(?MODULE, loop, [Transport, Peer])}.

loop(Transport = {udp, Server, _Sock}, Peer) ->
    receive
        {datagram, Server, <<Len:16, HostUUID:Len/binary>>} ->
            Pid = iot_host:get_pid(HostUUID),
            iot_host:heartbeat(Pid),
            loop(Transport, Peer);
        {datagram, Server, _} ->
            exit(normal)
    end.