function res = UR_comms(action,params,mess)
% UR_comms - handle communications (as messages)
% On input:
%     action (int): service action:
%       1: directory look up
%       2: send
%       3: receive
%       4: register
%     params (list): depend on action
%        action             params
%      ---------------------------------------
%       directory  ['flight_manager','comms']
%        send      [my_comm,other_comm]
%        receive   [my_comm,other_comm]
%       register   [my_name]
%     mess (message type): depends on action
%       .type (int): 1: USS2FM
%                    2: FM2USS
%       .code (int): sender's comm ID
%       .action (int): 1: register
%                      2: request
%       .data: depends on type, code and action
%            type code action    data
%          ---------------------------------------------
%  register   1    -      1     (string): USS_name
%  request    1    -      2     [USS_ID,speed,op_id,pf_id,
%                                launch_vertex,land_vertex,
%                                t_start,t_end,req_no]
%          ----------------------------------------------
%             2  FM_code  1     USS_index in FM
%             2           2     <flight_plan>
% On output:
%     res (int): depends on action
%      directory:  flight_manager or comms ID
%      register: number of registered entities
% Call:
%     UR_comms(action,params,mess); % Note: most calls don't use res
% Author:
%     T. Henderson
%     University of Utah
%     Summer 2019
%

persistent first messages num_messages clients num_clients

if isempty(first)
    first = 0;
    messages = [];
    num_messages = 0;
    clients(1).code = 1;
    clients(1).name = 'UR_FM';
    clients(2).code = 2;
    clients(2).name = 'UR_comms';
    num_clients = 2;
end

FLIGHT_MANAGER = 1;
COMMS = 2;
SERVICE1 = 'flight-manager';
SERVICE2 = 'comms';
DIRECTORY = 1;
SEND = 2;
RECEIVE = 3;
REGISTER = 4;

res = [];

%action
%params
%mess

switch action
    case DIRECTORY
        code = 0;
        switch params
            case SERVICE1  % flight management
                code = FLIGHT_MANAGER;
            case SERVICE2 % comms
                code = COMMS;
        end
        res = code;
    case SEND
        num_messages = num_messages + 1;
        messages(num_messages).sender = params(1);
        messages(num_messages).receiver = params(2);
        messages(num_messages).mess = mess;
    case RECEIVE
        num_received = 0;
        received = [];
        for m = 1:num_messages
            if messages(m).receiver==params(1)
                num_received = num_received + 1;
                res(num_received).sender = messages(m).sender;
                res(num_received).mess = messages(m).mess;
                received = [received;m];
            end
        end
        if ~isempty(received)
            messages(received) = [];
            num_messages = num_messages - length(received);
        end
    case REGISTER
        for c = 1:num_clients
            if strcmp(clients(c).name,params)
                res = c;
                return
            end
        end
        num_clients = num_clients + 1;
        clients(num_clients).code = num_clients;
        clients(num_clients).name = params;
        res = num_clients;
end
tch = 0;
