%%-----------------------------------------------------------------------------
%% module  :       unode.erl
%% @author  :       J
%% email   :       j-som@foxmail.com
%% Created :       2018-01-02
%% description:    节点
%%-----------------------------------------------------------------------------

-module(unode).
-export([
        apply/4
        ,is_my_node/1
    ]).
-include("common.hrl").

apply(undefined, Mod, Fun, Args) ->
    erlang:apply(Mod, Fun, Args);

apply(none, Mod, Fun, Args) ->
    erlang:apply(Mod, Fun, Args);

%% 错误调试
apply(_Node, lib_server_send, send_to_sid, [Sid, _Bin]) when (not is_pid(Sid)) ->
    ?ERR("unode apply send_to_sid/2: error sid:~w, CmdInfo:~w,~nerror:~p",
        [Sid, lib_server_send:unpack_cmd(_Bin), ?CURR_STACK()]
    );
apply(Node, Mod, Fun, Args) ->
    case node() of
        Node ->
            erlang:apply(Mod, Fun, Args);
        _ ->
            rpc:cast(Node, Mod, Fun, Args)
    end.

is_my_node(Node) ->
    case Node of
        undefined ->
            true;
        none ->
            true;
        _ ->
            node() =:= Node
    end.
