M4TNRFRPHEH6T673JAMJ3CHABASCWMAJVU57HH2XEMDJCB3QPT5QC DZZ4B3UGIYTN3OHAKS2HNCLK7KM2ZSHPZ4JC6YVQL6I2H4KCD5PAC PFC6VDDZ55HDVMZYGJJOBUA4PTKDYIOBV7GKHT2FCWUJGXHXAPCAC 755UGKECZ3PFYEA2TFFOUZTF27CRQTBZWO7UYFU6WQDJMZDBVPRAC CZG24QKIB45EL7PK4UA6XVG3VEVKI7OAQXN4YMFUKKI2MXLHRANQC YWCRGWVDIMCPXBQFM23MKCYZCXJJSD733NJEPB6WUU4G4BSREILAC JGA5PAC4QBPH62IPO4HPJ6ETR6BYZDR6SKE6NOTOP3YZVM4XY62AC WHYWDNSL67TTB2AGAUACXERJ6X54YMNYVC32KJQFEXFFRYA4K3HQC IHBNW3GI2XB6KAWUYRLL6KDOBUNUOU3N7RRLS6BFNW6SE7ZDHLWQC 55WLMLEEVBRSTAFRZ5RGF7TOGUF5OPVCPA2TMHAQK45OUO7PA3YQC YDZ3R5NY7II7WX4Q5O2UQ72TMCABDPPRLEMTTIBH5DGKOLAOPOPAC Handler = spawn_link(fun() -> event_loop(State) end),ListenProc = spawn_link(fun() -> listen_loop(Handler, Lis) end),
Handler = spawn(fun() -> event_loop(State) end),io:format("[DEBUG] Event Loop: ~p~n", [Handler]),ListenProc = spawn(fun() -> listen_loop(Handler, Lis) end),io:format("[DEBUG] Socket listener: ~p~n", [ListenProc]),
ConnReader = spawn(fun() -> decode_loop(Handler, Sock) end),%% TODO: read docs of this linkageok = gen_tcp:controlling_process(Sock, ConnReader),{ok, {RemoteIp, _}} = inet:peername(Sock),io:format("[Peer] Incoming Connection ~p (~p)~n", [RemoteIp, Sock]),
{ok, {RemoteIp, Port}} = inet:peername(Sock),io:format("[Peer] Incoming Connection ~p:~p (~p)~n", [RemoteIp, Port, Sock]),
FilterReqs = fun(ReqId) -> not maps:is_key(ReqId, PeersRequestIds) end,NewIn = lists:filter(FilterReqs, ActiveIn),NewOut = lists:filter(FilterReqs, ActiveOut),
RmActives = fun(ReqId, _) -> not maps:is_key(ReqId, PeersRequestIds) end,NewIn = maps:filter(RmActives, ActiveIn),NewOut = maps:filter(RmActives, ActiveOut),
io:format("[DEBUG] new message Type:~p - Body:~p~n", [MsgType, Body]),case maps:is_key(ReqId, ActiveOut) of
IsActiveOut = maps:is_key(ReqId, ActiveOut),io:format("[DEBUG] new message Type:~p - IsActiveOut: ~p - Body:~p~n", [MsgType, IsActiveOut, Body]),case IsActiveOut of
erlang:error("TODO cancel req");
CancelId = proplists:get_value(cancelId, Body, <<>>),case maps:take(CancelId, ActiveIn) oferror -> event_loop(State); % ignore{_, NewActiveIn} ->UpdateChans = fun(_Chan, ChanReqs) ->%% we might need to put the received atom in a variable. not sure if this pattern matching works like i think it does.UpdatedReqs = lists:filter(fun({received, ChanReqId, _}) -> CancelId =/= ChanReqId end, ChanReqs),{true, UpdatedReqs}end,event_loop(State#state{peers = update_peer_received(Peers, Peer, MsgSize),channels = maps:filtermap(UpdateChans, Chans),activeIn = NewActiveIn})end;
erlang:error("TODO channel time req");
RequestedChan = proplists:get_value(channel, Body),case maps:take(RequestedChan, Chans) oferror -> event_loop(State); % ignore{ChanReqs, TmpChans} ->%% TODO: sendio:format("[TEMP] Received incoming chan time req for ~p from ~p~n", [RequestedChan, Peer]),NewChanReqs = ChanReqs ++ [{received, ReqId, Peer}],event_loop(State#state{peers = update_peer_received(Peers, Peer, MsgSize),channels = maps:put(RequestedChan, NewChanReqs, TmpChans),activeIn = maps:put(ReqId, {Peer, Msg}, ActiveIn)})end;
send_channel_list_response(Peer, ReqId, ChanList)
RxPeers = update_peer_received(Peers, Peer, MsgSize),{ok, {_, _, ResponseSize}} = send_channel_list_response(Peer, ReqId, ChanList),RTxPeers = update_peer_sent(RxPeers, Peer, ResponseSize),event_loop(State#state{peers = RTxPeers});Unhandled ->io:format("[Warning] unhandled incoming message of type ~p that isn't state:activeOut - RequestId:~p~n", [Unhandled, ReqIdStr]),event_loop(State)
[Tx, Rx] = maps:get(bytesTransmitted, PeerMeta),NewPeers = Peers#{Peer => maps:update(bytesTransmitted, [Tx+Size, Rx], PeerMeta)},
{Tx, Rx} = maps:get(bytesTransmitted, PeerMeta),NewPeers = Peers#{Peer => maps:update(bytesTransmitted, {Tx+Size, Rx}, PeerMeta)},
PeerMeta = maps:get(Peer, Peers),[Tx, Rx] = maps:get(bytesTransmitted, PeerMeta),Peers#{Peer => PeerMeta#{bytesTransmitted => [Tx, Rx+Size],lastSeenAt => os:system_time(1000)}}.
case maps:take(Peer, Peers) oferror -> throw({peerNotActive, Peer});{PeerMeta, PeersSansPeer} ->{Tx, Rx} = maps:get(bytesTransmitted, PeerMeta),UpdatedMeta = PeerMeta#{bytesTransmitted => {Tx, Rx+Size}, lastSeenAt => os:system_time(1000)},maps:put(Peer, UpdatedMeta, PeersSansPeer)end.
io_lib:format("~.2f ~s", [float(S), M]).
lists:flatten(io_lib:format("~.1f~s", [float(S), M])).-ifdef(TEST).-include_lib("eunit/include/eunit.hrl").human_bytesize_test() ->?assertEqual("81.0mb", human_bytesize(84912712)),?assertEqual("512.0b", human_bytesize(512)),?assertEqual("1.0kb", human_bytesize(1024)).-endif.