PageRenderTime 84ms CodeModel.GetById 74ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 154 lines | 86 code | 22 blank | 46 comment | 13 complexity | 7081da0f25d9e49f51ba0bba4b26ee68 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.FastList;
 26
 27/**
 28 * 
 29 * @author amit bhayani
 30 * @author kulikov
 31 * 
 32 */
 33public class State {
 34	private String name;
 35	private FSM fsm;
 36
 37	private FastList<Transition> transitions = new FastList<Transition>();
 38	protected long timeout;
 39
 40	// The time in ms when this sate was entered
 41	private long activated;
 42
 43	// The time in ms when this states was entered. Its possible that
 44	// TransitionHandler cancels the transition in which case original activated
 45	// time is to be maintained
 46	private long oldActivated;
 47
 48	private StateEventHandler enterEventHandler;
 49	private StateEventHandler exitEventHandler;
 50	private StateEventHandler timeOutEventHandler;
 51
 52	protected State(FSM fsm, String name) {
 53		this.name = name;
 54		this.fsm = fsm;
 55		this.timeout = 0;
 56	}
 57
 58	public State setOnEnter(StateEventHandler handler) {
 59		this.enterEventHandler = handler;
 60		return this;
 61	}
 62
 63	public State setOnExit(StateEventHandler handler) {
 64		this.exitEventHandler = handler;
 65		return this;
 66	}
 67
 68	public State setOnTimeOut(StateEventHandler handler, long timeout) {
 69		this.timeOutEventHandler = handler;
 70		this.timeout = timeout;
 71		return this;
 72	}
 73
 74	protected void enter() {
 75		this.activated = System.currentTimeMillis();
 76		this.oldActivated = this.activated;
 77		if (this.enterEventHandler != null) {
 78			this.enterEventHandler.onEvent(this);
 79		}
 80	}
 81
 82	protected void leave() {
 83		this.activated = 0;
 84		if (this.exitEventHandler != null) {
 85			this.exitEventHandler.onEvent(this);
 86		}
 87	}
 88
 89	protected void cancelLeave() {
 90		this.activated = this.oldActivated;
 91	}
 92
 93	protected void tick(long now) {
 94		if (this.timeout > 0 && this.activated > 0 && (now - this.activated) > this.timeout) {
 95			// Call Time Out Event Handler if defined
 96			if (this.timeOutEventHandler != null) {
 97				this.timeOutEventHandler.onEvent(this);
 98			}
 99
100			// Now do the Transition
101			try {
102				fsm.signal("timeout");
103			} catch (UnknownTransitionException e) {
104			}
105		}
106	}
107
108	public String getName() {
109		return name;
110	}
111
112	public FSM getFSM() {
113		return fsm;
114	}
115
116	protected void add(Transition t) {
117		transitions.add(t);
118	}
119
120	/**
121	 * Signals to leave this state over specified transition
122	 * 
123	 * @param name
124	 *            the name of the transition.
125	 */
126	public State signal(String namem) throws UnknownTransitionException {
127		Transition t = find(namem);
128		if (t != null) {
129			return t.process(this);
130		}
131		throw new UnknownTransitionException(String.format("Transition=%s. %s", namem, this.fsm.toString()));
132	}
133
134	/**
135	 * Searches transition with specified name.
136	 * 
137	 * @param name
138	 *            the name of the transition.
139	 * @return the transition or null if not found.
140	 */
141	private Transition find(String name) {
142		for (Transition t : transitions) {
143			if (t.getName().matches(name)) {
144				return t;
145			}
146		}
147		return null;
148	}
149
150	@Override
151	public String toString() {
152		return name;
153	}
154}