use of java.nio.channels.SelectionKey in project http-kit by http-kit.
the class HttpServer method accept.
void accept(SelectionKey key) {
ServerSocketChannel ch = (ServerSocketChannel) key.channel();
SocketChannel s;
try {
while ((s = ch.accept()) != null) {
s.configureBlocking(false);
HttpAtta atta = new HttpAtta(maxBody, maxLine, proxyProtocolOption);
SelectionKey k = s.register(selector, OP_READ, atta);
atta.channel = new AsyncChannel(k, this);
}
} catch (Exception e) {
// eg: too many open files. do not quit
HttpUtils.printError("accept incoming request", e);
}
}
use of java.nio.channels.SelectionKey in project http-kit by http-kit.
the class MakeupIdelConnection method main.
public static void main(String[] args) throws IOException {
final Selector selector = Selector.open();
InetSocketAddress[] locals = { new InetSocketAddress("127.0.0.1", 4348) };
// InetSocketAddress remote = new InetSocketAddress("192.168.1.114",
// 9090);
long start = System.currentTimeMillis();
int connected = 0;
int currentConnectionPerIP = 0;
while (true) {
if (System.currentTimeMillis() - start > 1000 * 60 * 10) {
break;
}
for (int i = 0; i < connectionPerIP / STEPS && currentConnectionPerIP < connectionPerIP; ++i, ++currentConnectionPerIP) {
for (InetSocketAddress addr : locals) {
SocketChannel ch = SocketChannel.open();
ch.configureBlocking(false);
Socket s = ch.socket();
s.setReuseAddress(true);
// s.bind(addr);
ch.register(selector, SelectionKey.OP_CONNECT);
ch.connect(addr);
}
}
// 10s
int select = selector.select(1000 * 10);
if (select > 0) {
System.out.println("select return: " + select + " events ; current connection per ip: " + currentConnectionPerIP);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
if (key.isConnectable()) {
SocketChannel ch = (SocketChannel) key.channel();
if (ch.finishConnect()) {
++connected;
if (connected % (connectionPerIP * locals.length / 10) == 0) {
System.out.println("connected: " + connected);
}
key.interestOps(SelectionKey.OP_READ);
}
}
}
selectedKeys.clear();
}
}
}
use of java.nio.channels.SelectionKey in project Openfire by igniterealtime.
the class ConferenceReceiver method initLoneReceiverChannel.
private void initLoneReceiverChannel(int loneReceiverPort) {
if (this.loneReceiverPort != loneReceiverPort && loneReceiverChannel != null) {
close();
}
this.loneReceiverPort = loneReceiverPort;
try {
selector = Selector.open();
} catch (IOException e) {
Logger.println("Conference receiver failed to open selector " + e.getMessage());
return;
}
if (loneReceiverPort == 0) {
return;
}
Logger.println("Init lone channel using port " + loneReceiverPort);
try {
loneReceiverChannel = DatagramChannel.open();
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Opened lone receiver channel " + loneReceiverChannel);
}
} catch (IOException e) {
Logger.println("Conference receiver failed to open DatagramChannel " + " " + e.getMessage());
return;
}
try {
loneReceiverChannel.configureBlocking(false);
} catch (IOException e) {
Logger.println("Conference receiver failed to configureBlocking to false " + e.getMessage());
return;
}
DatagramSocket socket = loneReceiverChannel.socket();
try {
socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
} catch (SocketException e) {
Logger.println("ConferenceReceiver failed to set receive buffer size " + e.getMessage());
return;
}
try {
socket.setSoTimeout(0);
} catch (SocketException e) {
Logger.println("ConferenceReceiver failed to set timeout " + e.getMessage());
return;
}
InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), loneReceiverPort);
try {
socket.bind(isa);
} catch (IOException e) {
Logger.println("Conference receiver unable to bind to " + loneReceiverPort + " " + e.getMessage());
return;
}
try {
SelectionKey selectionKey = loneReceiverChannel.register(selector, SelectionKey.OP_READ);
} catch (Exception e) {
Logger.println("Conference receiver unable to register: " + e.getMessage());
return;
}
memberCount++;
Logger.println("Lone Channel uses port " + loneReceiverPort);
}
use of java.nio.channels.SelectionKey in project java-design-patterns by iluwatar.
the class NioReactor method registerChannel.
/**
* Registers a new channel (handle) with this reactor. Reactor will start waiting for events on this channel and
* notify of any events. While registering the channel the reactor uses {@link AbstractNioChannel#getInterestedOps()}
* to know about the interested operation of this channel.
*
* @param channel
* a new channel on which reactor will wait for events. The channel must be bound prior to being registered.
* @return this
* @throws IOException
* if any I/O error occurs.
*/
public NioReactor registerChannel(AbstractNioChannel channel) throws IOException {
SelectionKey key = channel.getJavaChannel().register(selector, channel.getInterestedOps());
key.attach(channel);
channel.setReactor(this);
return this;
}
use of java.nio.channels.SelectionKey in project hs4j by killme2008.
the class AbstractNioSession method flush0.
protected final void flush0() {
SelectionKey tmpKey = null;
Selector writeSelector = null;
int attempts = 0;
try {
while (true) {
if (writeSelector == null) {
writeSelector = SelectorFactory.getSelector();
if (writeSelector == null) {
return;
}
tmpKey = selectableChannel.register(writeSelector, SelectionKey.OP_WRITE);
}
if (writeSelector.select(1000) == 0) {
attempts++;
if (attempts > 2) {
return;
}
} else {
break;
}
}
onWrite(selectableChannel.keyFor(writeSelector));
} catch (ClosedChannelException cce) {
onException(cce);
log.error("Flush error", cce);
close();
} catch (IOException ioe) {
onException(ioe);
log.error("Flush error", ioe);
close();
} finally {
if (tmpKey != null) {
// Cancel the key.
tmpKey.cancel();
tmpKey = null;
}
if (writeSelector != null) {
try {
writeSelector.selectNow();
} catch (IOException e) {
log.error("Temp selector selectNow error", e);
}
// return selector
SelectorFactory.returnSelector(writeSelector);
}
}
}
Aggregations