4747import java .util .List ;
4848import java .util .UUID ;
4949import java .util .concurrent .ExecutionException ;
50+ import java .util .concurrent .atomic .AtomicBoolean ;
5051import java .util .concurrent .atomic .AtomicReference ;
5152
5253@ Component
@@ -60,6 +61,7 @@ public class MMSAgent {
6061
6162 private final KeystoreUtil keystoreUtil ;
6263 private final AtomicReference <MmtpMessage > lastSentMessage = new AtomicReference <>();
64+ private final AtomicBoolean shuttingDown = new AtomicBoolean (false );
6365
6466 private WebSocketSession webSocketSession ;
6567
@@ -71,21 +73,12 @@ public MMSAgent(KeystoreUtil keystoreUtil) {
7173 @ PostConstruct
7274 public void init () throws URISyntaxException , ExecutionException , InterruptedException , NoSuchAlgorithmException ,
7375 CertificateException , KeyStoreException , IOException , UnrecoverableKeyException , KeyManagementException {
74- StandardWebSocketClient webSocketClient = new StandardWebSocketClient ();
75-
76- KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
77- keyManagerFactory .init (keystoreUtil .getMmsKeystore (), keystoreUtil .getMmsKeystorePassword ());
78-
79- SSLContext sslContext = SSLContext .getInstance ("TLS" );
80- sslContext .init (keyManagerFactory .getKeyManagers (), null , null );
81-
82- webSocketClient .setSslContext (sslContext );
83- URI uri = new URI (edgeRouterURL );
84- webSocketSession = webSocketClient .execute (new MMSWebsocketHandler (), null , uri ).get ();
76+ setupWebSocket ();
8577 }
8678
8779 @ PreDestroy
8880 public void preDestroy () throws IOException , InterruptedException {
81+ shuttingDown .set (true );
8982 if (webSocketSession .isOpen ()) {
9083 MmtpMessage disconnect = MmtpMessage .newBuilder ()
9184 .setMsgType (MsgType .PROTOCOL_MESSAGE )
@@ -134,6 +127,23 @@ public void sendMessage(MmtpMessage mmtpMessage) throws IOException {
134127 lastSentMessage .set (mmtpMessage );
135128 }
136129
130+ private void setupWebSocket () throws NoSuchAlgorithmException , KeyStoreException , UnrecoverableKeyException ,
131+ CertificateException , IOException , KeyManagementException , URISyntaxException , InterruptedException ,
132+ ExecutionException {
133+ StandardWebSocketClient webSocketClient = new StandardWebSocketClient ();
134+
135+ KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
136+ keyManagerFactory .init (keystoreUtil .getMmsKeystore (), keystoreUtil .getMmsKeystorePassword ());
137+
138+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
139+ sslContext .init (keyManagerFactory .getKeyManagers (), null , null );
140+
141+ webSocketClient .setSslContext (sslContext );
142+ URI uri = new URI (edgeRouterURL );
143+ webSocketSession = webSocketClient .execute (new MMSWebsocketHandler (), null , uri ).get ();
144+ log .info ("Connected to edge router {}" , edgeRouterURL );
145+ }
146+
137147 private byte [] generateSignature (String subject , long expires , String ownMrn , byte [] body )
138148 throws SignatureException , UnrecoverableEntryException , CertificateException , NoSuchAlgorithmException ,
139149 KeyStoreException , IOException , InvalidKeyException {
@@ -199,12 +209,29 @@ protected void handleBinaryMessage(WebSocketSession session, BinaryMessage messa
199209
200210 @ Override
201211 public void afterConnectionClosed (WebSocketSession session , CloseStatus status ) throws Exception {
202- if (!status .equalsCode (CloseStatus .NORMAL )) {
212+ if (!status .equalsCode (CloseStatus .NORMAL ) && ! shuttingDown . get () ) {
203213 if (StringUtils .hasText (status .getReason ())) {
204214 log .error ("The websocket was closed with code {} and reason {}" , status .getCode (), status .getReason ());
205215 } else {
206216 log .error ("The websocket was closed with code {}" , status .getCode ());
207217 }
218+ try {
219+ session .close ();
220+ } catch (IOException e ) {
221+ log .error ("Failed to close websocket" , e );
222+ }
223+ int retryCount = 0 ;
224+ while (retryCount < 10 && !shuttingDown .get ()) {
225+ try {
226+ Thread .sleep (5000 );
227+ setupWebSocket ();
228+ return ;
229+ } catch (IOException | ExecutionException e ) {
230+ log .error ("Couldn't connect to the websocket, trying again: {}" , e .getMessage ());
231+ // If we fail, we just try again
232+ retryCount ++;
233+ }
234+ }
208235 }
209236 session .close ();
210237 }
0 commit comments