PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/parameter/ProtocolDataImpl.java

http://mobicents.googlecode.com/
Java | 160 lines | 95 code | 26 blank | 39 comment | 0 complexity | 7692c504a295de6945ca4da7224a5939 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.parameter;
 24
 25import org.mobicents.protocols.ss7.m3ua.parameter.ProtocolData;
 26import org.mobicents.protocols.ss7.mtp.Mtp3TransferPrimitive;
 27
 28/**
 29 * Implements Protocol Data parameter.
 30 * 
 31 * @author amit bhayani
 32 * @author kulikov
 33 */
 34public class ProtocolDataImpl extends ParameterImpl implements ProtocolData {
 35
 36	private int opc;
 37	private int dpc;
 38	private int si;
 39	private int ni;
 40	private int mp;
 41	private int sls;
 42	private byte[] data;
 43
 44	protected ProtocolDataImpl() {
 45		this.tag = ParameterImpl.Protocol_Data;
 46	}
 47
 48	protected ProtocolDataImpl(Mtp3TransferPrimitive mtp3TransferPrimitive) {
 49		this();
 50		this.opc = mtp3TransferPrimitive.getOpc();
 51		this.dpc = mtp3TransferPrimitive.getDpc();
 52		this.si = mtp3TransferPrimitive.getSi();
 53		this.ni = mtp3TransferPrimitive.getNi();
 54		this.mp = mtp3TransferPrimitive.getMp();
 55		this.sls = mtp3TransferPrimitive.getSls();
 56		this.data = mtp3TransferPrimitive.getData();
 57	}
 58
 59	protected ProtocolDataImpl(int opc, int dpc, int si, int ni, int mp, int sls, byte[] data) {
 60		this();
 61		this.opc = opc;
 62		this.dpc = dpc;
 63		this.si = si;
 64		this.ni = ni;
 65		this.mp = mp;
 66		this.sls = sls;
 67		this.data = data;
 68		encode();
 69	}
 70
 71	/**
 72	 * Creates new parameter with specified value.
 73	 * 
 74	 * @param valueData
 75	 *            the value of this parameter
 76	 */
 77	protected ProtocolDataImpl(byte[] valueData) {
 78		this();
 79
 80		this.opc = ((valueData[0] & 0xff) << 24) | ((valueData[1] & 0xff) << 16) | ((valueData[2] & 0xff) << 8)
 81				| (valueData[3] & 0xff);
 82		this.dpc = ((valueData[4] & 0xff) << 24) | ((valueData[5] & 0xff) << 16) | ((valueData[6] & 0xff) << 8)
 83				| (valueData[7] & 0xff);
 84
 85		this.si = valueData[8] & 0xff;
 86		this.ni = valueData[9] & 0xff;
 87		this.mp = valueData[10] & 0xff;
 88		this.sls = valueData[11] & 0xff;
 89
 90		this.data = new byte[valueData.length - 12];
 91		System.arraycopy(valueData, 12, data, 0, valueData.length - 12);
 92	}
 93
 94	private byte[] encode() {
 95		// create byte array taking into account data, point codes and
 96		// indicators;
 97		byte[] value = new byte[data.length + 12];
 98		// insert data
 99		System.arraycopy(data, 0, value, 12, data.length);
100
101		// encode originated point codes
102		value[0] = (byte) (opc >> 24);
103		value[1] = (byte) (opc >> 16);
104		value[2] = (byte) (opc >> 8);
105		value[3] = (byte) (opc);
106
107		// encode destination point code
108		value[4] = (byte) (dpc >> 24);
109		value[5] = (byte) (dpc >> 16);
110		value[6] = (byte) (dpc >> 8);
111		value[7] = (byte) (dpc);
112
113		// encode indicators
114		value[8] = (byte) (si);
115		value[9] = (byte) (ni);
116		value[10] = (byte) (mp);
117		value[11] = (byte) (sls);
118
119		return value;
120	}
121
122	public int getOpc() {
123		return opc;
124	}
125
126	public int getDpc() {
127		return dpc;
128	}
129
130	public int getSI() {
131		return si;
132	}
133
134	public int getNI() {
135		return ni;
136	}
137
138	public int getMP() {
139		return mp;
140	}
141
142	public int getSLS() {
143		return sls;
144	}
145
146	public byte[] getData() {
147		return data;
148	}
149
150	@Override
151	protected byte[] getValue() {
152		return this.encode();
153	}
154
155	@Override
156	public String toString() {
157		return String.format("Protocol opc=%d dpc=%d si=%d ni=%d sls=%d", opc, dpc, si, ni, sls);
158	}
159
160}