<commit_before>defmodule Vivaldi.Peer.PingServer do
  @moduledoc """
  Responds to ping requests from other coordinates with a :pong. 
  Along with the :pong, the current coordinate state is also sent so that the other node can use it to update its coordinates. 
  """
  use GenServer

  require Logger

  alias Vivaldi.Peer.CoordinateStash

  def start_link(node_id, session_id) do
    {:ok, pid} = GenServer.start_link(__MODULE__, {node_id, session_id})
    name = get_name(node_id)
    :global.register_name(name, pid)
    {:ok, pid}
  end

  @doc """
  Client API
  """
  def ping(client_node_id, client_session_id, server_node_id, ping_id, timeout) do
    # http://bit.ly/2idCMnc
    try do
      GenServer.call(get_server_pid(server_node_id),
                    {:ping,
                     [node_id: client_node_id, session_id: client_session_id, ping_id: ping_id]},
                     timeout)
    catch
      :exit, reason ->
        Logger.warn("#{inspect reason}")
        {:error, reason}
    end
  end

  # Implementation
  def handle_call({:ping,
                   [node_id: _, session_id: other_session_id, ping_id: ping_id]},
                   _,
                  {my_node_id, my_session_id}) do
    if other_session_id == my_session_id do
      coordinate = CoordinateStash.get_coordinate(my_node_id)
      payload = [session_id: my_session_id, 
                 ping_id: ping_id,
                 node_id: my_node_id,
                 coordinate: coordinate
                ]
      {:reply, {:pong, payload}, {my_node_id, my_session_id}}
    else
      message = "session_ids don't match. Received #{other_session_id}, my_session_id is #{my_session_id}"
      Logger.error message 
      {:reply, {:pang, message}, {my_node_id, my_session_id}}
    end
  end

  def get_name(node_id) do
    :"#{node_id}-ping-server"
  end

  def get_server_pid(node_id) do
    get_name(node_id) |> :global.whereis_name()
  end
  
end<commit_msg>Ping Server - Add comments regarding remote ping call.<commit_after>defmodule Vivaldi.Peer.PingServer do
  @moduledoc """
  Responds to ping requests from other coordinates with a :pong. 
  Along with the :pong, the current coordinate state is also sent so that the other node can use it to update its coordinates. 
  """
  use GenServer

  require Logger

  alias Vivaldi.Peer.CoordinateStash

  def start_link(node_id, session_id) do
    {:ok, pid} = GenServer.start_link(__MODULE__, {node_id, session_id})
    name = get_name(node_id)
    :global.register_name(name, pid)
    {:ok, pid}
  end

  @doc """
  Client API

  **TODO**: How to avoid try catch 
  and use some version of receive... after instead, similar to http://bit.ly/2j1PuY7
  """
  def ping(client_node_id, client_session_id, server_node_id, ping_id, timeout) do
    # As shown at http://bit.ly/2idCMnc
    try do
      GenServer.call(get_server_pid(server_node_id),
                    {:ping,
                     [node_id: client_node_id, session_id: client_session_id, ping_id: ping_id]},
                     timeout)
    catch
      :exit, reason ->
        Logger.warn("#{inspect reason}")
        {:error, reason}
    end
  end

  # Implementation
  def handle_call({:ping, [node_id: _, session_id: other_session_id, ping_id: ping_id]},
                   _,
                  {my_node_id, my_session_id}) do
    if other_session_id == my_session_id do
      coordinate = CoordinateStash.get_coordinate(my_node_id)
      payload = [session_id: my_session_id, 
                 ping_id: ping_id,
                 node_id: my_node_id,
                 coordinate: coordinate
                ]
      {:reply, {:pong, payload}, {my_node_id, my_session_id}}
    else
      message = "session_ids don't match. Received #{other_session_id}, my_session_id is #{my_session_id}"
      Logger.error message 
      {:reply, {:pang, message}, {my_node_id, my_session_id}}
    end
  end

  def get_name(node_id) do
    :"#{node_id}-ping-server"
  end

  def get_server_pid(node_id) do
    get_name(node_id) |> :global.whereis_name()
  end
  
end
{'fraction_non_alphanumeric': 0.0980295566502463, 'fraction_numerical': 0.0012315270935960591, 'mean_word_length': 2.6356311548791407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3374977', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1325, 'n_words': 330}