Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
Using IPC for spawning the manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Feb 20, 2021
1 parent 2e61a60 commit 908e9ab
Show file tree
Hide file tree
Showing 43 changed files with 369 additions and 193 deletions.
3 changes: 3 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ builder.build({
executableName: 'sentinel',
icon: './electron/',
target: [
'AppImage',
'deb',
'dir',
'tar.gz',
],
},
mac: {
Expand Down
5 changes: 5 additions & 0 deletions electron/channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
MANAGER_LISTEN_URL_GET_REQ: 'MANAGER_LISTEN_URL_GET_REQ',
MANAGER_START_REQ: 'MANAGER_START_REQ',
MANAGER_START_RES: 'MANAGER_START_RES',
};
11 changes: 11 additions & 0 deletions electron/globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { app } = require('electron');

const manager = null;
const listenURL = app.commandLine.getSwitchValue('listen-url') || 'http://127.0.0.1:8080';
const window = null;

module.exports = {
listenURL,
manager,
window,
};
1 change: 1 addition & 0 deletions electron/icon.png
77 changes: 16 additions & 61 deletions electron/index.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,48 @@
require('./ipc');
const {
app,
BrowserWindow,
Menu,
} = require('electron');
const path = require('path');
const { spawn } = require('child_process');
const menu = require('./menu');
const { isPortFree } = require('./utils/net');
const globals = require('./globals');

Menu.setApplicationMenu(menu);
app.commandLine.appendSwitch('ignore-certificate-errors', 'true');

const listenURL = app.commandLine.getSwitchValue('listen-url') || 'http://localhost:8080';
let manager = null;

const createWindow = () => {
const createMainWindow = () => {
const window = new BrowserWindow({
show: false,
height: 480,
autoHideMenuBar: true,
height: 720,
icon: path.join(__dirname, 'icon.png'),
title: 'Sentinel',
width: 852,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
width: 1280,
});

window.loadFile(path.join(__dirname, '../build/index.html'))
.then(() => {
window.show();
})
.catch(console.error);
const indexFile = path.join(__dirname, '../build/index.html');
window.loadFile(indexFile).then().catch(console.error);

globals.window = window;
};

app.on('ready', () => {
const url = new URL(listenURL);
isPortFree(url.port, url.hostname, (error) => {
if (error) {
console.error(error);
app.quit();
} else {
manager = spawn(
path.join(__dirname, '../bin/manager'),
[
'server',
'--listen-url',
listenURL,
],
{
detached: false,
},
);
manager.stdout.on('data', (data) => {
data = data.toString().trim();
console.log(data);
});
manager.stderr.on('data', (data) => {
data = data.toString().trim();
console.error(data);
});

let created = false;
manager.stdout.on('data', (data) => {
if (created) {
return;
}

data = data.toString().trim();
if (data.indexOf('Listening on URL') === -1) {
app.quit();
}

created = true;
createWindow();
});
manager.stderr.on('data', () => {
app.quit();
});
}
});
createMainWindow();
});

app.on('window-all-closed', () => {
app.quit();
});

app.on('will-quit', () => {
manager.kill();
globals.manager.kill();
});

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
createMainWindow();
}
});
59 changes: 59 additions & 0 deletions electron/ipc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require('path');
const { spawn } = require('child_process');
const { ipcMain } = require('electron');
const {
MANAGER_LISTEN_URL_GET_REQ,
MANAGER_START_REQ,
MANAGER_START_RES,
} = require('./channels');
const globals = require('./globals');

ipcMain.on(MANAGER_LISTEN_URL_GET_REQ, (event, args) => {
console.log('EVENT:', MANAGER_LISTEN_URL_GET_REQ, 'ARGS:', args);

event.returnValue = globals.listenURL;
});

