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 The Apache MINA Project (dev@mina.apache.org)
42   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
43   */
44  class NioDatagramSession extends NioSession {
45  
46      static final TransportMetadata METADATA =
47              new DefaultTransportMetadata(
48                      "nio", "datagram", true, false,
49                      InetSocketAddress.class,
50                      DatagramSessionConfig.class, IoBuffer.class);
51  
52      private final IoService service;
53      private final DatagramSessionConfig config;
54      private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
55      private final DatagramChannel ch;
56      private final IoHandler handler;
57      private final InetSocketAddress localAddress;
58      private final InetSocketAddress remoteAddress;
59      private final IoProcessor<NioSession> processor;
60  
61      private SelectionKey key;
62  
63      /**
64       * Creates a new acceptor-side session instance.
65       */
66      NioDatagramSession(IoService service,
67                          DatagramChannel ch, IoProcessor<NioSession> processor,
68                          SocketAddress remoteAddress) {
69          this.service = service;
70          this.ch = ch;
71          this.config = new NioDatagramSessionConfig(ch);
72          this.handler = service.getHandler();
73          this.processor = processor;
74          this.remoteAddress = (InetSocketAddress) remoteAddress;
75          this.localAddress = (InetSocketAddress) ch.socket().getLocalSocketAddress();
76      }
77  
78      /**
79       * Creates a new connector-side session instance.
80       */
81      NioDatagramSession(IoService service,
82                          DatagramChannel ch, IoProcessor<NioSession> processor) {
83          this(service, ch, processor, ch.socket().getRemoteSocketAddress());
84      }
85  
86      public IoService getService() {
87          return service;
88      }
89  
90      @Override
91      public IoProcessor<NioSession> getProcessor() {
92          return processor;
93      }
94  
95      public DatagramSessionConfig getConfig() {
96          return config;
97      }
98  
99      public IoFilterChain getFilterChain() {
100         return filterChain;
101     }
102 
103     @Override
104     DatagramChannel getChannel() {
105         return ch;
106     }
107 
108     @Override
109     SelectionKey getSelectionKey() {
110         return key;
111     }
112 
113     @Override
114     void setSelectionKey(SelectionKey key) {
115         this.key = key;
116     }
117 
118     public IoHandler getHandler() {
119         return handler;
120     }
121 
122     public TransportMetadata getTransportMetadata() {
123         return METADATA;
124     }
125 
126     public InetSocketAddress getRemoteAddress() {
127         return remoteAddress;
128     }
129 
130     public InetSocketAddress getLocalAddress() {
131         return localAddress;
132     }
133 
134     @Override
135     public InetSocketAddress getServiceAddress() {
136         return (InetSocketAddress) super.getServiceAddress();
137     }
138 }