/* * 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.client.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.ClientS13Session; import org.jdiameter.api.s13.ClientS13SessionListener; import org.jdiameter.api.s13.events.JMEIdentityCheckAnswer; import org.jdiameter.api.s13.events.JMEIdentityCheckRequest; import org.jdiameter.client.api.ISessionFactory; import org.jdiameter.client.impl.app.s13.Event.Type; 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.slf4j.Logger; import org.slf4j.LoggerFactory; public class S13ClientSessionImpl extends S13Session implements ClientS13Session, EventListener<Request, Answer>, NetworkReqListener { private static final Logger logger = LoggerFactory.getLogger(S13ClientSessionImpl.class); private transient ClientS13SessionListener listener; protected long appId = -1; protected IClientS13SessionData sessionData; public S13ClientSessionImpl(IClientS13SessionData sessionData, IS13MessageFactory fct, ISessionFactory sf, ClientS13SessionListener lst) { super(sf, sessionData); if (lst == null) { throw new IllegalArgumentException("Listener can not be null"); } if (fct.getApplicationId() < 0) { throw new IllegalArgumentException( "ApplicationId can not be less than zero"); } this.appId = fct.getApplicationId(); this.listener = lst; super.messageFactory = fct; this.sessionData = sessionData; } @Override @SuppressWarnings("unchecked") public <E> E getState(Class<E> stateType) { return stateType == S13SessionState.class ? (E) this.sessionData.getS13SessionState() : null; } @Override public Answer processRequest(Request request) { RequestDelivery rd = new RequestDelivery(); rd.session = this; rd.request = request; super.scheduler.execute(rd); return null; } @Override public void sendMEIdentityCheckRequest(JMEIdentityCheckRequest request) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException { send(Event.Type.SEND_MESSAGE, request, null); } @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); } } 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); } } @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 SEND_MESSAGE: newState = S13SessionState.MESSAGE_SENT_RECEIVED; super.session.send(((AppEvent) event.getData()).getMessage(), this); setState(newState); // FIXME: is this ok to be here? break; default: logger.error( "Invalid Event Type {} for S13 Client Session at state {}.", eventType, sessionData.getS13SessionState()); 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; case RECEIVE_ECA: newState = S13SessionState.TERMINATED; setState(newState); super.cancelMsgTimer(); listener.doMEIdentityCheckAnswerEvent(this,(JMEIdentityCheckRequest) localEvent.getRequest(), (JMEIdentityCheckAnswer) localEvent.getAnswer()); break; default: throw new InternalException("Unexpected/Unknown message received: " + 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 Client FSM in wrong state: {}", state); break; } } catch (Exception e) { throw new InternalException(e); } finally { sendAndStateLock.unlock(); } return true; } @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 int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + (int) (appId ^ (appId >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!super.equals(obj)) { return false; } if (getClass() != obj.getClass()) { return false; } S13ClientSessionImpl other = (S13ClientSessionImpl) obj; if (appId != other.appId) { return false; } return true; } @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 { ClientS13Session session; Request request; @Override public void run() { try { switch (request.getCommandCode()) { 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 { ClientS13Session session; Answer answer; Request request; @Override public void run() { try { switch (answer.getCommandCode()) { case JMEIdentityCheckAnswer.code: handleEvent(new Event(Event.Type.RECEIVE_ECA, messageFactory.createMEIdentityCheckRequest(request), messageFactory.createMEIdentityCheckAnswer(answer))); break; default: listener.doOtherEvent(session, new AppRequestEventImpl(request), new AppAnswerEventImpl(answer)); break; } } catch (Exception e) { logger.debug("Failed to process success message", e); } } } }