Package java.nio.channels

Examples of java.nio.channels.DatagramChannel


        boolean removeOpWrite = false;

        long writtenBytes = 0;

        final SocketSendBufferPool sendBufferPool = this.sendBufferPool;
        final DatagramChannel ch = channel.getDatagramChannel();
        final Queue<MessageEvent> writeBuffer = channel.writeBufferQueue;
        final int writeSpinCount = channel.getConfig().getWriteSpinCount();
        synchronized (channel.writeLock) {
            // inform the channel that write is in-progress
            channel.inWriteNowLoop = true;
View Full Code Here


    }

    @Override
    protected DatagramChannel newHandle(SocketAddress localAddress)
            throws Exception {
        DatagramChannel ch = DatagramChannel.open();
        if (localAddress != null) {
            ch.socket().bind(localAddress);
        }
        return ch;
    }
View Full Code Here

public class DatagramChannelSession extends AbstractChannelSession {

    private DatagramChannel channel;

    public DatagramSocket getSocket() {
        DatagramChannel dc = channel;
        if (dc == null)
            return null;
        return dc.socket();
    }
View Full Code Here

        {
            throw new NullPointerException( "localAddress" );
        }
       
        Selector selector = this.selector;
        DatagramChannel ch = ( DatagramChannel ) channels.get( localAddress );
        if( selector == null || ch == null )
        {
            throw new IllegalArgumentException( "Unknown localAddress: " + localAddress );
        }
           
        SelectionKey key = ch.keyFor( selector );
        if( key == null )
        {
            throw new IllegalArgumentException( "Unknown localAddress: " + localAddress );
        }
View Full Code Here

        while( it.hasNext() )
        {
            SelectionKey key = ( SelectionKey ) it.next();
            it.remove();

            DatagramChannel ch = ( DatagramChannel ) key.channel();

            RegistrationRequest req = ( RegistrationRequest ) key.attachment();
            DatagramSessionImpl session = new DatagramSessionImpl(
                    wrapper, this,
                    req.config,
View Full Code Here

        }
    }

