Example usage for io.netty.util TimerTask TimerTask

List of usage examples for io.netty.util TimerTask TimerTask

Introduction

In this page you can find the example usage for io.netty.util TimerTask TimerTask.

Prototype

TimerTask

Source Link

Usage

From source file:sas.systems.imflux.session.rtp.AbstractRtpSession.java

License:Apache License

/**
 * {@inheritDoc}/*  ww w.  j av a2s.c om*/
 */
@Override
public synchronized boolean init() {
    if (this.running.get()) {
        return true;
    }
    Class<? extends Channel> channelType;

    if (useNio) {
        // create data channel bootstrap
        //          EventLoopGroup bossGroup = new NioEventLoopGroup(5, Executors.defaultThreadFactory()); // if we want to use others than the defaults
        this.workerGroup = new NioEventLoopGroup();
        channelType = NioDatagramChannel.class;
    } else {
        this.workerGroup = new OioEventLoopGroup();
        channelType = OioDatagramChannel.class;
    }

    Bootstrap dataBootstrap = new Bootstrap();
    dataBootstrap.group(this.workerGroup).option(ChannelOption.SO_SNDBUF, this.sendBufferSize)
            .option(ChannelOption.SO_RCVBUF, this.receiveBufferSize)
            // option not set: "receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(this.receiveBufferSize)
            .channel(channelType) // use an UDP channel implementation => forces us to use AddressedEnvelope
            .handler(new ChannelInitializer<Channel>() { // is used to initialize the ChannelPipeline
                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("decoder", UdpDataPacketDecoder.getInstance());
                    pipeline.addLast("encoder", UdpDataPacketEncoder.getInstance());
                    pipeline.addLast("handler", new UdpDataHandler(AbstractRtpSession.this));
                }
            });

    // create control channel bootstrap
    Bootstrap controlBootstrap = new Bootstrap();
    controlBootstrap.group(this.workerGroup).option(ChannelOption.SO_SNDBUF, this.sendBufferSize)
            .option(ChannelOption.SO_RCVBUF, this.receiveBufferSize)
            // option not set: "receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(this.receiveBufferSize)
            .channel(channelType) // use an UDP channel implementation => forces us to use AddressedEnvelope
            .handler(new ChannelInitializer<Channel>() { // is used to initialize the ChannelPipeline
                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("decoder", UdpControlPacketDecoder.getInstance());
                    pipeline.addLast("encoder", UdpControlPacketEncoder.getInstance());
                    pipeline.addLast("handler", new UdpControlHandler(AbstractRtpSession.this));
                }
            });

    // create data channel
    SocketAddress dataAddress = this.localParticipant.getDataDestination();
    try {
        ChannelFuture future = dataBootstrap.bind(dataAddress);
        this.dataChannel = future.sync().channel(); // wait for future to complete and retrieve channel
    } catch (Exception e) {
        LOG.error("Failed to bind data channel for session with id " + this.id, e);
        shutdownEventLoopGroup();
        return false;
    }

    // create control channel
    SocketAddress controlAddress = this.localParticipant.getControlDestination();
    try {
        ChannelFuture future = controlBootstrap.bind(controlAddress);
        this.controlChannel = future.sync().channel(); // wait for future to complete and retrieve channel

    } catch (Exception e) {
        LOG.error("Failed to bind control channel for session with id " + this.id, e);
        this.dataChannel.close();
        shutdownEventLoopGroup();
        return false;
    }

    LOG.debug("Data & Control channels bound for RtpSession with id {}.", this.id);
    // Send first RTCP packet.
    this.joinSession(this.localParticipant.getSsrc());
    this.running.set(true);

    // Add the participant database cleaner.
    this.timer.newTimeout(new TimerTask() {
        @Override
        public void run(Timeout timeout) throws Exception {
            if (!running.get()) {
                return;
            }

            participantDatabase.cleanup();
            timer.newTimeout(this, participantDatabaseCleanup, TimeUnit.SECONDS);
        }
    }, this.participantDatabaseCleanup, TimeUnit.SECONDS);

    // Add the periodic RTCP report generator.
    if (this.automatedRtcpHandling) {
        this.timer.newTimeout(this, this.updatePeriodicRtcpSendInterval(), TimeUnit.SECONDS);
    }

    if (this.internalTimer) {
        this.timer.start();
    }

    return true;
}