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   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
46   */
47  public class Main {
48      /** Choose your favorite port number. */
49      private static final int PORT = 12345;
50      
51      private static IoHandler createIoHandler() {
52          StateMachine sm = StateMachineFactory.getInstance(
53                  IoHandlerTransition.class).create(TapeDeckServer.EMPTY,
54                  new TapeDeckServer());
55  
56          return new StateMachineProxyBuilder().setStateContextLookup(
57                  new IoSessionStateContextLookup(new StateContextFactory() {
58                      public StateContext create() {
59                          return new TapeDeckServer.TapeDeckContext();
60                      }
61                  })).create(IoHandler.class, sm);
62      }
63      
64      private static IoFilter createAuthenticationIoFilter() {
65          StateMachine sm = StateMachineFactory.getInstance(
66                  IoFilterTransition.class).create(AuthenticationHandler.START,
67                  new AuthenticationHandler());
68  
69          return new StateMachineProxyBuilder().setStateContextLookup(
70                  new IoSessionStateContextLookup(new StateContextFactory() {
71                      public StateContext create() {
72                          return new AuthenticationHandler.AuthenticationContext();
73                      }
74                  }, "authContext")).setIgnoreUnhandledEvents(true).setIgnoreStateContextLookupFailure(true).create(
75                  IoFilter.class, sm);
76      }
77      
78      public static void main(String[] args) throws Exception {
79          SocketAcceptor acceptor = new NioSocketAcceptor();
80          acceptor.setReuseAddress(true);
81          ProtocolCodecFilter pcf = new ProtocolCodecFilter(
82                  new TextLineEncoder(), new CommandDecoder());
83          acceptor.getFilterChain().addLast("log1", new LoggingFilter("log1"));
84          acceptor.getFilterChain().addLast("codec", pcf);
85  //        acceptor.getFilterChain().addLast("authentication", createAuthenticationIoFilter());
86          acceptor.getFilterChain().addLast("log2", new LoggingFilter("log2"));
87          acceptor.setHandler(createIoHandler());
88          acceptor.bind(new InetSocketAddress(PORT));
89      }
90  }