| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 9 | * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 10 | * |
| 11 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 12 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 14 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 15 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 16 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 17 | */ |
| 18 |
| 19 | package jawnae.pyronet; |
| 20 |
| 21 | import java.io.IOException; |
| 22 | import java.net.InetSocketAddress; |
| 23 | import java.nio.channels.SelectionKey; |
| 24 | import java.nio.channels.Selector; |
| 25 | import java.nio.channels.ServerSocketChannel; |
| 26 | import java.nio.channels.SocketChannel; |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Iterator; |
| 29 | import java.util.List; |
| 30 | import java.util.concurrent.CopyOnWriteArrayList; |
| 31 |
| 32 | import jawnae.pyronet.addon.PyroSelectorProvider; |
| 33 | import jawnae.pyronet.events.PyroServerListener; |
| 34 |
| 35 | public class PyroServer implements Iterable<PyroClient> { |
| 36 | private final PyroSelector selector; |
| 37 |
| 38 | public final SelectionKey serverKey; |
| 39 |
| 40 | final List<PyroClient> clients; |
| 41 |
| 42 | PyroServer(PyroSelector selector, Selector nioSelector, |
| 43 | InetSocketAddress endpoint, int backlog) throws IOException { |
| 44 | this.selector = selector; |
| 45 | this.selector.checkThread(); |
| 46 |
| 47 | ServerSocketChannel ssc; |
| 48 | ssc = ServerSocketChannel.open(); |
| 49 | ssc.socket().bind(endpoint, backlog); |
| 50 | ssc.configureBlocking(false); |
| 51 |
| 52 | this.serverKey = ssc.register(nioSelector, SelectionKey.OP_ACCEPT); |
| 53 | this.serverKey.attach(this); |
| 54 |
| 55 | this.clients = new ArrayList<PyroClient>(); |
| 56 | this.listeners = new CopyOnWriteArrayList<PyroServerListener>(); |
| 57 | } |
| 58 |
| 59 | // |
| 60 |
| 61 | private final List<PyroServerListener> listeners; |
| 62 |
| 63 | public void addListener(PyroServerListener listener) { |
| 64 | this.selector.checkThread(); |
| 65 |
| 66 | this.listeners.add(listener); |
| 67 | } |
| 68 |
| 69 | public void removeListener(PyroServerListener listener) { |
| 70 | this.selector.checkThread(); |
| 71 |
| 72 | this.listeners.remove(listener); |
| 73 | } |
| 74 |
| 75 | public void removeListeners() { |
| 76 | this.selector.checkThread(); |
| 77 |
| 78 | this.listeners.clear(); |
| 79 | } |
| 80 |
| 81 | /** |
| 82 | * Returns the network that created this server |
| 83 | */ |
| 84 |
| 85 | public PyroSelector selector() { |
| 86 | return this.selector; |
| 87 | } |
| 88 |
| 89 | private PyroSelectorProvider selectorProvider; |
| 90 |
| 91 | /** |
| 92 | * By installing a PyroSelectorProvider you alter the PyroSelector used to |
| 93 | * do the I/O of a PyroClient. This can be used for multi-threading your |
| 94 | * network code |
| 95 | */ |
| 96 |
| 97 | public void installSelectorProvider(PyroSelectorProvider selectorProvider) { |
| 98 | this.selector().checkThread(); |
| 99 |
| 100 | this.selectorProvider = selectorProvider; |
| 101 | } |
| 102 |
| 103 | void onInterestOp() { |
| 104 | if (!serverKey.isValid()) |
| 105 | throw new PyroException("invalid selection key"); |
| 106 |
| 107 | try { |
| 108 | if (serverKey.isAcceptable()) { |
| 109 | this.onReadyToAccept(); |
| 110 | } |
| 111 | } catch (IOException exc) { |
| 112 | throw new IllegalStateException(exc); |
| 113 | } |
| 114 | } |
| 115 |
| 116 | /** |
| 117 | * Returns an iterator to access all connected clients of this server |
| 118 | */ |
| 119 |
| 120 | @Override |
| 121 | public Iterator<PyroClient> iterator() { |
| 122 | this.selector.checkThread(); |
| 123 |
| 124 | List<PyroClient> copy = new ArrayList<PyroClient>(); |
| 125 | copy.addAll(this.clients); |
| 126 | return copy.iterator(); |
| 127 | } |
| 128 |
| 129 | /** |
| 130 | * Closes the server socket. Any current connections will continue. |
| 131 | */ |
| 132 |
| 133 | public void close() throws IOException { |
| 134 | this.selector.checkThread(); |
| 135 |
| 136 | this.serverKey.channel().close(); |
| 137 | } |
| 138 |
| 139 | /** |
| 140 | * Closes the server socket. Any current connections will be closed. |
| 141 | */ |
| 142 |
| 143 | public void terminate() throws IOException { |
| 144 | this.close(); |
| 145 |
| 146 | for (PyroClient client: this) { |
| 147 | client.dropConnection(); |
| 148 | } |
| 149 | } |
| 150 |
| 151 | private void onReadyToAccept() throws IOException { |
| 152 | this.selector.checkThread(); |
| 153 |
| 154 | final SocketChannel channel = ((ServerSocketChannel) serverKey |
| 155 | .channel()).accept(); |
| 156 |
| 157 | final PyroSelector acceptedClientSelector; |
| 158 | { |
| 159 | if (this.selectorProvider == null) |
| 160 | acceptedClientSelector = this.selector; |
| 161 | else |
| 162 | acceptedClientSelector = this.selectorProvider |
| 163 | .provideFor(channel); |
| 164 | } |
| 165 |
| 166 | if (acceptedClientSelector == this.selector) { |
| 167 | SelectionKey clientKey = PyroClient.configure( |
| 168 | acceptedClientSelector, channel, false); |
| 169 | PyroClient client = new PyroClient(acceptedClientSelector, this, |
| 170 | clientKey); |
| 171 | this.fireAcceptedClient(client); |
| 172 | this.clients.add(client); |
| 173 | } else { |
| 174 | // create client in PyroClient-selector thread |
| 175 | acceptedClientSelector.scheduleTask(new Runnable() { |
| 176 | @Override |
| 177 | public void run() { |
| 178 | SelectionKey clientKey; |
| 179 | try { |
| 180 | clientKey = PyroClient.configure( |
| 181 | acceptedClientSelector, channel, false); |
| 182 | } catch (IOException exc) { |
| 183 | throw new IllegalStateException(exc); |
| 184 | } |
| 185 | final PyroClient client = new PyroClient( |
| 186 | acceptedClientSelector, PyroServer.this, clientKey); |
| 187 | PyroServer.this.fireAcceptedClient(client); |
| 188 |
| 189 | // add client to list in PyroServer-selector thread |
| 190 | PyroServer.this.selector().scheduleTask(new Runnable() { |
| 191 | @Override |
| 192 | public void run() { |
| 193 | PyroServer.this.clients.add(client); |
| 194 | } |
| 195 | }); |
| 196 | PyroServer.this.selector().wakeup(); |
| 197 | } |
| 198 | }); |
| 199 | acceptedClientSelector.wakeup(); |
| 200 | } |
| 201 | } |
| 202 |
| 203 | void fireAcceptedClient(PyroClient client) { |
| 204 | for (PyroServerListener listener: this.listeners) { |
| 205 | listener.acceptedClient(client); |
| 206 | } |
| 207 | } |
| 208 |
| 209 | void onDisconnect(final PyroClient client) { |
| 210 | if (this.selector().isNetworkThread()) { |
| 211 | this.clients.remove(client); |
| 212 | } else { |
| 213 | // we are in the PyroClient-selector thread |
| 214 | this.selector().scheduleTask(new Runnable() { |
| 215 | @Override |
| 216 | public void run() { |
| 217 | // call again from the PyroServer-selector thread |
| 218 | PyroServer.this.onDisconnect(client); |
| 219 | } |
| 220 | }); |
| 221 | this.selector().wakeup(); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 |