-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.coffee
More file actions
239 lines (191 loc) · 7.38 KB
/
server.coffee
File metadata and controls
239 lines (191 loc) · 7.38 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Server for Chatternets
express = require("express")
logfmt = require("logfmt")
uuid = require('node-uuid')
app = express()
WebSocketServer = require('ws').Server
server = require('http').createServer(app)
wss = new WebSocketServer({server: server});
wss.broadcast = (data) =>
for client in wss.clients
client.send(data)
console.log('websocket server created');
app.use(logfmt.requestLogger())
app.use(express.bodyParser())
# Helper Functions
normalizeURL = (urlRaw) ->
urlNormal = urlRaw
return urlNormal.replace(/([\?#].*$)/gi, "")
generateId = () ->
return uuid.v1()
MAX_ROOM_SIZE = 5
urlToURLIds = {}
urlIdToURL = {}
urlIdToPeerIds = {}
pageIdToPeerAndUrlId = {}
getPeerCount = (url) =>
urlPeercount = 0
return 0 if not urlToURLIds[url]
for urlId in urlToURLIds[url]
console.log urlIdToPeerIds[urlId]
urlPeercount += if urlIdToPeerIds[urlId] then urlIdToPeerIds[urlId].length else 0
return urlPeercount
getFullSummary = =>
summary = []
for url, urlIds of urlToURLIds
urlPeercount = getPeerCount(url)
summary.push([url, urlPeercount])
console.log("URL: " + url + " peercount: " + urlPeercount)
summary.sort (item) =>
return item[1]
return summary
# The following conditions hold, as a result of this function:
# - the connected peer has been given an id
# - that id has been added to a room that has no more than MAX_ROOM_SIZE
# other peers
#
# Returns: peer id that has been created for the connected user
onPeerConnected = (urlRaw, pageId) ->
urlNormal = normalizeURL(urlRaw)
if not urlToURLIds.hasOwnProperty(urlNormal) # If the raw URL isn't yet mapped, map it.
urlId = generateId()
urlIdToURL[urlId] = urlNormal
urlToURLIds[urlNormal] = [urlId]
peerId = generateId()
peerIds = []
roomFound = false
for urlId in urlToURLIds[urlNormal]
if not urlIdToPeerIds.hasOwnProperty(urlId)
# We are the first peer for this id
urlIdToPeerIds[urlId] = [peerId]
roomFound = true
break
if urlIdToPeerIds[urlId].length < MAX_ROOM_SIZE
# There is space in this id for us
peerIds = urlIdToPeerIds[urlId].slice(0)
urlIdToPeerIds[urlId].push(peerId)
roomFound = true
break
if not roomFound
# If we get here, that means we didn't find a room to join.
# So, we create one.
urlId = generateId()
urlIdToURL[urlId] = urlNormal
urlToURLIds[urlNormal].push(urlId)
urlIdToPeerIds[urlId] = [peerId]
pageIdToPeerAndUrlId[pageId] = {"peerId": peerId, "urlId": urlId}
getFullSummary() # TODO REMOVE
console.log "EMIT PEER CONNECTED"
wss.broadcast(JSON.stringify({'name': 'peer-connected', 'data': {"url": urlNormal, "peer_count": getPeerCount(urlNormal)}}));
return { peer_id: peerId, url_id: urlId, peers: peerIds}
# Remove the peer from the room identified by urlId.
# Perform any cleanup necessary.
onPeerDisconnected = (pageId) ->
console.log "disconnecting..."
console.log pageId
# console.log(JSON.stringify(pageIdToPeerAndUrlId, null, 4))
if not pageIdToPeerAndUrlId[pageId]
return { success: false, message: "That page id was not recognized" }
peerId = pageIdToPeerAndUrlId[pageId].peerId
urlId = pageIdToPeerAndUrlId[pageId].urlId
url = urlIdToURL[urlId]
# Remove the peer id from the url id's room
index = urlIdToPeerIds[urlId].indexOf(peerId)
if index == -1
return {
success: false,
message: "That peer_id, url_id pair was not recognized" }
urlIdToPeerIds[urlId].splice(index, 1)
# If the removed peer was the last peer, do some clean up...
if urlIdToPeerIds[urlId].length == 0
delete urlIdToPeerIds[urlId]
# Remove the url id from the url id -> url map
url = urlIdToURL[urlId]
delete urlIdToURL[urlId]
# Remove the url id from the url -> [url id] map
index = urlToURLIds[url].indexOf(urlId)
if index == -1
console.log("Something is horribly wrong")
return
urlToURLIds[url].splice(index, 1)
# If this was the last url id, remove the url
if urlToURLIds[url].length == 0
delete urlToURLIds[url]
delete pageIdToPeerAndUrlId[pageId]
console.log("URL" + url)
wss.broadcast(JSON.stringify({'name': 'peer-disconnected', 'data': {"url": url, "peer_count": getPeerCount(url)}}));
return { success: true }
# These functions must take in (peerId, urlId) and return
# { success: true } or
# { success: false, message: "[error message]"}
VALID_UPDATE_STATUSES = {
"DEAD": onPeerDisconnected
}
#####################
# Server code
app.get '/', (req, res) ->
res.sendfile('dashboard/index.html')
app.get '/bookmarklet/:file(*)', (req, res) ->
res.sendfile('bookmarklet/' + req.params.file)
app.get '/dashboard/:file(*)', (req, res) ->
res.sendfile('dashboard/' + req.params.file)
app.get '/bookmarklet/compiled/:file', (req, res) ->
res.sendfile('bookmarklet/compiled/' + req.params.file)
app.get '/bookmarklet/library/:file', (req, res) ->
res.sendfile('bookmarklet/library/' + req.params.file)
app.get '/dashboard/compiled/:file', (req, res) ->
res.sendfile('dashboard/compiled/' + req.params.file)
app.get '/dashboard/library/:file', (req, res) ->
res.sendfile('dashboard/library/' + req.params.file)
# Create a new peer for the given url
app.post '/new_peer', (req, res) ->
if not req.body.hasOwnProperty("full_url") or not req.body.hasOwnProperty("page_id")
res.send(500, { error: "Must specify full_url and page_id parameters" })
return
result = onPeerConnected(req.body.full_url, req.body.page_id)
res.send(JSON.stringify(result))
# Delete myself as a peer, given my peer id and the url id that I'm part of
app.post '/delete_peer', (req, res) ->
res.header("Access-Control-Allow-Origin", "*") # Allow cross-site scripting here
if not (req.body.hasOwnProperty("page_id"))
console.log("ERROR: req body has no page_id param")
res.send(500, { error: "Must specify page_id parameter" })
return
result = onPeerDisconnected(req.body.page_id)
if result.success
console.log("SUCCESS!")
res.send(200)
else
console.log("ERROR: " + result.message)
res.send(500, { error: result.message })
# Update the status of some other peer, given their peer id, the url id, and
# what state I'm reporting for them.
# Valid statuses are those contained in VALID_UPDATE_STATUSES.
# app.post '/update_peer', (req, res) ->
# if not (req.body.hasOwnProperty("peer_id") and req.body.hasOwnProperty("url_id") and req.body.hasOwnProperty("status"))
# res.send(500, {
# error: "Must specify peer_id, url_id, and status parameters" })
# return
# if not VALID_UPDATE_STATUSES.hasOwnProperty(req.body.status)
# res.send(500, { error: req.body.status + " is not a valid status" })
# return
# result = VALID_UPDATE_STATUSES[req.body.status](
# req.body.peer_id, req.body.url_id)
# # TODO(brie): remove this. left in for now, for debugging
# console.log(JSON.stringify(urlToURLIds, null, 4))
# console.log(JSON.stringify(urlIdToURL, null, 4))
# console.log(JSON.stringify(urlIdToPeerIds, null, 4))
# if result.success
# res.send(200)
# else
# res.send(500, { error: result.message })
port = process.env.PORT || 5000
server.listen port, ->
console.log("Listening on " + port)
wss.on 'connection', (socket) =>
console.log 'saw connection'
summary = getFullSummary()
console.log("FULL SUMMARY")
console.log summary
# Send over the current data of how many people are on which pages
socket.send(JSON.stringify({"name": 'peer_urls', "data": summary}))