66import arraybuffers = require( '../../arraybuffers/arraybuffers' ) ;
77import linefeeder = require( '../../net/linefeeder' ) ;
88import logging = require( '../../logging/logging' ) ;
9+ import promises = require( '../../promises/promises' ) ;
910import queue = require( '../../handler/queue' ) ;
1011
1112// https://github.com/borisyankov/DefinitelyTyped/blob/master/ssh2/ssh2-tests.ts
@@ -25,6 +26,9 @@ const STORAGE_KEY = 'cloud-social-contacts';
2526// Timeout for establishing an SSH connection.
2627const CONNECT_TIMEOUT_MS = 10000 ;
2728
29+ // Maximum number of times to try establishing an SSH connection.
30+ const MAX_CONNECT_ATTEMPTS = 3 ;
31+
2832// Credentials for accessing a cloud instance.
2933// The serialised, base64 form is distributed amongst users.
3034interface Invite {
@@ -199,15 +203,23 @@ export class CloudSocialProvider {
199203 } ) ;
200204 }
201205
202- var connection = new Connection ( invite , ( message : Object ) => {
203- this . dispatchEvent_ ( 'onMessage' , {
204- from : makeClientState ( invite . host ) ,
205- // SIGNAL_FROM_SERVER_PEER,
206- message : JSON . stringify ( makeVersionedPeerMessage ( 3002 , message ) )
206+ let numAttempts = 0 ;
207+ const connect = ( ) => {
208+ log . debug ( 'connection attempt %1...' , ( ++ numAttempts ) ) ;
209+ const connection = new Connection ( invite , ( message : Object ) => {
210+ this . dispatchEvent_ ( 'onMessage' , {
211+ from : makeClientState ( invite . host ) ,
212+ // SIGNAL_FROM_SERVER_PEER,
213+ message : JSON . stringify ( makeVersionedPeerMessage ( 3002 , message ) )
214+ } ) ;
207215 } ) ;
208- } ) ;
209-
210- this . clients_ [ invite . host ] = connection . connect ( ) . then ( ( ) => {
216+ return connection . connect ( ) . then ( ( ) => {
217+ return connection ;
218+ } ) ;
219+ } ;
220+
221+ this . clients_ [ invite . host ] = promises . retry ( connect ,
222+ MAX_CONNECT_ATTEMPTS ) . then ( ( connection :Connection ) => {
211223 log . info ( 'connected to zork on %1' , invite . host ) ;
212224
213225 // Fetch the banner, if available, then emit an instance message.
0 commit comments