Mercurial > eldonilo > mpd
comparison lib.js @ 0:363600705376 default tip
Initial commit.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 23 Aug 2011 18:43:15 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:363600705376 |
---|---|
1 var net = require('net'); | |
2 var sys = require('sys'); | |
3 | |
4 function MPD(server, port) { | |
5 if (!port) port = '6600'; | |
6 if (!server) server = '::'; | |
7 | |
8 this.connect(port, server); | |
9 } | |
10 | |
11 MPD.prototype = { | |
12 stream: null, | |
13 is_connected: false, | |
14 updateTimer: null, | |
15 | |
16 status: {}, | |
17 cs: {}, | |
18 | |
19 _callbacks: {}, | |
20 | |
21 _callback: function() { | |
22 this._callback = null; | |
23 }, | |
24 | |
25 close: function() { | |
26 this.stream.end(); | |
27 this.is_connected = false; | |
28 }, | |
29 | |
30 connect: function(port, server) { | |
31 if (this.is_connected == true) { | |
32 this.close(); | |
33 } | |
34 | |
35 this.stream = net.createConnection(port, server); | |
36 | |
37 this.stream.setEncoding('utf8'); | |
38 | |
39 this.stream.on('connect', this.connected()); | |
40 this.stream.on('data', this.data()); | |
41 this.stream.on('close', this.closed()); | |
42 | |
43 this.stream.on('end', function() { | |
44 this.end(); | |
45 }); | |
46 }, | |
47 | |
48 connected: function() { | |
49 var self = this; | |
50 | |
51 return function() { | |
52 self.is_connected = true; | |
53 if (self.debug) console.log('MPD connection opened'); | |
54 | |
55 /*self.updateTimer = setInterval(function() { | |
56 if (self.is_connected == true) { | |
57 self.send('currentsong', function(cs) { | |
58 for (var v in cs) { | |
59 if (self.cs[v] != cs[v]) { | |
60 self.callback(v, cs[v]); | |
61 self.cs[v] = cs[v]; | |
62 } | |
63 } | |
64 }); | |
65 | |
66 self.send('status', function(status) { | |
67 for (var v in status) { | |
68 if (self.status[v] != status[v]) { | |
69 self.callback(v, status[v]); | |
70 self.status[v] = status[v]; | |
71 } | |
72 } | |
73 }); | |
74 } else { | |
75 clearInterval(self.updateTimer); | |
76 } | |
77 }, 1000);*/ | |
78 | |
79 self.callback('connect'); | |
80 }; | |
81 }, | |
82 | |
83 data: function() { | |
84 var self = this; | |
85 | |
86 var packet = []; | |
87 | |
88 return function(data) { | |
89 var data = data.toString(); | |
90 var commands = data.split('\n'); | |
91 | |
92 for (var i = 0; i < commands.length; i++) { | |
93 if (commands[i].length >= 2) { | |
94 packet.push(commands[i]); | |
95 | |
96 var command = commands[i].split(' '); | |
97 | |
98 switch (command[0]) { | |
99 case 'ACK': | |
100 case 'OK': | |
101 if (command[1] == 'MPD') { | |
102 self.version = command[2]; | |
103 } else { | |
104 self._callback.call(self, self._parsePacket(packet)); | |
105 packet = []; | |
106 this._callback = null; | |
107 } | |
108 break; | |
109 } | |
110 } | |
111 } | |
112 }; | |
113 }, | |
114 | |
115 closed: function() { | |
116 var self = this; | |
117 | |
118 return function(had_error) { | |
119 if (self.debug) console.log('MPD connection closed'); | |
120 self.callback('close'); | |
121 }; | |
122 }, | |
123 | |
124 _parsePacket: function(packet) { | |
125 var p = {}; | |
126 | |
127 for (var i = 0; i < packet.length; i++) { | |
128 var regx = /^(\w+):\s?(.*)$/i; | |
129 var result = regx.exec(packet[i]); | |
130 | |
131 if (result !== null) { | |
132 p[result[1]] = result[2]; | |
133 } | |
134 } | |
135 | |
136 return p; | |
137 }, | |
138 | |
139 send: function(str, cb) { | |
140 this._callback = cb; | |
141 this.stream.write(str + '\n'); | |
142 }, | |
143 | |
144 on: function(type, cb) { | |
145 if (!this._callbacks[type]) this._callbacks[type] = []; | |
146 | |
147 this._callbacks[type].push(cb); | |
148 }, | |
149 | |
150 callback: function(type, data) { | |
151 if (this._callbacks[type]) { | |
152 for (var c = 0; c < this._callbacks[type].length; c++) { | |
153 this._callbacks[type][c].call(this, data); | |
154 } | |
155 } | |
156 } | |
157 }; | |
158 | |
159 module.exports = MPD; |