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

io.crossbar.autobahn.wamp.messages.Event 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;

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

public class Event implements IMessage {

    public static final int MESSAGE_TYPE = 36;

    public final long subscription;
    public final long publication;
    public final String topic;
    public final boolean retained;
    public final List args;
    public final Map kwargs;

    public Event(long subscription, long publication, String topic, boolean retained, List args, Map kwargs) {
        this.subscription = subscription;
        this.publication = publication;
        this.topic = topic;
        this.retained = retained;
        this.args = args;
        this.kwargs = kwargs;
    }

    public static Event parse(List wmsg) {
        MessageUtil.validateMessage(wmsg, MESSAGE_TYPE, "EVENT", 3, 6);
        long subscription = MessageUtil.parseLong(wmsg.get(1));
        long publication = MessageUtil.parseLong(wmsg.get(2));

        Map details = (Map) wmsg.get(3);
        String topic = (String)details.get("topic");
        boolean retained = getOrDefault(details, "retained", false);

        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);
        }

        return new Event(subscription, publication, topic, retained, args, kwargs);
    }

    @Override
    public List marshal() {
        List marshaled = new ArrayList<>();
        marshaled.add(MESSAGE_TYPE);
        marshaled.add(subscription);
        marshaled.add(publication);
        Map details = new HashMap<>();
        if (topic != null) {
            details.put("topic", topic);
        }
        if (retained) {
            details.put("retained", retained);
        }
        marshaled.add(details);
        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;
    }
}