-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
145 lines (116 loc) · 3.21 KB
/
server.js
File metadata and controls
145 lines (116 loc) · 3.21 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
var util = require('util');
var pkg = require('./package');
var commander = require("commander");
var Server = require("upnpserver");
var directories = [];
commander.version(pkg.version);
commander.option("-d, --directory <path>", "Mount directory", function(path) {
var mountPoint = null;
var idx = path.indexOf("=");
if (idx > 0) {
mountPoint = path.substring(0, idx);
path = path.substring(idx + 1);
}
directories.push({
path: path,
mountPoint: mountPoint
});
return path;
});
commander.option("-m, --music <path>", "Mount music directory", function(path) {
var mountPoint = null;
var idx = path.indexOf("=");
if (idx > 0) {
mountPoint = path.substring(0, idx);
path = path.substring(idx + 1);
}
directories.push({
type: "music",
path: path,
mountPoint: mountPoint
});
});
commander.option("-v, --video <path>", "Mount video directory", function(path) {
var mountPoint = null;
var idx = path.indexOf("=");
if (idx > 0) {
mountPoint = path.substring(0, idx);
path = path.substring(idx + 1);
}
directories.push({
type: "video",
path: path,
mountPoint: mountPoint
});
});
commander.option("-n, --name <name>", "Name of server");
commander.option("-u, --uuid <uuid>", "UUID of server");
commander.option("--dlna", "Enable dlna support");
commander.option("--lang <lang>", "Specify language (en, fr)");
commander.option("--strict", "Use strict specification");
commander.option("--ssdpLog", "Enable log of SSDP engine");
commander.option("--ssdpLogLevel <level>", "Log level of SSDP engine");
commander.option("--profiler", "Enable memory profiler dump");
commander.option("--heapDump", "Enable heap dump (require heapdump)");
commander.option("-p, --httpPort <port>", "Http port", function(v) {
return parseInt(v, 10);
});
commander.dlna = !!commander.dlna;
if (!commander.uuid) {
commander.uuid = "142f98b7-c28b-4b6f-8ca2-b55d9f0657e3";
}
try {
commander.parse(process.argv);
} catch (x) {
console.error("Exception while parsing", x);
}
if(!directories.length) {
commander.help();
} else {
console.log('Keywords: ' + commander.args);
}
// commander.garbageItems = true;
// Create an UpnpServer with options
var server = new Server(commander, directories);
server.start();
server.on("waiting", function() {
console.log("Waiting connexions on port " + server.httpServer.address().port);
});
// Catch nodejs problem or signals
var stopped = false;
process.on('SIGINT', function() {
if (stopped) {
return;
}
console.log('disconnecting...');
stopped = true;
server.stop();
setTimeout(function() {
process.exit();
}, 1000);
});
process.on('uncaughtException', function(err) {
if (stopped) {
process.exit(0);
return;
}
console.error('Caught exception: ', err);
});
// Try to profile upnpserver manually !
if (commander.profiler) {
setInterval(function() {
console.log(util.inspect(process.memoryUsage()));
}, 1000 * 30);
}
if (commander.heapDump) {
var heapdump = require("heapdump");
console.log("***** HEAPDUMP enabled **************");
var nextMBThreshold = 100;
setInterval(function() {
var memMB = process.memoryUsage().rss / 1048576;
if (memMB > nextMBThreshold) {
heapdump.writeSnapshot();
nextMBThreshold += 100
}
}, 1000 * 60 * 10);
}