1 import { Injectable } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { Subject } from 'rxjs/Subject';
4 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
6 import { AlertService, IAlert } from '../services/alert.service';
7 import { XDSAgentService, IAgentStatus, IXDServerCfg } from '../../@core-xds/services/xdsagent.service';
9 import 'rxjs/add/operator/publish';
10 import 'rxjs/add/operator/map';
11 import 'rxjs/add/operator/catch';
15 export class XDSConfigService {
17 // Conf$: Observable<IXdsConfig>;
18 xdsServers: IXDServerCfg[];
20 // private confSubject: BehaviorSubject<IXdsConfig>;
21 // private confStore: IXdsConfig;
23 private _curServer: IXDServerCfg = { id: '', url: '', connRetry: 0, connected: false };
24 private curServer$ = new Subject<IXDServerCfg>();
27 private alert: AlertService,
28 private xdsAgentSvr: XDSAgentService,
31 this.confSubject = <BehaviorSubject<IXdsConfig>>new BehaviorSubject(this.confStore);
32 this.Conf$ = this.confSubject.asObservable();
35 // Update servers list
36 this.xdsAgentSvr.XdsConfig$.subscribe(cfg => {
37 if (!cfg || cfg.servers.length < 1) {
40 this.xdsServers = cfg.servers;
41 this._updateCurServer();
45 onCurServer(): Observable<IXDServerCfg> {
46 return this.curServer$.publish().refCount();
49 getCurServer(): IXDServerCfg {
50 return this._curServer;
53 setCurServer(svr: IXDServerCfg): Observable<IXDServerCfg> {
54 const curSvr = this._getCurServer();
56 if (!curSvr.connected || curSvr.url !== svr.url) {
57 return this.xdsAgentSvr.setServerUrl(curSvr.id, svr.url, svr.connRetry)
58 .map(cfg => this._updateCurServer())
60 this._curServer.connected = false;
61 this.curServer$.next(this._curServer);
62 return Observable.throw(err);
65 if (curSvr.connRetry !== svr.connRetry) {
66 return this.xdsAgentSvr.setServerRetry(curSvr.id, svr.connRetry)
67 .map(cfg => this._updateCurServer())
69 this.curServer$.next(this._curServer);
70 return Observable.throw(err);
74 return Observable.of(curSvr);
77 private _updateCurServer() {
78 this._curServer = this._getCurServer();
79 this.curServer$.next(this._curServer);
82 private _getCurServer(url?: string): IXDServerCfg {
83 if (!this.xdsServers) {
84 return this._curServer;
88 let svrUrl = url || this._curServer.url;
89 if (this._curServer.url === '' && this.xdsServers.length > 0) {
90 svrUrl = this.xdsServers[0].url;
93 const svr = this.xdsServers.filter(s => s.url === svrUrl);