Skip to content

Commit 7993842

Browse files
author
Daniel Borkan
committed
Changes for code review
1 parent 8508793 commit 7993842

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

src/lib/cloud/social/provider.ts

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ export class CloudSocialProvider {
173173
private onlineHosts_: { [host: string]: boolean } = {};
174174

175175
// Map from host to intervalId used for monitoring online presence.
176-
private onlinePresenceMonitorIds_: { [host: string]: number } = {};
176+
private onlinePresenceMonitorIds_: { [host: string]: NodeJS.Timer } = {};
177+
178+
static PING_INTERVAL = 60000;
177179

178180
constructor(private dispatchEvent_: (name: string, args: Object) => void) { }
179181

@@ -224,6 +226,9 @@ export class CloudSocialProvider {
224226
this.clients_[invite.host] = connection.connect().then(() => {
225227
log.info('connected to zork on %1', invite.host);
226228

229+
// Cloud server is online if a connection has succeeded.
230+
this.setOnlineStatus_(invite.host, true);
231+
227232
// Fetch the banner, if available, then emit an instance message.
228233
connection.getBanner().then((banner: string) => {
229234
if (banner.length < 1) {
@@ -239,8 +244,6 @@ export class CloudSocialProvider {
239244
description: banner,
240245
version: connection.getVersion()
241246
};
242-
// Cloud server must be online if it is responding with it's banner.
243-
this.setOnlineStatus_(invite.host, true);
244247
this.notifyOfUser_(this.savedContacts_[invite.host]);
245248
this.saveContacts_();
246249
});
@@ -444,20 +447,42 @@ export class CloudSocialProvider {
444447
return this.saveContacts_();
445448
}
446449

447-
private startMonitoringPresence_(host: string) {
450+
private startMonitoringPresence_ = (host: string) => {
448451
if (this.onlinePresenceMonitorIds_[host]) {
449452
log.error('unexpected call to startMonitoringPresence_ for ' + host);
450453
return;
451454
}
452-
// Ping server every minute to see if it is online
453-
const ping = this.ping_.bind(this, host);
454-
const PING_INTERVAL = 60000;
455-
this.onlinePresenceMonitorIds_[host] = setInterval(ping, PING_INTERVAL);
455+
// Ping server every minute to see if it is online. A server is considered
456+
// online if a connection can be established with the SSH port.
457+
const ping = () : Promise<boolean> => {
458+
var pinger = new Pinger(host, SSH_SERVER_PORT);
459+
return pinger.pingOnce().then(() => {
460+
return true;
461+
}).catch(() => {
462+
return false;
463+
}).then((newOnlineValue: boolean) => {
464+
var oldOnlineValue = this.isOnline_(host);
465+
this.setOnlineStatus_(host, newOnlineValue);
466+
if (newOnlineValue !== oldOnlineValue) {
467+
// status changed, emit a new onClientState.
468+
this.notifyOfUser_(this.savedContacts_[host]);
469+
if (newOnlineValue) {
470+
// Connect in the background in order to fetch metadata such as
471+
// the banner (description).
472+
const invite = this.savedContacts_[host].invite;
473+
this.reconnect_(invite).catch((e: Error) => {
474+
log.error('failed to log into cloud server once online: %1', e.message);
475+
});
476+
}
477+
}
478+
});
479+
}
480+
this.onlinePresenceMonitorIds_[host] = setInterval(ping, CloudSocialProvider.PING_INTERVAL);
456481
// Ping server immediately (so we don't have to wait 1 min for 1st result).
457482
ping();
458483
}
459484

460-
private stopMonitoringPresence_(host: string) {
485+
private stopMonitoringPresence_ = (host: string) => {
461486
if (!this.onlinePresenceMonitorIds_[host]) {
462487
log.error('unexpected call to stopMonitoringPresence_ for ' + host);
463488
return;
@@ -466,41 +491,17 @@ export class CloudSocialProvider {
466491
delete this.onlinePresenceMonitorIds_[host];
467492
}
468493

469-
private ping_(host: string) : Promise<boolean> {
470-
var pinger = new Pinger(host, SSH_SERVER_PORT);
471-
return pinger.pingOnce().then(() => {
472-
return Promise.resolve(true);
473-
}).catch(() => {
474-
return Promise.resolve(false);
475-
}).then((newOnlineValue: boolean) => {
476-
var oldOnlineValue = this.isOnline_(host);
477-
this.setOnlineStatus_(host, newOnlineValue);
478-
if (newOnlineValue !== oldOnlineValue) {
479-
// status changed, emit a new onClientState.
480-
this.notifyOfUser_(this.savedContacts_[host]);
481-
if (newOnlineValue) {
482-
// Connect in the background in order to fetch metadata such as
483-
// the banner (description).
484-
const invite = this.savedContacts_[host].invite;
485-
this.reconnect_(invite).catch((e: Error) => {
486-
log.error('failed to log into cloud server once online: %1', e.message);
487-
});
488-
}
489-
}
490-
});
491-
}
492-
493-
private isOnline_(host: string) {
494+
private isOnline_ = (host: string) => {
494495
return host === 'me' || this.onlineHosts_[host] === true;
495496
}
496497

497-
private setOnlineStatus_(host: string, isOnline: boolean) {
498+
private setOnlineStatus_ = (host: string, isOnline: boolean) => {
498499
this.onlineHosts_[host] = isOnline;
499500
}
500501

501502
// To see how these fields are handled, see
502503
// generic_core/social.ts#handleClient in the uProxy repo.
503-
private makeClientState_(address: string): freedom.Social.ClientState {
504+
private makeClientState_ = (address: string) : freedom.Social.ClientState => {
504505
return {
505506
userId: address,
506507
clientId: address,

src/lib/net/pinger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export default class Pinger {
2020
public ping = (): Promise<void> => {
2121
log.debug('pinging %1:%2...', this.host_ , this.port_);
2222

23-
return promises.retry(this.pingOnce.bind(this),
24-
this.timeout_, DEFAULT_INTERVAL_MS);
23+
return promises.retry(this.pingOnce, this.timeout_, DEFAULT_INTERVAL_MS);
2524
}
2625

2726
// Resolves if a connection has been established, or rejects if the connection

0 commit comments

Comments
 (0)