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 _socket.setSoTimeout(maxIdleTime>0?maxIdleTime:0);
56 super.setMaxIdleTime(maxIdleTime);
57 }
58
59
60
61
62 @Override
63 public boolean isOpen()
64 {
65 return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
66 }
67
68
69
70
71
72
73
74 @Override
75 public void shutdownOutput() throws IOException
76 {
77 if (!_socket.isClosed() && !_socket.isOutputShutdown())
78 _socket.shutdownOutput();
79 }
80
81
82
83
84 @Override
85 public void close() throws IOException
86 {
87 _socket.close();
88 _in=null;
89 _out=null;
90 }
91
92
93
94
95
96
97 @Override
98 public String getLocalAddr()
99 {
100 if (_local==null)
101 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
102
103 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
104 return StringUtil.ALL_INTERFACES;
105
106 return _local.getAddress().getHostAddress();
107 }
108
109
110
111
112
113 @Override
114 public String getLocalHost()
115 {
116 if (_local==null)
117 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
118
119 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
120 return StringUtil.ALL_INTERFACES;
121
122 return _local.getAddress().getCanonicalHostName();
123 }
124
125
126
127
128
129 @Override
130 public int getLocalPort()
131 {
132 if (_local==null)
133 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
134 if (_local==null)
135 return -1;
136 return _local.getPort();
137 }
138
139
140
141
142
143 @Override
144 public String getRemoteAddr()
145 {
146 if (_remote==null)
147 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
148 if (_remote==null)
149 return null;
150 InetAddress addr = _remote.getAddress();
151 return ( addr == null ? null : addr.getHostAddress() );
152 }
153
154
155
156
157
158 @Override
159 public String getRemoteHost()
160 {
161 if (_remote==null)
162 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
163 if (_remote==null)
164 return null;
165 return _remote.getAddress().getCanonicalHostName();
166 }
167
168
169
170
171
172 @Override
173 public int getRemotePort()
174 {
175 if (_remote==null)
176 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
177 if (_remote==null)
178 return -1;
179 return _remote.getPort();
180 }
181
182
183
184
185
186 @Override
187 public Object getTransport()
188 {
189 return _socket;
190 }
191
192
193
194
195
196 @Override
197 public void setMaxIdleTime(int timeMs) throws IOException
198 {
199 if (timeMs!=getMaxIdleTime())
200 _socket.setSoTimeout(timeMs>0?timeMs:0);
201 super.setMaxIdleTime(timeMs);
202 }
203
204
205 }