2014-04-07 00:33:49 +00:00
|
|
|
var http = require("http");
|
|
|
|
var socketIo = require("socket.io");
|
|
|
|
var config = require("../config");
|
|
|
|
var listeners = require("./listeners");
|
|
|
|
var database = require("./database");
|
2014-04-09 23:58:14 +00:00
|
|
|
var domain = require("domain");
|
2014-04-18 16:42:08 +00:00
|
|
|
var utils = require("./utils");
|
|
|
|
var routing = require("./routing");
|
2014-04-07 00:33:49 +00:00
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.connect(function(err) {
|
|
|
|
if(err)
|
|
|
|
throw err;
|
|
|
|
|
|
|
|
var app = http.createServer();
|
|
|
|
app.listen(config.port, config.host);
|
|
|
|
var io = socketIo.listen(app);
|
|
|
|
|
|
|
|
io.sockets.on("connection", function(socket) {
|
|
|
|
var d = domain.create();
|
|
|
|
d.add(socket);
|
|
|
|
|
|
|
|
var handlers = {
|
|
|
|
error : function(err) {
|
|
|
|
console.error("Error! Disconnecting client.");
|
|
|
|
console.error(err.stack);
|
|
|
|
socket.disconnect();
|
|
|
|
},
|
|
|
|
|
|
|
|
setPadId : function(padId) {
|
|
|
|
if(socket.padId != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-10 16:51:51 +00:00
|
|
|
socket.padId = true;
|
2014-04-18 18:11:50 +00:00
|
|
|
|
2014-05-10 16:51:51 +00:00
|
|
|
database.getPadData(padId, function(err, data) {
|
|
|
|
if(err)
|
|
|
|
return _sendData(socket, "padData", err);
|
|
|
|
|
|
|
|
socket.padId = data.id;
|
|
|
|
socket.writable = data.writable;
|
|
|
|
listeners.addPadListener(socket);
|
|
|
|
|
|
|
|
_sendData(socket, "padData", null, data);
|
|
|
|
_sendStreamData(socket, "view", database.getViews(socket.padId));
|
|
|
|
_sendStreamData(socket, "line", database.getPadLines(socket.padId));
|
2014-05-15 00:38:24 +00:00
|
|
|
|
|
|
|
if(socket.bbox) { // In case bbox is set while fetching pad data
|
|
|
|
_sendStreamData(socket, "marker", database.getPadMarkers(socket.padId, socket.bbox));
|
|
|
|
_sendStreamData(socket, "linePoints", database.getLinePoints(socket.padId, socket.bbox));
|
|
|
|
}
|
2014-05-10 16:51:51 +00:00
|
|
|
});
|
2014-04-18 18:11:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateBbox : function(bbox) {
|
2014-04-28 23:09:42 +00:00
|
|
|
var bboxWithExcept = utils.extend({ }, bbox);
|
|
|
|
if(socket.bbox && bbox.zoom == socket.bbox.zoom)
|
|
|
|
bboxWithExcept.except = socket.bbox;
|
2014-04-18 18:11:50 +00:00
|
|
|
|
2014-04-28 23:09:42 +00:00
|
|
|
socket.bbox = bbox;
|
2014-04-18 18:11:50 +00:00
|
|
|
|
2014-05-15 00:38:24 +00:00
|
|
|
if(socket.padId && socket.padId !== true) {
|
|
|
|
_sendStreamData(socket, "marker", database.getPadMarkers(socket.padId, bboxWithExcept));
|
|
|
|
_sendStreamData(socket, "linePoints", database.getLinePoints(socket.padId, bboxWithExcept));
|
|
|
|
}
|
2014-04-18 18:11:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
disconnect : function() {
|
|
|
|
if(socket.padId)
|
|
|
|
listeners.removePadListener(socket);
|
|
|
|
},
|
|
|
|
|
|
|
|
editPad : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.updatePadData(socket.padId, data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
addMarker : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.createMarker(socket.padId, data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
editMarker : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.updateMarker(data.id, data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteMarker : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.deleteMarker(data.id, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
addLine : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.createLine(socket.padId, data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
editLine : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.updateLine(data.id, data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteLine : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.deleteLine(data.id, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
addView : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.createView(socket.padId, data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
editView : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.updateView(data.id, data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteView : function(data, callback) {
|
2014-05-10 16:51:51 +00:00
|
|
|
if(!socket.writable)
|
|
|
|
return callback(new Error("In read-only mode."));
|
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
database.deleteView(data.id, callback);
|
2014-06-19 00:37:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
copyPad : function(data, callback) {
|
|
|
|
database.copyPad(socket.padId, data.toId, callback);
|
2014-04-13 23:34:05 +00:00
|
|
|
}
|
2014-04-18 18:11:50 +00:00
|
|
|
};
|
2014-04-13 23:34:05 +00:00
|
|
|
|
2014-04-18 18:11:50 +00:00
|
|
|
for(var i in handlers)
|
|
|
|
socket.on(i, handlers[i]);
|
|
|
|
});
|
2014-04-07 00:33:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function _sendData(socket, eventName, err, data) {
|
2014-04-18 17:15:26 +00:00
|
|
|
if(err) {
|
2014-04-26 16:23:22 +00:00
|
|
|
console.warn("_sendData", err, err.stack);
|
2014-04-07 00:33:49 +00:00
|
|
|
return socket.emit("error", err);
|
2014-04-18 17:15:26 +00:00
|
|
|
}
|
2014-04-07 00:33:49 +00:00
|
|
|
|
2014-04-13 23:34:05 +00:00
|
|
|
socket.emit(eventName, data);
|
2014-04-07 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _sendStreamData(socket, eventName, stream) {
|
|
|
|
stream.on("data", function(data) {
|
2014-04-18 16:42:08 +00:00
|
|
|
if(data != null)
|
|
|
|
socket.emit(eventName, data);
|
2014-04-07 00:33:49 +00:00
|
|
|
}).on("error", function(err) {
|
2014-04-26 16:23:22 +00:00
|
|
|
console.warn("_sendStreamData", err, err.stack);
|
2014-04-07 00:33:49 +00:00
|
|
|
socket.emit("error", err);
|
|
|
|
})
|
|
|
|
}
|