PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/M3UASelectorImpl.java

http://mobicents.googlecode.com/
Java | 136 lines | 61 code | 16 blank | 59 comment | 7 complexity | 0a12479054cd4e89ec6156abe83e18d2 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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. package org.mobicents.protocols.ss7.m3ua.impl;
  23. import java.io.IOException;
  24. import java.nio.channels.SelectionKey;
  25. import java.nio.channels.Selector;
  26. import java.nio.channels.spi.SelectorProvider;
  27. import java.util.Set;
  28. import javolution.util.FastList;
  29. import org.mobicents.protocols.ss7.m3ua.M3UASelectionKey;
  30. import org.mobicents.protocols.ss7.m3ua.M3UASelector;
  31. /**
  32. * Implements channel multiplexer.
  33. *
  34. * @author amit bhayani
  35. * @author kulikov
  36. */
  37. public class M3UASelectorImpl implements M3UASelector {
  38. // NIO multiplexer
  39. protected Selector selector;
  40. // list used for collecting selected keys.
  41. private FastList<M3UASelectionKey> list = new FastList<M3UASelectionKey>();
  42. /**
  43. * Constructs new multiplexer.
  44. *
  45. * @param selector
  46. * the NIO multiplexer.
  47. */
  48. protected M3UASelectorImpl(Selector selector) {
  49. this.selector = selector;
  50. }
  51. /**
  52. * Static method used for constructing the multiplexer.
  53. *
  54. * @return new multiplexer.
  55. * @throws java.io.IOException
  56. */
  57. public static M3UASelectorImpl open() throws IOException {
  58. return new M3UASelectorImpl(SelectorProvider.provider().openSelector());
  59. }
  60. /**
  61. * (Non Java-doc.)
  62. *
  63. * @see org.mobicents.protocols.ss7.m3ua.M3UASelector#selectNow()
  64. * @throws java.io.IOException
  65. */
  66. public FastList<M3UASelectionKey> selectNow() throws IOException {
  67. list.clear();
  68. selector.selectNow();
  69. Set<SelectionKey> selection = selector.selectedKeys();
  70. for (SelectionKey key : selection) {
  71. // if(!key.isValid()){
  72. // //Move to next key if this one is not valid
  73. // continue;
  74. // }
  75. try{
  76. if (key.isAcceptable()) {
  77. M3UASelectionKeyImpl k = (M3UASelectionKeyImpl) key.attachment();
  78. list.add(k);
  79. } else {
  80. if (key.isReadable()) {
  81. M3UASelectionKeyImpl k = (M3UASelectionKeyImpl) key.attachment();
  82. try {
  83. ((M3UAChannelImpl) k.channel()).doRead();
  84. } catch (IOException ex) {
  85. ((M3UAChannelImpl) k.channel()).ioException = ex;
  86. }
  87. if (k.isReadable()) {
  88. list.add(k);
  89. }
  90. }
  91. if (key.isWritable()) {
  92. // write....
  93. M3UASelectionKeyImpl k = (M3UASelectionKeyImpl) key.attachment();
  94. try {
  95. ((M3UAChannelImpl) k.channel()).doWrite();
  96. } catch (IOException ex) {
  97. ((M3UAChannelImpl) k.channel()).ioException = ex;
  98. }
  99. if (k.isWritable()) {
  100. list.add(k);
  101. }
  102. }
  103. }
  104. } catch(java.nio.channels.CancelledKeyException e){
  105. //Lets ignore? may be the far end closed the connection. In next iteration it will be removed anyway
  106. }
  107. }
  108. selection.clear();
  109. return list;
  110. }
  111. /**
  112. * (Non Java-doc.)
  113. *
  114. * @see org.mobicents.protocols.ss7.m3ua.M3UASelector#close()
  115. * @throws java.io.IOException
  116. */
  117. public void close() throws IOException {
  118. selector.close();
  119. }
  120. }