PageRenderTime 94ms CodeModel.GetById 68ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/isup/isup-impl/src/main/java/org/mobicents/protocols/ss7/isup/impl/ISUPStackImpl.java

http://mobicents.googlecode.com/
Java | 347 lines | 120 code | 44 blank | 183 comment | 18 complexity | 50c354ba14f3e15fe2751f7c526837df 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
 23/**
 24 * Start time:12:14:57 2009-09-04<br>
 25 * Project: mobicents-isup-stack<br>
 26 * 
 27 * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski </a>
 28 */
 29package org.mobicents.protocols.ss7.isup.impl;
 30
 31import java.io.IOException;
 32import java.util.Properties;
 33
 34import org.apache.log4j.Logger;
 35import org.mobicents.protocols.ss7.isup.CircuitManager;
 36import org.mobicents.protocols.ss7.isup.ISUPMessageFactory;
 37import org.mobicents.protocols.ss7.isup.ISUPParameterFactory;
 38import org.mobicents.protocols.ss7.isup.ISUPProvider;
 39import org.mobicents.protocols.ss7.isup.ISUPStack;
 40import org.mobicents.protocols.ss7.isup.ParameterException;
 41import org.mobicents.protocols.ss7.isup.impl.message.AbstractISUPMessage;
 42import org.mobicents.protocols.ss7.mtp.Mtp3;
 43import org.mobicents.protocols.ss7.mtp.Mtp3PausePrimitive;
 44import org.mobicents.protocols.ss7.mtp.Mtp3ResumePrimitive;
 45import org.mobicents.protocols.ss7.mtp.Mtp3StatusPrimitive;
 46import org.mobicents.protocols.ss7.mtp.Mtp3TransferPrimitive;
 47import org.mobicents.protocols.ss7.mtp.Mtp3UserPart;
 48import org.mobicents.protocols.ss7.mtp.Mtp3UserPartListener;
 49
 50/**
 51 * Start time:12:14:57 2009-09-04<br>
 52 * Project: mobicents-isup-stack<br>
 53 * 
 54 * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski </a>
 55 */
 56public class ISUPStackImpl implements ISUPStack, Mtp3UserPartListener {
 57
 58	private Logger logger = Logger.getLogger(ISUPStackImpl.class);
 59
 60	private State state = State.IDLE;
 61	// dont quite like the idea of so many threads... but.
 62	// private ExecutorService executor;
 63	// private ExecutorService layer3exec;
 64
 65	// protected ConcurrentLinkedQueue<byte[]> txDataQueue = new
 66	// ConcurrentLinkedQueue<byte[]>();
 67
 68	private Mtp3UserPart mtp3UserPart = null;
 69	private CircuitManager circuitManager = null;
 70	private ISUPProviderImpl provider;
 71	// local vars
 72	private ISUPMessageFactory messageFactory;
 73	private ISUPParameterFactory parameterFactory;
 74
 75	public ISUPStackImpl() {
 76		super();
 77	}
 78
 79	public ISUPProvider getIsupProvider() {
 80		return provider;
 81	}
 82
 83	public void start() throws IllegalStateException {
 84		if (state != State.CONFIGURED) {
 85			throw new IllegalStateException("Stack has not been configured or is already running!");
 86		}
 87		if (state == State.RUNNING) {
 88			// throw new StartFailedException("Can not start stack again!");
 89			throw new IllegalStateException("Can not start stack again!");
 90		}
 91		if (this.mtp3UserPart == null) {
 92			throw new IllegalStateException("No Mtp3UserPart present!");
 93		}
 94
 95		if (this.circuitManager == null) {
 96			throw new IllegalStateException("No CircuitManager present!");
 97		}
 98		// this.executor = Executors.newFixedThreadPool(1);
 99		// this.layer3exec = Executors.newFixedThreadPool(1);
100		this.provider.start();
101		// this.layer3exec.execute(new MtpStreamHandler());
102
103		this.mtp3UserPart.addMtp3UserPartListener(this);
104
105		this.state = State.RUNNING;
106
107	}
108
109	public void stop() {
110		if (state != State.RUNNING) {
111			throw new IllegalStateException("Stack is not running!");
112		}
113		// if(state == State.CONFIGURED)
114		// {
115		// throw new IllegalStateException("Can not stop stack again!");
116		// }
117
118		this.mtp3UserPart.removeMtp3UserPartListener(this);
119
120		// this.executor.shutdown();
121		// this.layer3exec.shutdown();
122		this.provider.stop();
123		this.state = State.CONFIGURED;
124
125	}
126
127	// ///////////////
128	// CONF METHOD //
129	// ///////////////
130	/**
131     *
132     */
133	public void configure(Properties props) {
134		if (state != State.IDLE) {
135			throw new IllegalStateException("Stack already been configured or is already running!");
136		}
137
138		this.provider = new ISUPProviderImpl(this, props);
139		this.parameterFactory = this.provider.getParameterFactory();
140		this.messageFactory = this.provider.getMessageFactory();
141		this.state = State.CONFIGURED;
142	}
143
144	public Mtp3UserPart getMtp3UserPart() {
145		return mtp3UserPart;
146	}
147
148	public void setMtp3UserPart(Mtp3UserPart mtp3UserPart) {
149		this.mtp3UserPart = mtp3UserPart;
150	}
151
152	public void setCircuitManager(CircuitManager mgr) {
153		this.circuitManager = mgr;
154
155	}
156
157	public CircuitManager getCircuitManager() {
158		return this.circuitManager;
159	}
160
161	// ---------------- private methods and class defs
162
163	/**
164	 * @param message
165	 */
166	void send(Mtp3TransferPrimitive message) throws IOException {
167		
168		if (this.state != State.RUNNING)
169			return;
170		
171		// here we have encoded msg, nothing more, need to add MTP3 label.
172		// txDataQueue.add(message);
173		try {
174			this.mtp3UserPart.sendMessage(message);
175		} catch (IOException e) {
176			// log here Exceptions from MTP3 level
177			logger.error("IOException when sending the message to MTP3 level: " + e.getMessage(), e);
178			e.printStackTrace();
179			throw e;
180		}
181	}
182
183	private enum State {
184		IDLE, CONFIGURED, RUNNING;
185	}
186
187	@Override
188	public void onMtp3PauseMessage(Mtp3PausePrimitive arg0) {
189		// TODO Auto-generated method stub
190
191	}
192
193	@Override
194	public void onMtp3ResumeMessage(Mtp3ResumePrimitive arg0) {
195		// TODO Auto-generated method stub
196
197	}
198
199	@Override
200	public void onMtp3StatusMessage(Mtp3StatusPrimitive arg0) {
201		// TODO Auto-generated method stub
202
203	}
204
205	@Override
206	public void onMtp3TransferMessage(Mtp3TransferPrimitive mtpMsg) {
207		// int commandCode = msu[7];
208		// http://pt.com/page/tutorials/ss7-tutorial/mtp
209		// byte[] payload = new byte[msu.length - 5];
210		// System.arraycopy(msu, 5, payload, 0, payload.length);
211		// byte sls = msu[5];
212		// for post processing
213		// AbstractISUPMessage msg = (AbstractISUPMessage)
214		// messageFactory.createCommand(commandCode);
215		// msg.decode(payload, parameterFactory);
216		// msg.setSls(sls); // store SLS...
217		// return msg;
218		
219		if (this.state != State.RUNNING)
220			return;
221
222		// process only ISUP messages
223		if (mtpMsg.getSi() != Mtp3._SI_SERVICE_ISUP)
224			return;
225		
226		// 1(SIO) + 3(RL) + 1(SLS) + 2(CIC) + 1(CODE)
227		byte[] payload = mtpMsg.getData();
228		int commandCode = payload[2];
229
230		AbstractISUPMessage msg = (AbstractISUPMessage) messageFactory.createCommand(commandCode);
231		try {
232			msg.decode(payload, parameterFactory);
233		} catch (ParameterException e) {
234			logger.error("Error decoding of incoming Mtp3TransferPrimitive" + e.getMessage(), e);
235			e.printStackTrace();
236		}
237		msg.setSls(mtpMsg.getSls()); // store SLS...
238		provider.receive(msg);
239	}
240
241	// private class MtpStreamHandler implements Runnable {
242	// ByteBuffer rxBuffer = ByteBuffer.allocateDirect(1000);
243	// ByteBuffer txBuffer = ByteBuffer.allocateDirect(1000);
244	// int rxBytes = 0;
245	// @SuppressWarnings("unused")
246	// int txBytes = 0;
247	//
248	// public void run() {
249	// // Execute only till state is Running
250	// while (state == State.RUNNING) {
251	//
252	// try {
253	// //Execute the MTP3UserPart
254	// mtp3UserPart.execute();
255	//
256	// rxBytes = 0;
257	// rxBuffer.clear();
258	// try {
259	// rxBytes = mtp3UserPart.read(rxBuffer);
260	// if (rxBytes != 0) {
261	// byte[] data = new byte[rxBytes];
262	// rxBuffer.flip();
263	// rxBuffer.get(data);
264	// MessageHandler handler = new MessageHandler(data);
265	// executor.execute(handler);
266	// }
267	// } catch (IOException e) {
268	// logger.error("Error while readig data from Mtp3UserPart", e);
269	// }
270	//
271	// // Iterate till we send all data
272	// while (!txDataQueue.isEmpty()) {
273	// txBuffer.clear();
274	// txBuffer.put(txDataQueue.poll());
275	// txBuffer.flip();
276	// try {
277	// txBytes = mtp3UserPart.write(txBuffer);
278	// } catch (IOException e) {
279	// logger.error("Error while writting data to Mtp3UserPart", e);
280	// }
281	// }// while txDataQueue
282	// } catch (IOException e1) {
283	// // TODO Auto-generated catch block
284	// e1.printStackTrace();
285	// }
286	// }// end of while
287	// }
288	// }
289	//
290	// private class MessageHandler implements Runnable {
291	// // MSU as input stream
292	// private byte[] msu;
293	// private ISUPMessage message;
294	//
295	// protected MessageHandler(byte[] msu) {
296	// this.msu = msu;
297	//
298	// }
299	//
300	// private ISUPMessage parse() throws IOException {
301	// try {
302	// // FIXME: change this, dont copy over and over?
303	//
304	// int commandCode = msu[7];// 1(SIO) + 3(RL) + 1(SLS) + 2(CIC) + 1(CODE)
305	// // http://pt.com/page/tutorials/ss7-tutorial/mtp
306	// byte[] payload = new byte[msu.length - 5];
307	// System.arraycopy(msu, 5, payload, 0, payload.length);
308	// byte sls = msu[5];
309	// // for post processing
310	// AbstractISUPMessage msg = (AbstractISUPMessage)
311	// messageFactory.createCommand(commandCode);
312	// msg.decode(payload, parameterFactory);
313	// msg.setSls(sls); //store SLS...
314	// return msg;
315	//
316	// } catch (Exception e) {
317	// // FIXME: what should we do here? send back?
318	// e.printStackTrace();
319	// logger.error("Failed on data: " + Utils.hexDump(null, msu));
320	// }
321	// return null;
322	// }
323	//
324	// public void run() {
325	// if (message == null) {
326	// try {
327	// message = parse();
328	// } catch (IOException e) {
329	// logger.warn("Corrupted message received");
330	// return;
331	// }
332	// }
333	// // deliver to provider, so it can check up on circuit, play with
334	// // timers and deliver.
335	// if(message!=null)
336	// {
337	// try{
338	// provider.receive(message);
339	// }catch(Exception e)
340	// {
341	// //TODO: add proper answer?
342	// }
343	// }
344	// }
345	// }
346
347}