Skip to content

Commit

Permalink
Fixes RestComm#158 - Diameter client will now attempt to reconnect to…
Browse files Browse the repository at this point in the history
… a Peer from which it has received a Disconnect-Peer-Request, specifically with Disconnect-Cause AVP containing value 0 REBOOTING.
  • Loading branch information
Steve Dwyer committed Jul 29, 2020
1 parent 3f9fafc commit f5dfa15
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.jdiameter.api.Avp;
import org.jdiameter.api.AvpDataException;
import org.jdiameter.api.Configuration;
import org.jdiameter.api.DisconnectCause;
import org.jdiameter.api.Message;
Expand Down Expand Up @@ -432,6 +434,14 @@ protected void doDisconnect() {
}
}

protected boolean willReconnect(IMessage message) throws AvpDataException {
// Only attempt to reconnect when the Disconnect-Cause AVP was 0 REBOOTING
// and the Peer's LocalActionContext is configured to attempt connection
Avp disconnectCause = message.getAvps().getAvp(Avp.DISCONNECT_CAUSE);
boolean willReconnect = (disconnectCause != null) ? (disconnectCause.getInteger32() == DisconnectCause.REBOOTING) : false;
return willReconnect && context.isRestoreConnection();
}

protected void setTimer(long value) {
timer = value + System.currentTimeMillis();
}
Expand All @@ -456,7 +466,7 @@ protected void clearTimer() {
protected org.jdiameter.api.app.State[] getStates() {
if (states == null) {
states = new org.jdiameter.api.app.State[] { // todo merge and redesign with server fsm
new MyState() { // OKEY
new MyState() { // OKAY
@Override
public void entryAction() {
setInActiveTimer();
Expand Down Expand Up @@ -518,8 +528,24 @@ public boolean processEvent(StateEvent event) {
catch (Throwable e) {
logger.debug("Can not send DPA", e);
}
doDisconnect();
switchToNextState(FsmState.DOWN);

IMessage message = (IMessage) event.getData();
try {
if (willReconnect(message)) {
doDisconnect();
doEndConnection();
}
else {
doDisconnect();
switchToNextState(DOWN);
}
}
catch (AvpDataException ade) {
logger.warn("Disconnect cause is bad.", ade);
doDisconnect();
switchToNextState(DOWN);
}

break;
case DWR_EVENT:
setInActiveTimer();
Expand Down Expand Up @@ -594,8 +620,24 @@ public boolean processEvent(StateEvent event) {
catch (Throwable e) {
logger.debug("Can not send DPA", e);
}
doDisconnect();
switchToNextState(FsmState.DOWN);

IMessage message = (IMessage) event.getData();
try {
if (willReconnect(message)) {
doDisconnect();
doEndConnection();
}
else {
doDisconnect();
switchToNextState(DOWN);
}
}
catch (AvpDataException ade) {
logger.warn("Disconnect cause is bad.", ade);
doDisconnect();
switchToNextState(DOWN);
}

break;
case DWA_EVENT:
switchToNextState(FsmState.OKAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public boolean isAttemptConnection() {

@Override
public IContext getContext() {
return new LocalActionConext();
return new LocalActionContext();
}

@Override
Expand Down Expand Up @@ -210,7 +210,7 @@ public String toString() {
return "SPeer{" + "Uri=" + uri + "; State=" + fsm + "; con=" + connection + "; incCon" + incConnections + " }";
}

protected class LocalActionConext extends ActionContext {
protected class LocalActionContext extends ActionContext {

@Override
public void sendCeaMessage(int resultCode, Message cer, String errMessage) throws TransportException, OverloadException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public boolean elementChanged(int i, Object data) {
protected State[] getStates() {
if (states == null) {
states = new State[] {
new MyState() { // OKEY
new MyState() { // OKAY
@Override
public void entryAction() { // todo send buffered messages
setInActiveTimer();
Expand Down

0 comments on commit f5dfa15

Please sign in to comment.