import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class ReactorFrameClient {
    public static void main(String[] args) throws Exception {
        int port = args.length > 0 ? Integer.parseInt(args[0]) : 9000;
        byte[][] messages = {new byte[12_000], new byte[0], new byte[65_536]};
        for (int index = 0; index < messages.length; index++) Arrays.fill(messages[index], (byte) index);
        try (Socket socket = connect(port)) {
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());
            for (byte[] message : messages) {
                byte[] header = ByteBuffer.allocate(4).putInt(message.length).array();
                for (byte value : header) {
                    output.write(value);
                    output.flush();
                    Thread.sleep(10);
                }
                for (int offset = 0; offset < message.length; offset += 997) {
                    output.write(message, offset, Math.min(997, message.length - offset));
                }
            }
            socket.shutdownOutput();
            DataInputStream input = new DataInputStream(socket.getInputStream());
            for (byte[] message : messages) {
                int length = input.readInt();
                if (length != message.length) throw new IllegalStateException("length mismatch");
                byte[] reply = new byte[length];
                input.readFully(reply);
                if (!Arrays.equals(message, reply)) throw new IllegalStateException("body mismatch");
            }
            if (input.read() != -1) throw new IllegalStateException("expected EOF");
            System.out.println("fragmented frames, 12000-byte body, zero body, max body and half-close: PASS");
        }
        rejects(port, ByteBuffer.allocate(4).putInt(65_537).array());
        rejects(port, new byte[]{0, 0});
        try (Socket socket = connect(port)) {
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());
            for (int index = 0; index < 1000; index++) {
                output.writeInt(1);
                output.writeByte(index);
            }
            socket.shutdownOutput();
            DataInputStream input = new DataInputStream(socket.getInputStream());
            for (int index = 0; index < 1000; index++) {
                if (input.readInt() != 1 || input.readUnsignedByte() != (index & 255)) {
                    throw new IllegalStateException("pipeline mismatch");
                }
            }
            if (input.read() != -1) throw new IllegalStateException("expected EOF");
            System.out.println("1000 pipelined frames after invalid peers: PASS");
        }
    }

    private static Socket connect(int port) throws IOException {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress("127.0.0.1", port), 2000);
            socket.setSoTimeout(5000);
            return socket;
        } catch (IOException failure) {
            socket.close();
            throw failure;
        }
    }

    private static void rejects(int port, byte[] invalid) throws IOException {
        try (Socket socket = connect(port)) {
            socket.getOutputStream().write(invalid);
            socket.shutdownOutput();
            try {
                if (socket.getInputStream().read() != -1) throw new IllegalStateException("invalid frame accepted");
            } catch (java.net.SocketException expected) {
                System.out.println("invalid peer reset=" + expected.getClass().getSimpleName());
            }
            System.out.println("invalid peer closed: PASS");
        }
    }
}
