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  import org.eclipse.jetty.util.log.Log;
23  
24  /**
25   *
26   * To change the template for this generated type comment go to
27   * Window - Preferences - Java - Code Generation - Code and Comments
28   */
29  public class SocketEndPoint extends StreamEndPoint
30  {
31      final Socket _socket;
32      final InetSocketAddress _local;
33      final InetSocketAddress _remote;
34      
35      /* ------------------------------------------------------------ */
36      /**
37       * 
38       */
39      public SocketEndPoint(Socket socket)
40      	throws IOException	
41      {
42          super(socket.getInputStream(),socket.getOutputStream());
43          _socket=socket;
44          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
45          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
46          super.setMaxIdleTime(_socket.getSoTimeout());
47      }
48      
49      /* ------------------------------------------------------------ */
50      /**
51       * 
52       */
53      protected SocketEndPoint(Socket socket, int maxIdleTime)
54          throws IOException      
55      {
56          super(socket.getInputStream(),socket.getOutputStream());
57          _socket=socket;
58          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
59          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
60          _socket.setSoTimeout(maxIdleTime>0?maxIdleTime:0);
61          super.setMaxIdleTime(maxIdleTime);
62      }
63      
64      /* ------------------------------------------------------------ */
65      /* (non-Javadoc)
66       * @see org.eclipse.io.BufferIO#isClosed()
67       */
68      @Override
69      public boolean isOpen()
70      {
71          return super.isOpen() && _socket!=null && !_socket.isClosed();
72      }
73      
74      /* ------------------------------------------------------------ */
75      @Override
76      public boolean isInputShutdown()
77      {
78          return !super.isOpen() || _socket!=null && _socket.isInputShutdown();
79      }
80      
81      /* ------------------------------------------------------------ */
82      @Override
83      public boolean isOutputShutdown()
84      {
85          return !super.isOpen() || _socket!=null && _socket.isOutputShutdown();
86      }
87  
88      /* ------------------------------------------------------------ */
89      /*
90       * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
91       */
92      @Override
93      public void shutdownOutput() throws IOException
94      {    
95          if (!_socket.isClosed() && !_socket.isOutputShutdown())
96              _socket.shutdownOutput();
97      }
98  
99  
100     /* ------------------------------------------------------------ */
101     /*
102      * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
103      */
104     @Override
105     public void shutdownInput() throws IOException
106     {    
107         if (!_socket.isClosed() && !_socket.isInputShutdown())
108             _socket.shutdownInput();
109     }
110     
111     /* ------------------------------------------------------------ */
112     /* (non-Javadoc)
113      * @see org.eclipse.io.BufferIO#close()
114      */
115     @Override
116     public void close() throws IOException
117     {
118         _socket.close();
119         _in=null;
120         _out=null;
121     }
122     
123 
124     /* ------------------------------------------------------------ */
125     /* 
126      * @see org.eclipse.io.EndPoint#getLocalAddr()
127      */
128     @Override
129     public String getLocalAddr()
130     {
131        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
132            return StringUtil.ALL_INTERFACES;
133         
134         return _local.getAddress().getHostAddress();
135     }
136 
137     /* ------------------------------------------------------------ */
138     /* 
139      * @see org.eclipse.io.EndPoint#getLocalHost()
140      */
141     @Override
142     public String getLocalHost()
143     {
144        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
145            return StringUtil.ALL_INTERFACES;
146         
147         return _local.getAddress().getCanonicalHostName();
148     }
149 
150     /* ------------------------------------------------------------ */
151     /* 
152      * @see org.eclipse.io.EndPoint#getLocalPort()
153      */
154     @Override
155     public int getLocalPort()
156     {
157         if (_local==null)
158             return -1;
159         return _local.getPort();
160     }
161 
162     /* ------------------------------------------------------------ */
163     /* 
164      * @see org.eclipse.io.EndPoint#getRemoteAddr()
165      */
166     @Override
167     public String getRemoteAddr()
168     {
169         if (_remote==null)
170             return null;
171         InetAddress addr = _remote.getAddress();
172         return ( addr == null ? null : addr.getHostAddress() );
173     }
174 
175     /* ------------------------------------------------------------ */
176     /* 
177      * @see org.eclipse.io.EndPoint#getRemoteHost()
178      */
179     @Override
180     public String getRemoteHost()
181     {
182         if (_remote==null)
183             return null;
184         return _remote.getAddress().getCanonicalHostName();
185     }
186 
187     /* ------------------------------------------------------------ */
188     /* 
189      * @see org.eclipse.io.EndPoint#getRemotePort()
190      */
191     @Override
192     public int getRemotePort()
193     {
194         if (_remote==null)
195             return -1;
196         return _remote.getPort();
197     }
198 
199     /* ------------------------------------------------------------ */
200     /* 
201      * @see org.eclipse.io.EndPoint#getConnection()
202      */
203     @Override
204     public Object getTransport()
205     {
206         return _socket;
207     }
208 
209     /* ------------------------------------------------------------ */
210     /**
211      * @see org.eclipse.jetty.io.bio.StreamEndPoint#setMaxIdleTime(int)
212      */
213     @Override
214     public void setMaxIdleTime(int timeMs) throws IOException
215     {
216         if (timeMs!=getMaxIdleTime())
217             _socket.setSoTimeout(timeMs>0?timeMs:0);
218         super.setMaxIdleTime(timeMs);
219     }
220 
221 
222     /* ------------------------------------------------------------ */
223     @Override
224     protected void idleExpired() throws IOException
225     {
226         try
227         {
228             if (!isInputShutdown())
229                 shutdownInput();
230         }
231         catch(IOException e)
232         {
233             Log.ignore(e);
234             _socket.close();
235         }
236     }
237     
238 }