package net.soundvibe.reacto.server.handlers;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.WebSocketFrame;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
 * @author OZY on 2016.01.21.
 */
public class WebSocketFrameHandler implements io.vertx.core.Handler<WebSocketFrame> {

    private final Consumer<Buffer> bufferConsumer;
    private final ConcurrentHashMap<Long, Buffer> buffers = new ConcurrentHashMap<>();


    public WebSocketFrameHandler(Consumer<Buffer> bufferConsumer) {
        this.bufferConsumer = bufferConsumer;
    }

    @Override
    public void handle(WebSocketFrame frame) {
        final Buffer buffer = buffers.merge(Thread.currentThread().getId(), createBuffer(frame),
                Buffer::appendBuffer);
        if (frame.isFinal()) {
            bufferConsumer.accept(buffer);
            buffers.remove(Thread.currentThread().getId());
        }
    }

    private Buffer createBuffer(WebSocketFrame frame) {
        final Buffer buffer = Buffer.buffer();
        buffer.appendBuffer(frame.binaryData());
        return buffer;
    }
}

@@ -123,55 +123,8 @@
 e;%0A%0A
-import java.util.concurrent.ConcurrentHashMap;%0A
 impo
@@ -347,80 +347,38 @@
 vate
- final ConcurrentHashMap%3CLong,
  Buffer
-%3E
  buffer
-s
  = 
-new ConcurrentHashMap%3C%3E
+Buffer.buffer
 ();%0A
@@ -573,267 +573,142 @@
     
-final Buffer buffer = buffers.merge(Thread.currentThread().getId(), createBuffer(frame),%0A                Buffer::appendBuffer);%0A        if (frame.isFinal()) %7B%0A            bufferConsumer.accept(buffer);%0A            buffers.remove(Thread.currentThread().getId()
+buffer.appendBuffer(frame.binaryData());%0A        if (frame.isFinal()) %7B%0A            try %7B%0A                bufferConsumer.accept(buffer
 );%0A 
@@ -718,196 +718,95 @@
     
-%7D%0A
     %7D
-%0A%0A    private Buffer createBuffer(WebSocketFrame frame) %7B%0A        final Buffer buffer = Buffer.buffer();%0A        buffer.appendBuffer(frame.binaryData());%0A
+ finally %7B%0A                buffer = Buffer.buffer();%0A        
     
+%7D%0A
     
-return buffer;
+    %7D
 %0A    %7D%0A
+%0A
 %7D%0A

Simplify websocket frame handler