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.tapedeck; 021 022import java.net.InetSocketAddress; 023 024import org.apache.mina.core.service.IoHandler; 025import org.apache.mina.filter.codec.ProtocolCodecFilter; 026import org.apache.mina.filter.codec.textline.TextLineEncoder; 027import org.apache.mina.filter.logging.LoggingFilter; 028import org.apache.mina.statemachine.StateMachine; 029import org.apache.mina.statemachine.StateMachineFactory; 030import org.apache.mina.statemachine.StateMachineProxyBuilder; 031import org.apache.mina.statemachine.annotation.IoHandlerTransition; 032import org.apache.mina.statemachine.context.IoSessionStateContextLookup; 033import org.apache.mina.statemachine.context.StateContext; 034import org.apache.mina.statemachine.context.StateContextFactory; 035import org.apache.mina.transport.socket.SocketAcceptor; 036import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 037 038/** 039 * Simple example demonstrating how to build a state machine for MINA's 040 * {@link IoHandler} interface. 041 * 042 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 043 */ 044public class Main { 045 /** Choose your favorite port number. */ 046 private static final int PORT = 12345; 047 048 private static IoHandler createIoHandler() { 049 StateMachine sm = StateMachineFactory.getInstance( 050 IoHandlerTransition.class).create(TapeDeckServer.EMPTY, 051 new TapeDeckServer()); 052 053 return new StateMachineProxyBuilder().setStateContextLookup( 054 new IoSessionStateContextLookup(new StateContextFactory() { 055 public StateContext create() { 056 return new TapeDeckServer.TapeDeckContext(); 057 } 058 })).create(IoHandler.class, sm); 059 } 060 061 public static void main(String[] args) throws Exception { 062 SocketAcceptor acceptor = new NioSocketAcceptor(); 063 acceptor.setReuseAddress(true); 064 ProtocolCodecFilter pcf = new ProtocolCodecFilter( 065 new TextLineEncoder(), new CommandDecoder()); 066 acceptor.getFilterChain().addLast("log1", new LoggingFilter("log1")); 067 acceptor.getFilterChain().addLast("codec", pcf); 068 acceptor.getFilterChain().addLast("log2", new LoggingFilter("log2")); 069 acceptor.setHandler(createIoHandler()); 070 acceptor.bind(new InetSocketAddress(PORT)); 071 } 072}