View Javadoc

    1   /*
    2    *   Licensed to the Apache Software Foundation (ASF) under one
    3    *   or more contributor license agreements.  See the NOTICE file
    4    *   distributed with this work for additional information
    5    *   regarding copyright ownership.  The ASF licenses this file
    6    *   to you under the Apache License, Version 2.0 (the
    7    *   "License"); you may not use this file except in compliance
    8    *   with the License.  You may obtain a copy of the License at
    9    *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *   Unless required by applicable law or agreed to in writing,
    13   *   software distributed under the License is distributed on an
    14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *   KIND, either express or implied.  See the License for the
    16   *   specific language governing permissions and limitations
    17   *   under the License.
    18   *
    19   */
    20  package org.apache.mina.example.imagine.step2.server;
    21
    22  import java.io.IOException;
    23  import java.net.InetSocketAddress;
    24  import java.util.concurrent.Executors;
    25
    26  import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
    27  import org.apache.mina.example.imagine.step1.codec.ImageCodecFactory;
    28  import org.apache.mina.example.imagine.step1.server.ImageServerIoHandler;
    29  import org.apache.mina.filter.codec.ProtocolCodecFilter;
    30  import org.apache.mina.filter.executor.ExecutorFilter;
    31  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
    32
    33  /**
    34   * entry point for the server used in the tutorial on protocol codecs
    35   *
    36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    37   */
    38
    39  public class ImageServer {
    40      public static final int PORT = 33789;
    41
    42      public static void main(String[] args) throws IOException {
    43          // Create a class that handles sessions, incoming and outgoing data
    44          ImageServerIoHandler handler = new ImageServerIoHandler();
    45
    46          // This socket acceptor will handle incoming connections
    47          NioSocketAcceptor acceptor = new NioSocketAcceptor();
    48
    49          // add an IoFilter .  This class is responsible for converting the incoming and
    50          // outgoing raw data to ImageRequest and ImageResponse objects
    51          acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new ImageCodecFactory(false)));
    52
    53          // get a reference to the filter chain from the acceptor
    54          DefaultIoFilterChainBuilder filterChainBuilder = acceptor.getFilterChain();
    55
    56          // add an ExecutorFilter to the filter chain.  The preferred order is to put the executor filter
    57          // after any protocol filters due to the fact that protocol codecs are generally CPU-bound
    58          // which is the same as I/O filters.
    59          filterChainBuilder.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
    60
    61          // set this NioSocketAcceptor's handler to the ImageServerHandler
    62          acceptor.setHandler(handler);
    63
    64          // Bind to the specified address.  This kicks off the listening for
    65          // incoming connections
    66          acceptor.bind(new InetSocketAddress(PORT));
    67          System.out.println("Step 2 server is listenig at port " + PORT);
    68      }
    69  }