// Path: siden-core/src/main/java/ninja/siden/WebSocket.java
// public interface WebSocket {
// 
// 	default void onConnect(Connection connection) throws Exception {
// 	}
// 
// 	default void onText(String payload) throws Exception {
// 	}
// 
// 	default void onBinary(ByteBuffer[] payload) throws Exception {
// 	}
// 
// 	default void onPong(ByteBuffer[] payload) throws Exception {
// 	}
// 
// 	default void onPing(ByteBuffer[] payload) throws Exception {
// 	}
// 
// 	default void onClose(ByteBuffer[] payload) throws Exception {
// 	}
// }
// 
// Path: siden-core/src/main/java/ninja/siden/util/ExceptionalConsumer.java
// @FunctionalInterface
// public interface ExceptionalConsumer<T, EX extends Exception> {
// 
// 	void accept(T t) throws EX;
// }


import io.undertow.websockets.core.AbstractReceiveListener;
import io.undertow.websockets.core.BufferedBinaryMessage;
import io.undertow.websockets.core.BufferedTextMessage;
import io.undertow.websockets.core.WebSocketChannel;
import java.io.IOException;
import java.nio.ByteBuffer;
import ninja.siden.WebSocket;
import ninja.siden.util.ExceptionalConsumer;
import org.xnio.Pooled;
/*
 * Copyright 2014 SATO taichi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ninja.siden.internal;





/**
 * @author taichi
 */
public class ReceiveListenerAdapter extends AbstractReceiveListener {

	final WebSocket adaptee;

	public ReceiveListenerAdapter(WebSocket socket) {
		this.adaptee = socket;
	}

	@Override
	protected void onFullTextMessage(WebSocketChannel channel,
			BufferedTextMessage message) throws IOException {
		try {
			this.adaptee.onText(message.getData());
		} catch (IOException e) {
			throw e;
		} catch (Exception e) {
			throw new IOException(e);
		}
	}

	@Override
	protected void onFullBinaryMessage(WebSocketChannel channel,
			BufferedBinaryMessage message) throws IOException {
		deliver(this.adaptee::onBinary, message);
	}

// Path: siden-core/src/main/java/ninja/siden/WebSocket.java
// public interface WebSocket {
// 
// 	default void onConnect(Connection connection) throws Exception {
// 	}
// 
// 	default void onText(String payload) throws Exception {
// 	}
// 
// 	default void onBinary(ByteBuffer[] payload) throws Exception {
// 	}
// 
// 	default void onPong(ByteBuffer[] payload) throws Exception {
// 	}
// 
// 	default void onPing(ByteBuffer[] payload) throws Exception {
// 	}
// 
// 	default void onClose(ByteBuffer[] payload) throws Exception {
// 	}
// }
// 
// Path: siden-core/src/main/java/ninja/siden/util/ExceptionalConsumer.java
// @FunctionalInterface
// public interface ExceptionalConsumer<T, EX extends Exception> {
// 
// 	void accept(T t) throws EX;
// }


// Path: siden-core/src/main/java/ninja/siden/internal/ReceiveListenerAdapter.java
import io.undertow.websockets.core.AbstractReceiveListener;
import io.undertow.websockets.core.BufferedBinaryMessage;
import io.undertow.websockets.core.BufferedTextMessage;
import io.undertow.websockets.core.WebSocketChannel;
import java.io.IOException;
import java.nio.ByteBuffer;
import ninja.siden.WebSocket;
import ninja.siden.util.ExceptionalConsumer;
import org.xnio.Pooled;

/*
 * Copyright 2014 SATO taichi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ninja.siden.internal;





/**
 * @author taichi
 */
public class ReceiveListenerAdapter extends AbstractReceiveListener {

	final WebSocket adaptee;

	public ReceiveListenerAdapter(WebSocket socket) {
		this.adaptee = socket;
	}

	@Override
	protected void onFullTextMessage(WebSocketChannel channel,
			BufferedTextMessage message) throws IOException {
		try {
			this.adaptee.onText(message.getData());
		} catch (IOException e) {
			throw e;
		} catch (Exception e) {
			throw new IOException(e);
		}
	}

	@Override
	protected void onFullBinaryMessage(WebSocketChannel channel,
			BufferedBinaryMessage message) throws IOException {
		deliver(this.adaptee::onBinary, message);
	}

	void deliver(ExceptionalConsumer<ByteBuffer[], Exception> deliver,