BarterDEX/ipc/shepherd-ipc.js

226 lines
7.5 KiB
JavaScript
Raw Normal View History

const electron = require('electron'),
app = electron.app,
BrowserWindow = electron.BrowserWindow,
path = require('path'),
url = require('url'),
os = require('os'),
osPlatform = os.platform(),
fsnode = require('fs'),
fs = require('fs-extra'),
mkdirp = require('mkdirp'),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
portscanner = require('portscanner'),
fixPath = require('fix-path'),
2017-08-18 10:58:24 -07:00
numCPUs = require('os').cpus().length;
//ipc = require('electron').ipcMain;
Promise = require('bluebird');
var ps = require('ps-node'),
shepherd = '',
assetChainPorts = require('./ports.js');
2017-08-18 10:58:24 -07:00
// SETTING OS DIR TO RUN MARKETMAKER FROM
// SETTING APP ICON FOR LINUX AND WINDOWS
if (os.platform() === 'darwin') {
fixPath();
var marketmakerBin = path.join(__dirname, '../assets/bin/osx/marketmaker'),
2017-08-20 07:28:06 -07:00
marketmakerDir = `${process.env.HOME}/Library/Application Support/marketmaker`;
2017-08-18 10:58:24 -07:00
}
if (os.platform() === 'linux') {
var marketmakerBin = path.join(__dirname, '../assets/bin/linux64/marketmaker'),
2017-08-20 07:28:06 -07:00
marketmakerDir = `${process.env.HOME}/.marketmaker`;
2017-08-18 10:58:24 -07:00
}
if (os.platform() === 'win32') {
var marketmakerBin = path.join(__dirname, '../assets/bin/win64/marketmaker.exe');
marketmakerBin = path.normalize(marketmakerBin);
marketmakerDir = `${process.env.APPDATA}/marketmaker`;
marketmakerDir = path.normalize(marketmakerDir);
2017-08-20 07:28:06 -07:00
marketmakerIcon = path.join(__dirname, '/assets/icons/agama_icons/agama_app_icon.ico');
2017-08-18 10:58:24 -07:00
}
2017-08-20 07:28:06 -07:00
// DEFAULT COINS LIST FOR MARKETMAKER
defaultCoinsListFile = path.join(__dirname, '../assets/coinslist.json');
2017-08-18 10:58:24 -07:00
const {ipcMain} = require('electron');
2017-08-20 07:28:06 -07:00
/*ipcMain.on('shepherd-commandSync', (event, arg) => {
console.log(arg.command) // prints "ping"
event.returnValue = 'pong'
})*/
2017-08-18 10:58:24 -07:00
ipcMain.on('shepherd-command', (event, arg) => {
console.log(arg) // prints "ping"
2017-08-20 07:28:06 -07:00
switch (arg.command) {
2017-08-18 10:58:24 -07:00
case 'ping':
2017-08-20 07:28:06 -07:00
//event.sender.send('shepherd-reply', 'pong');
event.returnValue = 'pong'
2017-08-18 10:58:24 -07:00
break;
case 'login':
console.log(marketmakerBin);
console.log(marketmakerDir);
2017-08-20 07:28:06 -07:00
//event.sender.send('shepherd-reply', 'Logged In');
event.returnValue = 'Logged In';
//const _passphrase = 'scatter quote stumble confirm extra jacket lens abuse gesture soda rebel seed nature achieve hurt shoot farm middle venture fault mesh crew upset cotton';
StartMarketMaker({"passphrase":arg.passphrase});
2017-08-18 10:58:24 -07:00
break;
case 'logout':
killMarketmaker(true);
2017-08-20 07:28:06 -07:00
event.returnValue = 'Logged Out';
break;
case 'mmstatus':
2017-09-19 02:08:22 -07:00
portscanner.checkPortStatus(7783, '127.0.0.1', function(error, status) {
2017-08-20 07:28:06 -07:00
console.log(status)
//event.sender.send('shepherd-reply', status);
event.returnValue = status;
})
2017-08-18 10:58:24 -07:00
break;
}
})
// kill rogue marketmaker copies on start
killMarketmaker = function(data) {
if (data == true) {
let marketmakerGrep;
switch (osPlatform) {
case 'darwin':
marketmakerGrep = "ps -p $(ps -A | grep -m1 marketmaker | awk '{print $1}') | grep -i marketmaker";
break;
case 'linux':
marketmakerGrep = 'ps -p $(pidof marketmaker) | grep -i marketmaker';
break;
case 'win32':
marketmakerGrep = 'tasklist';
break;
}
exec(marketmakerGrep, function(error, stdout, stderr) {
if (stdout.indexOf('marketmaker') > -1) {
const pkillCmd = osPlatform === 'win32' ? 'taskkill /f /im marketmaker.exe' : 'pkill -15 marketmaker';
console.log('found another marketmaker process(es)');
exec(pkillCmd, function(error, stdout, stderr) {
console.log(`${pkillCmd} is issued`);
if (error !== null) {
console.log(`${pkillCmd} exec error: ${error}`);
};
});
}
if (error !== null) {
console.log(`${marketmakerGrep} exec error: ${error}`);
};
});
}
2017-08-18 10:58:24 -07:00
}
StartMarketMaker = function(data) {
2017-08-20 07:28:06 -07:00
//console.log(data.passphrase);
2017-08-18 10:58:24 -07:00
try {
// check if marketmaker instance is already running
2017-09-19 02:08:22 -07:00
portscanner.checkPortStatus(7783, '127.0.0.1', function(error, status) {
2017-08-18 10:58:24 -07:00
// Status is 'open' if currently in use or 'closed' if available
if (status === 'closed') {
2017-08-20 07:28:06 -07:00
const _coinsListFile = marketmakerDir+'/coinslist.json'
fs.pathExists(_coinsListFile, (err, exists) => {
if (exists === true) {
console.log('file exist');
data.coinslist = fs.readJsonSync(_coinsListFile, { throws: false });
ExecMarketMaker(data);
} else if (exists === false) {
console.log('file doesn\'t exist');
fs.copy(defaultCoinsListFile, _coinsListFile)
.then(() => {
console.log('file copied!')
data.coinslist = fs.readJsonSync(_coinsListFile, { throws: false });
ExecMarketMaker(data);
})
.catch(err => {
console.error(err)
})
}
if (err) {
console.log(err) // => null
}
})
} else {
2017-09-19 02:08:22 -07:00
console.log(`port 7783 marketmaker is already in use`);
2017-08-20 07:28:06 -07:00
}
});
} catch(e) {
console.log(`failed to start marketmaker err: ${e}`);
}
}
ExecMarketMaker = function(data) {
//console.log(data);
// start marketmaker via exec
2017-10-14 11:26:56 -07:00
/*if (os.platform() === 'win32') {
2017-10-14 09:08:06 -07:00
const _customParam = {
'gui':'uglygui',
'client':1,
'userhome':`${process.env.APPDATA}`,
2017-10-14 11:26:56 -07:00
'passphrase': data.passphrase,
'coins': data.coinslist
2017-10-14 09:08:06 -07:00
};
}
else {
const _customParam = {
'gui':'uglygui',
'client':1,
'userhome':`${process.env.HOME}`,
'passphrase': data.passphrase,
'coins': data.coinslist
};
2017-10-14 11:26:56 -07:00
}*/
const _customParam = {
'gui':'uglygui',
'client':1,
'userhome':`${process.env.HOME}`,
'passphrase': data.passphrase,
'coins': data.coinslist
};
2017-08-18 10:58:24 -07:00
2017-08-20 07:28:06 -07:00
//console.log(JSON.stringify(_customParam))
2017-08-18 10:58:24 -07:00
2017-08-20 07:28:06 -07:00
//console.log(`exec ${marketmakerBin} ${JSON.stringify(_customParam)}`);
2017-08-18 10:58:24 -07:00
2017-08-20 07:28:06 -07:00
exec(`${marketmakerBin} '${JSON.stringify(_customParam)}'`, {
cwd: marketmakerDir,
2017-08-18 10:58:24 -07:00
maxBuffer: 1024 * 10000 // 10 mb
2017-08-20 07:28:06 -07:00
}, function(error, stdout, stderr) {
2017-08-18 10:58:24 -07:00
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
/*if (error.toString().indexOf('using -reindex') > -1) {
cache.io.emit('service', {
'komodod': {
2017-08-20 07:28:06 -07:00
'error': 'run'
2017-08-18 10:58:24 -07:00
}
});
}*/
}
});
}
2017-08-20 07:28:06 -07:00