Skip to content

Commit

Permalink
feat: js-ipfsd-ctl daemon-remote
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Dec 26, 2017
1 parent caa0b70 commit a5afab7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"electron-menubar": "^1.0.1",
"electron-squirrel-startup": "^1.0.0",
"file-extension": "^4.0.0",
"go-ipfs-dep": "^0.4.13",
"ipfs-geoip": "^2.3.0",
"ipfsd-ctl": "^0.26.0",
"ipfsd-ctl": "ipfs/js-ipfsd-ctl#feat/remote-daemon",
"moment": "^2.19.3",
"multiaddr": "^3.0.1",
"normalize.css": "^7.0.0",
Expand Down
45 changes: 27 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Menubar} from 'electron-menubar'
import ipfsd from 'ipfsd-ctl'
import fs from 'fs'
import DaemonFactory from 'ipfsd-ctl'
import {join} from 'path'
import {dialog, ipcMain, app, BrowserWindow} from 'electron'

Expand Down Expand Up @@ -54,29 +55,31 @@ function onPollerChange (stats) {
}

function onRequestState (node, event) {
if (node.initialized) {
let status = 'stopped'
if (!node.initialized) {
return
}

if (node.daemonPid()) {
status = IPFS ? 'running' : 'starting'
}
let status = 'stopped'

send('node-status', status)
if (node.pid()) {
status = IPFS ? 'running' : 'starting'
}

send('node-status', status)
}

function onStartDaemon (node) {
logger.info('Start daemon')
send('node-status', 'starting')

node.startDaemon((err, ipfsNode) => {
node.start((err, api) => {
if (err) {
handleKnownErrors(err)
return
}

logger.info('Started daemon')
poller = new StatsPoller(ipfsNode, logger)
poller = new StatsPoller(api, logger)

if (menubar.window && menubar.window.isVisible()) {
poller.start()
Expand All @@ -88,8 +91,8 @@ function onStartDaemon (node) {

menubar.tray.setImage(config.logo.ice)

IPFS = api
send('node-status', 'running')
IPFS = ipfsNode
})
}

Expand All @@ -107,8 +110,10 @@ function onStopDaemon (node, done) {
menubar.removeListener('hide', stopPolling)
}

node.stopDaemon((err) => {
if (err) { return logger.error(err.stack) }
node.stop((err) => {
if (err) {
return logger.error(err.stack)
}

logger.info('Stopped daemon')
menubar.tray.setImage(config.logo.black)
Expand Down Expand Up @@ -201,7 +206,7 @@ function initialize (path, node) {
return send('initialization-error', String(err))
}

config.settingsStore.set('ipfsPath', path)
config.settingsStore.set('ipfsPath', userPath)

send('initialization-complete')
send('node-status', 'stopped')
Expand All @@ -213,10 +218,13 @@ function initialize (path, node) {
}

// main entry point
ipfsd.local((err, node) => {
DaemonFactory.create().spawn({
repoPath: config.ipfsPath,
disposable: false,
init: false,
start: false
}, (err, node) => {
if (err) {
// We can't start if we fail to aquire
// a ipfs node
logger.error(err)
process.exit(1)
}
Expand All @@ -236,10 +244,11 @@ ipfsd.local((err, node) => {

registerControls(config)

if (!node.initialized) {
let exists = fs.existsSync(node.repoPath)

if (!exists) {
initialize(config.settingsStore.get('ipfsPath'), node)
} else {
// Start up the daemon
onStartDaemon(node)
}
}
Expand Down

0 comments on commit a5afab7

Please sign in to comment.