package com.jscape.inet.ssh.protocol.v2.messages;

import com.jscape.inet.ssh.protocol.messages.Message;
import com.jscape.util.net.connection.MessageConnection;

import static com.jscape.util.Parameters.assertGreater;
import static com.jscape.util.Parameters.assertGreaterOrEqual;

/**
 * @author Alex Usun
 */
public class SshMsgChannelOpenConfirmation
        extends Message<SshMsgChannelOpenConfirmation.Handler> {

    public final int recipientChannel;
    public final int senderChannel;
    public final long initialWindowSize;
    public final int maxPacketSize;

    public SshMsgChannelOpenConfirmation(int recipientChannel, int senderChannel, long initialWindowSize, int maxPacketSize) {
        this.recipientChannel = recipientChannel;
        this.senderChannel = senderChannel;

        assertGreaterOrEqual(initialWindowSize, 0, "Bad initial window size.");
        this.initialWindowSize = initialWindowSize;

        assertGreater(maxPacketSize, 0, "Bad max packet size.");
        this.maxPacketSize = maxPacketSize;
    }

    @Override
    public Class handlerClass() {
        return Handler.class;
    }

    @Override
    public void accept(Handler handler, MessageConnection<Message> connection) {
        handler.handle(this, connection);
    }

    @Override
    public String toString() {
        return "SshMsgChannelOpenConfirmation {"
                + "recipientChannel=" + this.recipientChannel
                + ", senderChannel=" + this.senderChannel
                + ", initialWindowSize=" + this.initialWindowSize
                + ", maxPacketSize=" + this.maxPacketSize
                + '}';
    }

    public interface Handler
            extends Message.HandlerBase {

        void handle(SshMsgChannelOpenConfirmation message, MessageConnection<Message> connection);

    }
}
