View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.io.bio;
15  
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.InetSocketAddress;
19  import java.net.Socket;
20  
21  import org.eclipse.jetty.util.StringUtil;
22  
23  /**
24   *
25   * To change the template for this generated type comment go to
26   * Window - Preferences - Java - Code Generation - Code and Comments
27   */
28  public class SocketEndPoint extends StreamEndPoint
29  {
30      final Socket _socket;
31      final InetSocketAddress _local;
32      final InetSocketAddress _remote;
33      
34      /* ------------------------------------------------------------ */
35      /**
36       * 
37       */
38      public SocketEndPoint(Socket socket)
39      	throws IOException	
40      {
41          super(socket.getInputStream(),socket.getOutputStream());
42          _socket=socket;
43          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
44          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
45          super.setMaxIdleTime(_socket.getSoTimeout());
46      }
47      
48      /* ------------------------------------------------------------ */
49      /**
50       * 
51       */
52      protected SocketEndPoint(Socket socket, int maxIdleTime)
53          throws IOException      
54      {
55          super(socket.getInputStream(),socket.getOutputStream());
56          _socket=socket;
57          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
58          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
59          _socket.setSoTimeout(maxIdleTime>0?maxIdleTime:0);
60          super.setMaxIdleTime(maxIdleTime);
61      }
62      
63      /* ------------------------------------------------------------ */
64      /* (non-Javadoc)
65       * @see org.eclipse.io.BufferIO#isClosed()
66       */
67      @Override
68      public boolean isOpen()
69      {
70          return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
71      }
72  
73      /* ------------------------------------------------------------ */
74      /*
75       * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
76       */
77      @Override
78      public void shutdownOutput() throws IOException
79      {    
80          if (!_socket.isClosed() && !_socket.isOutputShutdown())
81              _socket.shutdownOutput();
82      }
83      
84      /* ------------------------------------------------------------ */
85      /* (non-Javadoc)
86       * @see org.eclipse.io.BufferIO#close()
87       */
88      @Override
89      public void close() throws IOException
90      {
91          _socket.close();
92          _in=null;
93          _out=null;
94      }
95      
96  
97      /* ------------------------------------------------------------ */
98      /* 
99       * @see org.eclipse.io.EndPoint#getLocalAddr()
100      */
101     @Override
102     public String getLocalAddr()
103     {
104        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
105            return StringUtil.ALL_INTERFACES;
106         
107         return _local.getAddress().getHostAddress();
108     }
109 
110     /* ------------------------------------------------------------ */
111     /* 
112      * @see org.eclipse.io.EndPoint#getLocalHost()
113      */
114     @Override
115     public String getLocalHost()
116     {
117        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
118            return StringUtil.ALL_INTERFACES;
119         
120         return _local.getAddress().getCanonicalHostName();
121     }
122 
123     /* ------------------------------------------------------------ */
124     /* 
125      * @see org.eclipse.io.EndPoint#getLocalPort()
126      */
127     @Override
128     public int getLocalPort()
129     {
130         if (_local==null)
131             return -1;
132         return _local.getPort();
133     }
134 
135     /* ------------------------------------------------------------ */
136     /* 
137      * @see org.eclipse.io.EndPoint#getRemoteAddr()
138      */
139     @Override
140     public String getRemoteAddr()
141     {
142         if (_remote==null)
143             return null;
144         InetAddress addr = _remote.getAddress();
145         return ( addr == null ? null : addr.getHostAddress() );
146     }
147 
148     /* ------------------------------------------------------------ */
149     /* 
150      * @see org.eclipse.io.EndPoint#getRemoteHost()
151      */
152     @Override
153     public String getRemoteHost()
154     {
155         if (_remote==null)
156             return null;
157         return _remote.getAddress().getCanonicalHostName();
158     }
159 
160     /* ------------------------------------------------------------ */
161     /* 
162      * @see org.eclipse.io.EndPoint#getRemotePort()
163      */
164     @Override
165     public int getRemotePort()
166     {
167         if (_remote==null)
168             return -1;
169         return _remote.getPort();
170     }
171 
172     /* ------------------------------------------------------------ */
173     /* 
174      * @see org.eclipse.io.EndPoint#getConnection()
175      */
176     @Override
177     public Object getTransport()
178     {
179         return _socket;
180     }
181 
182     /* ------------------------------------------------------------ */
183     /**
184      * @see org.eclipse.jetty.io.bio.StreamEndPoint#setMaxIdleTime(int)
185      */
186     @Override
187     public void setMaxIdleTime(int timeMs) throws IOException
188     {
189         if (timeMs!=getMaxIdleTime())
190             _socket.setSoTimeout(timeMs>0?timeMs:0);
191         super.setMaxIdleTime(timeMs);
192     }
193     
194     
195 }