ipcMain.on(MANAGER_START_REQ, (event, args) => {
console.log('EVENT:', MANAGER_START_REQ, 'ARGS:', args);

const manager = spawn(
path.join(__dirname, '../bin/manager'),
['server', '--listen-url', globals.listenURL],
{ detached: false },
);

let error = false;
let success = false;

manager.stderr.on('data', (data) => {
data = data.toString().trim();
console.error(data);

if (error) {
return;
}

error = true;
globals.window.webContents.send(MANAGER_START_RES, {
success: false,
data,
});
});
manager.stdout.on('data', (data) => {
data = data.toString().trim();
console.log(data);

if (success) {
return;
}

success = true;
globals.window.webContents.send(MANAGER_START_RES, {
success: true,
data,
});
});

globals.manager = manager;
});
3 changes: 0 additions & 3 deletions electron/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ const template = [
{
label: 'View',
submenu: [
{
role: 'reload',
},
{
role: 'toggleDevTools',
},
Expand Down
3 changes: 3 additions & 0 deletions electron/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { ipcRenderer } = require('electron');

window.ipcRenderer = ipcRenderer;
31 changes: 0 additions & 31 deletions electron/utils/net.js

This file was deleted.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"version": "0.3.0-rc0",
"main": "./electron/index.js",
"homepage": "./",
"description": "Sentinel desktop and web client.",
"author": {
"name": "Sentinel",
"email": "[email protected]",
"url": "https://sentinel.co"
},
"private": true,
"devDependencies": {
"@material-ui/core": "^4.11.3",
Expand Down
4 changes: 2 additions & 2 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ACCOUNT_GET_SUCCESS,
ACCOUNT_PASSWORD_SET,
ACCOUNT_PASSWORD_VISIBLE_SET,
getAccountURL,
accountGetURL,
} from '../constants/account';
import { emptyFunc } from '../constants/common';
import Async from 'async';
Expand Down Expand Up @@ -44,7 +44,7 @@ export const getAccount = (cb = emptyFunc) => (dispatch, getState) => {
},
} = getState();

const url = getAccountURL(items[index].address);
const url = accountGetURL(items[index].address);
Axios.get(url)
.then((res) => {
try {
Expand Down
5 changes: 3 additions & 2 deletions src/actions/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AUTHENTICATION_POST_IN_PROGRESS,
AUTHENTICATION_POST_SUBMIT,
AUTHENTICATION_POST_SUCCESS,
AUTHENTICATION_POST_URL,
authenticationPostURL,
} from '../constants/authentication';
import { emptyFunc } from '../constants/common';
import Async from 'async';
Expand Down Expand Up @@ -56,7 +56,8 @@ export const postAuthentication = (history, cb = emptyFunc) => (dispatch, getSta
}, (next) => {
const { authentication: { password } } = getState();

Axios.post(AUTHENTICATION_POST_URL, {
const url = authenticationPostURL();
Axios.post(url, {
password: password.value.trim(),
})
.then((res) => {
Expand Down
10 changes: 6 additions & 4 deletions src/actions/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
CONFIGURATION_GET_ERROR,
CONFIGURATION_GET_IN_PROGRESS,
CONFIGURATION_GET_SUCCESS,
CONFIGURATION_GET_URL,
CONFIGURATION_MODAL_HIDE,
CONFIGURATION_MODAL_SHOW,
CONFIGURATION_PUT_ERROR,
CONFIGURATION_PUT_IN_PROGRESS,
CONFIGURATION_PUT_SUCCESS,
CONFIGURATION_PUT_URL,
CONFIGURATION_SETUP_SET,
configurationGetURL,
configurationPutURL,
} from '../constants/configuration';
import { emptyFunc } from '../constants/common';
import Async from 'async';
Expand Down Expand Up @@ -121,7 +121,8 @@ export const getConfiguration = (history, cb = emptyFunc) => (dispatch, getState
dispatch(getConfigurationInProgress());
next(null);
}, (next) => {
Axios.get(CONFIGURATION_GET_URL)
const url = configurationGetURL();
Axios.get(url)
.then((res) => {
try {
next(null, res?.data?.result);
Expand Down Expand Up @@ -198,7 +199,8 @@ export const putConfiguration = (cb = emptyFunc) => (dispatch, getState) => {
},
} = getState();

Axios.put(CONFIGURATION_PUT_URL, {
const url = configurationPutURL();
Axios.put(url, {
from: items[index]?.name,
chain: {
broadcast_mode: broadcastMode.value.trim(),
Expand Down
4 changes: 2 additions & 2 deletions src/actions/delegations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
DELEGATIONS_GET_ERROR,
DELEGATIONS_GET_IN_PROGRESS,
DELEGATIONS_GET_SUCCESS,
getDelegationsURL,
delegationsGetURL,
} from '../constants/delegations';
import { emptyFunc } from '../constants/common';
import Async from 'async';
Expand Down Expand Up @@ -42,7 +42,7 @@ export const getDelegations = (cb = emptyFunc) => (dispatch, getState) => {
},
} = getState();

const url = getDelegationsURL(items[index].address);
const url = delegationsGetURL(items[index].address);
Axios.get(url)
.then((res) => {
try {
Expand Down
10 changes: 6 additions & 4 deletions src/actions/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import {
KEYS_GET_ERROR,
KEYS_GET_IN_PROGRESS,
KEYS_GET_SUCCESS,
KEYS_GET_URL,
KEYS_PASSWORD_VISIBLE_SET,
KEYS_POST_ERROR,
KEYS_POST_IN_PROGRESS,
KEYS_POST_SUCCESS,
KEYS_POST_URL,
KEY_MNEMONIC_SET,
KEY_NAME_SET,
KEY_PASSWORD_SET,
keysGetURL,
keysPostURL,
} from '../constants/keys';
import { emptyFunc } from '../constants/common';
import Async from 'async';
Expand Down Expand Up @@ -43,7 +43,8 @@ export const getKeys = (history, cb = emptyFunc) => (dispatch, getState) => {
dispatch(getKeysInProgress(null));
next(null);
}, (next) => {
Axios.get(KEYS_GET_URL)
const url = keysGetURL();
Axios.get(url)
.then((res) => {
try {
next(null, res?.data?.result);
Expand Down Expand Up @@ -138,7 +139,8 @@ export const postKeys = (history, cb = emptyFunc) => (dispatch, getState) => {
},
} = getState();

Axios.post(KEYS_POST_URL, {
const url = keysPostURL();
Axios.post(url, {
mnemonic: mnemonic.value.trim(),
name: name.value.trim(),
password: password.value.trim(),
Expand Down
Loading

0 comments on commit 908e9ab

Please sign in to comment.