/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/M3UASelectorImpl.java
Java | 136 lines | 61 code | 16 blank | 59 comment | 7 complexity | 0a12479054cd4e89ec6156abe83e18d2 MD5 | raw file
1/* 2 * JBoss, Home of Professional Open Source 3 * Copyright 2011, Red Hat, Inc. and individual contributors 4 * by the @authors tag. See the copyright.txt in the distribution for a 5 * full listing of individual contributors. 6 * 7 * This is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as 9 * published by the Free Software Foundation; either version 2.1 of 10 * the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this software; if not, write to the Free 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 21 */ 22 23package org.mobicents.protocols.ss7.m3ua.impl; 24 25import java.io.IOException; 26import java.nio.channels.SelectionKey; 27import java.nio.channels.Selector; 28import java.nio.channels.spi.SelectorProvider; 29import java.util.Set; 30 31import javolution.util.FastList; 32 33import org.mobicents.protocols.ss7.m3ua.M3UASelectionKey; 34import org.mobicents.protocols.ss7.m3ua.M3UASelector; 35 36/** 37 * Implements channel multiplexer. 38 * 39 * @author amit bhayani 40 * @author kulikov 41 */ 42public class M3UASelectorImpl implements M3UASelector { 43 // NIO multiplexer 44 protected Selector selector; 45 // list used for collecting selected keys. 46 private FastList<M3UASelectionKey> list = new FastList<M3UASelectionKey>(); 47 48 /** 49 * Constructs new multiplexer. 50 * 51 * @param selector 52 * the NIO multiplexer. 53 */ 54 protected M3UASelectorImpl(Selector selector) { 55 this.selector = selector; 56 } 57 58 /** 59 * Static method used for constructing the multiplexer. 60 * 61 * @return new multiplexer. 62 * @throws java.io.IOException 63 */ 64 public static M3UASelectorImpl open() throws IOException { 65 return new M3UASelectorImpl(SelectorProvider.provider().openSelector()); 66 } 67 68 /** 69 * (Non Java-doc.) 70 * 71 * @see org.mobicents.protocols.ss7.m3ua.M3UASelector#selectNow() 72 * @throws java.io.IOException 73 */ 74 public FastList<M3UASelectionKey> selectNow() throws IOException { 75 list.clear(); 76 selector.selectNow(); 77 Set<SelectionKey> selection = selector.selectedKeys(); 78 for (SelectionKey key : selection) { 79// if(!key.isValid()){ 80// //Move to next key if this one is not valid 81// continue; 82// } 83 try{ 84 if (key.isAcceptable()) { 85 M3UASelectionKeyImpl k = (M3UASelectionKeyImpl) key.attachment(); 86 list.add(k); 87 } else { 88 89 if (key.isReadable()) { 90 M3UASelectionKeyImpl k = (M3UASelectionKeyImpl) key.attachment(); 91 92 try { 93 ((M3UAChannelImpl) k.channel()).doRead(); 94 } catch (IOException ex) { 95 ((M3UAChannelImpl) k.channel()).ioException = ex; 96 } 97 98 if (k.isReadable()) { 99 list.add(k); 100 } 101 102 } 103 104 if (key.isWritable()) { 105 // write.... 106 M3UASelectionKeyImpl k = (M3UASelectionKeyImpl) key.attachment(); 107 108 try { 109 ((M3UAChannelImpl) k.channel()).doWrite(); 110 } catch (IOException ex) { 111 ((M3UAChannelImpl) k.channel()).ioException = ex; 112 } 113 114 if (k.isWritable()) { 115 list.add(k); 116 } 117 } 118 } 119 } catch(java.nio.channels.CancelledKeyException e){ 120 //Lets ignore? may be the far end closed the connection. In next iteration it will be removed anyway 121 } 122 } 123 selection.clear(); 124 return list; 125 } 126 127 /** 128 * (Non Java-doc.) 129 * 130 * @see org.mobicents.protocols.ss7.m3ua.M3UASelector#close() 131 * @throws java.io.IOException 132 */ 133 public void close() throws IOException { 134 selector.close(); 135 } 136}