PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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