public void buildHandlers( ApplicationState appState, XioServerConfig config, XioServerState state, ChannelPipeline pipeline) { super.buildHandlers(appState, config, state, pipeline); if (fragment != null) { fragment.buildHandlers(appState, config, state, pipeline); } } }
public void buildHandlers(ChannelPipeline pipeline) { for (XioPipelineFragment fragment : fragments) { fragment.buildHandlers(appState, config, state, pipeline); } }
public XioServerBootstrap addToPipeline(XioPipelineFragment fragment) { // TODO(CK): interrogate fragment for channel options pipelineAssembler.addFragment(fragment); return this; }
XioServerState state, ChannelPipeline pipeline) { ChannelHandler idleDisconnectHandler = getIdleDisconnectHandler(config.getLimits()); pipeline.addLast("idleDisconnectHandler", idleDisconnectHandler); pipeline.addLast( pipeline.addLast("connectionContext", new ConnectionContextHandler()); pipeline.addLast("globalChannelStatistics", state.getChannelStatistics()); ChannelHandler encryptionHandler = getEncryptionHandler(config, state); addHandler(pipeline, "encryptionHandler", encryptionHandler); addHandler(pipeline, "tls authentication handler", getTlsAuthenticationHandler()); if (config.isMessageLoggerEnabled()) { pipeline.addLast("messageLogger", new XioMessageLogger(XioServer.class, config.getName())); addHandler(pipeline, "codecNegotiation", getCodecNegotiationHandler(config)); ChannelHandler codecHandler = getCodecHandler(config); if (codecHandler != null) { pipeline.addLast("codec", codecHandler); addHandler(pipeline, "application codec", getApplicationCodec(config)); addHandler(pipeline, "metric handler", new MetricsHandler(appState.getMetricRegistry())); addHandler(pipeline, "distributed tracing", state.tracingHandler(appState)); addHandler(pipeline, "application router", getApplicationRouter()); addHandler(pipeline, "authentication handler", getAuthenticationHandler()); pipeline.addLast("l7DeterministicRuleEngine", new Http1Filter(appState.getHttp1FilterConfig())); addHandler(pipeline, "authorization handler", getAuthorizationHandler()); ChannelHandler applicationHandler = getApplicationHandler(); addHandler(pipeline, "applicationHandler", applicationHandler);
public static void main(String args[]) throws Exception { Application application = new ApplicationBootstrap("exhibitor-test") .addServer("main", bs -> bs.addToPipeline(new SmartHttpPipeline())) .build(); application .getState() .getZkClient() .getClient() .create() .creatingParentsIfNeeded() .forPath("/test/key", "value".getBytes()); } }
@Test public void testEchoServerLargePayload() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testEchoServer").addToPipeline(new XioEchoPipeline()); try (XioServer server = bootstrap.build(); EchoClient client = new EchoClient()) { client.connect(server.getInstrumentation().addressBound()); int n = 800; String payload = "Netty rocks!"; StringBuilder builder = new StringBuilder(n * payload.length()); for (int i = 0; i < n; i += 1) { builder.append(payload); } payload = builder.toString(); client.send(payload); String response = client.recv(); assertEquals(payload, response); } } }
@Test public void testEchoServerLargePayload() { try (EchoClient client = new EchoClient(); EchoServer server = new EchoServer()) { server.bind(new InetSocketAddress("127.0.0.1", 0)); XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testTcpProxyServer") .addToPipeline(new XioTcpProxyPipeline(server.addressBound())); try (XioServer proxy = bootstrap.build()) { client.connect(proxy.getInstrumentation().addressBound()); server.accept(); int n = 800; String payload = "Netty rocks!"; StringBuilder builder = new StringBuilder(n * payload.length()); for (int i = 0; i < n; i += 1) { builder.append(payload); } payload = builder.toString(); client.send(payload); String line = server.process(); String response = client.recv(); assertEquals(payload, line); assertEquals(payload, response); } } } }
public XioChannelInitializer build(XioServerInstrumentation instrumentation) { for (XioPipelineFragment fragment : fragments) { String protocol = fragment.applicationProtocol(); if (protocol != null) { if (instrumentation.applicationProtocol != null) { throw new RuntimeException( "Only one application protocol can be defined for a given pipeline"); } instrumentation.applicationProtocol = protocol; } } return buildInitializer(); } }
public SmartHttpPipeline(XioChannelHandlerFactory factory) { this.fragment = new XioSimplePipelineFragment(factory); }
@Override public void initChannel(SocketChannel channel) { pipelineAssembler.buildHandlers(channel.pipeline()); } }
public void buildHandlers( ApplicationState appState, XioServerConfig config, XioServerState state, ChannelPipeline pipeline) { pipeline.addLast(pipelineLabel, factory.build()); } }
private XioChannelInitializer buildInitializer() { if (fragments.size() == 0) { throw new RuntimeException( "At least one pipeline fragment needs to be added prior to calling build"); } return new XioChannelInitializer(this); }
public XioServer build() { log.debug("Building"); serverBootstrap.group(channelConfig.bossGroup(), channelConfig.workerGroup()); serverBootstrap.channel(channelConfig.channel()); final XioServerInstrumentation instrumentation = new XioServerInstrumentation(); serverBootstrap.childHandler(pipelineAssembler.build(instrumentation)); ChannelFuture future = serverBootstrap.bind(); future.awaitUninterruptibly(); if (future.isSuccess()) { instrumentation.addressBound = (InetSocketAddress) future.channel().localAddress(); } else { log.error("Couldn't bind channel", future.cause()); throw new RuntimeException(future.cause()); } return new XioServer(future.channel(), instrumentation, config, state); } }
public XioServerBootstrap( ApplicationState appState, XioServerConfig config, XioServerState state) { serverBootstrap = new ServerBootstrap(); pipelineAssembler = new XioPipelineAssembler(appState, config, state); this.config = config; this.state = state; if (XioServiceLocator.getInstance() == null) { XioServiceLocator.buildInstance(appState.config(), appState); } bindAddress(config.getBindAddress()); channelConfig(appState.getChannelConfiguration()); }
@Before public void before() { application = new ApplicationBootstrap("xio.testZipkinApplication") .addServer( "exampleServer", (bs) -> bs.addToPipeline(new SmartHttpPipeline(TestHandler::new))) .build(); MetricRegistry metricRegistry = mock(MetricRegistry.class); requestsMeter = mock(Meter.class); when(metricRegistry.meter("requests")).thenReturn(requestsMeter); successMeter = mock(Meter.class); when(metricRegistry.meter("statusClassSuccess")).thenReturn(successMeter); application.getState().setMetricRegistry(metricRegistry); }
@Test public void testEchoServer() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testEchoServer").addToPipeline(new XioEchoPipeline()); try (XioServer server = bootstrap.build(); EchoClient client = new EchoClient()) { client.connect(server.getInstrumentation().addressBound()); String payload = "test message"; client.send(payload); String response = client.recv(); assertEquals(payload, response); } }
@Test public void testProxyServer() { try (EchoClient client = new EchoClient(); EchoServer server = new EchoServer()) { server.bind(new InetSocketAddress("127.0.0.1", 0)); XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testTcpProxyServer") .addToPipeline(new XioTcpProxyPipeline(server.addressBound())); try (XioServer proxy = bootstrap.build()) { client.connect(proxy.getInstrumentation().addressBound()); server.accept(); String payload = "test message"; client.send(payload); String line = server.process(); String response = client.recv(); assertEquals(payload, line); assertEquals(payload, response); } } }
@Before public void before() throws Exception { ApplicationConfig appConfig = ApplicationConfig.fromConfig("xio.testZipkinApplication"); application = new ApplicationBootstrap(appConfig, XioTracingDecorator::new) .addServer( "exampleServer", (bs) -> bs.addToPipeline(new SmartHttpPipeline(TestHandler::new))) .build(); }
@Test public void buildHttp11Server() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testHttpsServer").addToPipeline(new SmartHttpPipeline()); XioServer server = bootstrap.build(); assertEquals("ssl-http/1.1", server.getInstrumentation().applicationProtocol()); } }
@Test public void testServe404() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testHttpServer") .addToPipeline( new SmartHttpPipeline( new XioPipelineFragment() { public String applicationProtocol() { return ""; } public void buildHandlers( ApplicationState appState, XioServerConfig config, XioServerState state, ChannelPipeline pipeline) { pipeline.addLast(new XioHttp404Handler()); } })); try (XioServer server = bootstrap.build()) { Response response = ClientHelper.http(server.getInstrumentation().addressBound()); assertEquals(404, response.code()); assertEquals(server.getInstrumentation() != null, true); } } }