annotate src/org/eclipse/jetty/server/nio/BlockingChannelConnector.java @ 989:900e5b8ccd19

remove useless listeners
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 17 Oct 2016 05:39:54 -0600
parents bdb6eb0fbf93
children d9cfec64899c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 // ------------------------------------------------------------------------
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 // All rights reserved. This program and the accompanying materials
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 // are made available under the terms of the Eclipse Public License v1.0
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 // and Apache License v2.0 which accompanies this distribution.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 // The Eclipse Public License is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 // http://www.eclipse.org/legal/epl-v10.html
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 // The Apache License v2.0 is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 // http://www.opensource.org/licenses/apache2.0.php
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 // You may elect to redistribute this code under either of these licenses.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 package org.eclipse.jetty.server.nio;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import java.io.IOException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 import java.net.InetSocketAddress;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 import java.net.Socket;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 import java.nio.channels.ByteChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 import java.nio.channels.SelectionKey;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 import java.nio.channels.ServerSocketChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 import java.nio.channels.SocketChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 import java.util.Set;
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
29 import java.util.concurrent.ConcurrentHashMap;
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 840
diff changeset
30 import java.util.concurrent.RejectedExecutionException;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 import org.eclipse.jetty.http.HttpException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 import org.eclipse.jetty.io.Buffer;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 import org.eclipse.jetty.io.EndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 import org.eclipse.jetty.io.EofException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 import org.eclipse.jetty.io.nio.ChannelEndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 import org.eclipse.jetty.server.BlockingHttpConnection;
907
bc4e299de953 remove AbstractNIOConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 906
diff changeset
38 import org.eclipse.jetty.server.Connector;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 import org.eclipse.jetty.server.Request;
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
40 import org.eclipse.jetty.server.Server;
820
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
41 import org.slf4j.Logger;
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
42 import org.slf4j.LoggerFactory;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 /* ------------------------------------------------------------------------------- */
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 /** Blocking NIO connector.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 * This connector uses efficient NIO buffers with a traditional blocking thread model.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 * Direct NIO buffers are used and a thread is allocated per connections.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 * This connector is best used when there are a few very active connections.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 * @org.apache.xbean.XBean element="blockingNioConnector" description="Creates a blocking NIO based socket connector"
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 */
907
bc4e299de953 remove AbstractNIOConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 906
diff changeset
57 public final class BlockingChannelConnector extends Connector
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 {
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
59 private static final Logger LOG = LoggerFactory.getLogger(BlockingChannelConnector.class);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
61 private final Set<BlockingChannelEndPoint> _endpoints = new ConcurrentHashMap<BlockingChannelEndPoint,Boolean>().newKeySet();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
64 /* ------------------------------------------------------------ */
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
65 /** Constructor.
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
66 *
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
67 */
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
68 public BlockingChannelConnector(Server server,int port)
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
69 {
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
70 super(server,port);
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
71 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
74 @Override
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
75 protected void doStart() throws Exception
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
76 {
909
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
77 // Create a new server socket and set to non blocking mode
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
78 _acceptChannel = ServerSocketChannel.open();
909
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
79 _acceptChannel.configureBlocking(true);
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
80
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
81 // Bind the server socket to the local host and port
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
82 InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port);
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
83 _acceptChannel.bind(addr);
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
84
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
85 super.doStart();
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
86 server.threadPool.execute(new Runnable()
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
87 {
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
89 public void run()
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
90 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
91 while (isRunning())
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
92 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
93 try
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
94 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
95 Thread.sleep(400);
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
96 long now = System.currentTimeMillis();
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
97 for (BlockingChannelEndPoint endp : _endpoints)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
98 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
99 endp.checkIdleTimestamp(now);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
100 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
101 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
102 catch(InterruptedException e)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
103 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
104 LOG.trace("",e);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
105 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
106 catch(Exception e)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
107 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
108 LOG.warn("",e);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
109 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
110 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
111 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
113 });
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
115 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
117 @Override
906
Franklin Schmidt <fschmidt@gmail.com>
parents: 903
diff changeset
118 public void accept()
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
119 throws IOException, InterruptedException
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
120 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
121 SocketChannel channel = _acceptChannel.accept();
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
122 channel.configureBlocking(true);
909
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
123 Socket socket = channel.socket();
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
124 configure(socket);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
126 BlockingChannelEndPoint connection = new BlockingChannelEndPoint(channel);
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
127 try {
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
128 server.threadPool.execute(connection);
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
129 } catch(RejectedExecutionException e) {
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
130 LOG.warn("dispatch failed for {}",connection._connection);
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
131 connection.close();
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
132 }
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
133 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
135 @Override
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
136 public void customize(EndPoint endpoint, Request request)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
137 throws IOException
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
138 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
139 super.customize(endpoint, request);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
140 endpoint.setMaxIdleTime(_maxIdleTime);
978
bdb6eb0fbf93 simplify ChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents: 975
diff changeset
141 configure(endpoint.getChannel().socket());
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
142 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144
968
0d20943cfea2 remove ConnectedEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents: 967
diff changeset
145 private class BlockingChannelEndPoint extends ChannelEndPoint implements Runnable
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
146 {
972
5ee36654b383 simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents: 968
diff changeset
147 private final BlockingHttpConnection _connection;
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
148 private int _timeout;
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
149 private volatile long _idleTimestamp;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150
978
bdb6eb0fbf93 simplify ChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents: 975
diff changeset
151 BlockingChannelEndPoint(SocketChannel channel)
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
152 throws IOException
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
153 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
154 super(channel,BlockingChannelConnector.this._maxIdleTime);
972
5ee36654b383 simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents: 968
diff changeset
155 _connection = new BlockingHttpConnection(BlockingChannelConnector.this,this);
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
156 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
158 private void checkIdleTimestamp(long now)
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
159 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
160 if (_idleTimestamp!=0 && _timeout>0 && now>(_idleTimestamp+_timeout))
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
161 {
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
162 try
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
163 {
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
164 close();
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
165 }
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
166 catch (IOException e)
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
167 {
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
168 LOG.trace("",e);
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
169 }
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
170 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
171 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
173 @Override
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
174 public int fill(Buffer buffer) throws IOException
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
175 {
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
176 _idleTimestamp = System.currentTimeMillis();
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
177 return super.fill(buffer);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
178 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
180 @Override
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
181 public int flush(Buffer buffer) throws IOException
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
182 {
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
183 _idleTimestamp = System.currentTimeMillis();
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
184 return super.flush(buffer);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
185 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
187 @Override
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
188 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
189 {
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
190 _idleTimestamp = System.currentTimeMillis();
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
191 return super.flush(header,buffer,trailer);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
192 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
194 @Override
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
195 public void run()
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
196 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
197 try
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
198 {
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
199 _timeout = getMaxIdleTime();
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
200 _endpoints.add(this);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
202 while (isOpen())
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
203 {
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
204 _idleTimestamp = System.currentTimeMillis();
965
866f2e801618 handle() returns void
Franklin Schmidt <fschmidt@gmail.com>
parents: 959
diff changeset
205 _connection.handle();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
207 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
208 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
209 catch (EofException e)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
210 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
211 LOG.debug("EOF", e);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
212 try{BlockingChannelEndPoint.this.close();}
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
213 catch(IOException e2){LOG.trace("",e2);}
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
214 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
215 catch (HttpException e)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
216 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
217 LOG.debug("BAD", e);
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
218 try{close();}
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
219 catch(IOException e2){LOG.trace("",e2);}
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
220 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
221 catch(Throwable e)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
222 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
223 LOG.warn("handle failed",e);
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
224 try{close();}
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
225 catch(IOException e2){LOG.trace("",e2);}
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
226 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
227 finally
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
228 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
229 _endpoints.remove(this);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
230
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
231 // wait for client to close, but if not, close ourselves.
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
232 try
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
233 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
234 if (!_socket.isClosed())
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
235 {
975
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
236 long timestamp = System.currentTimeMillis();
53b3f7d9714c simplify BlockingChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 974
diff changeset
237 int max_idle = getMaxIdleTime();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
238
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
239 _socket.setSoTimeout(getMaxIdleTime());
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
240 int c=0;
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
241 do
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
242 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
243 c = _socket.getInputStream().read();
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
244 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
245 while (c>=0 && (System.currentTimeMillis()-timestamp)<max_idle);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
246 if (!_socket.isClosed())
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
247 _socket.close();
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
248 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
249 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
250 catch(IOException e)
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
251 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
252 LOG.trace("",e);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
253 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
254 }
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
255 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
256
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
257 @Override
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
258 public String toString()
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
259 {
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
260 return String.format("BCEP@%x{l(%s)<->r(%s),open=%b,ishut=%b,oshut=%b}-{%s}",
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
261 hashCode(),
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
262 _socket.getRemoteSocketAddress(),
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
263 _socket.getLocalSocketAddress(),
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
264 isOpen(),
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
265 isInputShutdown(),
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
266 isOutputShutdown(),
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
267 _connection);
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
268 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269
840
0f53601ea489 remove ConcurrentHashSet
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
270 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271 }