Added mechanism for reporting the "playing" state of the music server.

Automatically query the playing state after requesting a command that is
likely to modify it.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@632 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-03-03 17:21:29 +00:00
parent ae0514ad79
commit 3f17073fec
@@ -1,5 +1,5 @@
// //
// $Id: ServerControl.java,v 1.4 2002/02/22 08:37:34 mdb Exp $ // $Id: ServerControl.java,v 1.5 2002/03/03 17:21:29 mdb Exp $
package robodj.util; package robodj.util;
@@ -7,6 +7,9 @@ import java.io.*;
import java.net.*; import java.net.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import javax.swing.SwingUtilities;
import com.samskivert.util.StringUtil;
import robodj.Log; import robodj.Log;
@@ -15,7 +18,24 @@ import robodj.Log;
* network interface. * network interface.
*/ */
public class ServerControl public class ServerControl
implements Runnable
{ {
/**
* Used to report changes to the playing song.
*/
public static interface PlayingListener
{
/**
* Called when the playing song is known to have changed to the
* specified songid.
*
* @param songid the id of the song that's playing or -1 if
* nothing is playing.
* @param paused true if the music daemon is paused.
*/
public void playingUpdated (int songid, boolean paused);
}
public ServerControl (String host, int port) public ServerControl (String host, int port)
throws IOException throws IOException
{ {
@@ -27,19 +47,32 @@ public class ServerControl
_out = new PrintWriter(_conn.getOutputStream()); _out = new PrintWriter(_conn.getOutputStream());
} }
public void addPlayingListener (PlayingListener listener)
{
_listeners.add(listener);
}
public void removePlayingListener (PlayingListener listener)
{
_listeners.remove(listener);
}
public void pause () public void pause ()
{ {
sendCommand("PAUSE"); sendCommand("PAUSE");
refreshPlaying();
} }
public void play () public void play ()
{ {
sendCommand("PLAY"); sendCommand("PLAY");
refreshPlaying();
} }
public void stop () public void stop ()
{ {
sendCommand("STOP"); sendCommand("STOP");
refreshPlaying();
} }
public void clear () public void clear ()
@@ -47,9 +80,16 @@ public class ServerControl
sendCommand("CLEAR"); sendCommand("CLEAR");
} }
public void back ()
{
sendCommand("BACK");
refreshPlaying();
}
public void skip () public void skip ()
{ {
sendCommand("SKIP"); sendCommand("SKIP");
refreshPlaying();
} }
public void append (int eid, int sid, String trackPath) public void append (int eid, int sid, String trackPath)
@@ -60,21 +100,48 @@ public class ServerControl
public void remove (int sid) public void remove (int sid)
{ {
sendCommand("REMOVE " + sid); sendCommand("REMOVE " + sid);
refreshPlaying();
} }
public void removeGroup (int sid, int count) public void removeGroup (int sid, int count)
{ {
sendCommand("REMOVEGRP " + sid + " " + count); sendCommand("REMOVEGRP " + sid + " " + count);
refreshPlaying();
} }
public void skipto (int sid) public void skipto (int sid)
{ {
sendCommand("SKIPTO " + sid); sendCommand("SKIPTO " + sid);
refreshPlaying();
} }
public String getPlaying () public int getPlaying ()
{ {
return sendCommand("PLAYING"); refreshPlaying();
return _playingSongId;
}
public void refreshPlaying ()
{
String playing = sendCommand("PLAYING");
// figure out if we're paused
_paused = (playing.indexOf("paused") != -1);
// figure out what song is playing
playing = StringUtil.split(playing, ":")[1].trim();
_playingSongId = -1;
if (!playing.equals("<none>")) {
try {
_playingSongId = Integer.parseInt(playing);
} catch (NumberFormatException nfe) {
Log.warning("Unable to parse currently playing id '" +
playing + "'.");
}
}
// let our listeners know about the new info
SwingUtilities.invokeLater(this);
} }
public String[] getPlaylist () public String[] getPlaylist ()
@@ -114,6 +181,22 @@ public class ServerControl
return plist; return plist;
} }
public void run ()
{
// notify our playing listeners
for (int i = 0; i < _listeners.size(); i++) {
PlayingListener listener = (PlayingListener)_listeners.get(i);
try {
listener.playingUpdated(_playingSongId, _paused);
} catch (Exception e) {
Log.warning("PlayingListener choked during update " +
"[listener=" + listener +
", songid=" + _playingSongId + "].");
Log.logStackTrace(e);
}
}
}
protected String sendCommand (String command) protected String sendCommand (String command)
{ {
try { try {
@@ -133,4 +216,14 @@ public class ServerControl
protected Socket _conn; protected Socket _conn;
protected PrintWriter _out; protected PrintWriter _out;
protected BufferedReader _in; protected BufferedReader _in;
/** A list of entities that are informed when the playing song
* changes. */
protected ArrayList _listeners = new ArrayList();
/** The most recently fetched playing song id. */
protected int _playingSongId;
/** The most recently fetched paused state. */
protected boolean _paused;
} }