<commit_before>%  Copyright 2007-2008 Konrad-Zuse-Zentrum f�r Informationstechnik Berlin
%
%   Licensed under the Apache License, Version 2.0 (the "License");
%   you may not use this file except in compliance with the License.
%   You may obtain a copy of the License at
%
%       http://www.apache.org/licenses/LICENSE-2.0
%
%   Unless required by applicable law or agreed to in writing, software
%   distributed under the License is distributed on an "AS IS" BASIS,
%   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%   See the License for the specific language governing permissions and
%   limitations under the License.
%%%-------------------------------------------------------------------
%%% File    : vivaldi_latency.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Description : vivaldi helper module for measuring latency between nodes
%%%
%%% Created :  8 July 2009 by <NAME> <<EMAIL>>
%%%-------------------------------------------------------------------
%% @author <NAME> <<EMAIL>>
%% @copyright 2009 Konrad-Zuse-Zentrum f�r Informationstechnik Berlin
%% @version $Id$
-module(vivaldi_latency).

-author('<EMAIL>').
-vsn('$Id$ ').

-behaviour(gen_component).

-export([measure_latency/3]).

-export([on/2, init/1]).

% vivaldi types
-type(latency() :: float()).

% state of the vivaldi loop
-type(state() :: {cs_send:mypid(),
                  cs_send:mypid(),
                  any(),
                  {integer(), integer(), integer()},
                  [latency()]}).


% accepted messages of vivaldi_latency processes
-type(message() :: any()).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Message Loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @doc message handler
-spec(on/2 :: (Message::message(), State::state()) -> state()).
on({pong, _}, {Owner, RemoteNode, Token, Start, Latencies}) ->
    Stop = erlang:now(),
    NewLatencies = [timer:now_diff(Stop, Start)| Latencies],
    case length(NewLatencies) == config:read(vivaldi_count_measurements, 4) of
        true ->
            cs_send:send_local(Owner,{update_vivaldi_coordinate, calc_latency(NewLatencies), Token}),
            exit;
        false ->
            cs_send:send_after(config:read(vivaldi_measurements_delay),
                              self(),
                              {start_ping}),
            {Owner, RemoteNode, Token, 0, NewLatencies}
    end;

on({start_ping}, {Owner, RemoteNode, Token, _, Latencies}) ->
    cs_send:send(RemoteNode, {ping, cs_send:this()}),
    {Owner, RemoteNode, Token, erlang:now(), Latencies};

on({shutdown}, _State) ->
    log:log(info, "shutdown vivaldi_latency", []),
    exit;

on(_, _State) ->
    unknown_event.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Init
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec(init/1 :: ([any()]) -> vivaldi:state()).
init([Owner, RemoteNode, Token]) ->
    %io:format("vivaldi_latency start ~n"),
    erlang:send_after(config:read(vivaldi_latency_timeout, 60*1000),<commit_msg>- fixed process leak in vivaldi<commit_after>%  Copyright 2007-2008 Konrad-Zuse-Zentrum f�r Informationstechnik Berlin
%
%   Licensed under the Apache License, Version 2.0 (the "License");
%   you may not use this file except in compliance with the License.
%   You may obtain a copy of the License at
%
%       http://www.apache.org/licenses/LICENSE-2.0
%
%   Unless required by applicable law or agreed to in writing, software
%   distributed under the License is distributed on an "AS IS" BASIS,
%   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%   See the License for the specific language governing permissions and
%   limitations under the License.
%%%-------------------------------------------------------------------
%%% File    : vivaldi_latency.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Description : vivaldi helper module for measuring latency between nodes
%%%
%%% Created :  8 July 2009 by <NAME> <<EMAIL>>
%%%-------------------------------------------------------------------
%% @author <NAME> <<EMAIL>>
%% @copyright 2009 Konrad-Zuse-Zentrum f�r Informationstechnik Berlin
%% @version $Id$
-module(vivaldi_latency).

-author('<EMAIL>').
-vsn('$Id$ ').

-behaviour(gen_component).

-export([measure_latency/3]).

-export([on/2, init/1]).

% vivaldi types
-type(latency() :: float()).

% state of the vivaldi loop
-type(state() :: {cs_send:mypid(),
                  cs_send:mypid(),
                  any(),
                  {integer(), integer(), integer()},
                  [latency()]}).


% accepted messages of vivaldi_latency processes
-type(message() :: any()).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Message Loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @doc message handler
-spec(on/2 :: (Message::message(), State::state()) -> state()).
on({pong, _}, {Owner, RemoteNode, Token, Start, Latencies}) ->
    Stop = erlang:now(),
    NewLatencies = [timer:now_diff(Stop, Start)| Latencies],
    case length(NewLatencies) == config:read(vivaldi_count_measurements, 4) of
        true ->
            cs_send:send_local(Owner,{update_vivaldi_coordinate, calc_latency(NewLatencies), Token}),
            kill;
        false ->
            cs_send:send_after(config:read(vivaldi_measurements_delay),
                              self(),
                              {start_ping}),
            {Owner, RemoteNode, Token, 0, NewLatencies}
    end;

on({start_ping}, {Owner, RemoteNode, Token, _, Latencies}) ->
    cs_send:send(RemoteNode, {ping, cs_send:this()}),
    {Owner, RemoteNode, Token, erlang:now(), Latencies};

on({shutdown}, _State) ->
    log:log(info, "shutdown vivaldi_latency", []),
    kill;

on(_, _State) ->
    unknown_event.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Init
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec(init/1 :: ([any()]) -> vivaldi:state()).
init([Owner, RemoteNode, Token]) ->
    %io:format("vivaldi_latency start ~n"),
    erlang:send_after(config:read(vivaldi_latency_timeout, 60*1000),
{'fraction_non_alphanumeric': 0.27248086734693877, 'fraction_numerical': 0.010841836734693877, 'mean_word_length': 3.1625746516257465, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '777771', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1714, 'n_words': 450}