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      private final IoProcessor<NioSession> processor;
59  
60      private SelectionKey key;
61  
62      /**
63       * Creates a new acceptor-side session instance.
64       */
65      NioDatagramSession(IoService service,
66                          DatagramChannel ch, IoProcessor<NioSession> processor,
67                          SocketAddress remoteAddress) {
68          this.service = service;
69          this.ch = ch;
70          this.config = new NioDatagramSessionConfig(ch);
71          this.handler = service.getHandler();
72          this.processor = processor;
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,
81                          DatagramChannel ch, IoProcessor<NioSession> processor) {
82          this(service, ch, processor, ch.socket().getRemoteSocketAddress());
83      }
84  
85      public IoService getService() {
86          return service;
87      }
88  
89      @Override
90      public IoProcessor<NioSession> getProcessor() {
91          return processor;
92      }
93  
94      public DatagramSessionConfig getConfig() {
95          return config;
96      }
97  
98      public IoFilterChain getFilterChain() {
99          return filterChain;
100     }
101 
102     @Override
103     DatagramChannel getChannel() {
104         return ch;
105     }
106 
107     @Override
108     SelectionKey getSelectionKey() {
109         return key;
110     }
111 
112     @Override
113     void setSelectionKey(SelectionKey key) {
114         this.key = key;
115     }
116 
117     public IoHandler getHandler() {
118         return handler;
119     }
120 
121     public TransportMetadata getTransportMetadata() {
122         return METADATA;
123     }
124 
125     public InetSocketAddress getRemoteAddress() {
126         return remoteAddress;
127     }
128 
129     public InetSocketAddress getLocalAddress() {
130         return localAddress;
131     }
132 
133     @Override
134     public InetSocketAddress getServiceAddress() {
135         return (InetSocketAddress) super.getServiceAddress();
136     }
137 }