PageRenderTime 33ms CodeModel.GetById 22ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 132 lines | 80 code | 28 blank | 24 comment | 8 complexity | 6c50816b0d705e626b56b5ecc52ab456 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 java.nio.ByteBuffer;
 26
 27import javolution.text.TextBuilder;
 28
 29import org.mobicents.protocols.ss7.m3ua.parameter.LocalRKIdentifier;
 30import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
 31import org.mobicents.protocols.ss7.m3ua.parameter.RegistrationResult;
 32import org.mobicents.protocols.ss7.m3ua.parameter.RegistrationStatus;
 33import org.mobicents.protocols.ss7.m3ua.parameter.RoutingContext;
 34
 35public class RegistrationResultImpl extends ParameterImpl implements RegistrationResult {
 36
 37    private LocalRKIdentifier localRKId;
 38    private RegistrationStatus status;
 39    private RoutingContext rc;
 40
 41    private ByteBuffer buffer = ByteBuffer.allocate(24);
 42    private byte[] value;
 43
 44    public RegistrationResultImpl(byte[] data) {
 45        this.tag = Parameter.Registration_Result;
 46        int pos = 0;
 47
 48        while (pos < data.length) {
 49            short tag = (short) ((data[pos] & 0xff) << 8 | (data[pos + 1] & 0xff));
 50            short len = (short) ((data[pos + 2] & 0xff) << 8 | (data[pos + 3] & 0xff));
 51
 52            byte[] value = new byte[len - 4];
 53
 54            System.arraycopy(data, pos + 4, value, 0, value.length);
 55            pos += len;
 56            // parameters.put(tag, factory.createParameter(tag, value));
 57            switch (tag) {
 58            case ParameterImpl.Local_Routing_Key_Identifier:
 59                this.localRKId = new LocalRKIdentifierImpl(value);
 60                break;
 61
 62            case ParameterImpl.Routing_Context:
 63                this.rc = new RoutingContextImpl(value);
 64                break;
 65
 66            case ParameterImpl.Registration_Status:
 67                this.status = new RegistrationStatusImpl(value);
 68                break;
 69
 70            }
 71
 72            // The Parameter Length does not include any padding octets. We have
 73            // to consider padding here
 74            pos += (pos % 4);
 75        }// end of while
 76    }
 77
 78    public RegistrationResultImpl(LocalRKIdentifier localRKId, RegistrationStatus status, RoutingContext rc) {
 79        this.tag = Parameter.Registration_Result;
 80        this.localRKId = localRKId;
 81        this.status = status;
 82        this.rc = rc;
 83
 84        this.encode();
 85    }
 86
 87    private void encode() {
 88        ((LocalRKIdentifierImpl) this.localRKId).write(buffer);
 89
 90        ((RoutingContextImpl) rc).write(buffer);
 91
 92        ((RegistrationStatusImpl) this.status).write(buffer);
 93
 94        value = buffer.array();
 95    }
 96
 97    @Override
 98    protected byte[] getValue() {
 99        return this.value;
100    }
101
102    public LocalRKIdentifier getLocalRKIdentifier() {
103        return this.localRKId;
104    }
105
106    public RegistrationStatus getRegistrationStatus() {
107        return this.status;
108    }
109
110    public RoutingContext getRoutingContext() {
111        return this.rc;
112    }
113
114    @Override
115    public String toString() {
116        TextBuilder tb = TextBuilder.newInstance();
117        tb.append("RegistrationResult(");
118        if (localRKId != null) {
119            tb.append(localRKId.toString());
120        }
121
122        if (status != null) {
123            tb.append(status.toString());
124        }
125
126        if (rc != null) {
127            tb.append(rc.toString());
128        }
129        tb.append(")");
130        return tb.toString();
131    }
132}