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

http://mobicents.googlecode.com/ · Java · 119 lines · 36 code · 15 blank · 68 comment · 4 complexity · 192554759abfc3556bdc5b9e18163adf 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. package org.mobicents.protocols.ss7.m3ua.impl;
  23. import java.io.IOException;
  24. import java.nio.channels.spi.AbstractSelectableChannel;
  25. import java.util.concurrent.ConcurrentLinkedQueue;
  26. import org.mobicents.protocols.ss7.m3ua.M3UAChannel;
  27. import org.mobicents.protocols.ss7.m3ua.impl.message.M3UAMessageImpl;
  28. import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
  29. /**
  30. * The base implementation for the M3UAChannel based on NIO.
  31. *
  32. * @author kulikov
  33. */
  34. public abstract class M3UAChannelImpl extends M3UASelectableChannelImpl implements M3UAChannel {
  35. //Queue for incoming messages
  36. protected ConcurrentLinkedQueue<M3UAMessageImpl> rxQueue = new ConcurrentLinkedQueue();
  37. //Queue for outgoing messages
  38. protected ConcurrentLinkedQueue<M3UAMessageImpl> txQueue = new ConcurrentLinkedQueue();
  39. protected IOException ioException;
  40. /**
  41. * Constructs new M3UA channel.
  42. *
  43. * @param channel the underlying network channel.
  44. * @throws java.io.IOException
  45. */
  46. protected M3UAChannelImpl(AbstractSelectableChannel channel) throws IOException {
  47. this.channel = channel;
  48. this.channel.configureBlocking(false);
  49. }
  50. /**
  51. * (Non Java-doc.)
  52. *
  53. * @see org.mobicents.protocols.ss7.m3ua.M3UAChannel#receive()
  54. */
  55. public M3UAMessage receive() throws IOException {
  56. //If underlying channel raised Exception at IO, throw the same here
  57. if(ioException != null){
  58. throw ioException;
  59. }
  60. //extracts next message from queue
  61. return rxQueue.poll();
  62. }
  63. /**
  64. * (Non Java-doc.)
  65. *
  66. * @see org.mobicents.protocols.ss7.m3ua.M3UAChannel#send(org.mobicents.protocols.ss7.m3ua.message.M3UAMessage)
  67. */
  68. public void send(M3UAMessage message) throws IOException {
  69. //If underlying channel raised Exception at IO, throw the same here
  70. if(ioException != null){
  71. throw ioException;
  72. }
  73. //queue next message for sending
  74. txQueue.offer((M3UAMessageImpl) message);
  75. }
  76. /**
  77. * Implements undelying network read operation.
  78. *
  79. * @throws java.io.IOException
  80. */
  81. protected abstract void doRead() throws IOException;
  82. /**
  83. * Implements undelying network write operation.
  84. *
  85. * @throws java.io.IOException
  86. */
  87. protected abstract void doWrite() throws IOException;
  88. /**
  89. * Tests if this channel contains data available for reading
  90. *
  91. * @return true if this channel contains data which can be read by user.
  92. */
  93. protected boolean isReadable() {
  94. return !rxQueue.isEmpty();
  95. }
  96. /**
  97. * Tests if write operation available for this channel
  98. *
  99. * @return true if this channel allows to user to use write operation.
  100. */
  101. protected boolean isWritable() {
  102. return txQueue.isEmpty();
  103. }
  104. }