diff --git a/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/fsm/PeerFSMImpl.java b/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/fsm/PeerFSMImpl.java index a94157cca..9a229606b 100644 --- a/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/fsm/PeerFSMImpl.java +++ b/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/fsm/PeerFSMImpl.java @@ -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; @@ -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(); } @@ -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(); @@ -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(); @@ -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); diff --git a/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/PeerImpl.java b/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/PeerImpl.java index b9df5465a..80f655d9c 100644 --- a/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/PeerImpl.java +++ b/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/PeerImpl.java @@ -160,7 +160,7 @@ public boolean isAttemptConnection() { @Override public IContext getContext() { - return new LocalActionConext(); + return new LocalActionContext(); } @Override @@ -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 { diff --git a/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/fsm/PeerFSMImpl.java b/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/fsm/PeerFSMImpl.java index 0e5ae3ff3..6c09609a5 100644 --- a/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/fsm/PeerFSMImpl.java +++ b/core/jdiameter/impl/src/main/java/org/jdiameter/server/impl/fsm/PeerFSMImpl.java @@ -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();