001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt.nio;
009    
010    import java.io.IOException;
011    import java.nio.channels.DatagramChannel;
012    import java.nio.channels.Pipe;
013    import java.nio.channels.spi.SelectorProvider;
014    
015    import com.barchart.udt.SocketUDT;
016    import com.barchart.udt.TypeUDT;
017    
018    /**
019     * selection provider for UDT
020     * <p>
021     * note: you must use the same system-wide provider instance for the same
022     * {@link TypeUDT} of UDT channels and UDT selectors;
023     */
024    public class SelectorProviderUDT extends SelectorProvider {
025    
026            /**
027             * system-wide provider instance, for {@link TypeUDT#DATAGRAM} UDT sockets
028             */
029            public static final SelectorProviderUDT DATAGRAM = //
030            new SelectorProviderUDT(TypeUDT.DATAGRAM);
031    
032            /**
033             * system-wide provider instance, for {@link TypeUDT#STREAM} UDT sockets
034             */
035            public static final SelectorProviderUDT STREAM = //
036            new SelectorProviderUDT(TypeUDT.STREAM);
037    
038            public static SelectorProviderUDT from(final TypeUDT type) {
039                    switch (type) {
040                    case DATAGRAM:
041                            return DATAGRAM;
042                    case STREAM:
043                            return STREAM;
044                    default:
045                            throw new IllegalStateException("wrong type=" + type);
046                    }
047            }
048    
049            private volatile int acceptQueueSize = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE;
050    
051            private volatile int maxSelectorSize = SocketUDT.DEFAULT_MAX_SELECTOR_SIZE;
052    
053            private final TypeUDT type;
054    
055            /**
056             * {@link TypeUDT} of UDT sockets generated by this provider
057             */
058            public final TypeUDT type() {
059                    return type;
060            }
061    
062            protected SelectorProviderUDT(final TypeUDT type) {
063                    this.type = type;
064            }
065    
066            public int getAcceptQueueSize() {
067                    return acceptQueueSize;
068            }
069    
070            public int getMaxSelectorSize() {
071                    return maxSelectorSize;
072            }
073    
074            /**
075             * Not supported.
076             */
077            @Override
078            public DatagramChannel openDatagramChannel() throws IOException {
079                    throw new UnsupportedOperationException("feature not available");
080            }
081    
082            /**
083             * Not supported.
084             */
085            @Override
086            public Pipe openPipe() throws IOException {
087                    throw new UnsupportedOperationException("feature not available");
088            }
089    
090            /**
091             * Open UDT {@link KindUDT#RENDEZVOUS} socket channel.
092             * 
093             * @see RendezvousChannelUDT
094             */
095            public RendezvousChannelUDT openRendezvousChannel() throws IOException {
096                    final SocketUDT socketUDT = new SocketUDT(type);
097                    return new RendezvousChannelUDT(this, socketUDT);
098            }
099    
100            /**
101             * Open UDT specific selector.
102             * 
103             * @see SelectorUDT
104             */
105            @Override
106            public SelectorUDT openSelector() throws IOException {
107                    return new SelectorUDT(this, maxSelectorSize);
108            }
109    
110            /**
111             * Open UDT {@link KindUDT#ACCEPTOR} socket channel.
112             * 
113             * @see ServerSocketChannelUDT
114             */
115            @Override
116            public ServerSocketChannelUDT openServerSocketChannel() throws IOException {
117                    final SocketUDT serverSocketUDT = new SocketUDT(type);
118                    return new ServerSocketChannelUDT(this, serverSocketUDT);
119            }
120    
121            /**
122             * Open UDT {@link KindUDT#CONNECTOR} socket channel.
123             * 
124             * @see SocketChannelUDT
125             */
126            @Override
127            public SocketChannelUDT openSocketChannel() throws IOException {
128                    final SocketUDT socketUDT = new SocketUDT(type);
129                    return new SocketChannelUDT(this, socketUDT);
130            }
131    
132            public void setAcceptQueueSize(final int queueSize) {
133                    acceptQueueSize = queueSize;
134            }
135    
136            public void setMaxSelectorSize(final int selectorSize) {
137                    maxSelectorSize = selectorSize;
138            }
139    
140    }