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

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

There is a newer version: 21.7.1
Show 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;

public class Yield implements IMessage {

    public static final int MESSAGE_TYPE = 70;

    public final long request;
    public final List args;
    public final Map kwargs;

    public Yield(long request, List args, Map kwargs) {
        this.request = request;
        this.args = args;
        this.kwargs = kwargs;
    }

    public static Yield parse(List wmsg) {
        MessageUtil.validateMessage(wmsg, MESSAGE_TYPE, "YIELD", 3, 6);

        Map options = (Map) wmsg.get(2);
        List args = null;
        if (wmsg.size() > 3) {
            if (wmsg.get(3) instanceof byte[]) {
                throw new ProtocolError("Binary payload not supported");
            }
            args = (List) wmsg.get(4);
        }
        Map kwargs = null;
        if (wmsg.size() > 4) {
            kwargs = (Map) wmsg.get(4);
        }
        return new Yield(MessageUtil.parseLong(wmsg.get(1)), args, kwargs);
    }

    @Override
    public List marshal() {
        List marshaled = new ArrayList<>();
        marshaled.add(MESSAGE_TYPE);
        marshaled.add(request);
        // Empty options.
        marshaled.add(Collections.emptyMap());
        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;
    }
}