PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/message/M3UAMessageImpl.java

http://mobicents.googlecode.com/
Java | 127 lines | 71 code | 26 blank | 30 comment | 4 complexity | 39e40df9a363378ad6a90009932f056a 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.message;
 24
 25import java.nio.ByteBuffer;
 26
 27import javolution.text.TextBuilder;
 28import javolution.util.FastMap;
 29
 30import org.mobicents.protocols.ss7.m3ua.impl.parameter.ParameterFactoryImpl;
 31import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
 32import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
 33
 34/**
 35 * @author amit bhayani
 36 * @author kulikov
 37 */
 38public abstract class M3UAMessageImpl implements M3UAMessage {
 39	// header part
 40	private int messageClass;
 41	private int messageType;
 42
 43	private String message;
 44	protected FastMap<Short, Parameter> parameters = new FastMap<Short, Parameter>();
 45
 46	private ParameterFactoryImpl factory = new ParameterFactoryImpl();
 47
 48	int initialPosition = 0;
 49
 50	public M3UAMessageImpl(String message) {
 51		this.message = message;
 52	}
 53
 54	protected M3UAMessageImpl(int messageClass, int messageType, String message) {
 55		this(message);
 56		this.messageClass = messageClass;
 57		this.messageType = messageType;
 58	}
 59
 60	protected abstract void encodeParams(ByteBuffer buffer);
 61
 62	public void encode(ByteBuffer buffer) {
 63
 64		initialPosition = buffer.position();
 65
 66		buffer.position(initialPosition + 8);
 67
 68		encodeParams(buffer);
 69
 70		int length = buffer.position() - initialPosition;
 71		// buffer.rewind();
 72
 73		buffer.put(initialPosition++, (byte) 1);
 74		buffer.put(initialPosition++, (byte) 0);
 75		buffer.put(initialPosition++, (byte) messageClass);
 76		buffer.put(initialPosition++, (byte) messageType);
 77		buffer.putInt(initialPosition++, length);
 78
 79		// buffer.position(length);
 80	}
 81
 82	protected void decode(byte[] data) {
 83		this.decode(data, 0);
 84	}
 85
 86	protected void decode(byte[] data, int initialPos) {
 87		int pos = initialPos;
 88		while (pos < data.length) {
 89			short tag = (short) ((data[pos] & 0xff) << 8 | (data[pos + 1] & 0xff));
 90			short len = (short) ((data[pos + 2] & 0xff) << 8 | (data[pos + 3] & 0xff));
 91
 92			byte[] value = new byte[len - 4];
 93
 94			System.arraycopy(data, pos + 4, value, 0, value.length);
 95			pos += len;
 96			parameters.put(tag, factory.createParameter(tag, value));
 97
 98			// The Parameter Length does not include any padding octets. We have
 99			// to consider padding here
100			int padding = 4 - (pos % 4);
101			if (padding < 4) {
102				pos += padding;
103			}
104		}
105	}
106
107	public int getMessageClass() {
108		return messageClass;
109	}
110
111	public int getMessageType() {
112		return messageType;
113	}
114
115	@Override
116	public String toString() {
117		TextBuilder tb = new TextBuilder();
118		tb.append(this.message).append(" Params(");
119		for (FastMap.Entry<Short, Parameter> e = parameters.head(), end = parameters.tail(); (e = e.getNext()) != end;) {
120			Parameter value = e.getValue();
121			tb.append(value.toString());
122			tb.append(", ");
123		}
124		tb.append(")");
125		return tb.toString();
126	}
127}