    private void flush( DatagramSessionImpl session ) throws IOException
    {
        DatagramChannel ch = session.getChannel();

        Queue writeRequestQueue = session.getWriteRequestQueue();

        WriteRequest req;
        for( ;; )
        {
            synchronized( writeRequestQueue )
            {
                req = ( WriteRequest ) writeRequestQueue.first();
            }

            if( req == null )
                break;

            ByteBuffer buf = ( ByteBuffer ) req.getMessage();
            if( buf.remaining() == 0 )
            {
                // pop and fire event
                synchronized( writeRequestQueue )
                {
                    writeRequestQueue.pop();
                }

                session.increaseWrittenWriteRequests();
                buf.reset();
                ( ( DatagramFilterChain ) session.getFilterChain() ).messageSent( session, req );
                continue;
            }

            SelectionKey key = session.getSelectionKey();
            if( key == null )
            {
                scheduleFlush( session );
                break;
            }
            if( !key.isValid() )
            {
                continue;
            }

            SocketAddress destination = req.getDestination();
            if( destination == null )
            {
                destination = session.getRemoteAddress();
            }
           
            int writtenBytes = ch.send( buf.buf(), destination );

            if( writtenBytes == 0 )
            {
                // Kernel buffer is full
                key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
View Full Code Here

            }

            if( req == null )
                break;

            DatagramChannel ch = null;
            try
            {
                ch = DatagramChannel.open();
                DatagramSessionConfig cfg;
                if( req.config.getSessionConfig() instanceof DatagramSessionConfig )
                {
                    cfg = ( DatagramSessionConfig ) req.config.getSessionConfig();
                }
                else
                {
                    cfg = ( DatagramSessionConfig ) getDefaultConfig().getSessionConfig();
                }

                ch.socket().setReuseAddress( cfg.isReuseAddress() );
                ch.socket().setBroadcast( cfg.isBroadcast() );
                ch.socket().setReceiveBufferSize( cfg.getReceiveBufferSize() );
                ch.socket().setSendBufferSize( cfg.getSendBufferSize() );

                if( ch.socket().getTrafficClass() != cfg.getTrafficClass() )
                {
                    ch.socket().setTrafficClass( cfg.getTrafficClass() );
                }

                ch.configureBlocking( false );
                ch.socket().bind( req.address );
                ch.register( selector, SelectionKey.OP_READ, req );
                synchronized( channels )
                {
                    channels.put( req.address, ch );
                }
               
                getListeners().fireServiceActivated(
                        this, req.address, req.handler, req.config);
            }
            catch( Throwable t )
            {
                req.exception = t;
            }
            finally
            {
                synchronized( req )
                {
                    req.done = true;
                    req.notify();
                }

                if( ch != null && req.exception != null )
                {
                    try
                    {
                        ch.disconnect();
                        ch.close();
                    }
                    catch( Throwable e )
                    {
                        ExceptionMonitor.getInstance().exceptionCaught( e );
                    }
View Full Code Here

            if( request == null )
            {
                break;
            }

            DatagramChannel ch;
            synchronized( channels )
            {
                ch = ( DatagramChannel ) channels.remove( request.address );
            }

            // close the channel
            try
            {
                if( ch == null )
                {
                    request.exception = new IllegalArgumentException(
                            "Address not bound: " + request.address );
                }
                else
                {
                    SelectionKey key = ch.keyFor( selector );
                    request.registrationRequest = ( RegistrationRequest ) key.attachment();
                    key.cancel();
                    selector.wakeup(); // wake up again to trigger thread death
                    ch.disconnect();
                    ch.close();
                }
            }
            catch( Throwable t )
            {
                ExceptionMonitor.getInstance().exceptionCaught( t );
View Full Code Here

        if (config == null) {
            config = getDefaultConfig();
        }

        DatagramChannel ch = null;
        boolean initialized = false;
        try {
            ch = DatagramChannel.open();
            DatagramSessionConfig cfg;
            if (config.getSessionConfig() instanceof DatagramSessionConfig) {
                cfg = (DatagramSessionConfig) config.getSessionConfig();
            } else {
                cfg = (DatagramSessionConfig) getDefaultConfig()
                        .getSessionConfig();
            }

            ch.socket().setReuseAddress(cfg.isReuseAddress());
            ch.socket().setBroadcast(cfg.isBroadcast());
            ch.socket().setReceiveBufferSize(cfg.getReceiveBufferSize());
            ch.socket().setSendBufferSize(cfg.getSendBufferSize());

            if (ch.socket().getTrafficClass() != cfg.getTrafficClass()) {
                ch.socket().setTrafficClass(cfg.getTrafficClass());
            }

            if (localAddress != null) {
                ch.socket().bind(localAddress);
            }
            ch.connect(address);
            ch.configureBlocking(false);
            initialized = true;
        } catch (IOException e) {
            return DefaultConnectFuture.newFailedFuture(e);
        } finally {
            if (!initialized && ch != null) {
                try {
                    ch.disconnect();
                    ch.close();
                } catch (IOException e) {
                    ExceptionMonitor.getInstance().exceptionCaught(e);
                }
            }
        }

        RegistrationRequest request = new RegistrationRequest(ch, handler,
                config);
        synchronized (this) {
            try {
                startupWorker();
            } catch (IOException e) {
                try {
                    ch.disconnect();
                    ch.close();
                } catch (IOException e2) {
                    ExceptionMonitor.getInstance().exceptionCaught(e2);
                }

                return DefaultConnectFuture.newFailedFuture(e);
View Full Code Here

            }
        }
    }

    private void flush(DatagramSessionImpl session) throws IOException {
        DatagramChannel ch = session.getChannel();

        Queue writeRequestQueue = session.getWriteRequestQueue();

        WriteRequest req;
        for (;;) {
            synchronized (writeRequestQueue) {
                req = (WriteRequest) writeRequestQueue.first();
            }

            if (req == null)
                break;

            ByteBuffer buf = (ByteBuffer) req.getMessage();
            if (buf.remaining() == 0) {
                // pop and fire event
                synchronized (writeRequestQueue) {
                    writeRequestQueue.pop();
                }

                session.increaseWrittenMessages();
                buf.reset();
                session.getFilterChain().fireMessageSent(session, req);
                continue;
            }

            SelectionKey key = session.getSelectionKey();
            if (key == null) {
                scheduleFlush(session);
                break;
            }
            if (!key.isValid()) {
                continue;
            }

            int writtenBytes = ch.write(buf.buf());

            if (writtenBytes == 0) {
                // Kernel buffer is full
                key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
            } else if (writtenBytes > 0) {
View Full Code Here

TOP

Related Classes of java.nio.channels.DatagramChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.