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