/** 
 * 
 * Copyright 2004 Hiram Chirino
 * 
 * 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 org.activeio.filter;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.activeio.AcceptListener;
import org.activeio.AsynchChannelServer;
import org.activeio.Channel;
import org.activeio.ChannelRequestTestSupport;
import org.activeio.ChannelServer;
import org.activeio.RequestChannel;
import org.activeio.RequestListener;
import org.activeio.SynchChannelFactory;
import org.activeio.adapter.AsynchChannelToClientRequestChannel;
import org.activeio.adapter.AsynchChannelToServerRequestChannel;
import org.activeio.adapter.SynchToAsynchChannelAdapter;
import org.activeio.adapter.SynchToAsynchChannelServerAdapter;
import org.activeio.net.SocketMetadata;
import org.activeio.net.SocketSynchChannelFactory;

/**
 */
public class PacketAggregatingChannelFilterTest extends ChannelRequestTestSupport {
    
    private URI serverURI;

    protected ChannelServer createChannelServer(final RequestListener requestListener) throws IOException, URISyntaxException {

        SynchChannelFactory factory = new SocketSynchChannelFactory();
        
        AsynchChannelServer server = new SynchToAsynchChannelServerAdapter(factory.bindSynchChannel(new URI("tcp://localhost:0")));
        
        server.setAcceptListener(new AcceptListener() {
            public void onAccept(Channel channel) {
            	
                RequestChannel requestChannel = null;
                try {
                		((SocketMetadata)channel.narrow(SocketMetadata.class)).setTcpNoDelay(true);
                    requestChannel = 
                        new AsynchChannelToServerRequestChannel(
                            new PacketAggregatingAsynchChannel(
                                    SynchToAsynchChannelAdapter.adapt(channel)));

                    requestChannel.setRequestListener(requestListener);
                    requestChannel.start();

                } catch (IOException e) {
                    if (requestChannel != null)
                        requestChannel.dispose();
                    else
                        channel.dispose();
                }
            }

            public void onAcceptError(IOException error) {
                error.printStackTrace();
            }
        });
        serverURI = server.getConnectURI();
        return server;
    }
    
    /**
     * @return
     * @throws IOException
     */
    protected RequestChannel createClientRequestChannel() throws IOException {
        SynchChannelFactory factory = new SocketSynchChannelFactory();
        PacketAggregatingSynchChannel channel = new PacketAggregatingSynchChannel(factory.openSynchChannel(serverURI));
		((SocketMetadata)channel.narrow(SocketMetadata.class)).setTcpNoDelay(true);
        return new AsynchChannelToClientRequestChannel(channel);
    }
    
}
