use of org.xnio.XnioWorker in project undertow by undertow-io.
the class ModClusterTestSetup method main.
public static void main(final String[] args) throws IOException {
final Undertow server;
final XnioWorker worker = Xnio.getInstance().createWorker(OptionMap.EMPTY);
final ModCluster modCluster = ModCluster.builder(worker).setHealthCheckInterval(TimeUnit.SECONDS.toMillis(3)).setRemoveBrokenNodes(TimeUnit.SECONDS.toMillis(30)).build();
try {
if (chost == null) {
// We are going to guess it.
chost = java.net.InetAddress.getLocalHost().getHostName();
System.out.println("Using: " + chost + ":" + cport);
}
modCluster.start();
// Create the proxy and mgmt handler
final HttpHandler proxy = modCluster.createProxyHandler();
final MCMPConfig config = MCMPConfig.builder().setManagementHost(chost).setManagementPort(cport).enableAdvertise().setSecurityKey("secret").getParent().build();
final MCMPConfig webConfig = MCMPConfig.webBuilder().setManagementHost(chost).setManagementPort(cport).build();
// Setup specific rewrite rules for the mod_cluster tests.
final HttpHandler root = Handlers.predicates(PredicatedHandlersParser.parse("regex[pattern='cluster.domain.com', value='%{i,Host}'] and equals[%R, '/'] -> rewrite['/myapp/MyCount']\n" + "regex[pattern='cluster.domain.org', value='%{i,Host}'] and regex['/(.*)'] -> rewrite['/myapp/${1}']\n" + "regex[pattern='cluster.domain.net', value='%{i,Host}'] and regex['/test/(.*)'] -> rewrite['/myapp/${1}']\n" + "regex[pattern='cluster.domain.info', value='%{i,Host}'] and path-template['/{one}/{two}'] -> rewrite['/test/${two}?partnerpath=/${one}&%q']\n", ModClusterTestSetup.class.getClassLoader()), proxy);
final HttpHandler mcmp = config.create(modCluster, root);
final HttpHandler web = webConfig.create(modCluster, ResponseCodeHandler.HANDLE_404);
server = Undertow.builder().addHttpListener(cport, chost).addHttpListener(pport, phost).setHandler(Handlers.path(mcmp).addPrefixPath("/mod_cluster_manager", web)).build();
server.start();
// Start advertising the mcmp handler
modCluster.advertise(config);
final Runnable r = new Runnable() {
@Override
public void run() {
modCluster.stop();
server.stop();
}
};
Runtime.getRuntime().addShutdownHook(new Thread(r));
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.xnio.XnioWorker in project undertow by undertow-io.
the class WebSocketExtensionBasicTestCase method testLongMessageWithoutExtensions.
@Test
@Ignore
public void testLongMessageWithoutExtensions() throws Exception {
XnioWorker client;
Xnio xnio = Xnio.getInstance(WebSocketExtensionBasicTestCase.class.getClassLoader());
client = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, 2).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 30).set(Options.WORKER_TASK_MAX_THREADS, 30).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
WebSocketProtocolHandshakeHandler handler = webSocketDebugHandler().addExtension(new PerMessageDeflateHandshake());
DebugExtensionsHeaderHandler debug = new DebugExtensionsHeaderHandler(handler);
DefaultServer.setRootHandler(path().addPrefixPath("/", debug));
final WebSocketClientNegotiation negotiation = null;
final WebSocketChannel clientChannel = WebSocketClient.connect(client, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI("http://localhost:8080"), WebSocketVersion.V13, negotiation).get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage() - Client - Received: " + data.getBytes().length + " bytes");
result.set(data);
latch.countDown();
}
@Override
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
message.getData().close();
WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
WebSocketLogger.ROOT_LOGGER.info("onError");
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
clientChannel.resumeReceives();
int LONG_MSG = 75 * 1024;
StringBuilder longMsg = new StringBuilder(LONG_MSG);
for (int i = 0; i < LONG_MSG; i++) {
longMsg.append(new Integer(i).toString().charAt(0));
}
StreamSinkFrameChannel sendChannel = clientChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener(longMsg.toString()).setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(longMsg.toString(), result.get());
clientChannel.sendClose();
stopWorker(client);
}
use of org.xnio.XnioWorker in project undertow by undertow-io.
the class WebSocketExtensionBasicTestCase method testLongTextMessage.
@Test
public void testLongTextMessage() throws Exception {
XnioWorker client;
Xnio xnio = Xnio.getInstance(WebSocketExtensionBasicTestCase.class.getClassLoader());
client = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, 2).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 30).set(Options.WORKER_TASK_MAX_THREADS, 30).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
WebSocketProtocolHandshakeHandler handler = webSocketDebugHandler().addExtension(new PerMessageDeflateHandshake());
DebugExtensionsHeaderHandler debug = new DebugExtensionsHeaderHandler(handler);
DefaultServer.setRootHandler(path().addPrefixPath("/", debug));
final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
List<WebSocketExtension> extensionsList = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensionsList);
Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
extensionHandshakes.add(new PerMessageDeflateHandshake(true));
final WebSocketChannel clientChannel = WebSocketClient.connect(client, null, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI(DefaultServer.getDefaultServerURL()), WebSocketVersion.V13, negotiation, extensionHandshakes).get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
// WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage() - Client - Received: " + data.getBytes().length + " bytes.");
result.set(data);
latch.countDown();
}
@Override
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
message.getData().close();
WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
WebSocketLogger.ROOT_LOGGER.info("onError");
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
clientChannel.resumeReceives();
int LONG_MSG = 125 * 1024;
StringBuilder longMsg = new StringBuilder(LONG_MSG);
for (int i = 0; i < LONG_MSG; i++) {
longMsg.append(Integer.toString(i).charAt(0));
}
WebSockets.sendTextBlocking(longMsg.toString(), clientChannel);
latch.await(300, TimeUnit.SECONDS);
Assert.assertEquals(longMsg.toString(), result.get());
clientChannel.sendClose();
stopWorker(client);
}
use of org.xnio.XnioWorker in project indy by Commonjava.
the class HttpProxy method start.
@Override
public void start() throws IndyLifecycleException {
if (!config.isEnabled()) {
logger.info("HTTProx proxy is disabled.");
return;
}
String bind;
if (bootOptions.getBind() == null) {
bind = "0.0.0.0";
} else {
bind = bootOptions.getBind();
}
logger.info("Starting HTTProx proxy on: {}:{}", bind, config.getPort());
XnioWorker worker;
try {
worker = Xnio.getInstance().createWorker(OptionMap.EMPTY);
final InetSocketAddress addr;
if (config.getPort() < 1) {
ThreadLocal<InetSocketAddress> using = new ThreadLocal<>();
ThreadLocal<IOException> errorHolder = new ThreadLocal<>();
server = PortFinder.findPortFor(16, (foundPort) -> {
InetSocketAddress a = new InetSocketAddress(bind, config.getPort());
AcceptingChannel<StreamConnection> result = worker.createStreamConnectionServer(a, acceptHandler, OptionMap.EMPTY);
result.resumeAccepts();
using.set(a);
return result;
});
addr = using.get();
config.setPort(addr.getPort());
} else {
addr = new InetSocketAddress(bind, config.getPort());
server = worker.createStreamConnectionServer(addr, acceptHandler, OptionMap.EMPTY);
server.resumeAccepts();
}
logger.info("HTTProxy listening on: {}", addr);
} catch (IllegalArgumentException | IOException e) {
throw new IndyLifecycleException("Failed to start HTTProx general content proxy: %s", e, e.getMessage());
}
}
use of org.xnio.XnioWorker in project undertow by undertow-io.
the class H2CUpgradeContinuationTestCase method beforeClass.
/**
* Initializes the server with the H2C handler and adds the echo handler to
* manage the requests.
* @throws IOException Some error
*/
@BeforeClass
public static void beforeClass() throws IOException {
// server and client pool is using 1024 for the buffer size
smallPool = new DebuggingSlicePool(new DefaultByteBufferPool(true, 1024, 1000, 10, 100));
final PathHandler path = new PathHandler().addExactPath(ECHO_PATH, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
sendEchoResponse(exchange);
}
});
server = Undertow.builder().setByteBufferPool(smallPool).addHttpListener(DefaultServer.getHostPort("default") + 1, DefaultServer.getHostAddress("default"), new Http2UpgradeHandler(path)).setSocketOption(Options.REUSE_ADDRESSES, true).build();
server.start();
// Create xnio worker
final Xnio xnio = Xnio.getInstance();
final XnioWorker xnioWorker = xnio.createWorker(null, OptionMap.builder().set(Options.WORKER_IO_THREADS, 8).set(Options.TCP_NODELAY, true).set(Options.KEEP_ALIVE, true).getMap());
worker = xnioWorker;
}
Aggregations