-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
254 lines (211 loc) · 5.91 KB
/
server.js
File metadata and controls
254 lines (211 loc) · 5.91 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// @ts-check
/* eslint-disable no-console */
'use strict';
var util = require('util');
var commander = require('commander');
const http = require('http');
const pubsub = require('pubsub-js');
const configuration = require('./lib/configuration');
const mediaProvider = require('./lib/mediaProvider');
const daemonizeProcess = require('daemonize-process');
// @ts-ignore
const pjson = require('./package.json');
var Server = require('./api');
const sysUI = require('./lib/sysUI');
var directories = [];
commander.version(pjson.version, '-v, --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
});
});
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('-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('--stop', 'Stop already running local MediaMonkey Server');
commander.option('--start', 'Start the server as a service');
commander.option('--status', 'Shows whether there\'s a server running');
commander.option('--datafolder', 'Sets data directory (where database and config files are stored)');
commander.option('-p, --httpPort <port>', 'Http port', function (v) {
return parseInt(v, 10);
});
commander.dlna = !!commander.dlna;
try {
commander.parse(process.argv);
} catch (x) {
console.error('Exception while parsing', x);
}
//commander.garbageItems = true;
function getStatus() {
return new Promise((resolve) => {
http.request({
port: configuration.getBasicConfig().httpPort,
path: '/api',
method: 'GET',
}, (res) => {
if (res.statusCode === 200)
resolve('running');
else
resolve('error');
}).on('error', () => {
resolve('stopped');
}).end();
});
}
function initDB() {
return new Promise((resolve) => {
var RegistryClass = require('./lib/db/sqlRegistry');
var db = new RegistryClass();
db.initialize((error) => {
if (error) {
console.error('Unable to load database: ' + error);
resolve();
} else
configuration.setRegistry(db, () => {
mediaProvider.setRegistry(db);
resolve(db);
});
});
});
}
function loadConfig() {
return new Promise((resolve) => {
configuration.loadConfig((err, config)=>{
if (err) {
console.error('Unable to load configuration: ' + err);
resolve();
} else {
resolve(config);
}
});
});
}
async function start() {
if (commander.datafolder) {
var dir = process.argv.pop();
if (dir == '--datafolder') {
console.error('--datafolder requires one parameter (folder path)');
return;
}
console.log('Setting custom data directory: ' + dir);
configuration.setDataDir(dir);
}
var config = await loadConfig();
if (!config)
return;
if (commander.start) {
if (await getStatus() !== 'stopped') {
console.error('MediaMonkey Server is already running.');
return;
}
console.log('MediaMonkey Server was started as a service.');
daemonizeProcess({
arguments: process.argv.filter(arg => arg !== '--start'),
});
return;
}
if (commander.stop) {
console.log('Stopping a running MediaMonkey Server...');
http.request({
port: configuration.getBasicConfig().httpPort,
path: '/api/stop',
method: 'POST',
}, (res) => {
if (res.statusCode === 200)
console.log('Server successfully stopped.');
else
console.warn('Server stop failed.');
}).on('error', () => {
console.error('Server not found.');
}).end();
return;
}
if (commander.status) {
switch (await getStatus()) {
case 'running': console.log('1: Server is running.'); break;
case 'stopped': console.log('0: Server is not running.'); break;
case 'error': console.log('99: Server failure.'); break;
}
return;
}
var db = await initDB();
if (!db)
return;
// Create an UpnpServer with options
var server = new Server(commander, directories);
server.start();
server.on('waiting',
function () {
}
);
// Catch nodejs problem or signals
var stopped = false;
var _stopAndExit = async function () {
console.log('disconnecting...');
stopped = true;
await server.stop(() => {});
// JH: we can remove this timeout sometimes, after we review all the stuff and use async/await everywhere consistently
setTimeout(function () {
process.exit();
}, 1000);
};
pubsub.subscribe('APP_END', () => {
_stopAndExit();
});
process.on('SIGINT', () => {
pubsub.publishSync('APP_END', null);
});
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.headDump) {
var heapdump = require('heapdump');
console.log('***** HEAPDUMP enabled **************');
var nextMBThreshold = 0;
setInterval(function () {
var memMB = process.memoryUsage().rss / 1048576;
if (memMB > nextMBThreshold) {
heapdump.writeSnapshot();
nextMBThreshold += 100;
}
}, 1000 * 60 * 10);
}
sysUI.installTrayIcon();
}
start();