/* * myLib - https://github.com/taktod/myLib * Copyright (c) 2014 ttProject. All rights reserved. * * Licensed under GNU LESSER GENERAL PUBLIC LICENSE Version 3. */ package com.ttProject.flazr.test; import java.net.InetSocketAddress; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.flazr.rtmp.client.ClientHandler; import com.flazr.rtmp.client.ClientHandshakeHandler; import com.flazr.rtmp.client.ClientOptions; import com.ttProject.flazr.rtmp.RtmpDecoderEx; import com.ttProject.flazr.rtmp.RtmpEncoderEx; public class RtmpClientEntry { private static final Logger logger = LoggerFactory.getLogger(RtmpClientEntry.class); public static void main(String[] args) { ClientOptions options = new ClientOptions(); if(!options.parseCli(args)) { return; } if(options.getLoad() == 1 && options.getClientOptionsList() == null) { options.setWriterToSave(new DlWriter()); connect(options); return; } logger.error("only for one connection."); } public static void connect(ClientOptions options) { ClientBootstrap bootstrap = getBootstrap(Executors.newCachedThreadPool(), options); ChannelFuture future = bootstrap.connect(new InetSocketAddress(options.getHost(), options.getPort())); future.awaitUninterruptibly(); if(!future.isSuccess()) { logger.error("failed to make client connection."); } future.getChannel().getCloseFuture().awaitUninterruptibly(); bootstrap.getFactory().releaseExternalResources(); } private static ClientBootstrap getBootstrap(Executor executor, final ClientOptions options) { ChannelFactory factory = new NioClientSocketChannelFactory(executor, executor); ClientBootstrap bootstrap = new ClientBootstrap(factory); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("handshaker", new ClientHandshakeHandler(options)); pipeline.addLast("decoder", new RtmpDecoderEx()); pipeline.addLast("encoder", new RtmpEncoderEx()); pipeline.addLast("handler", new ClientHandler(options)); return pipeline; } }); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); return bootstrap; } }