/*
* 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.mobicents.diameter.impl.ha.common.s13;
import java.io.Serializable;
import java.nio.ByteBuffer;
import org.restcomm.cache.FqnWrapper;
import org.jdiameter.api.AvpDataException;
import org.jdiameter.api.Request;
import org.jdiameter.client.api.IContainer;
import org.jdiameter.client.api.IMessage;
import org.jdiameter.client.api.parser.IMessageParser;
import org.jdiameter.client.api.parser.ParseException;
import org.jdiameter.common.api.app.s13.IS13SessionData;
import org.jdiameter.common.api.app.s13.S13SessionState;
import org.restcomm.cluster.MobicentsCluster;
import org.mobicents.diameter.impl.ha.common.AppSessionDataReplicatedImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
* @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
*/
public abstract class S13SessionDataReplicatedImpl extends AppSessionDataReplicatedImpl implements IS13SessionData {
private static final Logger logger = LoggerFactory.getLogger(S13SessionDataReplicatedImpl.class);
private static final String STATE = "STATE";
private static final String BUFFER = "BUFFER";
private static final String TS_TIMERID = "TS_TIMERID";
private IMessageParser messageParser;
/**
* @param nodeFqnWrapper
* @param mobicentsCluster
* @param container
*/
public S13SessionDataReplicatedImpl(FqnWrapper nodeFqnWrapper, MobicentsCluster mobicentsCluster, IContainer container) {
super(nodeFqnWrapper, mobicentsCluster);
this.messageParser = container.getAssemblerFacility().getComponentInstance(IMessageParser.class);
}
/*
* (non-Javadoc)
*
* @see org.jdiameter.common.api.app.s13.IS13SessionData#setS13SessionState(org.jdiameter.common.api.app.s13.S13SessionState)
*/
@Override
public void setS13SessionState(S13SessionState state) {
if (exists()) {
putNodeValue(STATE, state);
}
else {
throw new IllegalStateException();
}
}
/*
* (non-Javadoc)
*
* @see org.jdiameter.common.api.app.s13.IS13SessionData#getS13SessionState()
*/
@Override
public S13SessionState getS13SessionState() {
if (exists()) {
return (S13SessionState) getNodeValue(STATE);
}
else {
throw new IllegalStateException();
}
}
/*
* (non-Javadoc)
*
* @see org.jdiameter.common.api.app.s13.IS13SessionData#getTsTimerId()
*/
@Override
public Serializable getTsTimerId() {
if (exists()) {
return (Serializable) getNodeValue(TS_TIMERID);
}
else {
throw new IllegalStateException();
}
}
/*
* (non-Javadoc)
*
* @see org.jdiameter.common.api.app.s13.IS13SessionData#setTsTimerId(java.io.Serializable)
*/
@Override
public void setTsTimerId(Serializable tid) {
if (exists()) {
putNodeValue(TS_TIMERID, tid);
}
else {
throw new IllegalStateException();
}
}
@Override
public Request getBuffer() {
byte[] data = (byte[]) getNodeValue(BUFFER);
if (data != null) {
try {
return this.messageParser.createMessage(ByteBuffer.wrap(data));
}
catch (AvpDataException e) {
logger.error("Unable to recreate message from buffer.");
return null;
}
}
else {
return null;
}
}
@Override
public void setBuffer(Request buffer) {
if (buffer != null) {
try {
byte[] data = this.messageParser.encodeMessage((IMessage) buffer).array();
putNodeValue(BUFFER, data);
}
catch (ParseException e) {
logger.error("Unable to encode message to buffer.");
}
}
else {
removeNodeValue(BUFFER);
}
}
}