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

import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.Uint32Codec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgChannelOpen;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgChannelOpenDirectTcpIp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Alex Usun
 */
public class SshMsgChannelOpenDirectTcpIpCodec
        implements SshMsgChannelOpenCodec.TypeCodec {

    @Override
    public SshMsgChannelOpen read(InputStream in, int senderChannel, long initialWindowSize, int maxPacketSize)
            throws IOException {
        String host = StringCodec.readUsAsciiValue(in);
        int port = Uint32Codec.readValue(in);
        String originatorAddress = StringCodec.readUsAsciiValue(in);
        int originatorPort = Uint32Codec.readValue(in);

        return new SshMsgChannelOpenDirectTcpIp(senderChannel, initialWindowSize, maxPacketSize, host, port, originatorAddress, originatorPort);
    }

    @Override
    public void write(SshMsgChannelOpen value, OutputStream out)
            throws IOException {
        SshMsgChannelOpenDirectTcpIp message = (SshMsgChannelOpenDirectTcpIp) value;
        StringCodec.writeUsAsciiValue(message.host, out);
        Uint32Codec.writeValue(message.port, out);
        StringCodec.writeUsAsciiValue(message.originatorAddress, out);
        Uint32Codec.writeValue(message.originatorPort, out);
    }
}
