1 /*** 2 * 3 * Copyright 2004 Hiram Chirino 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.activeio.adapter; 18 19 import java.io.IOException; 20 import java.io.InterruptedIOException; 21 import java.net.InetAddress; 22 import java.net.InetSocketAddress; 23 import java.net.ServerSocket; 24 import java.net.Socket; 25 import java.net.SocketAddress; 26 import java.net.SocketException; 27 import java.net.URI; 28 import java.nio.channels.ServerSocketChannel; 29 30 import org.activeio.Channel; 31 import org.activeio.SynchChannel; 32 import org.activeio.SynchChannelServer; 33 34 /*** 35 */ 36 public class SynchChannelServerToServerSocketAdapter extends ServerSocket { 37 38 private final SynchChannelServer channelServer; 39 private long timeout = Channel.WAIT_FOREVER_TIMEOUT; 40 boolean closed; 41 private InetAddress inetAddress; 42 private int localPort; 43 private SocketAddress localSocketAddress; 44 private int receiveBufferSize; 45 private boolean reuseAddress; 46 47 /*** 48 * @throws IOException 49 */ 50 public SynchChannelServerToServerSocketAdapter(SynchChannelServer channelServer) throws IOException { 51 this.channelServer = channelServer; 52 URI connectURI = channelServer.getConnectURI(); 53 localPort = connectURI.getPort(); 54 inetAddress = InetAddress.getByName(connectURI.getHost()); 55 localSocketAddress = new InetSocketAddress(inetAddress, localPort); 56 } 57 58 public synchronized void setSoTimeout(int timeout) throws SocketException { 59 if( timeout <= 0 ) 60 this.timeout = Channel.WAIT_FOREVER_TIMEOUT; 61 else 62 this.timeout = timeout; 63 } 64 65 public synchronized int getSoTimeout() throws IOException { 66 if( timeout == Channel.WAIT_FOREVER_TIMEOUT ) 67 return 0; 68 return (int) timeout; 69 } 70 71 public Socket accept() throws IOException { 72 Channel channel = channelServer.accept(timeout); 73 if( channel==null ) 74 throw new InterruptedIOException(); 75 76 SynchChannel synchChannel = AsynchToSynchChannelAdapter.adapt(channel); 77 synchChannel.start(); 78 return new SynchChannelToSocketAdapter(synchChannel); 79 80 } 81 82 public void bind(SocketAddress endpoint, int backlog) throws IOException { 83 if (isClosed()) 84 throw new SocketException("Socket is closed"); 85 throw new SocketException("Already bound"); 86 } 87 88 public void bind(SocketAddress endpoint) throws IOException { 89 if (isClosed()) 90 throw new SocketException("Socket is closed"); 91 throw new SocketException("Already bound"); 92 } 93 94 public void close() throws IOException { 95 if (!isClosed()) { 96 channelServer.dispose(); 97 } 98 } 99 100 public ServerSocketChannel getChannel() { 101 return null; 102 } 103 104 public InetAddress getInetAddress() { 105 return inetAddress; 106 } 107 public int getLocalPort() { 108 return localPort; 109 } 110 public SocketAddress getLocalSocketAddress() { 111 return localSocketAddress; 112 } 113 public synchronized int getReceiveBufferSize() throws SocketException { 114 return receiveBufferSize; 115 } 116 117 public boolean getReuseAddress() throws SocketException { 118 return reuseAddress; 119 } 120 121 public boolean isBound() { 122 return true; 123 } 124 125 public boolean isClosed() { 126 return closed; 127 } 128 129 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) { 130 } 131 132 public synchronized void setReceiveBufferSize(int size) throws SocketException { 133 this.receiveBufferSize = size; 134 } 135 136 public void setReuseAddress(boolean on) throws SocketException { 137 reuseAddress = on; 138 } 139 }