/** ****************************************************************************** * Xenia : Xbox 360 Emulator Research Project * ****************************************************************************** * Copyright 2013 Ben Vanik. All rights reserved. * * Released under the BSD license - see LICENSE in the root for more details. * ****************************************************************************** */ 'use strict'; var module = angular.module('xe.session', []); module.service('Session', function( $rootScope, $q, $http, log, FileDataSource, RemoteDataSource) { var Session = function(id, opt_dataSource) { this.id = id; this.dataSource = opt_dataSource || null; }; Session.prototype.dispose = function() { this.disconnect(); }; Session.DEFAULT_HOST = '127.0.0.1:6200'; Session.getHost = function(opt_host) { return opt_host || Session.DEFAULT_HOST; }; Session.query = function(opt_host) { var url = 'http://' + Session.getHost(opt_host); var p = $http({ method: 'GET', url: url + '/sessions', cache: false, timeout: 500, responseType: 'json' }); var d = $q.defer(); p.then(function(response) { if (!response.data || !response.data.length) { d.reject(new Error('No session data')); return; } d.resolve(response.data); }, function(e) { d.reject(e); }); return d.promise; }; Session.prototype.connect = function(opt_host) { this.disconnect(); var url = 'ws://' + Session.getHost(opt_host); log.info('Connecting to ' + url + '...'); log.setProgress(0); var d = $q.defer(); var dataSource = new RemoteDataSource(url); var p = dataSource.open(); p.then((function() { log.info('Connected!'); log.clearProgress(); this.dataSource = dataSource; d.resolve(this); }).bind(this), (function(e) { log.error('Unable to connect: ' + e); log.clearProgress(); d.reject(e); }).bind(this), function(update) { log.setProgress(update.progress); d.notify(update); }); return d.promise; }; Session.prototype.disconnect = function() { if (this.dataSource) { this.dataSource.dispose(); this.dataSource = null; $rootScope.$emit('refresh'); } }; return Session; });