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 The Apache MINA Project (dev@mina.apache.org)
37   * @version $Rev$, $Date$
38   */
39  
40  public class ImageServer {
41      public static final int PORT = 33789;
42  
43      public static void main(String[] args) throws IOException {
44          // Create a class that handles sessions, incoming and outgoing data
45          ImageServerIoHandler handler = new ImageServerIoHandler();
46          
47          // This socket acceptor will handle incoming connections
48          NioSocketAcceptor acceptor = new NioSocketAcceptor();
49          
50          // add an IoFilter .  This class is responsible for converting the incoming and 
51          // outgoing raw data to ImageRequest and ImageResponse objects
52          acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new ImageCodecFactory(false)));
53          
54          // get a reference to the filter chain from the acceptor
55          DefaultIoFilterChainBuilder filterChainBuilder = acceptor.getFilterChain();
56          
57          // add an ExecutorFilter to the filter chain.  The preferred order is to put the executor filter
58          // after any protocol filters due to the fact that protocol codecs are generally CPU-bound
59          // which is the same as I/O filters.
60          filterChainBuilder.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
61          
62          // set this NioSocketAcceptor's handler to the ImageServerHandler
63          acceptor.setHandler(handler);
64          
65          // Bind to the specified address.  This kicks off the listening for 
66          // incoming connections
67          acceptor.bind(new InetSocketAddress(PORT));
68          System.out.println("Step 2 server is listenig at port " + PORT);
69      }
70  }