PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/sg/RemAsStatePenTimeout.java

http://mobicents.googlecode.com/
Java | 130 lines | 64 code | 19 blank | 47 comment | 8 complexity | d01c5d1bf2f7e97615e6cd2ea3f83493 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.sg;
 24
 25import javolution.util.FastList;
 26
 27import org.apache.log4j.Logger;
 28import org.mobicents.protocols.ss7.m3ua.impl.Asp;
 29import org.mobicents.protocols.ss7.m3ua.impl.AspState;
 30import org.mobicents.protocols.ss7.m3ua.impl.TransitionState;
 31import org.mobicents.protocols.ss7.m3ua.impl.fsm.FSM;
 32import org.mobicents.protocols.ss7.m3ua.impl.fsm.State;
 33import org.mobicents.protocols.ss7.m3ua.impl.fsm.StateEventHandler;
 34import org.mobicents.protocols.ss7.m3ua.impl.fsm.UnknownTransitionException;
 35import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
 36import org.mobicents.protocols.ss7.m3ua.message.MessageType;
 37import org.mobicents.protocols.ss7.m3ua.message.mgmt.Notify;
 38import org.mobicents.protocols.ss7.m3ua.parameter.Status;
 39
 40/**
 41 * {@link RemAsStatePenTimeout#onEvent(State)} is called when the pending timer
 42 * T(r) expires.
 43 * 
 44 * @author amit bhayani
 45 * 
 46 */
 47public class RemAsStatePenTimeout implements StateEventHandler {
 48
 49	private RemAsImpl as;
 50	private FSM fsm;
 51	private static final Logger logger = Logger.getLogger(RemAsStatePenTimeout.class);
 52
 53	boolean inactive = false;
 54
 55	public RemAsStatePenTimeout(RemAsImpl as, FSM fsm) {
 56		this.as = as;
 57		this.fsm = fsm;
 58	}
 59
 60	/**
 61	 * <p>
 62	 * An active ASP has transitioned to ASP-INACTIVE or ASP DOWN and it was the
 63	 * last remaining active ASP in the AS. A recovery timer T(r) SHOULD be
 64	 * started, and all incoming signalling messages SHOULD be queued by the
 65	 * SGP. If an ASP becomes ASP-ACTIVE before T(r) expires, the AS is moved to
 66	 * the AS-ACTIVE state, and all the queued messages will be sent to the ASP.
 67	 * </p>
 68	 * <p>
 69	 * If T(r) expires before an ASP becomes ASP-ACTIVE, and the SGP has no
 70	 * alternative, the SGP may stop queuing messages and discard all previously
 71	 * queued messages. The AS will move to the AS-INACTIVE state if at least
 72	 * one ASP is in ASP-INACTIVE; otherwise, it will move to AS-DOWN state.
 73	 * </p>
 74	 */
 75	public void onEvent(State state) {
 76		this.inactive = false;
 77		
 78		//Clear the Pending Queue for this As
 79		this.as.clearPendingQueue();
 80		
 81		// check if there are any ASP's who are INACTIVE, transition to
 82		// INACTIVE else DOWN
 83		for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList().tail(); (n = n.getNext()) != end;) {
 84			RemAspImpl remAspImpl = (RemAspImpl) n.getValue();
 85			if (remAspImpl.getState() == AspState.INACTIVE) {
 86				try {
 87
 88					if (!this.inactive) {
 89						this.fsm.signal(TransitionState.AS_INACTIVE);
 90						inactive = true;
 91					}
 92					Notify msg = createNotify(remAspImpl);
 93					remAspImpl.getAspFactory().write(msg);
 94				} catch (UnknownTransitionException e) {
 95					logger.error(String.format("Error while translating Rem AS to INACTIVE. %s", this.fsm.toString()),
 96							e);
 97				}
 98
 99			}// if (remAspImpl.getState() == AspState.INACTIVE)
100		}// for
101
102		if (!this.inactive) {
103			// else transition to DOWN
104			try {
105				this.fsm.signal(TransitionState.AS_DOWN);
106				inactive = true;
107			} catch (UnknownTransitionException e) {
108				logger.error(String.format("Error while translating Rem AS to DOWN. %s", this.fsm.toString()), e);
109			}
110		}
111	}
112
113	private Notify createNotify(RemAspImpl remAsp) {
114		Notify msg = (Notify) this.as.getM3UAProvider().getMessageFactory()
115				.createMessage(MessageClass.MANAGEMENT, MessageType.NOTIFY);
116
117		Status status = this.as.getM3UAProvider().getParameterFactory()
118				.createStatus(Status.STATUS_AS_State_Change, Status.INFO_AS_INACTIVE);
119		msg.setStatus(status);
120
121		if (remAsp.getASPIdentifier() != null) {
122			msg.setASPIdentifier(remAsp.getASPIdentifier());
123		}
124
125		msg.setRoutingContext(this.as.getRoutingContext());
126
127		return msg;
128	}
129
130}