com.mobius.software.telco.protocols.diameter.impl.app.ServerAuthSessionImpl Maven / Gradle / Ivy
package com.mobius.software.telco.protocols.diameter.impl.app;
/*
* Mobius Software LTD
* Copyright 2023, Mobius Software LTD 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
*/
import java.util.Collection;
import com.mobius.software.common.dal.timers.Task;
import com.mobius.software.telco.protocols.diameter.AsyncCallback;
import com.mobius.software.telco.protocols.diameter.DiameterProvider;
import com.mobius.software.telco.protocols.diameter.ResultCodes;
import com.mobius.software.telco.protocols.diameter.app.ServerAuthListener;
import com.mobius.software.telco.protocols.diameter.app.ServerAuthSession;
import com.mobius.software.telco.protocols.diameter.app.SessionStateEnum;
import com.mobius.software.telco.protocols.diameter.commands.DiameterAnswer;
import com.mobius.software.telco.protocols.diameter.commands.DiameterRequest;
import com.mobius.software.telco.protocols.diameter.commands.commons.AbortSessionAnswer;
import com.mobius.software.telco.protocols.diameter.commands.commons.AbortSessionRequest;
import com.mobius.software.telco.protocols.diameter.commands.commons.ReAuthAnswer;
import com.mobius.software.telco.protocols.diameter.commands.commons.ReAuthRequest;
import com.mobius.software.telco.protocols.diameter.commands.commons.SessionTerminationAnswer;
import com.mobius.software.telco.protocols.diameter.commands.commons.SessionTerminationRequest;
import com.mobius.software.telco.protocols.diameter.exceptions.DiameterException;
import com.mobius.software.telco.protocols.diameter.exceptions.MissingAvpException;
import com.mobius.software.telco.protocols.diameter.impl.DiameterSessionImpl;
/**
*
* @author yulian oifa
*
*/
public class ServerAuthSessionImpl extends DiameterSessionImpl implements ServerAuthSession
{
private DiameterProvider, ? extends ServerAuthListener, ?, ?, ?> provider;
//for serialization
public ServerAuthSessionImpl(Long applicationID)
{
super(applicationID);
}
public ServerAuthSessionImpl(String sessionID, Long applicationID, String remoteHost, String remoteRealm, DiameterProvider, ? extends ServerAuthListener, ?, ?, ?> provider)
{
super(sessionID, applicationID, remoteHost, remoteRealm, provider);
this.provider = provider;
}
@SuppressWarnings("unchecked")
@Override
public void setProvider(DiameterProvider, ?, ?, ?, ?> provider)
{
this.provider = (DiameterProvider, ? extends ServerAuthListener, ?, ?, ?>)provider;
super.setProvider(provider);
}
@Override
public void sendInitialAnswer(A1 answer, AsyncCallback callback)
{
if(getSessionState()!=null && getSessionState()!=SessionStateEnum.IDLE && getSessionState()!=SessionStateEnum.OPEN)
{
callback.onError(new DiameterException("session state is invalid, can not send message", null, ResultCodes.DIAMETER_UNABLE_TO_COMPLY, null));
return;
}
final Long startTime = System.currentTimeMillis();
provider.getStack().getQueue().offerLast(new Task()
{
@Override
public long getStartTime()
{
return startTime;
}
@Override
public void execute()
{
if(answer.getIsError()!=null && answer.getIsError())
{
setSessionState(SessionStateEnum.IDLE);
terminate(answer.getResultCode());
}
else
{
setSessionState(SessionStateEnum.OPEN);
answerSent(answer, null, callback);
}
provider.getStack().sendAnswer(answer, getRemoteHost(), getRemoteRealm(), callback);
}
});
}
@Override
public void sendReauthRequest(R2 request, AsyncCallback callback)
{
if(getSessionState()==null || (getSessionState()!=SessionStateEnum.IDLE && getSessionState()!=SessionStateEnum.OPEN))
{
callback.onError(new DiameterException("session state is invalid, can not send message", null, ResultCodes.DIAMETER_UNABLE_TO_COMPLY, null));
return;
}
final Long startTime = System.currentTimeMillis();
provider.getStack().getQueue().offerLast(new Task()
{
@Override
public long getStartTime()
{
return startTime;
}
@Override
public void execute()
{
setSessionState(SessionStateEnum.PENDING);
if(request.getDestinationRealm()==null && getRemoteRealm()!=null)
{
try
{
request.setDestinationRealm(getRemoteRealm());
}
catch(MissingAvpException ex)
{
}
}
setLastSentRequest(request);
requestSent(false, request, callback);
provider.getStack().sendRequest(request, callback);
}
});
}
@Override
public void sendSessionTerminationAnswer(A4 answer, AsyncCallback callback)
{
if(getSessionState()==null || (getSessionState()!=SessionStateEnum.IDLE && getSessionState()!=SessionStateEnum.OPEN))
{
callback.onError(new DiameterException("session state is invalid, can not send message", null, ResultCodes.DIAMETER_UNABLE_TO_COMPLY, null));
return;
}
final Long startTime = System.currentTimeMillis();
provider.getStack().getQueue().offerLast(new Task()
{
@Override
public long getStartTime()
{
return startTime;
}
@Override
public void execute()
{
setSessionState(SessionStateEnum.IDLE);
terminate(answer.getResultCode());
provider.getStack().sendAnswer(answer, getRemoteHost(), getRemoteRealm(), callback);
}
});
}
@Override
public void sendAbortSessionRequest(R3 request, AsyncCallback callback)
{
if(getSessionState()==null || (getSessionState()!=SessionStateEnum.IDLE && getSessionState()!=SessionStateEnum.OPEN))
{
callback.onError(new DiameterException("session state is invalid, can not send message", null, ResultCodes.DIAMETER_UNABLE_TO_COMPLY, null));
return;
}
final Long startTime = System.currentTimeMillis();
provider.getStack().getQueue().offerLast(new Task()
{
@Override
public long getStartTime()
{
return startTime;
}
@Override
public void execute()
{
setSessionState(SessionStateEnum.DISCONNECTED);
if(request.getDestinationRealm()==null && getRemoteRealm()!=null)
{
try
{
request.setDestinationRealm(getRemoteRealm());
}
catch(MissingAvpException ex)
{
}
}
requestSent(false, request, callback);
provider.getStack().sendRequest(request, callback);
}
});
}
@SuppressWarnings("unchecked")
@Override
public void requestReceived(DiameterRequest request,String linkID,AsyncCallback callback)
{
Collection> listeners = null;
if(provider.getServerListeners()!=null)
listeners = (Collection>) provider.getServerListeners().values();
if(request instanceof SessionTerminationRequest)
{
try
{
R4 castedRequest = (R4)request;
super.requestReceived(request, linkID,callback);
if(listeners!=null)
{
for(ServerAuthListener listener:listeners)
listener.onSessionTerminationRequest(castedRequest, this, linkID,callback);
}
}
catch(Exception ex)
{
logger.warn("An error occured while processing incoming request," + ex.getMessage(),ex);
callback.onError(new DiameterException("Received unexpected request", null, ResultCodes.DIAMETER_COMMAND_UNSUPPORTED, null));
return;
}
}
else
{
try
{
R1 castedRequest = (R1)request;
super.requestReceived(request, linkID,callback);
if(listeners!=null)
{
for(ServerAuthListener listener:listeners)
listener.onInitialRequest(castedRequest, this, linkID,callback);
}
}
catch(Exception ex)
{
logger.warn("An error occured while processing incoming request," + ex.getMessage(),ex);
callback.onError(new DiameterException("Received unexpected request", null, ResultCodes.DIAMETER_COMMAND_UNSUPPORTED, null));
return;
}
}
}
@SuppressWarnings("unchecked")
@Override
public void answerReceived(DiameterAnswer answer, Long idleTime,Boolean stopSendTimer,String linkID, AsyncCallback callback)
{
try
{
validateAnswer(answer);
}
catch(DiameterException ex)
{
callback.onError(new DiameterException("Received unexpected answer", null, ResultCodes.DIAMETER_COMMAND_UNSUPPORTED, null));
return;
}
DiameterRequest request = getLastSendRequest();
if(request!=null)
{
Collection> listeners = null;
if(provider.getServerListeners()!=null)
listeners = (Collection>) provider.getServerListeners().values();
if(request instanceof AbortSessionRequest)
{
if(answer instanceof AbortSessionAnswer)
{
try
{
A3 castedAnswer = (A3)answer;
setSessionState(SessionStateEnum.IDLE);
terminate(castedAnswer.getResultCode());
if(listeners!=null)
{
for(ServerAuthListener listener:listeners)
listener.onAbortSessionAnswer(castedAnswer, this, linkID,callback);
}
}
catch(Exception ex)
{
callback.onError(new DiameterException("Received unexpected answer", null, ResultCodes.DIAMETER_COMMAND_UNSUPPORTED, null));
return;
}
}
}
else if(request instanceof ReAuthRequest)
{
if(answer instanceof ReAuthAnswer)
{
try
{
A2 castedAnswer = (A2)answer;
if(castedAnswer.getResultCode()!=null && !castedAnswer.getIsError())
{
setSessionState(SessionStateEnum.OPEN);
super.answerReceived(answer, idleTime, stopSendTimer,linkID, callback);
if(listeners!=null)
{
for(ServerAuthListener listener:listeners)
listener.onReauthAnswer(castedAnswer, this, linkID, callback);
}
}
else
{
setSessionState(SessionStateEnum.IDLE);
terminate(castedAnswer.getResultCode());
if(listeners!=null)
{
for(ServerAuthListener listener:listeners)
listener.onReauthAnswer(castedAnswer, this, linkID, callback);
}
}
}
catch(Exception ex)
{
callback.onError(new DiameterException("Received unexpected answer", null, ResultCodes.DIAMETER_COMMAND_UNSUPPORTED, null));
return;
}
}
}
}
else
callback.onError(new DiameterException("Received unexpected answer", null, ResultCodes.DIAMETER_COMMAND_UNSUPPORTED, null));
}
@Override
public boolean isServer()
{
return true;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy