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.util.Collections;
26  import java.util.Iterator;
27  
28  import org.apache.mina.core.polling.AbstractPollingIoConnector;
29  import org.apache.mina.core.service.IoConnector;
30  import org.apache.mina.core.service.IoProcessor;
31  import org.apache.mina.core.service.TransportMetadata;
32  import org.apache.mina.transport.socket.DatagramConnector;
33  import org.apache.mina.transport.socket.DatagramSessionConfig;
34  import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
35  
36  /**
37   * {@link IoConnector} for datagram transport (UDP/IP).
38   *
39   * @author The Apache MINA Project (dev@mina.apache.org)
40   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
41   */
42  public final class NioDatagramConnector
43          extends AbstractPollingIoConnector<NioSession, DatagramChannel>
44          implements DatagramConnector {
45  
46      /**
47       * Creates a new instance.
48       */
49      public NioDatagramConnector() {
50          super(new DefaultDatagramSessionConfig(), NioProcessor.class);
51      }
52  
53      /**
54       * Creates a new instance.
55       */
56      public NioDatagramConnector(int processorCount) {
57          super(new DefaultDatagramSessionConfig(), NioProcessor.class, processorCount);
58      }
59  
60      /**
61       * Creates a new instance.
62       */
63      public NioDatagramConnector(IoProcessor<NioSession> processor) {
64          super(new DefaultDatagramSessionConfig(), processor);
65      }
66      
67      public TransportMetadata getTransportMetadata() {
68          return NioDatagramSession.METADATA;
69      }
70      
71      @Override
72      public DatagramSessionConfig getSessionConfig() {
73          return (DatagramSessionConfig) super.getSessionConfig();
74      }
75      
76      @Override
77      public InetSocketAddress getDefaultRemoteAddress() {
78          return (InetSocketAddress) super.getDefaultRemoteAddress();
79      }
80      
81      public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress) {
82          super.setDefaultRemoteAddress(defaultRemoteAddress);
83      }
84  
85      @Override
86      protected void init() throws Exception {
87      }
88  
89      @Override
90      protected DatagramChannel newHandle(SocketAddress localAddress)
91              throws Exception {
92          DatagramChannel ch = DatagramChannel.open();
93          if (localAddress != null) {
94              ch.socket().bind(localAddress);
95          }
96          return ch;
97      }
98  
99      @Override
100     protected boolean connect(DatagramChannel handle,
101             SocketAddress remoteAddress) throws Exception {
102         handle.connect(remoteAddress);
103         return true;
104     }
105 
106     @Override
107     protected NioSession newSession(IoProcessor<NioSession> processor,
108             DatagramChannel handle) {
109         NioSession session = new NioDatagramSession(this, handle, processor);
110         session.getConfig().setAll(getSessionConfig());
111         return session;
112     }
113 
114     @Override
115     protected void close(DatagramChannel handle) throws Exception {
116         handle.disconnect();
117         handle.close();
118     }
119     
120     // Unused extension points.
121     @Override
122     @SuppressWarnings("unchecked")
123     protected Iterator<DatagramChannel> allHandles() {
124         return Collections.EMPTY_LIST.iterator();
125     }
126 
127     @Override
128     protected ConnectionRequest connectionRequest(DatagramChannel handle) {
129         throw new UnsupportedOperationException();
130     }
131 
132     @Override
133     protected void destroy() throws Exception {
134     }
135 
136     @Override
137     protected boolean finishConnect(DatagramChannel handle) throws Exception {
138         throw new UnsupportedOperationException();
139     }
140 
141     @Override
142     protected void register(DatagramChannel handle, ConnectionRequest request)
143             throws Exception {
144         throw new UnsupportedOperationException();
145     }
146 
147     @Override
148     protected boolean select(int timeout) throws Exception {
149         return false;
150     }
151 
152     @Override
153     @SuppressWarnings("unchecked")
154     protected Iterator<DatagramChannel> selectedHandles() {
155         return Collections.EMPTY_LIST.iterator();
156     }
157 
158     @Override
159     protected void wakeup() {
160     }
161 }