PageRenderTime 37ms CodeModel.GetById 18ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/as/ClientM3UAProcess.java

http://mobicents.googlecode.com/
Java | 213 lines | 137 code | 40 blank | 36 comment | 17 complexity | 42b83866e76acf9954bf23016a292072 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.as;
 24
 25import java.io.IOException;
 26import java.nio.ByteBuffer;
 27import java.nio.channels.ClosedSelectorException;
 28
 29import javolution.util.FastList;
 30
 31import org.apache.log4j.Logger;
 32import org.mobicents.protocols.ss7.m3ua.M3UAChannel;
 33import org.mobicents.protocols.ss7.m3ua.M3UAProcess;
 34import org.mobicents.protocols.ss7.m3ua.M3UAProvider;
 35import org.mobicents.protocols.ss7.m3ua.M3UASelectionKey;
 36import org.mobicents.protocols.ss7.m3ua.impl.As;
 37import org.mobicents.protocols.ss7.m3ua.impl.AspFactory;
 38import org.mobicents.protocols.ss7.m3ua.impl.CommunicationListener.CommunicationState;
 39import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
 40import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
 41import org.mobicents.protocols.ss7.m3ua.message.MessageType;
 42import org.mobicents.protocols.ss7.m3ua.message.transfer.PayloadData;
 43import org.mobicents.protocols.ss7.m3ua.parameter.ProtocolData;
 44
 45/**
 46 * 
 47 * @author amit bhayani
 48 * 
 49 */
 50public class ClientM3UAProcess implements M3UAProcess {
 51
 52	private static Logger logger = Logger.getLogger(ClientM3UAProcess.class);
 53
 54	private volatile boolean started = false;
 55	private ClientM3UAManagement clientM3UAManagement;
 56
 57	public ClientM3UAProcess() {
 58	}
 59
 60	public ClientM3UAManagement getClientM3UAManagement() {
 61		return clientM3UAManagement;
 62	}
 63
 64	public void setClientM3UAManagement(ClientM3UAManagement clientM3UAManagement) {
 65		this.clientM3UAManagement = clientM3UAManagement;
 66	}
 67
 68	public int read(ByteBuffer b) throws IOException {
 69		if (!started) {
 70			throw new IOException("Client M3UAProcess is stopped");
 71		}
 72
 73		int initialPosition = b.position();
 74		FastList<As> appServers = clientM3UAManagement.getAppServers();
 75		for (FastList.Node<As> n = appServers.head(), end = appServers.tail(); (n = n.getNext()) != end;) {
 76			As as = n.getValue();
 77			byte[] msu = as.poll();
 78			if (msu != null) {
 79				b.put(msu);
 80				// Remember read is only one message at a time
 81				break;
 82			}
 83		}// for
 84		return (b.position() - initialPosition);
 85	}
 86
 87	public int write(ByteBuffer b) throws IOException {
 88
 89		if (!started) {
 90			throw new IOException("Client M3UAProcess is stopped");
 91		}
 92
 93		int write = 0;
 94
 95		if (b.hasRemaining()) {
 96			byte[] msu = new byte[b.limit() - b.position()];
 97			write = msu.length;
 98			b.get(msu);
 99			M3UAProvider m3uaProvider = this.clientM3UAManagement.getM3uaProvider();
100			ProtocolData data = m3uaProvider.getParameterFactory().createProtocolData(0, msu);
101			PayloadData payload = (PayloadData) m3uaProvider.getMessageFactory().createMessage(
102					MessageClass.TRANSFER_MESSAGES, MessageType.PAYLOAD);
103			payload.setData(data);
104			
105			//System.out.println(String.format("Sending payload data = %s", payload));
106			
107			As as = this.clientM3UAManagement.getAsForDpc(data.getDpc(), data.getSLS());
108			if (as == null) {
109				logger.error(String.format("Tx : No AS found for routing message %s", payload));
110				return write;
111			}
112
113			payload.setRoutingContext(as.getRoutingContext());
114			as.write(payload);
115
116		}
117
118		return write;
119	}
120
121	public void start() throws IOException {
122		this.started = true;
123	}
124
125	public void stop() {
126		this.started = false;
127	}
128
129	public void execute() throws IOException {
130
131		if (!started) {
132			throw new IOException("Client M3UAProcess is stopped");
133		}
134
135		clientM3UAManagement.getM3uaScheduler().tick();
136		try{
137			FastList<M3UASelectionKey> selections = clientM3UAManagement.selector.selectNow();
138	
139			for (FastList.Node<M3UASelectionKey> n = selections.head(), end = selections.tail(); (n = n.getNext()) != end;) {
140				M3UASelectionKey key = n.getValue();
141				
142	//			if(!key.isValid()){
143	//				//If Key is not valid, lets go to next one
144	//				continue;
145	//			}
146				
147				if (key.isReadable()) {
148					if (logger.isTraceEnabled()) {
149						logger.trace("Transmitting data from M3UA channel to hardware");
150					}
151					read(key);
152				}
153	
154				if (key.isWritable()) {
155					write(key);
156				}
157			}
158		}catch(ClosedSelectorException cse)
159		{
160			//do nothing, its a valid case for competing threads here.
161		}
162	}
163	
164
165	private void read(M3UASelectionKey key) {
166		M3UAChannel channel = null;
167		try {
168			channel = (M3UAChannel) key.channel();
169			M3UAMessage message = channel.receive();
170			((AspFactory) key.attachment()).read(message);
171		} catch (IOException e) {
172			logger.error(
173					String.format("IOException while reading for Aspfactory name=%s ",
174							((AspFactory) key.attachment()).getName()), e);
175
176			try {
177				channel.close();
178			} catch (Exception ex) {
179				// Ignore
180			}
181
182			((AspFactory) key.attachment()).onCommStateChange(CommunicationState.LOST);
183
184		}
185	}
186
187	private void write(M3UASelectionKey key) {
188		M3UAChannel channel = null;
189		try {
190			channel = (M3UAChannel) key.channel();
191			AspFactory factory = ((AspFactory) key.attachment());
192			M3UAMessage message = null;
193			while ((message = factory.txPoll()) != null) {
194				channel.send(message);
195			}
196		} catch (IOException e) {
197			logger.error(
198					String.format("IOException while writting for Aspfactory name=%s ",
199							((AspFactory) key.attachment()).getName()), e);
200
201			try {
202				channel.close();
203			} catch (Exception ex) {
204				// Ignore
205			}
206
207			// TODO Transition to COMM_DOWN
208			((AspFactory) key.attachment()).onCommStateChange(CommunicationState.LOST);
209
210		}
211	}
212
213}