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      public DatagramSessionConfig getConfig() {
70          return (DatagramSessionConfig) config;
71      }
72  
73      @Override
74      DatagramChannel getChannel() {
75          return (DatagramChannel) channel;
76      }
77  
78      public TransportMetadata getTransportMetadata() {
79          return METADATA;
80      }
81  
82      public InetSocketAddress getRemoteAddress() {
83          return remoteAddress;
84      }
85  
86      public InetSocketAddress getLocalAddress() {
87          return localAddress;
88      }
89  
90      @Override
91      public InetSocketAddress getServiceAddress() {
92          return (InetSocketAddress) super.getServiceAddress();
93      }
94  }