/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/fsm/FSM.java

http://mobicents.googlecode.com/ · Java · 172 lines · 94 code · 37 blank · 41 comment · 16 complexity · 05322dd7598b2de7193411c92ba0b63f 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.m3ua.impl.fsm;
  23. import javolution.util.FastMap;
  24. import org.apache.log4j.Logger;
  25. import org.mobicents.protocols.ss7.m3ua.impl.scheduler.M3UATask;
  26. /**
  27. * @author amit bhayani
  28. */
  29. public class FSM extends M3UATask {
  30. static final protected Logger logger = Logger.getLogger(FSM.class);
  31. public static final String ATTRIBUTE_MESSAGE = "message";
  32. private String name;
  33. // first and last states in fsm
  34. protected State start;
  35. protected State end;
  36. // intermediate states
  37. private FastMap<String, State> states = new FastMap<String, State>();
  38. protected State currentState;
  39. private FastMap attributes = new FastMap();
  40. private State oldState;
  41. public FSM(String name) {
  42. this.name = name;
  43. }
  44. public State getState() {
  45. return currentState;
  46. }
  47. public void setStart(String name) {
  48. // the start state already has value which differs from current state?
  49. if (this.start != null && currentState != null) {
  50. throw new IllegalStateException("Start state can't be changed now");
  51. }
  52. this.start = states.get(name);
  53. this.currentState = start;
  54. }
  55. public void setEnd(String name) {
  56. this.end = states.get(name);
  57. }
  58. public State createState(String name) {
  59. State s = new State(this, name);
  60. states.put(name, s);
  61. return s;
  62. }
  63. public void setAttribute(String name, Object value) {
  64. attributes.put(name, value);
  65. }
  66. public Object getAttribute(String name) {
  67. return attributes.get(name);
  68. }
  69. public void removeAttribute(String name) {
  70. attributes.remove(name);
  71. }
  72. public Transition createTransition(String name, String from, String to) {
  73. if (name.equals("timeout")) {
  74. throw new IllegalArgumentException("timeout is illegal name for transition");
  75. }
  76. if (!states.containsKey(from)) {
  77. throw new IllegalStateException("Unknown state: " + from);
  78. }
  79. if (!states.containsKey(to)) {
  80. throw new IllegalStateException("Unknown state: " + to);
  81. }
  82. Transition t = new Transition(name, states.get(to));
  83. states.get(from).add(t);
  84. return t;
  85. }
  86. public Transition createTimeoutTransition(String from, String to, long timeout) {
  87. if (!states.containsKey(from)) {
  88. throw new IllegalStateException("Unknown state: " + from);
  89. }
  90. if (!states.containsKey(to)) {
  91. throw new IllegalStateException("Unknown state: " + to);
  92. }
  93. Transition t = new Transition("timeout", states.get(to));
  94. states.get(from).timeout = timeout;
  95. states.get(from).add(t);
  96. return t;
  97. }
  98. /**
  99. * Processes transition.
  100. *
  101. * @param name
  102. * the name of transition.
  103. */
  104. public void signal(String name) throws UnknownTransitionException {
  105. // check that start state defined
  106. if (start == null) {
  107. throw new IllegalStateException("The start sate is not defined");
  108. }
  109. // check that end state defined
  110. if (end == null) {
  111. throw new IllegalStateException("The end sate is not defined");
  112. }
  113. // ignore any signals if fsm reaches end state
  114. // if (state == end) {
  115. // return;
  116. // }
  117. oldState = currentState;
  118. // switch to next state
  119. currentState = currentState.signal(name);
  120. if (logger.isDebugEnabled()) {
  121. logger.debug(String.format("%s Transition to=%s", toString(), name));
  122. }
  123. }
  124. public void tick(long now) {
  125. // if (state != null && state != start && state != end) {
  126. if (currentState != null) {
  127. currentState.tick(now);
  128. }
  129. }
  130. @Override
  131. public String toString() {
  132. return String.format("FSM.name=%s old state=%s, current state=%s", this.name, this.oldState.getName(),
  133. currentState.getName());
  134. }
  135. }