5c8c0fdc435f61897150089a0dbe9484417edaee
[org.ibex.io.git] / src / org / ibex / io / Connection.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.io;
6
7 import java.net.*;
8 import java.io.*;
9 import org.ibex.util.*;
10
11 /** a stream backed by a socket */
12 public class Connection extends Stream {
13     private final Socket s;
14     public final String vhost;
15     public Connection(Socket s, String vhost) { super(s); this.vhost = vhost; this.s = s; }
16     protected Connection(InputStream i, OutputStream o) { super(i, o); vhost = null; s = null; }
17     public Socket getSocket() { return s; }
18     public void close()                       { try{if (s!=null) s.close();}catch(Exception e){throw new Stream.StreamException(e);} super.close(); }
19     public int getLocalPort()                 { return s.getLocalPort(); }
20     public InetAddress getLocalAddress()      { return ((InetSocketAddress)s.getLocalSocketAddress()).getAddress(); }
21     public int getRemotePort()                { return s.getPort(); }
22     public InetAddress getRemoteAddress()     { return ((InetSocketAddress)s.getRemoteSocketAddress()).getAddress(); }
23     public String getRemoteHostname()         { return getRemoteAddress().getHostName(); }
24     public String getVirtualHost()            { return vhost; }
25     public void setTimeout(int ms)            { try { s.setSoTimeout(ms); } catch (Exception e){throw new Stream.StreamException(e); }}
26     public void setTcpNoDelay(boolean delay)  { try { s.setTcpNoDelay(delay);}catch(Exception e){throw new Stream.StreamException(e); }}
27 }