forked from ewenig/node-mpdsocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpdsocket.js
More file actions
184 lines (166 loc) · 4.51 KB
/
Copy pathmpdsocket.js
File metadata and controls
184 lines (166 loc) · 4.51 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
/*******
** node-mpdsocket :: an MPD library for node.js
**
** author: Eli Wenig (http://eliwenig.com/) <eli@csh.rit.edu>
**
** copyright (c) 2011 Eli Wenig
** made available under the MIT license
** http://www.opensource.org/licenses/mit-license.php
**
*******/
var net = require('net');
var sys = require('sys');
function mpdSocket(host,port,autoreopen) {
if (!host) {
this.host = "localhost";
} else {
this.host = host;
}
if (!port){
this.port = 6600;
} else {
this.port = port;
}
if (typeof autoreopen === 'undefined') {
this.autoreopen = true;
} else {
this.autoreopen = autoreopen;
}
this.callbacks = [];
this.commands = [];
this.buffer = '';
this.lines = [];
this.isOpen = false;
this.socket = null;
this.version = undefined;
this.open(this.host,this.port);
}
mpdSocket.prototype = {
bufferData: function(data) {
this.buffer += data;
var pos = 0;
var i;
while ((i = this.buffer.indexOf('\n', pos)) > -1) {
var line = this.buffer.substr(pos, i-pos);
this.handleLine(line);
pos = i+1; // the +1 skips the \n
}
if (pos < this.buffer.length) {
// incomplete line, save it for the next round
this.buffer = this.buffer.substr(pos);
} else {
this.buffer = '';
}
},
handleLine: function(line) {
if (!this.version && line.match(/^OK MPD /)) {
this.version = line.split(' ')[2];
//console.log('mpd protocol version ' + this.version);
return;
}
//console.log('<', line);
this.lines.push(line);
if (line.match(/^OK$|^ACK \[/)) {
this.handleLines(this.lines);
this.lines = [];
}
},
handleLines: function(lines) {
var response = [new Object];
var first_attr = false;
for (var i in lines) {
var line = lines[i];
if (line.match(/^OK$|^ACK \[/)) {
break;
} else {
var colon = line.indexOf(':');
var attr = line.substr(0,colon);
var value = line.substr(colon+1);
value = value.replace(/^\s+|\s+$/g, ''); // trim whitespace
if (typeof(response[response.length-1][attr]) !== 'undefined') {
if (attr === first_attr) {
// assume that when the first attribute we saw is repeated,
// it represents the start of a new object
response.push(new Object);
response[response.length-1][attr] = value;
} else {
// otherwise handle multiple occurances of an attribute as an array
response[response.length-1][attr] = [].concat(
response[response.length-1][attr],
value
); // [].concat() will convert it to an array if it's not already one
}
} else {
response[response.length-1][attr] = value;
if (!first_attr) {
first_attr = attr;
}
}
}
}
// uncomment this next section if you want the responses
// to be (more) compatiable with old versions of mpdsocket
// otherwise all responses are returned in a list
// // convert single line responses to not be in a 1 item list
// // except playlist, file, and directory are always lists
// if (["playlist", "file", "directory"].indexOf(first_attr) === -1) {
// if (response.length === 1) {
// response = response[0];
// }
// }
response._OK = (line === 'OK');
if (!response._OK) {
//console.log(line);
if (line) {
response._error = line.substr(line.indexOf('}') +2);
}
}
return this.callbacks.shift()(response);
},
on: function(event, fn) {
this.socket.on(event,fn);
},
open: function(host,port) {
var self = this;
if (!(this.isOpen)) {
if (host.indexOf('/') === 0) {
// support unix domain sockets
this.socket = net.createConnection(host);
} else {
this.socket = net.createConnection(port,host);
}
this.isOpen = true;
this.socket.setEncoding('UTF-8');
this.socket.addListener('data',function(data) { self.bufferData.call(self,data); self._send(); });
this.socket.addListener('end',function() {
self.isOpen = false;
if (!this.autoreopen) {
this.commands = [];
this.callbacks = [];
}
});
}
},
_send: function() {
if (this.commands.length != 0) this.socket.write(this.commands.shift() + "\n");
},
send: function(req,callback) {
//console.log('>', req);
if (this.isOpen) {
this.callbacks.push(callback);
this.commands.push(req);
if (this.commands.length == 1) this._send();
} else {
if (this.autoreopen) {
var self = this;
this.open(this.host,this.port);
this.on('connect',function() {
self.send(req,callback);
});
} else {
throw "mpdsocketNotOpenException";
}
}
}
};
module.exports = mpdSocket;