1 import { Injectable } from '@angular/core';
2 import { Http, Headers, RequestOptionsArgs, Response } from '@angular/http';
3 import { Location } from '@angular/common';
4 import { Observable } from 'rxjs/Observable';
5 import { Subject } from 'rxjs/Subject';
6 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
7 import * as io from 'socket.io-client';
9 import { AlertService } from './alert.service';
10 import { ISdk } from './sdk.service';
11 import { ProjectType} from "./project.service";
13 // Import RxJs required methods
14 import 'rxjs/add/operator/map';
15 import 'rxjs/add/operator/catch';
16 import 'rxjs/add/observable/throw';
17 import 'rxjs/add/operator/mergeMap';
18 import 'rxjs/add/observable/of';
19 import 'rxjs/add/operator/retryWhen';
22 export interface IXDSConfigProject {
25 clientSyncThingID: string;
28 defaultSdkID?: string;
31 interface IXDSBuilderConfig {
37 export interface IXDSProjectConfig {
49 export interface IXDSVer {
56 export interface IXDSVersions {
61 export interface IXDServerCfg {
70 export interface IXDSConfig {
71 servers: IXDServerCfg[];
74 export interface ISdkMessage {
80 export interface ICmdOutput {
87 export interface ICmdExit {
94 export interface IAgentStatus {
95 WS_connected: boolean;
100 export class XDSAgentService {
102 public XdsConfig$: Observable<IXDSConfig>;
103 public Status$: Observable<IAgentStatus>;
104 public ProjectState$ = <Subject<IXDSProjectConfig>>new Subject();
105 public CmdOutput$ = <Subject<ICmdOutput>>new Subject();
106 public CmdExit$ = <Subject<ICmdExit>>new Subject();
108 private baseUrl: string;
109 private wsUrl: string;
110 private _config = <IXDSConfig>{ servers: [] };
111 private _status = { WS_connected: false };
113 private configSubject = <BehaviorSubject<IXDSConfig>>new BehaviorSubject(this._config);
114 private statusSubject = <BehaviorSubject<IAgentStatus>>new BehaviorSubject(this._status);
116 private socket: SocketIOClient.Socket;
118 constructor(private http: Http, private _window: Window, private alert: AlertService) {
120 this.XdsConfig$ = this.configSubject.asObservable();
121 this.Status$ = this.statusSubject.asObservable();
123 this.baseUrl = this._window.location.origin + '/api/v1';
125 let re = this._window.location.origin.match(/http[s]?:\/\/([^\/]*)[\/]?/);
126 if (re === null || re.length < 2) {
127 console.error('ERROR: cannot determine Websocket url');
129 this.wsUrl = 'ws://' + re[1];
130 this._handleIoSocket();
131 this._RegisterEvents();
135 private _WSState(sts: boolean) {
136 this._status.WS_connected = sts;
137 this.statusSubject.next(Object.assign({}, this._status));
139 // Update XDS config including XDS Server list when connected
141 this.getConfig().subscribe(c => {
143 this.configSubject.next(
144 Object.assign({ servers: [] }, this._config)
150 private _handleIoSocket() {
151 this.socket = io(this.wsUrl, { transports: ['websocket'] });
153 this.socket.on('connect_error', (res) => {
154 this._WSState(false);
155 console.error('XDS Agent WebSocket Connection error !');
158 this.socket.on('connect', (res) => {
162 this.socket.on('disconnection', (res) => {
163 this._WSState(false);
164 this.alert.error('WS disconnection: ' + res);
167 this.socket.on('error', (err) => {
168 console.error('WS error:', err);
171 this.socket.on('make:output', data => {
172 this.CmdOutput$.next(Object.assign({}, <ICmdOutput>data));
175 this.socket.on('make:exit', data => {
176 this.CmdExit$.next(Object.assign({}, <ICmdExit>data));
179 this.socket.on('exec:output', data => {
180 this.CmdOutput$.next(Object.assign({}, <ICmdOutput>data));
183 this.socket.on('exec:exit', data => {
184 this.CmdExit$.next(Object.assign({}, <ICmdExit>data));
188 // (project-add and project-delete events are managed by project.service)
189 this.socket.on('event:server-config', ev => {
191 let cfg: IXDServerCfg = ev.data;
192 let idx = this._config.servers.findIndex(el => el.id === cfg.id);
194 this._config.servers[idx] = Object.assign({}, cfg);
196 this.configSubject.next(Object.assign({}, this._config));
200 this.socket.on('event:project-state-change', ev => {
202 this.ProjectState$.next(Object.assign({}, ev.data));
211 addEventListener(ev: string, fn: Function): SocketIOClient.Emitter {
212 return this.socket.addEventListener(ev, fn);
218 getVersion(): Observable<IXDSVersions> {
219 return this._get('/version');
225 getConfig(): Observable<IXDSConfig> {
226 return this._get('/config');
229 setConfig(cfg: IXDSConfig): Observable<IXDSConfig> {
230 return this._post('/config', cfg);
233 setServerRetry(serverID: string, r: number) {
234 let svr = this._getServer(serverID);
236 return Observable.of([]);
240 this.setConfig(this._config).subscribe(
242 this._config = newCfg;
243 this.configSubject.next(Object.assign({}, this._config));
246 this.alert.error(err);
251 setServerUrl(serverID: string, url: string) {
252 let svr = this._getServer(serverID);
254 return Observable.of([]);
257 this.setConfig(this._config).subscribe(
259 this._config = newCfg;
260 this.configSubject.next(Object.assign({}, this._config));
263 this.alert.error(err);
271 getSdks(serverID: string): Observable<ISdk[]> {
272 let svr = this._getServer(serverID);
273 if (!svr || !svr.connected) {
274 return Observable.of([]);
277 return this._get(svr.partialUrl + '/sdks');
283 getProjects(): Observable<IXDSProjectConfig[]> {
284 return this._get('/projects');
287 addProject(cfg: IXDSProjectConfig): Observable<IXDSProjectConfig> {
288 return this._post('/project', cfg);
291 deleteProject(id: string): Observable<IXDSProjectConfig> {
292 return this._delete('/project/' + id);
295 syncProject(id: string): Observable<string> {
296 return this._post('/project/sync/' + id, {});
302 exec(prjID: string, dir: string, cmd: string, sdkid?: string, args?: string[], env?: string[]): Observable<any> {
303 return this._post('/exec',
314 make(prjID: string, dir: string, sdkid?: string, args?: string[], env?: string[]): Observable<any> {
315 // SEB TODO add serverID
316 return this._post('/make',
331 private _RegisterEvents() {
332 // Register to all existing events
333 this._post('/events/register', { "name": "all" })
337 this.alert.error("ERROR while registering to all events: ", error);
342 private _getServer(ID: string): IXDServerCfg {
343 let svr = this._config.servers.filter(item => item.id === ID);
344 if (svr.length < 1) {
350 private _attachAuthHeaders(options?: any) {
351 options = options || {};
352 let headers = options.headers || new Headers();
353 // headers.append('Authorization', 'Basic ' + btoa('username:password'));
354 headers.append('Accept', 'application/json');
355 headers.append('Content-Type', 'application/json');
356 // headers.append('Access-Control-Allow-Origin', '*');
358 options.headers = headers;
362 private _get(url: string): Observable<any> {
363 return this.http.get(this.baseUrl + url, this._attachAuthHeaders())
364 .map((res: Response) => res.json())
365 .catch(this._decodeError);
367 private _post(url: string, body: any): Observable<any> {
368 return this.http.post(this.baseUrl + url, JSON.stringify(body), this._attachAuthHeaders())
369 .map((res: Response) => res.json())
371 return this._decodeError(error);
374 private _delete(url: string): Observable<any> {
375 return this.http.delete(this.baseUrl + url, this._attachAuthHeaders())
376 .map((res: Response) => res.json())
377 .catch(this._decodeError);
380 private _decodeError(err: any) {
382 if (err instanceof Response) {
383 const body = err.json() || 'Agent error';
384 e = body.error || JSON.stringify(body);
385 if (!e || e === "") {
386 e = `${err.status} - ${err.statusText || 'Unknown error'}`;
388 } else if (typeof err === "object") {
389 if (err.statusText) {
391 } else if (err.error) {
392 e = String(err.error);
394 e = JSON.stringify(err);
397 e = err.message ? err.message : err.toString();
399 return Observable.throw(e);