forked from sdubois/JazzMPC
/
mpdsocket.js
124 lines (113 loc) · 3.05 KB
/
mpdsocket.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
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
/*******
** 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) {
if (!host) {
this.host = "localhost";
} else {
this.host = host;
}
if (!port){
this.port = 6600;
} else {
this.port = port;
}
this.open(this.host,this.port);
}
mpdSocket.prototype = {
callbacks: [],
commands: [],
i: 0,
response: {},
isOpen: false,
socket: null,
version: "0",
host: null,
port: null,
handleData: function(data) {
var lines = data.split("\n");
for (var l in lines) {
if (lines[l].match(/^ACK/)) {
this.response._error = lines[l].substr(10);
this.response._OK = false;
this.callbacks.shift()(this.response)
this.response = {};
return;
} else if (lines[l].match(/^OK MPD/)) {
if (this.version == "0") {
this.version = lines[l].split(' ')[2];
return;
}
} else if (lines[l].match(/^OK$/)) {
this.response._OK = true;
this.i = 0;
this.callbacks.shift()(this.response);
this.response = {};
return;
} else {
var attr = lines[l].substr(0,lines[l].indexOf(":"));
var value = lines[l].substr((lines[l].indexOf(":"))+1);
value = value.replace(/^\s+|\s+$/g, ''); // trim whitespace
if (this.response._ordered_list != true) {
if (typeof(this.response[attr]) != 'undefined') {
//make ordered list
var tempResponse = new Object;
tempResponse[++(this.i)] = this.response;
this.response = tempResponse;
this.response._ordered_list = true;
this.response[++(this.i)] = new Object;
this.response[this.i][attr] = value;
} else {
this.response[attr] = value;
}
} else {
if (typeof(this.response[(this.i)][attr]) != 'undefined' || attr == "playlist" || attr == "file" || attr == "directory") {
this.response[++(this.i)] = new Object;
this.response[this.i][attr] = value;
} else {
this.response[this.i][attr] = value;
}
}
}
}
},
on: function(event, fn) {
this.socket.on(event,fn);
},
open: function(host,port) {
var self = this;
if (!(this.isOpen)) {
this.socket = net.createConnection(port,host);
this.socket.setEncoding('UTF-8');
this.socket.addListener('connect',function() { self.isOpen = true; });
this.socket.addListener('data',function(data) { self.handleData.call(self,data); self._send(); });
this.socket.addListener('end',function() { self.isOpen = false; });
}
},
_send: function() {
if (this.commands.length != 0) this.socket.write(this.commands.shift() + "\n");
},
send: function(req,callback) {
if (this.isOpen) {
this.callbacks.push(callback);
this.commands.push(req);
if (this.commands.length == 1) this._send();
} else {
var self = this;
this.open(this.host,this.port);
this.on('connect',function() {
self.send(req,callback);
});
}
}
}
module.exports = mpdSocket;