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