Added copyright headers
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / xds-config.service.ts
1 /**
2 * @license
3 * Copyright (C) 2017 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
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
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
19 import { Injectable } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { Subject } from 'rxjs/Subject';
22 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
23
24 import { AlertService, IAlert } from '../services/alert.service';
25 import { XDSAgentService, IAgentStatus, IXDServerCfg } from '../../@core-xds/services/xdsagent.service';
26
27 import 'rxjs/add/operator/publish';
28 import 'rxjs/add/operator/map';
29 import 'rxjs/add/operator/catch';
30
31
32 @Injectable()
33 export class XDSConfigService {
34
35   // Conf$: Observable<IXdsConfig>;
36   xdsServers: IXDServerCfg[];
37
38   // private confSubject: BehaviorSubject<IXdsConfig>;
39   // private confStore: IXdsConfig;
40
41   private _curServer: IXDServerCfg = { id: '', url: '', connRetry: 0, connected: false };
42   private curServer$ = new Subject<IXDServerCfg>();
43
44   constructor(
45     private alert: AlertService,
46     private xdsAgentSvr: XDSAgentService,
47   ) {
48     /*
49     this.confSubject = <BehaviorSubject<IXdsConfig>>new BehaviorSubject(this.confStore);
50     this.Conf$ = this.confSubject.asObservable();
51     */
52
53     // Update servers list
54     this.xdsAgentSvr.XdsConfig$.subscribe(cfg => {
55       if (!cfg || cfg.servers.length < 1) {
56         return;
57       }
58       this.xdsServers = cfg.servers;
59       this._updateCurServer();
60     });
61   }
62
63   onCurServer(): Observable<IXDServerCfg> {
64     return this.curServer$.publish().refCount();
65   }
66
67   getCurServer(): IXDServerCfg {
68     return this._curServer;
69   }
70
71   setCurServer(svr: IXDServerCfg): Observable<IXDServerCfg> {
72     const curSvr = this._getCurServer();
73
74     if (!curSvr.connected || curSvr.url !== svr.url) {
75       return this.xdsAgentSvr.setServerUrl(curSvr.id, svr.url, svr.connRetry)
76         .map(cfg => this._updateCurServer())
77         .catch(err => {
78           this._curServer.connected = false;
79           this.curServer$.next(this._curServer);
80           return Observable.throw(err);
81         });
82     } else {
83       if (curSvr.connRetry !== svr.connRetry) {
84         return this.xdsAgentSvr.setServerRetry(curSvr.id, svr.connRetry)
85           .map(cfg => this._updateCurServer())
86           .catch(err => {
87             this.curServer$.next(this._curServer);
88             return Observable.throw(err);
89           });
90       }
91     }
92     return Observable.of(curSvr);
93   }
94
95   private _updateCurServer(): IXDServerCfg {
96     this._curServer = this._getCurServer();
97     this.curServer$.next(this._curServer);
98     return this._curServer;
99   }
100
101   private _getCurServer(url?: string): IXDServerCfg {
102     if (!this.xdsServers) {
103       return this._curServer;
104     }
105
106     // Init the 1st time
107     let svrUrl = url || this._curServer.url;
108     if (this._curServer.url === '' && this.xdsServers.length > 0) {
109       svrUrl = this.xdsServers[0].url;
110     }
111
112     const svr = this.xdsServers.filter(s => s.url === svrUrl);
113     return svr[0];
114   }
115
116 }