diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-07-07 09:54:54 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-07-07 09:54:54 +0200 |
| commit | 35f2a91585f4c94de2e4668eab99890fa50c22a9 (patch) | |
| tree | e89ffb2c783fb047ece5163ab80d78f419906ded /app/lib | |
| parent | db78c15f3445899bc0760bfea5194ae1d7896f47 (diff) | |
| parent | 518c934c98395f165977fd28837f4817cee664e7 (diff) | |
| download | mullvadvpn-35f2a91585f4c94de2e4668eab99890fa50c22a9.tar.xz mullvadvpn-35f2a91585f4c94de2e4668eab99890fa50c22a9.zip | |
Merge branch 'up-down-events'
Diffstat (limited to 'app/lib')
| -rw-r--r-- | app/lib/backend.js | 42 | ||||
| -rw-r--r-- | app/lib/ipc-facade.js | 26 |
2 files changed, 51 insertions, 17 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index 377e27e702..da2bc60613 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -9,6 +9,9 @@ import connectionActions from '../redux/connection/actions'; import type { ReduxStore } from '../redux/store'; import { push } from 'react-router-redux'; +import type { BackendState } from './ipc-facade'; +import type { ConnectionState } from '../redux/connection/reducers'; + export type EventType = 'connect' | 'connecting' | 'disconnect' | 'login' | 'logging' | 'logout' | 'updatedIp' | 'updatedLocation' | 'updatedReachability'; export type ErrorType = 'NO_CREDIT' | 'NO_INTERNET' | 'INVALID_ACCOUNT'; @@ -231,15 +234,6 @@ export class Backend { .then( () => { return this._ipc.connect(); }) - .then(() => { - this._emit('connect', addr); - this._store.dispatch(connectionActions.connectionChange({ - status: 'connected', - serverAddress: addr, - })); - - this.sync(); // TODO: This is a pooooooor way of updating the location and the IP and stuff - }) .catch(e => { log.info('Failed connecting to', addr, e); this._emit('connect', undefined, e); @@ -252,11 +246,6 @@ export class Backend { disconnect(): Promise<void> { // @TODO: Failure modes return this._ipc.disconnect() - .then(() => { - // emit: disconnect - this._emit('disconnect'); - this.sync(); // TODO: This is a pooooooor way of updating the location and the IP and stuff - }) .catch(e => { log.info('Failed to disconnect', e); }); @@ -280,9 +269,28 @@ export class Backend { } _registerIpcListeners() { - /*this._ipc.on('connection-info', (newConnectionInfo) => { - log.info('Got new connection info from backend', newConnectionInfo); - });*/ + this._ipc.registerStateListener(newState => { + log.info('Got new state from backend', newState); + + const newStatus = this._backendStateToConnectionState(newState); + this._store.dispatch(connectionActions.connectionChange({ + status: newStatus, + })); + + this.sync(); + }); + } + + _backendStateToConnectionState(backendState: BackendState): ConnectionState { + switch(backendState) { + case 'unsecured': + return 'disconnected'; + case 'secured': + return 'connected'; + + default: + throw new Error('Unknown backend state: ' + backendState); + } } on(event: EventType, listener: Function) { diff --git a/app/lib/ipc-facade.js b/app/lib/ipc-facade.js index 89c29ce795..19daf71b97 100644 --- a/app/lib/ipc-facade.js +++ b/app/lib/ipc-facade.js @@ -18,6 +18,7 @@ const LocationSchema = object({ city: string, }); +export type BackendState = 'secured' | 'unsecured'; export interface IpcFacade { getAccountData(AccountNumber): Promise<AccountData>, @@ -27,6 +28,8 @@ export interface IpcFacade { disconnect(): Promise<void>, getIp(): Promise<Ip>, getLocation(): Promise<Location>, + getState(): Promise<BackendState>, + registerStateListener((BackendState) => void): void, } export class RealIpc implements IpcFacade { @@ -93,4 +96,27 @@ export class RealIpc implements IpcFacade { } }); } + + getState(): Promise<BackendState> { + return this._ipc.send('get_state') + .then(raw => { + return this._parseBackendState(raw); + }); + } + + _parseBackendState(raw: mixed): BackendState { + if (raw === 'secured' || raw === 'unsecured') { + return raw; + } else { + throw new InvalidReply(raw); + } + } + + registerStateListener(listener: (BackendState) => void) { + this._ipc.on('new_state', (rawEvent) => { + const parsedEvent : BackendState = this._parseBackendState(rawEvent); + + listener(parsedEvent); + }); + } } |
