Example usage for java.nio.channels DatagramChannel isOpen

List of usage examples for java.nio.channels DatagramChannel isOpen

Introduction

In this page you can find the example usage for java.nio.channels DatagramChannel isOpen.

Prototype

public final boolean isOpen() 

Source Link

Usage

From source file:org.sipfoundry.sipxbridge.symmitron.DataShuffler.java

/**
 * Sit in a loop running the following algorthim till exit:
 * /* w ww  . j a va 2s .  c om*/
 * <pre>
 * Let Si be a Sym belonging to Bridge B where an inbound packet P is received
 * Increment received packet count for B.
 * Record time for the received packet.
 * Record inboundAddress from where the packet was received
 * send(B,chan,inboundAddress)
 *            
 * </pre>
 * 
 */
public void run() {

    // Wait for an event one of the registered channels
    logger.debug("Starting Shuffler");

    while (true) {
        Bridge bridge = null;
        try {

            if (initializeSelectors.get()) {
                initializeSelector();
            }

            selector.select();

            while (!workQueue.isEmpty()) {
                logger.debug("Got a work item");
                WorkItem workItem = (WorkItem) workQueue.remove(0);
                workItem.doWork();
            }

            // Iterate over the set of keys for which events are
            // available
            Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
            while (selectedKeys.hasNext()) {
                SelectionKey key = (SelectionKey) selectedKeys.next();
                // The key must be removed or you can get one way audio ( i.e. will read a
                // null byte ).
                // (see issue 2075 ).
                selectedKeys.remove();
                if (!key.isValid()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Discarding packet:Key not valid");
                    }

                    continue;
                }
                if (key.isReadable()) {
                    readBuffer.clear();
                    DatagramChannel datagramChannel = (DatagramChannel) key.channel();
                    if (!datagramChannel.isOpen()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("DataShuffler: Datagram channel is closed -- discarding packet.");
                        }
                        continue;
                    }
                    bridge = ConcurrentSet.getBridge(datagramChannel);
                    if (bridge == null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("DataShuffler: Discarding packet: Could not find bridge");
                        }
                        continue;
                    }
                    Sym packetReceivedSym = bridge.getReceiverSym(datagramChannel);
                    /*
                     * Note the original hold value and put the transmitter on which this packet was received on hold.
                     */
                    if (packetReceivedSym == null || packetReceivedSym.getTransmitter() == null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "DataShuffler: Could not find sym for inbound channel -- discarding packet");
                        }
                        continue;
                    }
                    boolean holdValue = packetReceivedSym.getTransmitter().isOnHold();
                    packetReceivedSym.getTransmitter().setOnHold(true);

                    InetSocketAddress remoteAddress = (InetSocketAddress) datagramChannel.receive(readBuffer);

                    bridge.pakcetsReceived++;
                    if (bridge.getState() != BridgeState.RUNNING) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "DataShuffler: Discarding packet: Bridge state is " + bridge.getState());
                        }
                        packetReceivedSym.getTransmitter().setOnHold(holdValue);
                        continue;
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("got something on " + datagramChannel.socket().getLocalPort());
                    }

                    long stamp = getPacketCounter();
                    send(bridge, datagramChannel, remoteAddress, stamp, false);
                    /*
                     * Reset the old value.
                     */
                    packetReceivedSym.getTransmitter().setOnHold(holdValue);

                }
            }
        } catch (Exception ex) {
            logger.error("Unexpected exception occured", ex);
            if (bridge != null && bridge.sessions != null) {
                for (Sym rtpSession : bridge.sessions) {
                    rtpSession.close();
                }
            }
            if (bridge != null)
                bridge.setState(BridgeState.TERMINATED);
            continue;
        }

    }

}

From source file:org.sipfoundry.sipxrelay.DataShuffler.java

/**
 * Sit in a loop running the following algorthim till exit:
 * /*  w  w  w  . j av  a  2s.  c  om*/
 * <pre>
 * Let Si be a Sym belonging to Bridge B where an inbound packet P is received
 * Increment received packet count for B.
 * Record time for the received packet.
 * Record inboundAddress from where the packet was received
 * send(B,chan,inboundAddress)
 *            
 * </pre>
 * 
 */
public void run() {

    // Wait for an event one of the registered channels
    logger.debug("Starting Shuffler");

    while (true) {
        Bridge bridge = null;
        try {

            if (initializeSelectors.get()) {
                initializeSelector();
            }

            selector.select();

            checkWorkQueue();

            // Iterate over the set of keys for which events are
            // available
            Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
            while (selectedKeys.hasNext()) {
                SelectionKey key = (SelectionKey) selectedKeys.next();
                // The key must be removed or you can get one way audio ( i.e. will read a
                // null byte ).
                // (see issue 2075 ).
                selectedKeys.remove();
                if (!key.isValid()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Discarding packet:Key not valid");
                    }

                    continue;
                }
                if (key.isReadable()) {
                    readBuffer.clear();
                    DatagramChannel datagramChannel = (DatagramChannel) key.channel();
                    if (!datagramChannel.isOpen()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("DataShuffler: Datagram channel is closed -- discarding packet.");
                        }
                        continue;
                    }
                    bridge = ConcurrentSet.getBridge(datagramChannel);
                    if (bridge == null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("DataShuffler: Discarding packet: Could not find bridge");
                        }
                        continue;
                    }
                    Sym packetReceivedSym = bridge.getReceiverSym(datagramChannel);
                    /*
                     * Note the original hold value and put the transmitter on which this packet was received on hold.
                     */
                    if (packetReceivedSym == null || packetReceivedSym.getTransmitter() == null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "DataShuffler: Could not find sym for inbound channel -- discarding packet");
                        }
                        continue;
                    }
                    boolean holdValue = packetReceivedSym.getTransmitter().isOnHold();
                    packetReceivedSym.getTransmitter().setOnHold(true);

                    InetSocketAddress remoteAddress = (InetSocketAddress) datagramChannel.receive(readBuffer);

                    bridge.pakcetsReceived++;
                    if (bridge.getState() != BridgeState.RUNNING) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "DataShuffler: Discarding packet: Bridge state is " + bridge.getState());
                        }
                        packetReceivedSym.getTransmitter().setOnHold(holdValue);
                        continue;
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("got something on " + datagramChannel.socket().getLocalPort());
                    }

                    long stamp = getPacketCounter();
                    send(bridge, datagramChannel, remoteAddress, stamp, false);
                    /*
                     * Reset the old value.
                     */
                    packetReceivedSym.getTransmitter().setOnHold(holdValue);

                }
            }
        } catch (Exception ex) {
            logger.error("Unexpected exception occured", ex);
            if (bridge != null && bridge.sessions != null) {
                for (Sym rtpSession : bridge.sessions) {
                    rtpSession.close();
                }
            }
            if (bridge != null)
                bridge.setState(BridgeState.TERMINATED);
            continue;
        }

    }

}