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.echoserver; 021 022import java.net.InetSocketAddress; 023 024import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; 025import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory; 026import org.apache.mina.filter.ssl.SslFilter; 027import org.apache.mina.transport.socket.SocketAcceptor; 028import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 029 030/** 031 * (<b>Entry point</b>) Echo server 032 * 033 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 034 */ 035public class Main { 036 /** Choose your favorite port number. */ 037 private static final int PORT = 8080; 038 039 /** Set this to true if you want to make the server SSL */ 040 private static final boolean USE_SSL = false; 041 042 public static void main(String[] args) throws Exception { 043 SocketAcceptor acceptor = new NioSocketAcceptor(); 044 acceptor.setReuseAddress( true ); 045 DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); 046 047 // Add SSL filter if SSL is enabled. 048 if (USE_SSL) { 049 addSSLSupport(chain); 050 } 051 052 // Bind 053 acceptor.setHandler(new EchoProtocolHandler()); 054 acceptor.bind(new InetSocketAddress(PORT)); 055 056 System.out.println("Listening on port " + PORT); 057 058 for (;;) { 059 System.out.println("R: " + acceptor.getStatistics().getReadBytesThroughput() + 060 ", W: " + acceptor.getStatistics().getWrittenBytesThroughput()); 061 Thread.sleep(3000); 062 } 063 } 064 065 private static void addSSLSupport(DefaultIoFilterChainBuilder chain) 066 throws Exception { 067 SslFilter sslFilter = new SslFilter(BogusSslContextFactory 068 .getInstance(true)); 069 chain.addLast("sslFilter", sslFilter); 070 System.out.println("SSL ON"); 071 } 072}