001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.mina.example.imagine.step2.server;
021
022import java.io.IOException;
023import java.net.InetSocketAddress;
024import java.util.concurrent.Executors;
025
026import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
027import org.apache.mina.example.imagine.step1.codec.ImageCodecFactory;
028import org.apache.mina.example.imagine.step1.server.ImageServerIoHandler;
029import org.apache.mina.filter.codec.ProtocolCodecFilter;
030import org.apache.mina.filter.executor.ExecutorFilter;
031import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
032
033/**
034 * entry point for the server used in the tutorial on protocol codecs
035 *
036 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
037 */
038
039public class ImageServer {
040    public static final int PORT = 33789;
041
042    public static void main(String[] args) throws IOException {
043        // Create a class that handles sessions, incoming and outgoing data
044        ImageServerIoHandler handler = new ImageServerIoHandler();
045        
046        // This socket acceptor will handle incoming connections
047        NioSocketAcceptor acceptor = new NioSocketAcceptor();
048        
049        // add an IoFilter .  This class is responsible for converting the incoming and 
050        // outgoing raw data to ImageRequest and ImageResponse objects
051        acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new ImageCodecFactory(false)));
052        
053        // get a reference to the filter chain from the acceptor
054        DefaultIoFilterChainBuilder filterChainBuilder = acceptor.getFilterChain();
055        
056        // add an ExecutorFilter to the filter chain.  The preferred order is to put the executor filter
057        // after any protocol filters due to the fact that protocol codecs are generally CPU-bound
058        // which is the same as I/O filters.
059        filterChainBuilder.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
060        
061        // set this NioSocketAcceptor's handler to the ImageServerHandler
062        acceptor.setHandler(handler);
063        
064        // Bind to the specified address.  This kicks off the listening for 
065        // incoming connections
066        acceptor.bind(new InetSocketAddress(PORT));
067        System.out.println("Step 2 server is listenig at port " + PORT);
068    }
069}