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 The Apache MINA Project (dev@mina.apache.org)
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      private static IoFilter createAuthenticationIoFilter() {
64          StateMachine sm = StateMachineFactory.getInstance(
65                  IoFilterTransition.class).create(AuthenticationHandler.START,
66                  new AuthenticationHandler());
67  
68          return new StateMachineProxyBuilder().setStateContextLookup(
69                  new IoSessionStateContextLookup(new StateContextFactory() {
70                      public StateContext create() {
71                          return new AuthenticationHandler.AuthenticationContext();
72                      }
73                  }, "authContext")).setIgnoreUnhandledEvents(true).setIgnoreStateContextLookupFailure(true).create(
74                  IoFilter.class, sm);
75      }
76      
77      public static void main(String[] args) throws Exception {
78          SocketAcceptor acceptor = new NioSocketAcceptor();
79          acceptor.setReuseAddress(true);
80          ProtocolCodecFilter pcf = new ProtocolCodecFilter(
81                  new TextLineEncoder(), new CommandDecoder());
82          acceptor.getFilterChain().addLast("log1", new LoggingFilter("log1"));
83          acceptor.getFilterChain().addLast("codec", pcf);
84  //        acceptor.getFilterChain().addLast("authentication", createAuthenticationIoFilter());
85          acceptor.getFilterChain().addLast("log2", new LoggingFilter("log2"));
86          acceptor.setHandler(createIoHandler());
87          acceptor.bind(new InetSocketAddress(PORT));
88      }
89  }