001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.transport.socket.nio;
021:
022: import java.net.InetSocketAddress;
023: import java.net.SocketAddress;
024: import java.nio.channels.DatagramChannel;
025: import java.util.Collections;
026: import java.util.Iterator;
027:
028: import org.apache.mina.common.AbstractPollingIoConnector;
029: import org.apache.mina.common.IoConnector;
030: import org.apache.mina.common.IoProcessor;
031: import org.apache.mina.common.TransportMetadata;
032: import org.apache.mina.transport.socket.DatagramConnector;
033: import org.apache.mina.transport.socket.DatagramSessionConfig;
034: import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
035:
036: /**
037: * {@link IoConnector} for datagram transport (UDP/IP).
038: *
039: * @author The Apache MINA Project (dev@mina.apache.org)
040: * @version $Rev: 600803 $, $Date: 2007-12-03 23:38:46 -0700 (Mon, 03 Dec 2007) $
041: */
042: public final class NioDatagramConnector extends
043: AbstractPollingIoConnector<NioSession, DatagramChannel>
044: implements DatagramConnector {
045:
046: /**
047: * Creates a new instance.
048: */
049: public NioDatagramConnector() {
050: super (new DefaultDatagramSessionConfig(), NioProcessor.class);
051: }
052:
053: /**
054: * Creates a new instance.
055: */
056: public NioDatagramConnector(int processorCount) {
057: super (new DefaultDatagramSessionConfig(), NioProcessor.class,
058: processorCount);
059: }
060:
061: /**
062: * Creates a new instance.
063: */
064: public NioDatagramConnector(IoProcessor<NioSession> processor) {
065: super (new DefaultDatagramSessionConfig(), processor);
066: }
067:
068: public TransportMetadata getTransportMetadata() {
069: return NioDatagramSession.METADATA;
070: }
071:
072: @Override
073: public DatagramSessionConfig getSessionConfig() {
074: return (DatagramSessionConfig) super .getSessionConfig();
075: }
076:
077: @Override
078: public InetSocketAddress getDefaultRemoteAddress() {
079: return (InetSocketAddress) super .getDefaultRemoteAddress();
080: }
081:
082: public void setDefaultRemoteAddress(
083: InetSocketAddress defaultRemoteAddress) {
084: super .setDefaultRemoteAddress(defaultRemoteAddress);
085: }
086:
087: @Override
088: protected void init() throws Exception {
089: }
090:
091: @Override
092: protected DatagramChannel newHandle(SocketAddress localAddress)
093: throws Exception {
094: DatagramChannel ch = DatagramChannel.open();
095: ch.socket()
096: .setReuseAddress(getSessionConfig().isReuseAddress());
097: ch.socket().setReuseAddress(true);
098: ch.socket().setBroadcast(getSessionConfig().isBroadcast());
099:
100: if (localAddress != null) {
101: ch.socket().bind(localAddress);
102: }
103:
104: return ch;
105: }
106:
107: @Override
108: protected boolean connect(DatagramChannel handle,
109: SocketAddress remoteAddress) throws Exception {
110: handle.connect(remoteAddress);
111: return true;
112: }
113:
114: @Override
115: protected NioSession newSession(IoProcessor<NioSession> processor,
116: DatagramChannel handle) {
117: return new NioDatagramSession(this , handle, processor);
118: }
119:
120: @Override
121: protected void close(DatagramChannel handle) throws Exception {
122: handle.disconnect();
123: handle.close();
124: }
125:
126: // Unused extension points.
127: @Override
128: @SuppressWarnings("unchecked")
129: protected Iterator<DatagramChannel> allHandles() {
130: return Collections.EMPTY_LIST.iterator();
131: }
132:
133: @Override
134: protected ConnectionRequest connectionRequest(DatagramChannel handle) {
135: throw new UnsupportedOperationException();
136: }
137:
138: @Override
139: protected void destroy() throws Exception {
140: }
141:
142: @Override
143: protected boolean finishConnect(DatagramChannel handle)
144: throws Exception {
145: throw new UnsupportedOperationException();
146: }
147:
148: @Override
149: protected void register(DatagramChannel handle,
150: ConnectionRequest request) throws Exception {
151: throw new UnsupportedOperationException();
152: }
153:
154: @Override
155: protected boolean select(int timeout) throws Exception {
156: return false;
157: }
158:
159: @Override
160: @SuppressWarnings("unchecked")
161: protected Iterator<DatagramChannel> selectedHandles() {
162: return Collections.EMPTY_LIST.iterator();
163: }
164:
165: @Override
166: protected void wakeup() {
167: }
168: }
|