git ssb

4+

Dominic / scuttlebot



Commit 18ee3195e540e23418c2e93e441e1d9468672ad5

Add gossip.enable, gossip.disable functions

cel committed on 6/24/2017, 2:47:07 AM
Parent: 484aa4e616c9dcc5dc7d8e706228aaa005a374dd

Files changed

plugins/gossip.mdchanged
plugins/gossip/index.jschanged
plugins/gossip.mdView
@@ -100,4 +100,32 @@
100100
101101 Tell sbot to reinitiate gossip connections now.
102102
103103
104 +## enable: sync
105 +
106 +Update the config to enable a gossip type.
107 +
108 +```bash
109 +enable {type}
110 +```
111 +```js
112 +enable(type, cb)
113 +```
114 +
115 + - type (string): The type of gossip to enable: local, global, or seed. Default
116 + global.
117 +
118 +
119 +## disable: sync
120 +
121 +Update the config to disable a gossip type.
122 +
123 +```bash
124 +disable {type}
125 +```
126 +```js
127 +disable(type, cb)
128 +```
129 +
130 + - type (string): The type of gossip to enable: local, global, or seed. Default
131 + global.
plugins/gossip/index.jsView
@@ -10,8 +10,9 @@
1010 var stats = require('statistics')
1111 var Schedule = require('./schedule')
1212 var Init = require('./init')
1313 var AtomicFile = require('atomic-file')
14 +var fs = require('fs')
1415 var path = require('path')
1516 var deepEqual = require('deep-equal')
1617
1718 function isFunction (f) {
@@ -77,8 +78,27 @@
7778 })
7879
7980 var timer_ping = 5*6e4
8081
82 + function setConfig(name, value) {
83 + config.gossip = config.gossip || {}
84 + config.gossip[name] = value
85 +
86 + var cfgPath = path.join(config.path, 'config')
87 + var existingConfig = {}
88 +
89 + // load ~/.ssb/config
90 + try { existingConfig = JSON.parse(fs.readFileSync(cfgPath, 'utf-8')) }
91 + catch (e) {}
92 +
93 + // update the plugins config
94 + existingConfig.gossip = existingConfig.gossip || {}
95 + existingConfig.gossip[name] = value
96 +
97 + // write to disc
98 + fs.writeFileSync(cfgPath, JSON.stringify(existingConfig, null, 2), 'utf-8')
99 + }
100 +
81101 var gossip = {
82102 wakeup: 0,
83103 peers: function () {
84104 return peers
@@ -190,9 +210,19 @@
190210 server.peers[id].forEach(function (peer) {
191211 peer.close(true)
192212 })
193213 return gossip.wakeup = Date.now()
194- }
214 + },
215 + enable: valid.sync(function (type) {
216 + type = type || 'global'
217 + setConfig(type, true)
218 + return 'enabled gossip type ' + type
219 + }, 'string?'),
220 + disable: valid.sync(function (type) {
221 + type = type || 'global'
222 + setConfig(type, false)
223 + return 'disabled gossip type ' + type
224 + }, 'string?')
195225 }
196226
197227 closeScheduler = Schedule (gossip, config, server)
198228 Init (gossip, config, server)

Built with git-ssb-web