const Gio = imports.gi.Gio; const GLib = imports.gi.GLib; const Gssdp = imports.gi.GSSDP; const Gupnp = imports.gi.GUPnP; const Signals = imports.signals; const Soup = imports.gi.Soup; const Lang = imports.lang; const Params = imports.misc.params; const DialDevice = imports.protocols.dialDevice; const WebSocket = imports.protocols.webSocket; const DIAL_MULTISCREEN_TARGET_URN = 'urn:dial-multiscreen-org:service:dial:1'; const DialManager = new Lang.Class({ Name: 'DialManager', _init: function(params) { params = Params.parse(params, { httpSession: null, appName: null }); if (params.httpSession) this._httpSession = params.httpSession; else this._httpSession = new Soup.Session(); this._appName = params.appName; // FIXME: use NetworkManager or something to get a list of all interfaces, // and then have one context per interface this._upnpContext = new Gupnp.Context({ interface: 'wlp2s0' }); this._upnpContext.init(null); this._upnpControlPoint = new Gupnp.ControlPoint({ client: this._upnpContext, target: DIAL_MULTISCREEN_TARGET_URN }); this._upnpControlPoint.connect('service-proxy-available', Lang.bind(this, this._onFoundDevice)); }, _onFoundDevice: function(controlPoint, proxy) { print('found device', proxy.location); let message = new Soup.Message({ method: 'HEAD', uri: new Soup.URI(proxy.location) }); this._httpSession.queue_message(message, Lang.bind(this, function() { let restServiceUri = message.response_headers.get('Application-URL'); if (restServiceUri) { print('REST uri', restServiceUri); let dialDevice = new DialDevice.DialDevice({ restServiceUri: new Soup.URI(restServiceUri), appName: this._appName }); dialDevice.refresh(Lang.bind(this, function() { this.emit('device-found', dialDevice); })); } })); }, discover: function() { print('beginning discovery'); this._upnpControlPoint.set_active(true); } }); Signals.addSignalMethods(DialManager.prototype);