3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import { Injectable } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { Subject } from 'rxjs/Subject';
22 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24 import { AlertService, IAlert } from '../services/alert.service';
25 import { XDSAgentService, IAgentStatus, IXDServerCfg } from '../../@core-xds/services/xdsagent.service';
27 import 'rxjs/add/operator/publish';
28 import 'rxjs/add/operator/map';
29 import 'rxjs/add/operator/catch';
33 export class XDSConfigService {
35 // Conf$: Observable<IXdsConfig>;
36 xdsServers: IXDServerCfg[];
38 // private confSubject: BehaviorSubject<IXdsConfig>;
39 // private confStore: IXdsConfig;
41 private _curServer: IXDServerCfg = { id: '', url: '', connRetry: 0, connected: false };
42 private curServer$ = new Subject<IXDServerCfg>();
45 private alert: AlertService,
46 private xdsAgentSvr: XDSAgentService,
49 this.confSubject = <BehaviorSubject<IXdsConfig>>new BehaviorSubject(this.confStore);
50 this.Conf$ = this.confSubject.asObservable();
53 // Update servers list
54 this.xdsAgentSvr.XdsConfig$.subscribe(cfg => {
55 if (!cfg || cfg.servers.length < 1) {
58 this.xdsServers = cfg.servers;
59 this._updateCurServer();
63 onCurServer(): Observable<IXDServerCfg> {
64 return this.curServer$.publish().refCount();
67 getCurServer(): IXDServerCfg {
68 return this._curServer;
71 setCurServer(svr: IXDServerCfg): Observable<IXDServerCfg> {
72 const curSvr = this._getCurServer();
74 if (!curSvr.connected || curSvr.url !== svr.url) {
75 return this.xdsAgentSvr.setServerUrl(curSvr.id, svr.url, svr.connRetry)
76 .map(cfg => this._updateCurServer())
78 this._curServer.connected = false;
79 this.curServer$.next(this._curServer);
80 return Observable.throw(err);
83 if (curSvr.connRetry !== svr.connRetry) {
84 return this.xdsAgentSvr.setServerRetry(curSvr.id, svr.connRetry)
85 .map(cfg => this._updateCurServer())
87 this.curServer$.next(this._curServer);
88 return Observable.throw(err);
92 return Observable.of(curSvr);
95 private _updateCurServer(): IXDServerCfg {
96 this._curServer = this._getCurServer();
97 this.curServer$.next(this._curServer);
98 return this._curServer;
101 private _getCurServer(url?: string): IXDServerCfg {
102 if (!this.xdsServers) {
103 return this._curServer;
107 let svrUrl = url || this._curServer.url;
108 if (this._curServer.url === '' && this.xdsServers.length > 0) {
109 svrUrl = this.xdsServers[0].url;
112 const svr = this.xdsServers.filter(s => s.url === svrUrl);