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.tapedeck;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.core.service.IoHandler;
25  import org.apache.mina.filter.codec.ProtocolCodecFilter;
26  import org.apache.mina.filter.codec.textline.TextLineEncoder;
27  import org.apache.mina.filter.logging.LoggingFilter;
28  import org.apache.mina.statemachine.StateMachine;
29  import org.apache.mina.statemachine.StateMachineFactory;
30  import org.apache.mina.statemachine.StateMachineProxyBuilder;
31  import org.apache.mina.statemachine.annotation.IoHandlerTransition;
32  import org.apache.mina.statemachine.context.IoSessionStateContextLookup;
33  import org.apache.mina.statemachine.context.StateContext;
34  import org.apache.mina.statemachine.context.StateContextFactory;
35  import org.apache.mina.transport.socket.SocketAcceptor;
36  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
37  
38  /**
39   * Simple example demonstrating how to build a state machine for MINA's 
40   * {@link IoHandler} interface.
41   *
42   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
43   */
44  public class Main {
45      /** Choose your favorite port number. */
46      private static final int PORT = 12345;
47      
48      private static IoHandler createIoHandler() {
49          StateMachine sm = StateMachineFactory.getInstance(
50                  IoHandlerTransition.class).create(TapeDeckServer.EMPTY,
51                  new TapeDeckServer());
52  
53          return new StateMachineProxyBuilder().setStateContextLookup(
54                  new IoSessionStateContextLookup(new StateContextFactory() {
55                      public StateContext create() {
56                          return new TapeDeckServer.TapeDeckContext();
57                      }
58                  })).create(IoHandler.class, sm);
59      }
60      
61      public static void main(String[] args) throws Exception {
62          SocketAcceptor acceptor = new NioSocketAcceptor();
63          acceptor.setReuseAddress(true);
64          ProtocolCodecFilter pcf = new ProtocolCodecFilter(
65                  new TextLineEncoder(), new CommandDecoder());
66          acceptor.getFilterChain().addLast("log1", new LoggingFilter("log1"));
67          acceptor.getFilterChain().addLast("codec", pcf);
68          acceptor.getFilterChain().addLast("log2", new LoggingFilter("log2"));
69          acceptor.setHandler(createIoHandler());
70          acceptor.bind(new InetSocketAddress(PORT));
71      }
72  }