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