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.transport.socket.nio;
21  
22  import java.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  import java.nio.channels.DatagramChannel;
25  import java.nio.channels.SelectionKey;
26  
27  import org.apache.mina.core.buffer.IoBuffer;
28  import org.apache.mina.core.filterchain.DefaultIoFilterChain;
29  import org.apache.mina.core.filterchain.IoFilterChain;
30  import org.apache.mina.core.service.DefaultTransportMetadata;
31  import org.apache.mina.core.service.IoHandler;
32  import org.apache.mina.core.service.IoProcessor;
33  import org.apache.mina.core.service.IoService;
34  import org.apache.mina.core.service.TransportMetadata;
35  import org.apache.mina.core.session.IoSession;
36  import org.apache.mina.transport.socket.DatagramSessionConfig;
37  
38  /**
39   * An {@link IoSession} for datagram transport (UDP/IP).
40   *
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
42   */
43  class NioDatagramSession extends NioSession {
44  
45      static final TransportMetadata METADATA =
46              new DefaultTransportMetadata(
47                      "nio", "datagram", true, false,
48                      InetSocketAddress.class,
49                      DatagramSessionConfig.class, IoBuffer.class);
50  
51      private final IoService service;
52      private final DatagramSessionConfig config;
53      private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
54      private final DatagramChannel ch;
55      private final IoHandler handler;
56      private final InetSocketAddress localAddress;
57      private final InetSocketAddress remoteAddress;
58  
59      private SelectionKey key;
60  
61      /**
62       * Creates a new acceptor-side session instance.
63       */
64      NioDatagramSession(IoService service,
65                          DatagramChannel ch, IoProcessor<NioSession> processor,
66                          SocketAddress remoteAddress) {
67          super(processor);
68          this.service = service;
69          this.ch = ch;
70          this.config = new NioDatagramSessionConfig(ch);
71          this.config.setAll(service.getSessionConfig());
72          this.handler = service.getHandler();
73          this.remoteAddress = (InetSocketAddress) remoteAddress;
74          this.localAddress = (InetSocketAddress) ch.socket().getLocalSocketAddress();
75      }
76  
77      /**
78       * Creates a new connector-side session instance.
79       */
80      NioDatagramSession(IoService service, DatagramChannel ch, IoProcessor<NioSession> processor) {
81          this(service, ch, processor, ch.socket().getRemoteSocketAddress());
82      }
83  
84      public IoService getService() {
85          return service;
86      }
87  
88      public DatagramSessionConfig getConfig() {
89          return config;
90      }
91  
92      public IoFilterChain getFilterChain() {
93          return filterChain;
94      }
95  
96      @Override
97      DatagramChannel getChannel() {
98          return ch;
99      }
100 
101     @Override
102     SelectionKey getSelectionKey() {
103         return key;
104     }
105 
106     @Override
107     void setSelectionKey(SelectionKey key) {
108         this.key = key;
109     }
110 
111     public IoHandler getHandler() {
112         return handler;
113     }
114 
115     public TransportMetadata getTransportMetadata() {
116         return METADATA;
117     }
118 
119     public InetSocketAddress getRemoteAddress() {
120         return remoteAddress;
121     }
122 
123     public InetSocketAddress getLocalAddress() {
124         return localAddress;
125     }
126 
127     @Override
128     public InetSocketAddress getServiceAddress() {
129         return (InetSocketAddress) super.getServiceAddress();
130     }
131 }