public static ChannelHandler createDecoder() { int maxObjectSize = AppProperties.getAsInt("rpc_common_maxObjectSize", 1024 * 1024); String f = AppProperties.get("rpc_common_codec", "java"); if (f.equalsIgnoreCase("java")) { return new ObjectDecoder(maxObjectSize, ClassResolvers.softCachingConcurrentResolver(null)); } else if (f.equalsIgnoreCase("jbossSerialization")) { return new JBossSerializationDecoder(maxObjectSize); } else if (f.equalsIgnoreCase("json")) { throw new AppRuntimeException("not support " + f + " yet!"); } else if (f.equalsIgnoreCase("hessian")) { return new HessianDecoder(maxObjectSize); } else { throw new AppRuntimeException("not support " + f + " yet!"); } }
@Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = super.createRegistry(); // setup the String encoder and decoder StringDecoder stringDecoder = new StringDecoder(); registry.bind("string-decoder", stringDecoder); StringEncoder stringEncoder = new StringEncoder(); registry.bind("string-encoder", stringEncoder); List<ChannelHandler> decoders = new ArrayList<ChannelHandler>(); decoders.add(stringDecoder); List<ChannelHandler> encoders = new ArrayList<ChannelHandler>(); encoders.add(stringEncoder); registry.bind("encoders", encoders); registry.bind("decoders", decoders); return registry; }
public MasterServer(final ChannelHandler handler){ NioServerSocketChannelFactory channelFactory= new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); bootstrap=new ServerBootstrap(channelFactory); pipelineFactory=new ChannelPipelineFactory(){ private final ProtobufVarint32LengthFieldPrepender frameEncoder = new ProtobufVarint32LengthFieldPrepender(); private final ProtobufEncoder protobufEncoder = new ProtobufEncoder(); public ChannelPipeline getPipeline() throws Exception { ChannelPipeline p = pipeline(); p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder()); p.addLast("protobufDecoder",new ProtobufDecoder(Protocol.SocketMessage.getDefaultInstance())); p.addLast("frameEncoder", frameEncoder); p.addLast("protobufEncoder", protobufEncoder); p.addLast("handler", handler); return p; } }; try { bootstrap.setPipeline(pipelineFactory.getPipeline()); } catch (Exception e) { e.printStackTrace(); } }
private static ClientBootstrap prepareBootstrap(Logger logger, final ChannelPipeline pipeline, ChannelHandler handler, SslHandler sslHandler, int connectTimeoutMillis) { ClientBootstrap bootstrap = new ClientBootstrap(nioClientSocketChannelFactory); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("reuseAddress", true); bootstrap.setOption("connectTimeoutMillis", connectTimeoutMillis); bootstrap.setOption("writeBufferHighWaterMark", 10 * 1024 * 1024); if (sslHandler != null) { pipeline.addFirst("ssl", sslHandler); } if (handler != null) { pipeline.addLast("handler", handler); } bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return pipeline; } }); return bootstrap; }
/** * creates a new pipeline for interacting with the * controller. This is where the controllerHandler and * the timeouthandler come into play * @return the pipeline (ChannelPipeline) for a new Socket. */ private ChannelPipeline getPipeline(){ ChannelPipeline pipe = Channels.pipeline(); ChannelHandler idleHandler = new IdleStateHandler(timer, 20, 25, 0); ChannelHandler readTimeoutHandler = new ReadTimeoutHandler(timer, 30); OFControllerChannelHandler controllerHandler = new OFControllerChannelHandler(); pipe.addLast("ofmessagedecoder", new OFMessageDecoder()); pipe.addLast("ofmessageencoder", new OFMessageEncoder()); pipe.addLast("idle", idleHandler); pipe.addLast("timeout", readTimeoutHandler); pipe.addLast("handshaketimeout", new ControllerHandshakeTimeoutHandler(controllerHandler, timer, 15)); pipe.addLast("handler", controllerHandler); return pipe; }
public static ChannelHandler getIsdnClientStateMachineHandler(IsdnChannel channel, String handlerName) { StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(IsdnConnectionHandler.PLCI_IDLE, new IsdnConnectionHandler()); StateContextLookup stateContextLookup = new ChannelHandlerContextLookup(new DefaultStateContextFactory(), channel, handlerName); StateMachineProxyBuilder proxyBuilder = new StateMachineProxyBuilder(); proxyBuilder.setName("IsdnClientChannelStateMachine"); proxyBuilder.setStateContextLookup(stateContextLookup); // proxyBuilder.setEventArgumentsInterceptor(new // NettyEventInterceptor()); proxyBuilder.setEventFactory(new NettyEventFactory()); IStateMachineChannelHandler engine = proxyBuilder.create(IStateMachineChannelHandler.class, sm); return new ChannelAllCoverageWrapper(new DefaultStateMachineChannelHandler(engine)); }
public static ChannelHandler getAcceptedChannelStateMachineHandler(IsdnChannel channel, String handlerName) { StateMachine sm = StateMachineFactory.getInstance(Transition.class).create( IsdnConnectionHandler.P4_WF_CONNECT_ACTIVE_IND, new IsdnConnectionHandler()); StateContextLookup stateContextLookup = new ChannelHandlerContextLookup(new DefaultStateContextFactory(), channel, handlerName); StateMachineProxyBuilder proxyBuilder = new StateMachineProxyBuilder(); proxyBuilder.setName("IsdnAcceptedChannelStateMachine"); proxyBuilder.setStateContextLookup(stateContextLookup); // proxyBuilder.setEventArgumentsInterceptor(new // NettyEventInterceptor()); proxyBuilder.setEventFactory(new NettyEventFactory()); IStateMachineChannelHandler engine = proxyBuilder.create(IStateMachineChannelHandler.class, sm); return new ChannelAllCoverageWrapper(new DefaultStateMachineChannelHandler(engine)); }
/** * Replace this {@link FrameDecoder} in the {@link ChannelPipeline} with the * given {@link ChannelHandler}. All remaining bytes in the * {@link ChannelBuffer} will get send to the new {@link ChannelHandler} * that was used as replacement * */ public void replace(String handlerName, ChannelHandler handler) { if (ctx == null) { throw new IllegalStateException("Replace cann only be called once the FrameDecoder is added to the ChannelPipeline"); } ChannelPipeline pipeline = ctx.getPipeline(); pipeline.addAfter(ctx.getName(), handlerName, handler); try { if (cumulation != null) { Channels.fireMessageReceived(ctx, cumulation.readBytes(actualReadableBytes())); } } finally { pipeline.remove(this); } }
/** * Dependency injection friendly convenience method for * {@link #setPipeline(ChannelPipeline)} which sets the default pipeline of * this bootstrap from an ordered map. * <p> * Please note that this method is a convenience method that works only * when <b>1)</b> you create only one channel from this bootstrap (e.g. * one-time client-side or connectionless channel) or <b>2)</b> all handlers * in the pipeline is stateless. You have to use * {@link #setPipelineFactory(ChannelPipelineFactory)} if <b>1)</b> your * pipeline contains a stateful {@link ChannelHandler} and <b>2)</b> one or * more channels are going to be created by this bootstrap (e.g. server-side * channels). * * @throws IllegalArgumentException * if the specified map is not an ordered map */ public void setPipelineAsMap(Map<String, ChannelHandler> pipelineMap) { if (pipelineMap == null) { throw new NullPointerException("pipelineMap"); } if (!isOrderedMap(pipelineMap)) { throw new IllegalArgumentException( "pipelineMap is not an ordered map. " + "Please use " + LinkedHashMap.class.getName() + '.'); } ChannelPipeline pipeline = pipeline(); for (Map.Entry<String, ChannelHandler> e: pipelineMap.entrySet()) { pipeline.addLast(e.getKey(), e.getValue()); } setPipeline(pipeline); }
public ChannelPipeline getPipeline() throws Exception { ChannelPipeline p = Channels.pipeline(); if (extraHandlers != null) { for (final ChannelHandler handler : this.extraHandlers.getClientHandlers()) { p.addLast("" + handler.hashCode(), handler); } } // Do not use FixedLengthFrameDecoder provided by netty to avoid // copy and an extra handler to parse the messages // p.addLast("pnpDecoder", new PNPClientFrameDecoder()); p.addLast("pnpDecoder", new PNPClientFrameDecoder()); p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("pnpEncoder", new PNPEncoder()); long idle_timeout = PNPConfig.PA_PNP_IDLE_TIMEOUT.getValue(); if (idle_timeout != 0) { p.addLast("timer", new IdleStateHandler(timer, 0, idle_timeout, 0, TimeUnit.MILLISECONDS)); } p.addLast(PNPClientHandler.NAME, new PNPClientHandler()); return p; }
public ChannelPipeline getPipeline() throws Exception { PNPServerHandler pnpServerHandler = new PNPServerHandler(this.executor); ChannelPipeline p = Channels.pipeline(); if (extraHandlers != null) { for (final ChannelHandler handler : extraHandlers.getServertHandlers()) { p.addLast("" + handler.hashCode(), handler); } } p.addLast("pnpDecoder", new PNPServerFrameDecoder(pnpServerHandler, timer)); p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("pnpEncoder", new PNPEncoder()); p.addLast(PNPServerHandler.NAME, pnpServerHandler); return p; }
public NettyCodecAdapter(Codec2 codec, URL url, com.alibaba.dubbo.remoting.ChannelHandler handler) { this.codec = codec; this.url = url; this.handler = handler; int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE); this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE; }
private void addDynamicHandlers(ChannelPipeline pipeline) { if (Context.getConfig().hasKey("extra.handlers")) { String[] handlers = Context.getConfig().getString("extra.handlers").split(","); for (int i = 0; i < handlers.length; i++) { try { pipeline.addLast("extraHandler." + i, (ChannelHandler) Class.forName(handlers[i]).newInstance()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException error) { Log.warning(error); } } } }
@Override protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getFinalChannelHandlers(MessageInput input) { final LinkedHashMap<String, Callable<? extends ChannelHandler>> finalChannelHandlers = super.getFinalChannelHandlers(input); final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlers = new LinkedHashMap<>(); handlers.put("beats", BeatsFrameDecoder::new); handlers.putAll(finalChannelHandlers); return handlers; }
@Test public void getFinalChannelHandlers() throws Exception { final BeatsTransport transport = new BeatsTransport( new Configuration(null), new ThroughputCounter(new HashedWheelTimer()), new LocalMetricRegistry(), Executors.newSingleThreadExecutor(), new ConnectionCounter() ); final MessageInput input = mock(MessageInput.class); final LinkedHashMap<String, Callable<? extends ChannelHandler>> channelHandlers = transport.getFinalChannelHandlers(input); assertThat(channelHandlers).containsKey("beats"); }
@Bean public ChannelHandler metricChannelFactory() { return new MetricChannel( (CounterMetric) beanFactory .getBean("connectionsReceivedMetric"), (CounterMetric) beanFactory .getBean("connectionsProcessedMetric"), (CounterMetric) beanFactory.getBean("kilobytesWrttenMetric"), (CounterMetric) beanFactory.getBean("kilobytesReceivedMetric"), (CounterMetric) beanFactory.getBean("errorsMetric")); }
public CollectorServerImpl(int port, ChannelHandler channelHandler, Configuration conf, ChannelHandler metricsHandler, IpFilterHandler ipFilterHandler) { super(); this.port = port; this.channelHandler = channelHandler; this.conf = conf; this.metricsHandler = metricsHandler; this.ipFilterHandler = ipFilterHandler; }
public CoordinationServerImpl(int lockPort, int releaseLockPort, ChannelHandler lockHandler, ChannelHandler unlockHandler, ChannelHandler metricHandler) { super(); this.lockPort = lockPort; this.releaseLockPort = releaseLockPort; this.lockHandler = lockHandler; this.unlockHandler = unlockHandler; this.metricHandler = metricHandler; }
public void start() { ResteasyDeployment dp = new ResteasyDeployment(); Collection<Object> providers = ac.getBeansWithAnnotation(Provider.class).values(); Collection<Object> controllers = ac.getBeansWithAnnotation(Controller.class).values(); Assert.notEmpty(controllers); // extract providers if (providers != null) { dp.getProviders().addAll(providers); } // extract only controller annotated beans dp.getResources().addAll(controllers); Map<String, Object> channelOptions = new HashMap<String, Object>(); channelOptions.put("reuseAddress", true); List<ChannelHandler> channelHandlerList = new ArrayList<ChannelHandler>(); channelHandlerList.add(channelHandler); channelHandlerList.add(idleStateHandler); channelHandlerList.add(healthCheckHandler); netty = new NettyJaxrsServer(); netty.setChannelOptions(channelOptions); netty.setDeployment(dp); netty.setPort(port); netty.setRootResourcePath("/resteasy"); netty.setIoWorkerCount(ioWorkerCount); netty.setExecutorThreadCount(executorThreadCount); netty.setMaxRequestSize(maxRequestSize); netty.setSSLContext(sslContext); netty.setKeepAlive(true); netty.setChannelHandlers(channelHandlerList); netty.setSecurityDomain(null); netty.start(); }
public static ChannelHandler createEncoder() { String f = AppProperties.get("rpc_common_codec", "java"); if (f.equalsIgnoreCase("java")) { return new ObjectEncoder(); } else if (f.equalsIgnoreCase("jbossSerialization")) { return new JBossSerializationEncoder(); } else if (f.equalsIgnoreCase("json")) { throw new AppRuntimeException("not support " + f + " yet!"); } else if (f.equalsIgnoreCase("hessian")) { return new HessianEncoder(); } else { throw new AppRuntimeException("not support " + f + " yet!"); } }
/** * @param remoteHost A unique name for the host to which the connection is being made. * @param port The remote host port being used for the connection. * @param usePrivilegedPort * <ul> * <li>If <code>true</code>, use a privileged port (below 1024) * for RPC communication.</li> * <li>If <code>false</code>, use any non-privileged port for RPC * communication.</li> * </ul> */ public Connection(String remoteHost, int port, boolean usePrivilegedPort) { _remoteHost = remoteHost; _port = port; _usePrivilegedPort = usePrivilegedPort; _clientBootstrap = new ClientBootstrap(NetMgr.getInstance().getFactory()); // Configure the client. _clientBootstrap.setOption(REMOTE_ADDRESS_OPTION, new InetSocketAddress(_remoteHost, _port)); _clientBootstrap.setOption("connectTimeoutMillis", CONNECT_TIMEOUT); // set // connection // timeout // value // to // 10 // seconds _clientBootstrap.setOption("tcpNoDelay", true); _clientBootstrap.setOption("keepAlive", true); _clientBootstrap.setOption(CONNECTION_OPTION, this); // Configure the pipeline factory. _clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() { /** * Netty helper instance. */ private final ChannelHandler ioHandler = new ClientIOHandler(_clientBootstrap); /* (non-Javadoc) * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline() */ public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new RPCRecordDecoder(), ioHandler); } }); }
public NettyCodecAdapter(Codec2 codec, URL url, net.jahhan.spi.ChannelHandler handler) { this.codec = codec; this.url = url; this.handler = handler; int b = url.getPositiveParameter(Constants.BUFFER_KEY, Constants.DEFAULT_BUFFER_SIZE); this.bufferSize = b >= Constants.MIN_BUFFER_SIZE && b <= Constants.MAX_BUFFER_SIZE ? b : Constants.DEFAULT_BUFFER_SIZE; }
@Override public NettyHttpConfiguration copy() { try { // clone as NettyHttpConfiguration NettyHttpConfiguration answer = (NettyHttpConfiguration) clone(); // make sure the lists is copied in its own instance List<ChannelHandler> encodersCopy = new ArrayList<ChannelHandler>(getEncoders()); answer.setEncoders(encodersCopy); List<ChannelHandler> decodersCopy = new ArrayList<ChannelHandler>(getDecoders()); answer.setDecoders(decodersCopy); return answer; } catch (CloneNotSupportedException e) { throw new RuntimeCamelException(e); } }
@Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = super.createRegistry(); List<ChannelHandler> decoders = new ArrayList<ChannelHandler>(); decoders.add(new HttpContentDecompressor()); registry.bind("myDecoders", decoders); return registry; }
public static ChannelHandlerFactory newObjectDecoder() { return new ChannelHandlerFactory() { @Override public ChannelHandler newChannelHandler() { return new ObjectDecoder(ClassResolvers.weakCachingResolver(null)); } }; }
public static ChannelHandlerFactory newDelimiterBasedFrameDecoder(final int maxFrameLength, final ChannelBuffer[] delimiters) { return new ChannelHandlerFactory() { @Override public ChannelHandler newChannelHandler() { return new DelimiterBasedFrameDecoder(maxFrameLength, true, delimiters); } }; }
public static ChannelHandlerFactory newLengthFieldBasedFrameDecoder(final int maxFrameLength, final int lengthFieldOffset, final int lengthFieldLength, final int lengthAdjustment, final int initialBytesToStrip) { return new ChannelHandlerFactory() { @Override public ChannelHandler newChannelHandler() { return new LengthFieldBasedFrameDecoder(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip); } }; }
/** * Returns a copy of this configuration */ public NettyConfiguration copy() { try { NettyConfiguration answer = (NettyConfiguration) clone(); // make sure the lists is copied in its own instance List<ChannelHandler> encodersCopy = new ArrayList<ChannelHandler>(encoders); answer.setEncoders(encodersCopy); List<ChannelHandler> decodersCopy = new ArrayList<ChannelHandler>(decoders); answer.setDecoders(decodersCopy); return answer; } catch (CloneNotSupportedException e) { throw new RuntimeCamelException(e); } }