package org.jvirtanen.parity.net.poe;

import static org.jvirtanen.parity.net.poe.POE.*;

import com.paritytrading.nassau.MessageListener;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * A parser for inbound messages on the server side.
 */
public class POEServerParser implements MessageListener {

    private EnterOrder  enterOrder;
    private CancelOrder cancelOrder;

    private POEServerListener listener;

    /**
     * Create a parser for inbound messages on the server side.
     *
     * @param listener the message listener
     */
    public POEServerParser(POEServerListener listener) {
        this.enterOrder  = new EnterOrder();
        this.cancelOrder = new CancelOrder();

        this.listener = listener;
    }

    @Override
    public void message(ByteBuffer buffer) throws IOException {
        byte messageType = buffer.get();

        switch (messageType) {
        case MESSAGE_TYPE_ENTER_ORDER:
            enterOrder(buffer);
            break;
        case MESSAGE_TYPE_CANCEL_ORDER:
            cancelOrder(buffer);
            break;
        default:
            throw new POEException("Unknown message type: " + (char)messageType);
        }
    }

    private void enterOrder(ByteBuffer buffer) throws IOException {
        enterOrder.get(buffer);

        listener.enterOrder(enterOrder);
    }

    private void cancelOrder(ByteBuffer buffer) throws IOException {
        cancelOrder.get(buffer);

        listener.cancelOrder(cancelOrder);
    }

}

@@ -953,24 +953,28 @@
   enterOrder
+.get
 (buffer);%0A  
@@ -963,32 +963,77 @@
 er.get(buffer);%0A
+            listener.enterOrder(enterOrder);%0A
             brea
@@ -1098,16 +1098,20 @@
 celOrder
+.get
 (buffer)
@@ -1104,32 +1104,79 @@
 er.get(buffer);%0A
+            listener.cancelOrder(cancelOrder);%0A
             brea
@@ -1298,308 +1298,6 @@
  %7D%0A%0A
-    private void enterOrder(ByteBuffer buffer) throws IOException %7B%0A        enterOrder.get(buffer);%0A%0A        listener.enterOrder(enterOrder);%0A    %7D%0A%0A    private void cancelOrder(ByteBuffer buffer) throws IOException %7B%0A        cancelOrder.get(buffer);%0A%0A        listener.cancelOrder(cancelOrder);%0A    %7D%0A%0A
 %7D%0A

Simplify POE server parser in network protocols