-
Notifications
You must be signed in to change notification settings - Fork 1
/
buoy.js
111 lines (95 loc) · 2.74 KB
/
buoy.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
var util = require("util");
var uuid = require("node-uuid");
var EventEmitter = require("events").EventEmitter;
var Peer = require("./peer").Peer;
var Room = require("./room").Room;
/*
* Buoy class
* Represents a buoy (basically a server).
* ----
* Events
* Name: newRoom
* Data: { room: Room }
*/
function Buoy(server) {
var self = this;
this.svr = server;
this.peers = {};
this.rooms = {};
this.svr.on("request", function(req) {
var conn = req.accept("groovebuoy-0.1", req.origin);
var pid = uuid.v1();
console.log("[debug] Peer "+ pid +" connected");
var peer = new Peer(self, conn);
peer.id = pid;
self.peers[pid] = peer;
var rooms = [];
for(var room in self.rooms) {
rooms.push(room);
}
// Give the peer some info
peer.send("welcome", {
id: pid,
rooms: rooms
});
conn.on("message", function(msg) {
if(msg.type != "utf8") {
conn.drop(conn.CLOSE_REASON_PROTOCOL_ERROR);
console.log("[err] Received bad data");
return;
}
try {
msg = JSON.parse(msg.utf8Data);
} catch(e) {
conn.drop(conn.CLOSE_REASON_PROTOCOL_ERROR);
console.log("[err] Received bad JSON data");
return;
}
if(!msg['e']) {
conn.drop(conn.CLOSE_REASON_PROTOCOL_ERROR);
console.log("[err] Received bad object data");
}
peer.emit(msg['e'], msg);
});
conn.on("close", function() {
if(self.peers[pid]) {
self.peers[pid].cleanUp();
delete self.peers[pid];
console.log("[debug] Peer "+ pid +" disconnected");
} else {
console.log("[error] Invalid client "+ pid +" disconnected");
}
});
});
}
util.inherits(Buoy, EventEmitter);
/*
* Sends a message to a peer with the given ID
*/
Buoy.prototype.sendPeer = function(id, event, data) {
var peer = this.peers[id];
if(!peer) return;
peer.send(event, data);
}
/*
* Gets a room given its name. It will create this room if it does not
* exist.
*/
Buoy.prototype.getRoom = function(name) {
var room = this.rooms[name];
if(!room) {
room = new Room(this, name);
this.rooms[name] = room;
this.emit("newRoom", { room: room });
}
return room;
}
/*
* Deletes a room from the buoy given its name
*/
Buoy.prototype.deleteRoom = function(name) {
delete this.rooms[name];
console.log("[debug] Deleting room "+ name);
this.emit("deleteRoom", { room: name });
}
exports.Buoy = Buoy;