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