-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (160 loc) · 4.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const assert = require('assert')
const fs = require('fs')
const uuidv4 = require('uuid/v4')
const {get_server, handle_gossip, return_peers} = require('./server')
const P = require('bluebird')
const request = P.promisifyAll(require('request'))
const bunyan = require('bunyan');
const log = bunyan.createLogger({
name: "index",
pid: `port=${process.env.PORT}`
});
const MASTER_NODE_PORT = '1111'
const ROOT_URI = 'http://localhost:'
const run = P.coroutine(function* (port, master_node) {
log.info('starting up', {port: port})
const state = initial_state({favorite_book: pick_random_book(), version: 1, my_port: port})
const peers = yield lookup_peers(process.env.PORT, MASTER_NODE_PORT)
state.peers = new Set(peers)
const {start, server} = get_server(port)
const gossip_handler = handle_gossip(state, handle_message)
const send_peers = return_peers(state)
server.get('/peers', send_peers)
server.post('/gossip', gossip_handler)
update_book = book_update_dater(state)
setInterval(update_book, 10 * 1000)
setInterval(() => update_state_display(state), 2 * 1000)
setInterval(() => ask_for_new_friends(state), 12 * 1000)
start()
})
function ask_for_new_friends(state) {
P.resolve(state.peers.values()).map(peer => {
log.info('asking peers for their friend list', {peer: peer})
return lookup_peers(process.env.PORT, peer)
.then(new_peers => {
state.peers = new Set(...state.peers, ...new_peers)
})
})
}
function update_state_display(state) {
process.stdout.write('\033c'); // clear iterm
console.log('')
log.info('My state:\n', state)
}
function lookup_peers(my_port, their_port) {
return request.getAsync({
url: ROOT_URI.concat(their_port, '/peers'),
qs: {port: my_port}
})
.then(({body}) => JSON.parse(body))
.then(({peers}) => peers.filter(p => p !== String(my_port)))
}
function pick_random_book() {
books = fs.readFileSync('./books.txt', 'utf-8').split('\n')
randomInt = Math.floor(Math.random() * Math.floor(books.length))
return books[randomInt]
}
function book_update_dater(state) {
return () => {
const favorite_book = pick_random_book()
state.favorite_book = favorite_book
state.version = state.version + 1
message = {
originator: process.env.PORT,
TTL: 10,
uuid: uuidv4(),
payload: {book: favorite_book, version: state.version}
}
forward_message(state, message)
}
}
function initial_state(overrides) {
return Object.assign({
my_port: 80,
favorite_book: 'Fountain head',
version: 1,
peers: new Set([]),
others_favorite_books: {},
recent_messages: new Set()
}, overrides)
}
function build_message(overrides) {
const defaults = {
originator: null,
TTL: 10,
uuid: uuidv4(),
payload: {}
}
return Object.assign(
defaults,
overrides
)
}
function forward_message(state, message) {
P.resolve(state.peers.values())
.map((peer) => {
log.debug('forwardingint to', `http://localhost:${peer}/gossip`, message)
request.postAsync(`http://localhost:${peer}/gossip`, {body: message, json: true})
.catch((e) => {
log.warn('removing peer', {peer: peer})
state.peers.delete(peer)
})
})
}
function handle_message(state, message) {
if (state.recent_messages.has(message.uuid)) {
log.debug('seen message, doing nothing')
return
}
updated_state = update_state(state, message)
log.debug('updated_state', updated_state)
if (message.TTL > 1) {
forward_message(
state,
Object.assign(message, {TTL: message.TTL - 1})
)
} else {
log.warn('message.TTL expired')
}
}
function update_state(state, message) {
state.peers.add(message.originator)
state.recent_messages.add(message.uuid)
state.others_favorite_books = update_known_favorites(
state.others_favorite_books,
message.originator,
message.payload.version,
message.payload.book
)
return state
}
function update_known_favorites(known, sender, version, book) {
current_favorite_for_peer = known[sender]
if (!current_favorite_for_peer) {
known[sender] = {version: version, book: book}
} else if (current_favorite_for_peer.version < version) {
known[sender] = {version: version, book: book}
}
return known
}
// functional tests
state = initial_state()
assert.deepEqual(
update_state(initial_state(), build_message({originator: 88, uuid: 1234, payload: {book: 'dog', version: 1}})),
{
my_port: 80,
favorite_book: 'Fountain head',
version: 1,
peers: new Set([88]),
others_favorite_books: {88: {book: 'dog', version: 1}},
recent_messages: new Set([1234])
}
)
known = {80: {version: 1, book: 'cat'}}
sender = 80
version = 1
book = 'bat'
assert.deepEqual(update_known_favorites(known, sender, version, book), known)
assert.deepEqual(update_known_favorites(known, sender, 2, book), {80: {version: 2, book: 'bat'}})
assert.deepEqual(update_known_favorites({}, sender, 2, book), {80: {version: 2, book: 'bat'}})
run(process.env.PORT)