PageRenderTime 44ms CodeModel.GetById 34ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/Linkset.java

http://mobicents.googlecode.com/
Java | 128 lines | 60 code | 14 blank | 54 comment | 15 complexity | 68fc12fc69064c45d794c137eca03ea5 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.mtp;
 24
 25
 26
 27/**
 28 * Implements relation between link code and signaling link selection indicator.
 29 * 
 30 * @author kulikov
 31 */
 32public class Linkset {
 33    /** The list of links. Maximum available 16 links */
 34    private Mtp2[] links = new Mtp2[16];
 35    private int count;
 36    
 37    /** The relation between sls and link */
 38    private int[] map = new int[16];
 39    
 40    /**
 41     * Adds link to this link set.
 42     * 
 43     * @param link the link to add
 44     */
 45    public void add(Mtp2 link) {
 46        //add link at the first empty place
 47        for (int i = 0; i < links.length; i++) {
 48            if (links[i] == null) {
 49                links[i] = link;
 50                break;
 51            }
 52        }
 53        count++;
 54        remap();
 55    }
 56    
 57    /**
 58     * Removes links from linkset.
 59     * 
 60     * @param link the link to remove.
 61     */
 62    public void remove(Mtp2 link) {
 63        for (int i = 0; i < links.length; i++) {
 64            if (links[i] == link) {
 65                links[i] = null;
 66                break;
 67            }
 68        }
 69        count--;
 70        remap();
 71    }
 72    
 73    /**
 74     * Gets the state of the link.
 75     * 
 76     * @return true if linkset has at least one active link.
 77     */
 78    public boolean isActive() {
 79        return count > 0;
 80    }
 81    
 82    /**
 83     * Selects the link using specified link selection indicator.
 84     * 
 85     * @param sls signaling link selection indicator.
 86     * @return
 87     */
 88    public Mtp2 select(byte sls) {
 89        return links[map[sls]];
 90    }
 91    
 92    /**
 93     * This method is called each time when number of links has changed
 94     * to reestablish relation between link selection indicator and link
 95     */
 96    private void remap() {
 97        int k = -1;
 98        for (int i = 0; i < map.length; i++) {
 99            boolean found = false;
100            
101            for (int j = k + 1; j < links.length; j++) {
102                if (links[j] != null) {
103                    found = true;
104                    k = j;
105                    map[i] = k;
106                    break;
107                }
108            }
109            
110            if (found) {
111                continue;
112            }
113            
114            for (int j = 0; j < k; j++) {
115                if (links[j] != null) {
116                    found = true;
117                    k = j;
118                    map[i] = k;
119                    break;
120                }
121            }
122            
123            if (!found) {
124                map[i] = 0;
125            }
126        }
127    }
128}