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.io.InputStream;
012    import java.io.OutputStream;
013    import java.net.InetAddress;
014    import java.net.InetSocketAddress;
015    import java.net.Socket;
016    import java.net.SocketAddress;
017    import java.net.SocketException;
018    import java.nio.channels.SocketChannel;
019    
020    import org.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    
023    import com.barchart.udt.ExceptionUDT;
024    import com.barchart.udt.SocketUDT;
025    import com.barchart.udt.TypeUDT;
026    import com.barchart.udt.anno.ThreadSafe;
027    
028    /**
029     * {@link Socket} - like wrapper for {@link SocketUDT}
030     */
031    public class NetSocketUDT extends Socket implements IceSocket, IceCommon {
032    
033            private final Logger log = LoggerFactory.getLogger(getClass());
034    
035            @ThreadSafe("this")
036            protected InputStream inputStream;
037            @ThreadSafe("this")
038            protected OutputStream outputStream;
039    
040            protected final SocketUDT socketUDT;
041    
042            /** uses {@link TypeUDT#STREAM} socket in blocking mode */
043            public NetSocketUDT() throws ExceptionUDT {
044                    this(new SocketUDT(TypeUDT.STREAM));
045                    this.socketUDT.setBlocking(true);
046            }
047    
048            /** uses provided socket keeping blocking mode */
049            protected NetSocketUDT(final SocketUDT socketUDT) {
050                    this.socketUDT = socketUDT;
051            }
052    
053            @Override
054            public void bind(SocketAddress bindpoint) throws IOException {
055                    if (bindpoint == null) {
056                            bindpoint = new InetSocketAddress(0);
057                    }
058                    socketUDT.bind((InetSocketAddress) bindpoint);
059            }
060    
061            @Override
062            public synchronized void close() throws IOException {
063                    socketUDT.close();
064            }
065    
066            @Override
067            public void connect(final SocketAddress endpoint) throws IOException {
068                    socketUDT.connect((InetSocketAddress) endpoint);
069            }
070    
071            @Override
072            public void connect(final SocketAddress endpoint, final int timeout)
073                            throws IOException {
074                    throw new UnsupportedOperationException("feature not available");
075            }
076    
077            @Override
078            public SocketChannel getChannel() {
079                    throw new UnsupportedOperationException("feature not available");
080            }
081    
082            @Override
083            public InetAddress getInetAddress() {
084                    return socketUDT.getRemoteInetAddress();
085            }
086    
087            @Override
088            public synchronized InputStream getInputStream() throws IOException {
089                    if (inputStream == null) {
090                            inputStream = new NetInputStreamUDT(socketUDT);
091                    }
092                    return inputStream;
093            }
094    
095            @Override
096            public boolean getKeepAlive() throws SocketException {
097                    // UDT has keep alive automatically under the
098                    // hood which I believe you cannot turn off
099                    return true;
100            }
101    
102            @Override
103            public InetAddress getLocalAddress() {
104                    return socketUDT.getLocalInetAddress();
105            }
106    
107            @Override
108            public int getLocalPort() {
109                    return socketUDT.getLocalInetPort();
110            }
111    
112            @Override
113            public SocketAddress getLocalSocketAddress() {
114                    try {
115                            return socketUDT.getLocalSocketAddress();
116                    } catch (final ExceptionUDT e) {
117                            return null;
118                    }
119            }
120    
121            @Override
122            public boolean getOOBInline() throws SocketException {
123                    return false;
124            }
125    
126            @Override
127            public synchronized OutputStream getOutputStream() throws IOException {
128                    if (outputStream == null) {
129                            outputStream = new NetOutputStreamUDT(socketUDT);
130                    }
131                    return outputStream;
132            }
133    
134            @Override
135            public int getPort() {
136                    return socketUDT.getRemoteInetPort();
137            }
138    
139            @Override
140            public synchronized int getReceiveBufferSize() throws SocketException {
141                    return socketUDT.getReceiveBufferSize();
142            }
143    
144            @Override
145            public SocketAddress getRemoteSocketAddress() {
146                    try {
147                            return socketUDT.getRemoteSocketAddress();
148                    } catch (final ExceptionUDT e) {
149                            return null;
150                    }
151            }
152    
153            @Override
154            public boolean getReuseAddress() throws SocketException {
155                    return socketUDT.getReuseAddress();
156            }
157    
158            @Override
159            public synchronized int getSendBufferSize() throws SocketException {
160                    return socketUDT.getSendBufferSize();
161            }
162    
163            @Override
164            public int getSoLinger() throws SocketException {
165                    return socketUDT.getSoLinger();
166            }
167    
168            @Override
169            public synchronized int getSoTimeout() throws SocketException {
170                    return socketUDT.getSoTimeout();
171            }
172    
173            @Override
174            public boolean getTcpNoDelay() throws SocketException {
175                    return false;
176            }
177    
178            @Override
179            public int getTrafficClass() throws SocketException {
180                    return 0;
181            }
182    
183            @Override
184            public boolean isBound() {
185                    return socketUDT.isBound();
186            }
187    
188            @Override
189            public boolean isClosed() {
190                    return socketUDT.isClosed();
191            }
192    
193            @Override
194            public boolean isConnected() {
195                    return socketUDT.isConnected();
196            }
197    
198            @Override
199            public boolean isInputShutdown() {
200                    return socketUDT.isClosed();
201            }
202    
203            @Override
204            public boolean isOutputShutdown() {
205                    return socketUDT.isClosed();
206            }
207    
208            @Override
209            public void sendUrgentData(final int data) throws IOException {
210                    log.debug("Sending urgent data not supported in Barchart UDT...");
211            }
212    
213            @Override
214            public void setKeepAlive(final boolean on) throws SocketException {
215                    log.debug("Keep alive not supported in Barchart UDT...");
216            }
217    
218            @Override
219            public void setOOBInline(final boolean on) throws SocketException {
220                    log.debug("OOB inline  not supported in Barchart UDT...");
221            }
222    
223            @Override
224            public void setPerformancePreferences(final int connectionTime,
225                            final int latency, final int bandwidth) {
226            }
227    
228            @Override
229            public synchronized void setReceiveBufferSize(final int size)
230                            throws SocketException {
231                    socketUDT.setReceiveBufferSize(size);
232            }
233    
234            @Override
235            public void setReuseAddress(final boolean on) throws SocketException {
236                    socketUDT.setReuseAddress(on);
237            }
238    
239            @Override
240            public synchronized void setSendBufferSize(final int size)
241                            throws SocketException {
242                    socketUDT.setSendBufferSize(size);
243            }
244    
245            @Override
246            public void setSoLinger(final boolean on, final int linger)
247                            throws SocketException {
248                    socketUDT.setSoLinger(on, linger);
249            }
250    
251            @Override
252            public synchronized void setSoTimeout(final int timeout)
253                            throws SocketException {
254                    socketUDT.setSoTimeout(timeout);
255            }
256    
257            @Override
258            public void setTcpNoDelay(final boolean on) throws SocketException {
259                    log.debug("TCP no delay not supported in Barchart UDT...");
260            }
261    
262            @Override
263            public void setTrafficClass(final int tc) throws SocketException {
264                    log.debug("Traffic class not supported in Barchart UDT...");
265            }
266    
267            @Override
268            public void shutdownInput() throws IOException {
269                    socketUDT.close();
270            }
271    
272            @Override
273            public void shutdownOutput() throws IOException {
274                    socketUDT.close();
275            }
276    
277            @Override
278            public SocketUDT socketUDT() {
279                    return socketUDT;
280            }
281    
282    }