Created
December 5, 2012 17:56
-
-
Save kazupon/4217913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var Model = require('scuttlebutt/model'); | |
var net = require('net'); | |
var m = new Model(); | |
var ms = m.createStream(); | |
ms.pipe(net.connect(8888, 'localhost')).pipe(ms); | |
m.on('update', function cb (pairs, ts, source) { | |
if (pairs && pairs[0] !== 'count') { | |
return; | |
} | |
m.removeListener('update', cb); | |
setInterval(function () { | |
m.set('count', Number(m.get('count')) + 1); | |
}, 100); | |
}); | |
m.on('update', function (pairs, ts, source) { | |
log(format('client: %s = %s', pairs[0], pairs[1])); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var ReliableEventEmitter = require('scuttlebutt/events'); | |
var ev1 = new ReliableEventEmitter(); | |
var ev2 = new ReliableEventEmitter(); | |
var evs1 = ev1.createStream(); | |
var evs2 = ev2.createStream(); | |
log('events: ', ev1.id, ev2.id); | |
ev1.on('foo', function () { | |
log(format('%s foo: %s', this.id, JSON.stringify(arguments))); | |
}); | |
ev1.on('dispose', function () { | |
log(format('%s dispose: %s', this.id, JSON.stringify(arguments))); | |
}); | |
ev2.on('hoge', function () { | |
log(format('%s hoge: %s', this.id, JSON.stringify(arguments))); | |
}); | |
evs1.pipe(evs2).pipe(evs1); | |
ev1.emit('hoge', { hoge: 1 }, 1); | |
ev2.emit('foo', [1, 2, 3]); | |
process.nextTick(function () { | |
ev1.dispose(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var ReliableEventEmitter = require('scuttlebutt/events'); | |
var ev1 = new ReliableEventEmitter(); | |
var ev2 = new ReliableEventEmitter(); | |
var ev3 = new ReliableEventEmitter(); | |
var evs1 = ev1.createStream(); | |
var evs2 = ev2.createStream(); | |
var evs3 = ev3.createStream(); | |
log('events: ', ev1.id, ev2.id, ev3.id); | |
ev1.on('foo', function () { | |
log(format('%s foo: %s', this.id, JSON.stringify(arguments))); | |
}); | |
ev2.on('hoge', function () { | |
log(format('%s hoge: %s', this.id, JSON.stringify(arguments))); | |
}); | |
ev2.on('zoo', function () { | |
log(format('%s zoo: %s', this.id, JSON.stringify(arguments))); | |
}); | |
ev3.on('hoge', function () { | |
log(format('%s hoge: %s', this.id, JSON.stringify(arguments))); | |
}); | |
ev3.on('foo', function () { | |
log(format('%s foo: %s', this.id, JSON.stringify(arguments))); | |
}); | |
ev3.on('zoo', function () { | |
log(format('%s zoo: %s', this.id, JSON.stringify(arguments))); | |
}); | |
evs1.pipe(evs2).pipe(evs1); | |
evs2.pipe(evs3).pipe(evs2); | |
ev1.emit('hoge', { hoge: 1 }, 1); | |
ev1.emit('zoo', { zoo: 1 }, 1); | |
ev2.emit('foo', [1, 2, 3]); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var Model = require('scuttlebutt/model'); | |
var m1 = new Model(); | |
var ms1 = m1.createStream({ meta: 'a' }); | |
var m2 = new Model(); | |
var ms2 = m2.createStream({ meta: 'b' }); | |
var m3 = new Model(); | |
var ms3 = m3.createStream({ meta: 'c' }); | |
log('models: ', m1.id, m2.id, m3.id); | |
ms1.on('header', function (m) { | |
log(format('%s header: %s', m1.id, JSON.stringify(m))); | |
}); | |
ms2.on('header', function (m) { | |
log(format('%s header: %s', m2.id, JSON.stringify(m))); | |
}); | |
ms3.on('header', function (m) { | |
log(format('%s header: %s', m3.id, JSON.stringify(m))); | |
}); | |
ms1.pipe(ms2).pipe(ms1); | |
ms2.pipe(ms3).pipe(ms2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var Model = require('scuttlebutt/model'); | |
var models = []; | |
var m1 = new Model(); | |
var ms1 = m1.createStream(); | |
models.push(m1); | |
var m2 = new Model(); | |
var ms2 = m2.createStream(); | |
models.push(m2); | |
var m3 = new Model(); | |
var ms3 = m3.createStream(); | |
models.push(m3); | |
var m4 = new Model(); | |
var ms4 = m4.createStream(); | |
models.push(m4); | |
var m5 = new Model(); | |
var ms5 = m5.createStream(); | |
models.push(m5); | |
log('model id list:'); | |
models.forEach(function (model) { | |
log(model.id); | |
}); | |
log(); | |
ms1.pipe(ms2).pipe(ms1); | |
ms2.pipe(ms3).pipe(ms2); | |
ms2.pipe(ms4).pipe(ms2); | |
ms4.pipe(ms5).pipe(ms4); | |
models.forEach(function (model) { | |
model.on('update', function (pairs, ts, source) { | |
log(format('update %s: %s => %s from %s (%s)', this.id, pairs[0], pairs[1], source, ts)); | |
}); | |
}); | |
var key = 'x'; | |
var value = 555; | |
log(format('%s set %s => %s', m1.id, key, value)); | |
m1.set('x', 555); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"5304709762D1E4FA63065242" | |
[["x",222],1354757674825,"20B2736C287DB2C27E6A621"] | |
[["count",0],1354757674829,"20B2736C287DB2C27E6A621"] | |
[["count",1],1354757675309,"5304709762D1E4FA63065242"] | |
[["count",2],1354757675810,"5304709762D1E4FA63065242"] | |
[["count",3],1354757676311,"5304709762D1E4FA63065242"] | |
[["count",4],1354757676812,"5304709762D1E4FA63065242"] | |
[["count",5],1354757677313,"5304709762D1E4FA63065242"] | |
[["count",6],1354757677814,"5304709762D1E4FA63065242"] | |
[["count",7],1354757678315,"5304709762D1E4FA63065242"] | |
[["count",8],1354757678816,"5304709762D1E4FA63065242"] | |
[["count",9],1354757679317,"5304709762D1E4FA63065242"] | |
[["count",10],1354757679818,"5304709762D1E4FA63065242"] | |
[["count",11],1354757680319,"5304709762D1E4FA63065242"] | |
[["count",12],1354757680820,"5304709762D1E4FA63065242"] | |
[["count",13],1354757681320,"5304709762D1E4FA63065242"] | |
[["count",14],1354757681822,"5304709762D1E4FA63065242"] | |
[["count",15],1354757682322,"5304709762D1E4FA63065242"] | |
[["count",16],1354757682824,"5304709762D1E4FA63065242"] | |
[["count",17],1354757683325,"5304709762D1E4FA63065242"] | |
[["count",18],1354757683826,"5304709762D1E4FA63065242"] | |
[["count",19],1354757684326,"5304709762D1E4FA63065242"] | |
[["count",20],1354757684828,"5304709762D1E4FA63065242"] | |
[["count",21],1354757685328,"5304709762D1E4FA63065242"] | |
[["count",22],1354757685830,"5304709762D1E4FA63065242"] | |
[["count",23],1354757686330,"5304709762D1E4FA63065242"] | |
[["count",24],1354757686832,"5304709762D1E4FA63065242"] | |
[["count",25],1354757687331,"5304709762D1E4FA63065242"] | |
[["count",26],1354757687833,"5304709762D1E4FA63065242"] | |
[["count",27],1354757688334,"5304709762D1E4FA63065242"] | |
[["count",28],1354757688835,"5304709762D1E4FA63065242"] | |
[["count",29],1354757689336,"5304709762D1E4FA63065242"] | |
[["count",30],1354757689837,"5304709762D1E4FA63065242"] | |
[["count",31],1354757690338,"5304709762D1E4FA63065242"] | |
[["count",32],1354757690838,"5304709762D1E4FA63065242"] | |
[["count",33],1354757691340,"5304709762D1E4FA63065242"] | |
[["count",34],1354757691841,"5304709762D1E4FA63065242"] | |
[["count",35],1354757692342,"5304709762D1E4FA63065242"] | |
[["count",36],1354757692844,"5304709762D1E4FA63065242"] | |
[["count",37],1354757693344,"5304709762D1E4FA63065242"] | |
[["count",38],1354757693846,"5304709762D1E4FA63065242"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var fs = require('fs'); | |
var Model = require('scuttlebutt/model'); | |
var m1 = new Model(); | |
m1.on('update', function (pairs, ts, src) { | |
log(format('%s update: %s -> %s', m1.id, pairs[0], pairs[1])); | |
}); | |
// to disk | |
fs.createReadStream('./persist.txt').pipe(m1.createWriteStream()); | |
// from disk | |
m1.on('sync', function () { | |
log(format('%s sync -> %s', m1.id, JSON.stringify(m1))); | |
m1.createReadStream().pipe(fs.createWriteStream('./persist.txt')); | |
}); | |
m1.set('x', 222); | |
m1.set('count', 0); | |
setInterval(function () { | |
m1.set('count', Number(m1.get('count')) + 1); | |
}, 500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var fs = require('fs'); | |
var Emitter = require('scuttlebutt/events'); | |
var security = require('scuttlebutt/security'); | |
var keys = {}; | |
var PRIVATE = fs.readFileSync(__dirname + '/hoge'); | |
var PUBLIC = fs.readFileSync(__dirname + '/hoge.pem'); | |
var secure = security(keys, PRIVATE, PUBLIC); | |
var ev1 = new Emitter(secure); | |
var ev2 = new Emitter(security(keys, '', PUBLIC)); | |
var evs1 = ev1.createStream(); | |
evs1.pipe(ev2.createStream()).pipe(evs1); | |
ev2.on('hello', function () { | |
log(format('%s hello: %s', this.id, JSON.stringify(arguments))); | |
}); | |
process.nextTick(function () { | |
ev1.emit('hello', { world: true }); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var Model = require('scuttlebutt/model'); | |
var net = require('net'); | |
var m = new Model(); | |
m.set('count', 0); | |
m.on('update', function (pairs, ts, source) { | |
log(format('server: %s = %s', pairs[0], pairs[1])); | |
}); | |
var server = net.createServer(function (socket) { | |
socket.pipe(m.createStream()).pipe(socket); | |
}); | |
server.listen(8888); | |
setInterval(function () { | |
m.set('count', Number(m.get('count')) + 1); | |
}, 320); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var log = console.log.bind(console); | |
var format = require('util').format; | |
var ReliableEventEmitter = require('scuttlebutt/events'); | |
var ev1 = new ReliableEventEmitter(); | |
var ev2 = new ReliableEventEmitter(); | |
var evs1 = ev1.createStream(); | |
var evs2 = ev2.createStream(); | |
log('events: ', ev1.id, ev2.id); | |
evs1.on('synced', function () { | |
log(format('synced: %s', JSON.stringify(arguments))); | |
}); | |
evs2.on('synced', function () { | |
log(format('synced: %s', JSON.stringify(arguments))); | |
}); | |
evs1.pipe(evs2).pipe(evs1); | |
ev1.emit('event', { hoge: 1 }, 1); | |
ev2.emit('event', [1, 2, 3]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment