001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt.net;
009    
010    import java.io.IOException;
011    import java.net.InetAddress;
012    import java.net.InetSocketAddress;
013    import java.net.ServerSocket;
014    import java.net.Socket;
015    import java.net.SocketAddress;
016    import java.net.SocketException;
017    import java.nio.channels.ServerSocketChannel;
018    
019    import com.barchart.udt.ExceptionUDT;
020    import com.barchart.udt.SocketUDT;
021    import com.barchart.udt.TypeUDT;
022    
023    /**
024     * {@link ServerSocket} - like wrapper for {@link SocketUDT}
025     */
026    public class NetServerSocketUDT extends ServerSocket implements
027                    IceServerSocket, IceCommon {
028    
029            protected final SocketUDT socketUDT;
030    
031            /** uses {@link TypeUDT#STREAM} socket in blocking mode */
032            public NetServerSocketUDT() throws IOException {
033                    this(new SocketUDT(TypeUDT.STREAM));
034                    this.socketUDT.setBlocking(true);
035            }
036    
037            /** uses provided socket keeping blocking mode */
038            protected NetServerSocketUDT(final SocketUDT socketUDT) throws IOException {
039                    this.socketUDT = socketUDT;
040            }
041    
042            @Override
043            public Socket accept() throws IOException {
044                    final SocketUDT clientUDT = socketUDT.accept();
045                    return new NetSocketUDT(clientUDT);
046            }
047    
048            @Override
049            public void bind(final SocketAddress endpoint) throws IOException {
050                    final int backlog = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE;
051                    bind(endpoint, backlog);
052            }
053    
054            /**
055             * NOTE: bind() means listen() for UDT server socket
056             */
057            @Override
058            public void bind(SocketAddress bindpoint, int backlog) throws IOException {
059                    if (bindpoint == null) {
060                            bindpoint = new InetSocketAddress(0);
061                    }
062                    if (backlog <= 0) {
063                            backlog = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE;
064                    }
065                    socketUDT.bind((InetSocketAddress) bindpoint);
066                    socketUDT.listen(backlog);
067            }
068    
069            @Override
070            public void close() throws IOException {
071                    socketUDT.close();
072            }
073    
074            @Override
075            public ServerSocketChannel getChannel() {
076                    throw new UnsupportedOperationException("feature not available");
077            }
078    
079            @Override
080            public InetAddress getInetAddress() {
081                    return socketUDT.getLocalInetAddress();
082            }
083    
084            @Override
085            public int getLocalPort() {
086                    return socketUDT.getLocalInetPort();
087            }
088    
089            @Override
090            public SocketAddress getLocalSocketAddress() {
091                    try {
092                            return socketUDT.getLocalSocketAddress();
093                    } catch (final ExceptionUDT e) {
094                            return null;
095                    }
096            }
097    
098            @Override
099            public int getReceiveBufferSize() throws SocketException {
100                    return socketUDT.getReceiveBufferSize();
101            }
102    
103            @Override
104            public boolean getReuseAddress() throws SocketException {
105                    return socketUDT.getReuseAddress();
106            }
107    
108            @Override
109            public int getSoTimeout() throws IOException {
110                    return socketUDT.getSoTimeout();
111            }
112    
113            @Override
114            public boolean isBound() {
115                    return socketUDT.isBound();
116            }
117    
118            @Override
119            public boolean isClosed() {
120                    return socketUDT.isClosed();
121            }
122    
123            @Override
124            public void setPerformancePreferences(final int connectionTime,
125                            final int latency, final int bandwidth) {
126                    throw new UnsupportedOperationException("feature not available");
127            }
128    
129            // NOTE: set both send and receive, since they are inherited on accept()
130            @Override
131            public void setReceiveBufferSize(final int size) throws SocketException {
132                    socketUDT.setReceiveBufferSize(size);
133                    socketUDT.setSendBufferSize(size);
134            }
135    
136            @Override
137            public void setReuseAddress(final boolean on) throws SocketException {
138                    socketUDT.setReuseAddress(on);
139            }
140    
141            @Override
142            public void setSoTimeout(final int timeout) throws SocketException {
143                    socketUDT.setSoTimeout(timeout);
144            }
145    
146            @Override
147            public SocketUDT socketUDT() {
148                    return socketUDT;
149            }
150    
151    }