1
2
3
4
5
6
7
8
9
10
11
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
27
28
29
30 public class SocketEndPoint extends StreamEndPoint
31 {
32 Socket _socket;
33 InetSocketAddress _local;
34 InetSocketAddress _remote;
35
36
37
38
39 public SocketEndPoint(Socket socket)
40 throws IOException
41 {
42 super(socket.getInputStream(),socket.getOutputStream());
43 _socket=socket;
44 super.setMaxIdleTime(_socket.getSoTimeout());
45 }
46
47
48
49
50 protected SocketEndPoint(Socket socket, int maxIdleTime)
51 throws IOException
52 {
53 super(socket.getInputStream(),socket.getOutputStream());
54 _socket=socket;
55 super.setMaxIdleTime(maxIdleTime);
56 }
57
58
59
60
61 @Override
62 public boolean isOpen()
63 {
64 return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
65 }
66
67
68
69
70 @Override
71 public void close() throws IOException
72 {
73 if (!_socket.isClosed() && !_socket.isOutputShutdown())
74 {
75 try
76 {
77 _socket.shutdownOutput();
78 }
79 catch(IOException e)
80 {
81 Log.ignore(e);
82 }
83 catch(UnsupportedOperationException e)
84 {
85 Log.ignore(e);
86 }
87 }
88 _socket.close();
89 _in=null;
90 _out=null;
91 }
92
93
94
95
96
97
98 @Override
99 public String getLocalAddr()
100 {
101 if (_local==null)
102 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
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
113
114 @Override
115 public String getLocalHost()
116 {
117 if (_local==null)
118 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
119
120 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
121 return StringUtil.ALL_INTERFACES;
122
123 return _local.getAddress().getCanonicalHostName();
124 }
125
126
127
128
129
130 @Override
131 public int getLocalPort()
132 {
133 if (_local==null)
134 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
135 if (_local==null)
136 return -1;
137 return _local.getPort();
138 }
139
140
141
142
143
144 @Override
145 public String getRemoteAddr()
146 {
147 if (_remote==null)
148 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
149 if (_remote==null)
150 return null;
151 InetAddress addr = _remote.getAddress();
152 return ( addr == null ? null : addr.getHostAddress() );
153 }
154
155
156
157
158
159 @Override
160 public String getRemoteHost()
161 {
162 if (_remote==null)
163 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
164 if (_remote==null)
165 return null;
166 return _remote.getAddress().getCanonicalHostName();
167 }
168
169
170
171
172
173 @Override
174 public int getRemotePort()
175 {
176 if (_remote==null)
177 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
178 if (_remote==null)
179 return -1;
180 return _remote.getPort();
181 }
182
183
184
185
186
187 @Override
188 public Object getTransport()
189 {
190 return _socket;
191 }
192
193
194
195
196
197 @Override
198 public void setMaxIdleTime(int timeMs) throws IOException
199 {
200 if (timeMs!=getMaxIdleTime())
201 _socket.setSoTimeout(timeMs>0?timeMs:0);
202 super.setMaxIdleTime(timeMs);
203 }
204
205
206 }