-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c60063d
commit 5c515c6
Showing
11 changed files
with
170 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface PingResponse { | ||
machineName: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { PingService } from './ping.service'; | ||
|
||
describe('PingService', () => { | ||
let service: PingService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(PingService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {EventEmitter, Injectable} from '@angular/core'; | ||
import {HttpClient} from "@angular/common/http"; | ||
import {catchError, interval, mergeMap, Observable, of, retry, Subscription, switchMap, timeout} from "rxjs"; | ||
import {PingResponse} from "../../datatypes/ping-response"; | ||
import {ConnectionService} from "../connection/connection.service"; | ||
import {Connection} from "../../datatypes/connection"; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class PingService { | ||
public connectionAvailable: EventEmitter<string> = new EventEmitter(); | ||
|
||
public connectionUnavailable: EventEmitter<string> = new EventEmitter(); | ||
|
||
private subscription: Subscription = new Subscription(); | ||
|
||
private availableConnections: string[] = []; | ||
|
||
constructor(private http: HttpClient, | ||
private connectionService: ConnectionService) { } | ||
|
||
async start() { | ||
console.log("Start ping"); | ||
this.subscription = new Subscription(); | ||
let connections = await this.connectionService.getConnections(); | ||
for (const connection of connections) { | ||
this.subscription.add(this.periodicRequest(this.getPingUrl(connection), 1000).subscribe(response => { | ||
if (response !== null) { | ||
this.addAvailableConnection(connection); | ||
} else { | ||
this.removeAvailableConnection(connection); | ||
} | ||
})); | ||
} | ||
} | ||
|
||
stop() { | ||
console.log("Stop ping"); | ||
this.subscription.unsubscribe(); | ||
} | ||
|
||
async restart() { | ||
this.stop(); | ||
await this.start(); | ||
} | ||
|
||
private getPingUrl(connection: Connection) { | ||
return `${connection.ssl ? "https" : "http"}://${connection.host}:${connection.port}/ping`; | ||
} | ||
|
||
private removeAvailableConnection(connection: Connection) { | ||
let existingConnectionIndex = this.availableConnections.findIndex(x => x == connection.id); | ||
if (existingConnectionIndex !== -1) { | ||
this.availableConnections.splice(existingConnectionIndex, 1); | ||
this.connectionUnavailable.emit(connection.id); | ||
} | ||
} | ||
|
||
private addAvailableConnection(connection: Connection) { | ||
if (this.availableConnections.find(x => x == connection.id)) { | ||
return; | ||
} | ||
this.availableConnections.push(connection.id); | ||
this.connectionAvailable.emit(connection.id); | ||
} | ||
|
||
private periodicRequest(url: string, intervalTime: number): Observable<any> { | ||
return interval(intervalTime).pipe( | ||
switchMap(() => this.http.get(url).pipe( | ||
timeout(500), | ||
catchError(error => { | ||
return of(null); | ||
}) | ||
)) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters