<commit_before>{#-
 #  Jinja2 template for generating Java client stubs Legato APIs.
 #
 #  Note: Java comments apply to the generated code.  For example this template itself is not
 #  autogenerated, but the comment is copied verbatim into the generated file when the template is
 #  expanded.
 #
 #  Copyright (C) Sierra Wireless Inc.
 #}
{%- import "pack.templ" as pack with context %}
// Generated client implementation of the API {{apiName}}.
// This is a generated file, do not edit.

package io.legato.api.implementation;

import java.math.BigInteger;
import java.io.FileDescriptor;
import java.util.Arrays;
import java.lang.AutoCloseable;
import io.legato.Ref;
import io.legato.Result;
import io.legato.OnOff;
import io.legato.SafeRef;
import io.legato.Message;
import io.legato.MessageEvent;
import io.legato.Protocol;
import io.legato.MessageBuffer;
import io.legato.ClientSession;
import io.legato.IntType;
{%- for import in imports %}
import io.legato.api.{{import}};
{%- else %}
{% endfor %}
import io.legato.api.{{apiName}};

public class {{apiName}}Client implements AutoCloseable, {{apiName}}
{
    private static final String protocolIdStr = "{{idString}}";
    private static final String serviceInstanceName = "{{apiName}}";
    /// Max message size.  Id (4 bytes) + max packed function call size
    private static final int maxMsgSize = 4 + {{messageSize}};

    private class HandlerMapper
    {
        public Object handler;
        public long serverRef;
        public boolean isOneShot;

        public HandlerMapper(Object newHandler, boolean newIsOneShot)
        {
            handler = newHandler;
            isOneShot = newIsOneShot;
            serverRef = 0;
        }
    }

    private ClientSession session;
    private SafeRef<HandlerMapper> handlerMap;

    public {{apiName}}Client()
    {
        session = null;
        handlerMap = new SafeRef<HandlerMapper>();
    }

    public void open()
    {
        open(serviceInstanceName);
    }

    public void open(String serviceName)
    {
        session = new ClientSession(new Protocol(protocolIdStr, maxMsgSize), serviceName);

        session.setReceiveHandler(
            new MessageEvent()
            {
                public void handle(Message message)
                {
                    OnServerMessageReceived(message);
                }
            });

        session.open();
    }

    @Override
    public void close()
    {
        if (session != null)
        {
            session.close();
            session = null;
        }
    }
    {%- for handler in types if handler is HandlerType %}

    private void handle{{handler.name}}(MessageBuffer buffer)
    {
        long handlerId = buffer.readLongRef();
        HandlerMapper mapper = handlerMap.get(handlerId);
        {{handler.name}} handler = ({{handler.name}})mapper.handler;
        {%- for parameter in handler.parameters %}
        {{parameter.apiType|FormatType}} _{{parameter.name}} =
            {#- #} {{pack.UnpackValue(parameter.apiType, "_"+parameter.name)}};
        {%- endfor %}

        handler.handle(
            {%- for parameter in handler.parameters -%}
            _{{parameter.name}}{% if not loop.last %}, {% endif -%}
            {%- endfor %});

        if (mapper.isOneShot)
        {<commit_msg>[Java codegen] Handle arrays in Handler parameters<commit_after>{#-
 #  Jinja2 template for generating Java client stubs Legato APIs.
 #
 #  Note: Java comments apply to the generated code.  For example this template itself is not
 #  autogenerated, but the comment is copied verbatim into the generated file when the template is
 #  expanded.
 #
 #  Copyright (C) Sierra Wireless Inc.
 #}
{%- import "pack.templ" as pack with context %}
// Generated client implementation of the API {{apiName}}.
// This is a generated file, do not edit.

package io.legato.api.implementation;

import java.math.BigInteger;
import java.io.FileDescriptor;
import java.util.Arrays;
import java.lang.AutoCloseable;
import io.legato.Ref;
import io.legato.Result;
import io.legato.OnOff;
import io.legato.SafeRef;
import io.legato.Message;
import io.legato.MessageEvent;
import io.legato.Protocol;
import io.legato.MessageBuffer;
import io.legato.ClientSession;
import io.legato.IntType;
{%- for import in imports %}
import io.legato.api.{{import}};
{%- else %}
{% endfor %}
import io.legato.api.{{apiName}};

public class {{apiName}}Client implements AutoCloseable, {{apiName}}
{
    private static final String protocolIdStr = "{{idString}}";
    private static final String serviceInstanceName = "{{apiName}}";
    /// Max message size.  Id (4 bytes) + max packed function call size
    private static final int maxMsgSize = 4 + {{messageSize}};

    private class HandlerMapper
    {
        public Object handler;
        public long serverRef;
        public boolean isOneShot;

        public HandlerMapper(Object newHandler, boolean newIsOneShot)
        {
            handler = newHandler;
            isOneShot = newIsOneShot;
            serverRef = 0;
        }
    }

    private ClientSession session;
    private SafeRef<HandlerMapper> handlerMap;

    public {{apiName}}Client()
    {
        session = null;
        handlerMap = new SafeRef<HandlerMapper>();
    }

    public void open()
    {
        open(serviceInstanceName);
    }

    public void open(String serviceName)
    {
        session = new ClientSession(new Protocol(protocolIdStr, maxMsgSize), serviceName);

        session.setReceiveHandler(
            new MessageEvent()
            {
                public void handle(Message message)
                {
                    OnServerMessageReceived(message);
                }
            });

        session.open();
    }

    @Override
    public void close()
    {
        if (session != null)
        {
            session.close();
            session = null;
        }
    }
    {%- for handler in types if handler is HandlerType %}

    private void handle{{handler.name}}(MessageBuffer buffer)
    {
        long handlerId = buffer.readLongRef();
        HandlerMapper mapper = handlerMap.get(handlerId);
        {{handler.name}} handler = ({{handler.name}})mapper.handler;
        {%- for parameter in handler.parameters %}
        {%- if parameter is ArrayParameter %}
        {{parameter.apiType|FormatBoxedType}}[] _{{parameter.name}} = new {{parameter.apiType|FormatBoxedType}}[buffer.readInt()];
        if (_{{parameter.name}}.length > {{parameter.maxCount}}) {
            throw new IllegalStateException("Invalid size for parameter: {{parameter.name}}");
        }
        for (int i = 0; i < _{{parameter.name}}.length; i++) {
            _{{parameter.name}}[i] = {{pack.UnpackValue(parameter.apiType, parameter.name)}};
        }
        {%- else %}
        {{parameter.apiType|FormatType}} _{{parameter.name}} =
            {#- #} {{pack.UnpackValue(parameter.apiType, "_"+parameter.name)}};
        {%- endif %}
        {%- endfor %}

        handler.handle(
            {%- for parameter in handler.parameters -%}
            _{{parameter.name}}{% if not loop.last %}, {% endif -%}
            {%- endfor %});

        if (mapper.isOneShot)
        {
{'fraction_non_alphanumeric': 0.11623051224944321, 'fraction_numerical': 0.0012527839643652562, 'mean_word_length': 2.513447432762836, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '4944781', 'n_tokens_mistral': 2072, 'n_tokens_neox': 1961, 'n_words': 546}