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 client side.
 */
public class POEClientParser implements MessageListener {

    private OrderAccepted orderAccepted;
    private OrderRejected orderRejected;
    private OrderExecuted orderExecuted;
    private OrderCanceled orderCanceled;
    private BrokenTrade   brokenTrade;

    private POEClientListener listener;

    /**
     * Create a parser for inbound messages on the client side.
     *
     * @param listener the message listener
     */
    public POEClientParser(POEClientListener listener) {
        this.orderAccepted = new OrderAccepted();
        this.orderRejected = new OrderRejected();
        this.orderExecuted = new OrderExecuted();
        this.orderCanceled = new OrderCanceled();
        this.brokenTrade   = new BrokenTrade();

        this.listener = listener;
    }

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

        switch (messageType) {
        case MESSAGE_TYPE_ORDER_ACCEPTED:
            orderAccepted(buffer);
            break;
        case MESSAGE_TYPE_ORDER_REJECTED:
            orderRejected(buffer);
            break;
        case MESSAGE_TYPE_ORDER_EXECUTED:
            orderExecuted(buffer);
            break;
        case MESSAGE_TYPE_ORDER_CANCELED:
            orderCanceled(buffer);
            break;
        case MESSAGE_TYPE_BROKEN_TRADE:
            brokenTrade(buffer);
            break;
        default:
            throw new POEException("Unknown message type: " + (char)messageType);
        }
    }

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

        listener.orderAccepted(orderAccepted);
    }

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

        listener.orderRejected(orderRejected);
    }

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

        listener.orderExecuted(orderExecuted);
    }

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

        listener.orderCanceled(orderCanceled);
    }

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

        listener.brokenTrade(brokenTrade);
    }

}

@@ -1250,16 +1250,20 @@
 Accepted
+.get
 (buffer)
@@ -1280,89 +1280,44 @@
     
-break;%0A        case MESSAGE_TYPE_ORDER_REJECTED:%0A            orderRejected(buffer
+listener.orderAccepted(orderAccepted
 );%0A 
@@ -1370,13 +1370,13 @@
 DER_
-EX
+REJ
 EC
-U
 TED:
@@ -1393,24 +1393,28 @@
    order
-Ex
+Rej
 ec
-u
 ted
+.get
 (buffer)
@@ -1431,299 +1431,195 @@
     
-break;%0A        case MESSAGE_TYPE_ORDER_CANCELED:%0A            orderCanceled(buffer);%0A            break;%0A        case MESSAGE_TYPE_BROKEN_TRADE:%0A            brokenTrade(buffer);%0A            break;%0A        default:%0A            throw new POEException(%22Unknown message type: %22 + (char)messageType
+listener.orderRejected(orderRejected);%0A            break;%0A        case MESSAGE_TYPE_ORDER_EXECUTED:%0A            orderExecuted.get(buffer);%0A            listener.orderExecuted(orderExecuted
 );%0A 
@@ -1629,88 +1629,65 @@
     
-%7D%0A
     
-%7D%0A%0A    private void orderAccepted(ByteBuffer buffer) throws IOException %7B%0A
+break;%0A        case MESSAGE_TYPE_ORDER_CANCELED:%0A    
     
@@ -1687,38 +1687,38 @@
            order
-Accept
+Cancel
 ed.get(buffer);%0A
@@ -1709,33 +1709,36 @@
 ed.get(buffer);%0A
-%0A
+    
         listener
@@ -1739,38 +1739,38 @@
 stener.order
-Accept
+Cancel
 ed(order
 Accepted);%0A 
@@ -1749,38 +1749,38 @@
 erCanceled(order
-Accept
+Cancel
 ed);%0A    %7D%0A%0A    
@@ -1776,264 +1776,86 @@
     
-%7D%0A%0A
     
-private void orderRejected(ByteBuffer buffer) throws IOException %7B%0A        orderRejected.get(buffer);%0A%0A        listener.orderRejected(orderRejected);
+    break;%0A        case MESSAGE_TYPE_BROKEN_TRADE:
 %0A    
-%7D%0A%0A
     
-private void orderExecuted(ByteBuffer buffer) throws IOException %7B%0A        orderExecuted
+    brokenTrade
 .get
@@ -1860,33 +1860,34 @@
 et(buffer);%0A
-%0A
+  
         
 listener.ord
@@ -1866,32 +1866,34 @@
 fer);%0A          
+  
 listener.orderEx
@@ -1889,317 +1889,160 @@
 ner.
-orderExecuted(orderExecuted
+brokenTrade(brokenTrade
 );%0A    
-%7D%0A%0A
     
-private void orderCanceled(ByteBuffer buffer) throws IOException %7B%0A        orderCanceled.get(buffer);%0A%0A        listener.orderCanceled(orderCanceled);
+    break;%0A        default:
 %0A    
-%7D%0A%0A
     
-private void brokenTrade(ByteBuffer buffer) throws IOException %7B%0A        brokenTrade.get(buffer
+    throw new POEException(%22Unknown message type: %22 + (char)messageType
 );%0A
-%0A
         
 list
@@ -2041,42 +2041,9 @@
     
-listener.brokenTrade(brokenTrade);
+%7D
 %0A   

Simplify POE client parser in network protocols