/* * TeleStax, Open Source Cloud Communications * Copyright 2011-2016, TeleStax Inc. and individual contributors * by the @authors tag. * * This program is free software: you can redistribute it and/or modify * under the terms of the GNU Affero General Public License as * published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> */ package org.jdiameter.server.impl.app.s13; import org.jdiameter.api.Answer; import org.jdiameter.api.EventListener; import org.jdiameter.api.IllegalDiameterStateException; import org.jdiameter.api.InternalException; import org.jdiameter.api.NetworkReqListener; import org.jdiameter.api.OverloadException; import org.jdiameter.api.Request; import org.jdiameter.api.RouteException; import org.jdiameter.api.app.AppEvent; import org.jdiameter.api.app.StateChangeListener; import org.jdiameter.api.app.StateEvent; import org.jdiameter.api.s13.ServerS13Session; import org.jdiameter.api.s13.ServerS13SessionListener; import org.jdiameter.api.s13.events.JMEIdentityCheckAnswer; import org.jdiameter.api.s13.events.JMEIdentityCheckRequest; import org.jdiameter.client.api.ISessionFactory; import org.jdiameter.common.api.app.s13.IS13MessageFactory; import org.jdiameter.common.api.app.s13.S13SessionState; import org.jdiameter.common.impl.app.AppAnswerEventImpl; import org.jdiameter.common.impl.app.AppRequestEventImpl; import org.jdiameter.common.impl.app.s13.S13Session; import org.jdiameter.server.impl.app.s13.Event.Type; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class S13ServerSessionImpl extends S13Session implements ServerS13Session, EventListener<Request, Answer>, NetworkReqListener { private static final Logger logger = LoggerFactory.getLogger(S13ServerSessionImpl.class); // Factories and Listeners // -------------------------------------------------- private transient ServerS13SessionListener listener; protected long appId = -1; protected IServerS13SessionData sessionData; public S13ServerSessionImpl(IServerS13SessionData sessionData, IS13MessageFactory fct, ISessionFactory sf, ServerS13SessionListener lst) { super(sf, sessionData); if (lst == null) { throw new IllegalArgumentException("Listener can not be null"); } if ((this.appId = fct.getApplicationId()) < 0) { throw new IllegalArgumentException("ApplicationId can not be less than zero"); } this.listener = lst; super.messageFactory = fct; this.sessionData = sessionData; } @Override public void sendMEIdentityCheckAnswer(JMEIdentityCheckAnswer answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException { send(Event.Type.SEND_MESSAGE, null, answer); } @Override @SuppressWarnings("unchecked") public <E> E getState(Class<E> stateType) { return stateType == S13SessionState.class ? (E) this.sessionData.getS13SessionState() : null; } @Override public boolean handleEvent(StateEvent event) throws InternalException, OverloadException { try { sendAndStateLock.lock(); if (!super.session.isValid()) { // FIXME: throw new InternalException("Generic session is not valid."); return false; } final S13SessionState state = this.sessionData.getS13SessionState(); S13SessionState newState = null; Event localEvent = (Event) event; Event.Type eventType = (Type) event.getType(); switch (state) { case IDLE: switch (eventType) { case RECEIVE_ECR: this.sessionData.setBuffer((Request) ((AppEvent) event.getData()).getMessage()); super.cancelMsgTimer(); super.startMsgTimer(); newState = S13SessionState.MESSAGE_SENT_RECEIVED; setState(newState); listener.doMEIdentityCheckRequestEvent(this,(JMEIdentityCheckRequest) event.getData()); break; case SEND_MESSAGE: super.session.send(((AppEvent) event.getData()).getMessage(), this); newState = S13SessionState.MESSAGE_SENT_RECEIVED; setState(newState); break; default: logger.error("Wrong action in S13 Server FSM. State: IDLE, Event Type: {}", eventType); break; } break; case MESSAGE_SENT_RECEIVED: switch (eventType) { case TIMEOUT_EXPIRES: newState = S13SessionState.TIMEDOUT; setState(newState); break; case SEND_MESSAGE: try { super.session.send(((AppEvent) event.getData()).getMessage(), this); } finally { newState = S13SessionState.TERMINATED; setState(newState); } break; default: throw new InternalException("Should not receive more messages after initial. Command: " + event.getData()); } break; case TERMINATED: throw new InternalException("Cant receive message in state TERMINATED. Command: " + event.getData()); case TIMEDOUT: throw new InternalException("Cant receive message in state TIMEDOUT. Command: " + event.getData()); default: logger.error("S13 Server FSM in wrong state: {}", state); break; } } catch (Exception e) { throw new InternalException(e); } finally { sendAndStateLock.unlock(); } return true; } @Override public void receivedSuccessMessage(Request request, Answer answer) { AnswerDelivery rd = new AnswerDelivery(); rd.session = this; rd.request = request; rd.answer = answer; super.scheduler.execute(rd); } @Override public void timeoutExpired(Request request) { try { handleEvent(new Event(Event.Type.TIMEOUT_EXPIRES, new AppRequestEventImpl(request), null)); } catch (Exception e) { logger.debug("Failed to process timeout message", e); } } @Override public Answer processRequest(Request request) { RequestDelivery rd = new RequestDelivery(); rd.session = this; rd.request = request; super.scheduler.execute(rd); return null; } protected void send(Event.Type type, AppEvent request, AppEvent answer) throws InternalException { try { if (type != null) { handleEvent(new Event(type, request, answer)); } } catch (Exception e) { throw new InternalException(e); } } @SuppressWarnings("unchecked") protected void setState(S13SessionState newState) { S13SessionState oldState = this.sessionData.getS13SessionState(); this.sessionData.setS13SessionState(newState); for (StateChangeListener i : stateListeners) { i.stateChanged(this, oldState, newState); } if (newState == S13SessionState.TERMINATED || newState == S13SessionState.TIMEDOUT) { super.cancelMsgTimer(); this.release(); } } @Override public void onTimer(String timerName) { if (timerName.equals(IDLE_SESSION_TIMER_NAME)) { checkIdleAppSession(); } else if (timerName.equals(S13Session.TIMER_NAME_MSG_TIMEOUT)) { try { sendAndStateLock.lock(); try { handleEvent(new Event(Event.Type.TIMEOUT_EXPIRES, new AppRequestEventImpl(this.sessionData.getBuffer()), null)); } catch (Exception e) { logger.debug("Failure handling Timeout event."); } this.sessionData.setBuffer(null); this.sessionData.setTsTimerId(null); } finally { sendAndStateLock.unlock(); } } else { logger.warn("Received an unknown timer '{}' for Session-ID '{}'", timerName, getSessionId()); } } @Override public void release() { if (isValid()) { try { sendAndStateLock.lock(); super.release(); } catch (Exception e) { logger.debug("Failed to release session", e); } finally { sendAndStateLock.unlock(); } } else { logger.debug("Trying to release an already invalid session, with Session ID '{}'", getSessionId()); } } private class RequestDelivery implements Runnable { ServerS13Session session; Request request; @Override public void run() { try { switch (request.getCommandCode()) { case JMEIdentityCheckRequest.code: handleEvent(new Event(Event.Type.RECEIVE_ECR, messageFactory.createMEIdentityCheckRequest(request), null)); break; default: listener.doOtherEvent(session, new AppRequestEventImpl(request), null); break; } } catch (Exception e) { logger.debug("Failed to process request message", e); } } } private class AnswerDelivery implements Runnable { ServerS13Session session; Answer answer; Request request; @Override public void run() { try { switch (answer.getCommandCode()) { default: listener.doOtherEvent(session, new AppRequestEventImpl(request), new AppAnswerEventImpl(answer)); break; } } catch (Exception e) { logger.debug("Failed to process success message", e); } } } }