1/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.jivesoftware.smackx.bytestreams.socks5;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.OutputStream;
23import java.net.Socket;
24import java.net.SocketException;
25
26import org.jivesoftware.smackx.bytestreams.BytestreamSession;
27
28/**
29 * Socks5BytestreamSession class represents a SOCKS5 Bytestream session.
30 *
31 * @author Henning Staib
32 */
33public class Socks5BytestreamSession implements BytestreamSession {
34
35    /* the underlying socket of the SOCKS5 Bytestream */
36    private final Socket socket;
37
38    /* flag to indicate if this session is a direct or mediated connection */
39    private final boolean isDirect;
40
41    protected Socks5BytestreamSession(Socket socket, boolean isDirect) {
42        this.socket = socket;
43        this.isDirect = isDirect;
44    }
45
46    /**
47     * Returns <code>true</code> if the session is established through a direct connection between
48     * the initiator and target, <code>false</code> if the session is mediated over a SOCKS proxy.
49     *
50     * @return <code>true</code> if session is a direct connection, <code>false</code> if session is
51     *         mediated over a SOCKS5 proxy
52     */
53    public boolean isDirect() {
54        return this.isDirect;
55    }
56
57    /**
58     * Returns <code>true</code> if the session is mediated over a SOCKS proxy, <code>false</code>
59     * if this session is established through a direct connection between the initiator and target.
60     *
61     * @return <code>true</code> if session is mediated over a SOCKS5 proxy, <code>false</code> if
62     *         session is a direct connection
63     */
64    public boolean isMediated() {
65        return !this.isDirect;
66    }
67
68    public InputStream getInputStream() throws IOException {
69        return this.socket.getInputStream();
70    }
71
72    public OutputStream getOutputStream() throws IOException {
73        return this.socket.getOutputStream();
74    }
75
76    public int getReadTimeout() throws IOException {
77        try {
78            return this.socket.getSoTimeout();
79        }
80        catch (SocketException e) {
81            throw new IOException("Error on underlying Socket");
82        }
83    }
84
85    public void setReadTimeout(int timeout) throws IOException {
86        try {
87            this.socket.setSoTimeout(timeout);
88        }
89        catch (SocketException e) {
90            throw new IOException("Error on underlying Socket");
91        }
92    }
93
94    public void close() throws IOException {
95        this.socket.close();
96    }
97
98}
99