All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.crossbar.autobahn.wamp.messages.Call Maven / Gradle / Ivy

The newest version!
///////////////////////////////////////////////////////////////////////////////
//
//   AutobahnJava - http://crossbar.io/autobahn
//
//   Copyright (c) Crossbar.io Technologies GmbH and contributors
//
//   Licensed under the MIT License.
//   http://www.opensource.org/licenses/mit-license.php
//
///////////////////////////////////////////////////////////////////////////////

package io.crossbar.autobahn.wamp.messages;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.crossbar.autobahn.wamp.exceptions.ProtocolError;
import io.crossbar.autobahn.wamp.interfaces.IMessage;
import io.crossbar.autobahn.wamp.utils.MessageUtil;

import static io.crossbar.autobahn.wamp.utils.Shortcuts.getOrDefault;

public class Call implements IMessage {

    public static final int MESSAGE_TYPE = 48;

    private static final int TIMEOUT_DEFAULT = 0;

    public final long request;
    public final String procedure;
    public final List args;
    public final Map kwargs;
    public final int timeout;

    public Call(long request, String procedure, List args, Map kwargs, int timeout) {
        this.request = request;
        this.procedure = procedure;
        this.args = args;
        this.kwargs = kwargs;
        if (timeout < 1) {
            this.timeout = TIMEOUT_DEFAULT;
        } else {
            this.timeout = timeout;
        }
    }

    public static Call parse(List wmsg) {
        MessageUtil.validateMessage(wmsg, MESSAGE_TYPE, "CALL", 4, 6);

        long request = MessageUtil.parseLong(wmsg.get(1));
        Map options = (Map) wmsg.get(2);
        String procedure = (String) wmsg.get(3);

        List args = null;
        if (wmsg.size() > 4) {
            if (wmsg.get(4) instanceof byte[]) {
                throw new ProtocolError("Binary payload not supported");
            }
            args = (List) wmsg.get(4);
        }

        Map kwargs = null;
        if (wmsg.size() > 5) {
            kwargs = (Map) wmsg.get(5);
        }

        int timeout = getOrDefault(options, "timeout", TIMEOUT_DEFAULT);

        return new Call(request, procedure, args, kwargs, timeout);
    }

    @Override
    public List marshal() {
        List marshaled = new ArrayList<>();
        marshaled.add(MESSAGE_TYPE);
        marshaled.add(request);
        Map options = new HashMap<>();
        if (timeout > TIMEOUT_DEFAULT) {
            options.put("timeout", timeout);
        }
        marshaled.add(options);
        marshaled.add(procedure);
        if (kwargs != null) {
            if (args == null) {
                // Empty args.
                marshaled.add(Collections.emptyList());
            } else {
                marshaled.add(args);
            }
            marshaled.add(kwargs);
        } else if (args != null) {
            marshaled.add(args);
        }
        return marshaled;
    }
}