-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncServer.js
93 lines (93 loc) · 2.45 KB
/
SyncServer.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
var net = require("net");
var config = require("config");
var fs = require('fs-extra');
var SyncUtils = require("./SyncUtils");
function SyncServer(configParam){
this.serverEnabled = configParam.config.enabled;
this.port = configParam.port;
this.ip = configParam.ip;
this.clients = new Array();
if(this.serverEnabled){
this.server = net.createServer();
}
}
SyncServer.prototype.start = function(){
if(this.serverEnabled){
var that = this;
this.server.on("connection", function(client){
console.log("one client connected:"+client)
that.clients.push(client);
});
console.log("Start listen:"+this.ip+":"+this.port);
this.server.listen(this.port, this.ip);
}
}
SyncServer.prototype.sendFileData = function(type, source, params){
var that = this;
var configItem = params.item;
if(configItem==null){
configItem = {};
}
if(this.serverEnabled){
var target = that.getTargetPath(configItem, source);
var fileContent = fs.readFileSync(source);
var dataEndSign = config.get("remote.dataEndSign");
var sendData = {
"type": type,
"source": source,
"target": target,
"content": fileContent
}
sendData = JSON.stringify(sendData);
sendData = sendData + dataEndSign;
var sendChange = function(){
that.sendDataToAllClient(sendData);
}
var sendAdd = function(){
that.sendDataToAllClient(sendData);
}
var sendDelete = function(){
that.sendDataToAllClient(sendData);
}
switch (type){
case "change":
sendChange();
break;
case "add":
sendAdd();
break;
case "remove":
sendRemove();
break;
}
}
}
SyncServer.prototype.getTargetPath = function(configItem, sourcePath){
var relTarget = SyncUtils.getRelationPath(configItem, sourcePath);
var remoteRoot = config.get("remote.rootPath");
var remoteTargetFolder = configItem.remoteTargetFolder;
var remoteFilePath = remoteRoot+remoteTargetFolder+relTarget;
return remoteFilePath;
};
SyncServer.prototype.sendDataToAllClient = function(data){
if(this.serverEnabled){
var clients = this.clients;
var cleanupClients = new Array();
for(var i=0;i<clients.length;i++){
var client = clients[i];
if(client.writable){
client.write(data);
}else{
cleanupClients.push(client);
client.end();
}
}
for(var i=0;i<cleanupClients.length;i++){
var cleanupClient = cleanupClients[i];
clients.splice(clients.indexOf(cleanupClient),1);
}
}
}
/*var syncServer = new SyncServer("127.0.0.1", 8888);
syncServer.start();*/
module.exports = SyncServer;