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.gettingstarted.timeserver; 021 022import java.io.IOException; 023import java.net.InetSocketAddress; 024import java.nio.charset.Charset; 025 026import org.apache.mina.core.service.IoAcceptor; 027import org.apache.mina.core.session.IdleStatus; 028import org.apache.mina.filter.codec.ProtocolCodecFilter; 029import org.apache.mina.filter.codec.textline.TextLineCodecFactory; 030import org.apache.mina.filter.logging.LoggingFilter; 031import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 032 033/** 034 * A minimal 'time' server, returning the current date. Opening 035 * a telnet server, you will get the current date by typing 036 * any string followed by a new line. 037 * 038 * In order to quit, just send the 'quit' message. 039 * 040 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 041 */ 042public class MinaTimeServer { 043 /** We will use a port above 1024 to be able to launch the server with a standard user */ 044 private static final int PORT = 9123; 045 046 /** 047 * The server implementation. It's based on TCP, and uses a logging filter 048 * plus a text line decoder. 049 * 050 * @param args The arguments 051 * @throws IOException If something went wrong 052 */ 053 public static void main(String[] args) throws IOException { 054 // Create the acceptor 055 IoAcceptor acceptor = new NioSocketAcceptor(); 056 057 // Add two filters : a logger and a codec 058 acceptor.getFilterChain().addLast( "logger", new LoggingFilter() ); 059 acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" )))); 060 061 // Attach the business logic to the server 062 acceptor.setHandler( new TimeServerHandler() ); 063 064 // Configurate the buffer size and the iddle time 065 acceptor.getSessionConfig().setReadBufferSize( 2048 ); 066 acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 ); 067 068 // And bind ! 069 acceptor.bind( new InetSocketAddress(PORT) ); 070 } 071}