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.common.AbstractPollingIoConnector;
29  import org.apache.mina.common.IoConnector;
30  import org.apache.mina.common.IoProcessor;
31  import org.apache.mina.common.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: 600803 $, $Date: 2007-12-03 23:38:46 -0700 (Mon, 03 Dec 2007) $
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          ch.socket().setReuseAddress(getSessionConfig().isReuseAddress());
94          ch.socket().setReuseAddress(true);
95          ch.socket().setBroadcast(getSessionConfig().isBroadcast());
96  
97          if (localAddress != null) {
98              ch.socket().bind(localAddress);
99          }
100         
101         return ch;
102     }
103 
104     @Override
105     protected boolean connect(DatagramChannel handle,
106             SocketAddress remoteAddress) throws Exception {
107         handle.connect(remoteAddress);
108         return true;
109     }
110 
111     @Override
112     protected NioSession newSession(IoProcessor<NioSession> processor,
113             DatagramChannel handle) {
114         return new NioDatagramSession(this, handle, processor);
115     }
116 
117     @Override
118     protected void close(DatagramChannel handle) throws Exception {
119         handle.disconnect();
120         handle.close();
121     }
122     
123     // Unused extension points.
124     @Override
125     @SuppressWarnings("unchecked")
126     protected Iterator<DatagramChannel> allHandles() {
127         return Collections.EMPTY_LIST.iterator();
128     }
129 
130     @Override
131     protected ConnectionRequest connectionRequest(DatagramChannel handle) {
132         throw new UnsupportedOperationException();
133     }
134 
135     @Override
136     protected void destroy() throws Exception {
137     }
138 
139     @Override
140     protected boolean finishConnect(DatagramChannel handle) throws Exception {
141         throw new UnsupportedOperationException();
142     }
143 
144     @Override
145     protected void register(DatagramChannel handle, ConnectionRequest request)
146             throws Exception {
147         throw new UnsupportedOperationException();
148     }
149 
150     @Override
151     protected boolean select(int timeout) throws Exception {
152         return false;
153     }
154 
155     @Override
156     @SuppressWarnings("unchecked")
157     protected Iterator<DatagramChannel> selectedHandles() {
158         return Collections.EMPTY_LIST.iterator();
159     }
160 
161     @Override
162     protected void wakeup() {
163     }
164 }