Before connection to the server we need to check minimal supported version

This commit is contained in:
Piotr Pekala 2019-11-19 02:01:20 -08:00
parent 58a02c7066
commit 91fa54cf9b
5 changed files with 46 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import { Server } from '../../../models/server';
import { ElectronService } from 'ngx-electron';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { ServerService } from '../../../services/server.service';
import { ToasterService } from '../../../services/toaster.service';
@Component({
@ -30,6 +31,7 @@ export class AddServerDialogComponent implements OnInit {
public dialogRef: MatDialogRef<AddServerDialogComponent>,
private electronService: ElectronService,
private serverService: ServerService,
private toasterService: ToasterService,
@Inject(MAT_DIALOG_DATA) public data: any,
) {}
@ -145,7 +147,21 @@ export class AddServerDialogComponent implements OnInit {
}
const server: Server = Object.assign({}, this.serverForm.value);
this.dialogRef.close(server);
this.serverService.checkServerVersion(server).subscribe(
(serverInfo) => {
if ((serverInfo.version.split('.')[1]>=2) && (serverInfo.version.split('.')[0]>=2)) {
this.dialogRef.close(server);
this.toasterService.success(`Server ${server.name} added.`)
} else {
this.dialogRef.close();
this.toasterService.error(`Server version is not supported.`)
}
},
error => {
this.dialogRef.close();
this.toasterService.error('Cannot connect to the server.')
}
);
}
onNoClick(): void {

View File

@ -39,7 +39,17 @@ export class ServersComponent implements OnInit, OnDestroy {
server.status = 'running';
}
});
this.serverDatabase.addServers(servers);
servers.forEach((server) => {
this.serverService.checkServerVersion(server).subscribe(
(serverInfo) => {
if ((serverInfo.version.split('.')[1]>=2) && (serverInfo.version.split('.')[0]>=2)) {
if (!this.serverDatabase.find(server.name)) this.serverDatabase.addServer(server);
}
},
error => {}
);
});
});
this.dataSource = new ServerDataSource(this.serverDatabase);

View File

@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
import { Project } from '../models/project';
import { Node } from '../cartography/models/node';
import { Observable } from 'rxjs';
import { Link } from '../models/link';
import { Server } from '../models/server';
import { HttpServer } from './http-server.service';

View File

@ -5,6 +5,8 @@ import { Server } from '../models/server';
import { IndexedDbService } from './indexed-db.service';
import { AngularIndexedDB } from 'angular2-indexeddb';
import Spy = jasmine.Spy;
import { HttpServer, ServerErrorHandler } from '../services/http-server.service';
import { HttpClient } from '@angular/common/http';
export class MockedServerService {
public servers: Server[] = [];
@ -46,6 +48,7 @@ describe('ServerService', () => {
let db: AngularIndexedDB;
let service: ServerService;
let openDatabaseSpy: Spy;
let httpServer = new HttpServer({} as HttpClient, {} as ServerErrorHandler);
beforeEach(() => {
indexedDbService = new IndexedDbService();
@ -55,7 +58,11 @@ describe('ServerService', () => {
openDatabaseSpy = spyOn(db, 'openDatabase').and.returnValue(Promise.resolve(true));
TestBed.configureTestingModule({
providers: [ServerService, { provide: IndexedDbService, useValue: indexedDbService }]
providers: [
ServerService,
{ provide: IndexedDbService, useValue: indexedDbService },
{ provide: HttpServer, useValue: httpServer }
]
});
service = TestBed.get(ServerService);

View File

@ -1,14 +1,18 @@
import { Injectable } from '@angular/core';
import { IndexedDbService } from './indexed-db.service';
import { Server } from '../models/server';
import { Observable } from 'rxjs';
import { HttpServer } from './http-server.service';
@Injectable()
export class ServerService {
private tablename = 'servers';
private ready: Promise<any>;
constructor(private indexedDbService: IndexedDbService) {
constructor(
private indexedDbService: IndexedDbService,
private httpServer: HttpServer
) {
this.ready = indexedDbService.get().openDatabase(1, evt => {
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
});
@ -59,6 +63,10 @@ export class ServerService {
return `http://${server.host}:${server.port}/`;
}
public checkServerVersion(server: Server): Observable<any> {
return this.httpServer.get(server, '/version');
}
public getLocalServer(host: string, port: number) {
const promise = new Promise((resolve, reject) => {
this.findAll().then((servers: Server[]) => {