All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ua.mobius.media.server.io.network.UdpManager Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

/*
 * 15/07/13 - Change notice:
 * This file has been modified by Mobius Software Ltd.
 * For more information please visit http://www.mobius.ua
 */
package ua.mobius.media.server.io.network;

import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;
import ua.mobius.media.server.scheduler.Scheduler;
import ua.mobius.media.server.scheduler.Task;

/**
 * Implements schedulable IO over UDP
 *
 * Important! Any CPU-bound action here are illegal!
 *
 * @author yulian oifa
 */
public class UdpManager {
    private final static int PORT_ANY = -1;

    /** Channel selector */
    private Selector[] selectors;

    /** bind address */
    private String bindAddress = "127.0.0.1";
    private String localBindAddress="127.0.0.1";
    
    /** local network address and subnet*/
    private byte[] localNetwork;
    private IPAddressType currNetworkType;
    private byte[] localSubnet;
    private IPAddressType currSubnetType;
        
    /** use sbc */
    private Boolean useSbc=false;
    
    /** rtp timeout in seconds */
    private int rtpTimeout=0;
    
    //port manager
    private PortManager portManager = new PortManager();
    private PortManager localPortManager=new PortManager();
    
    //poll task
    private PollTask[] pollTasks;

    //state flag
    private volatile boolean isActive;

    private volatile int count;

    //name of the interface
    private String name = "unknown";
    
    private Scheduler scheduler;
    //logger instance
    private final static Logger logger = Logger.getLogger(UdpManager.class);
    private final Object LOCK = new Object();
    
    private AtomicInteger currSelectorIndex=new AtomicInteger(0);
    /**
     * Creates UDP periphery.
     * 
     * @param name the name of the interface.
     * @scheduler the job scheduler instance.
     * @throws IOException
     */
    public UdpManager(Scheduler scheduler) throws IOException {
    	this.scheduler=scheduler;
        this.selectors =new Selector[scheduler.getPoolSize()];
        this.pollTasks=new PollTask[scheduler.getPoolSize()];
        for(int i=0;i it = localSelector.selectedKeys().iterator();
                while (it.hasNext()) {
                    SelectionKey key = it.next();
                    it.remove();
                    //get references to channel and associated RTP socket
                    DatagramChannel channel = (DatagramChannel) key.channel();
                    ProtocolHandler handler = (ProtocolHandler) key.attachment();

                    if (handler == null) {
                    	continue;
                    }
                    
                    if(!channel.isOpen())
                    {
                    	handler.onClosed();
                    	continue;
                    }
                    
                    //do read
                    if (key.isReadable()) {
                        handler.receive(channel);
                        count++;
                    }
                    
                    //do write
//                    if (key.isWritable()) {
//                        handler.send(channel);
//                    }

                }
                localSelector.selectedKeys().clear();
            } catch (IOException e) {
            	logger.error(e);
                return 0;
            } finally {
                scheduler.submit(this,scheduler.UDP_MANAGER_QUEUE);
            }

            return 0;
        }

        /**
         * Immediately start current task
         */
        public void startNow() {
            scheduler.submit(this,scheduler.UDP_MANAGER_QUEUE);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy