blob: 62f19c186f6a239a2843dc30f10fcb7f6ea370e5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;
import java.io.IOException;
import java.nio.channels.SocketChannel;
import java.nio.channels.ServerSocketChannel;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.net.InetSocketAddress;
/**
* Class for accepting new connections in separate thread.
*
* @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
*
*/
public class Acceptor extends Thread {
private static Logger log = Logger.getLogger(Acceptor.class.getName());
private int port;
ServerSocketChannel socket;
private Listener listener;
private boolean initialized = false;
private ConnectionFactory factory;
private FatalErrorHandler fatalErrorHandler;
public Acceptor(Listener listener, ConnectionFactory factory, int port) {
super("Acceptor-" + listener.getName() + "-" + port);
this.listener = listener;
this.factory = factory;
this.port = port;
}
public Acceptor listen() throws IOException {
socket = ServerSocketChannel.open();
socket.configureBlocking(true);
socket.socket().setReuseAddress(true);
socket.socket().bind(new InetSocketAddress(port));
initialized = true;
return this;
}
/**
* Register a handler for fatal errors.
*
* @param f The FatalErrorHandler instance to be registered
*/
public synchronized void setFatalErrorHandler(FatalErrorHandler f) {
fatalErrorHandler = f;
}
public void run() {
try {
log.fine("Acceptor thread started");
if (!initialized) {
log.severe("Acceptor was not initialized. aborting");
return;
}
while (!isInterrupted()) {
SocketChannel c = null; // hush jikes
try {
c = socket.accept();
c.configureBlocking(false);
listener.addNewConnection(factory.newConnection(c, listener));
} catch (java.nio.channels.IllegalBlockingModeException e) {
log.log(Level.SEVERE, "Unable to set nonblocking", e);
try {
if (c != null) {
c.close();
}
} catch (IOException ee) {}
} catch (IOException e) {
log.log(Level.WARNING,
"Error accepting connection on port=" + port, e);
try {
if (c != null) {
c.close();
}
} catch (IOException ee) {}
}
}
} catch (Throwable t) {
if (fatalErrorHandler != null) {
fatalErrorHandler.handle(t, null);
}
}
}
}
|