Created a simple remote control interface; refactored the code that

connects to the music server and subscribes to the DJObject.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1496 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2004-08-23 06:19:40 +00:00
parent d61977538d
commit 1f98a87829
3 changed files with 222 additions and 113 deletions
@@ -3,7 +3,6 @@
package robodj.chooser;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
@@ -19,40 +18,23 @@ import com.samskivert.util.ConfigUtil;
import com.samskivert.util.PropertiesUtil;
import com.samskivert.util.RunAnywhere;
import com.threerings.util.Name;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.ClientObserver;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.ObjectAccessException;
import com.threerings.presents.dobj.Subscriber;
import com.threerings.presents.net.UsernamePasswordCreds;
import robodj.Log;
import robodj.client.DJService;
import robodj.data.DJObject;
import robodj.repository.Model;
import robodj.repository.Repository;
import robodj.util.ErrorUtil;
import robodj.util.RDJPrefs;
import robodj.util.RDJPrefsPanel;
import robodj.util.ServerControl;
/**
* The chooser is the GUI-based application for browsing the music
* collection and managing the playlist.
*/
public class Chooser
implements Client.Invoker, ClientObserver, Subscriber
public class Chooser extends DJClient
{
public static Repository repository;
public static Model model;
public static Client client;
public static DJObject djobj;
public static ChooserFrame frame;
public void init ()
@@ -93,25 +75,7 @@ public class Chooser
continue;
}
try {
String host = RDJPrefs.getMusicDaemonHost();
if (StringUtil.blank(host)) {
throw new IOException(
"No music server host specified in configuration.");
}
// establish a connection with the music server
Name name = new Name(RDJPrefs.getUser());
client = new Client(new UsernamePasswordCreds(name, ""), this);
client.addClientObserver(this);
client.setServer(host, Client.DEFAULT_SERVER_PORT);
client.logon();
} catch (IOException ioe) {
String errmsg = "Unable to communicate with music " +
"server on host '" + RDJPrefs.getMusicDaemonHost() + "' ";
if (ErrorUtil.reportError(errmsg, ioe)) {
System.exit(-1);
}
if (!connectToServer()) {
continue;
}
@@ -119,86 +83,18 @@ public class Chooser
}
}
// documentation inherited from interface
public void clientDidLogon (Client lclient)
{
// subscribe to our DJObject
DJService djsvc = (DJService)client.requireService(DJService.class);
djsvc.getDJOid(client, new DJService.ResultListener() {
public void requestProcessed (Object result) {
int djoid = ((Integer)result).intValue();
client.getDObjectManager().subscribeToObject(
djoid, Chooser.this);
}
public void requestFailed (String cause) {
ErrorUtil.reportError("Failed to fetch DJ oid: " + cause, null);
System.exit(-1);
}
});
}
// documentation inherited from interface
public void clientObjectDidChange (Client client)
{
}
// documentation inherited from interface
public void clientDidLogoff (Client client)
{
System.exit(-1);
}
// documentation inherited from interface
public void clientFailedToLogon (Client lclient, Exception cause)
{
if (ErrorUtil.reportError("Failed to connect to server", cause)) {
System.exit(-1);
} else {
Thread t = new Thread() {
public void run () {
RDJPrefsPanel.display(true);
client.setServer(RDJPrefs.getMusicDaemonHost(),
Client.DEFAULT_SERVER_PORT);
client.logon();
}
};
t.start();
}
}
// documentation inherited from interface
public void clientConnectionFailed (Client client, Exception cause)
{
ErrorUtil.reportError("Lost connection to server", cause);
System.exit(-1);
}
// documentation inherited from interface
public boolean clientWillLogoff (Client client)
{
return true;
}
// documentation inherited from interface
public void invokeLater (Runnable run)
{
SwingUtilities.invokeLater(run);
}
// documentation inherited from interface
public void objectAvailable (DObject object)
protected void didInit ()
{
djobj = (DJObject)object;
super.didInit();
displayInterface();
}
// documentation inherited from interface
public void requestFailed (int oid, ObjectAccessException cause)
{
ErrorUtil.reportError("Failed to fetch DJ object", cause);
System.exit(-1);
}
protected void displayInterface ()
{
if (frame == null) {
@@ -213,11 +109,6 @@ public class Chooser
}
}
public static void exit ()
{
client.logoff(false);
}
public static void main (String[] args)
{
Chooser chooser = new Chooser();
@@ -0,0 +1,155 @@
//
// $Id$
package robodj.chooser;
import java.io.IOException;
import com.samskivert.util.StringUtil;
import com.threerings.util.Name;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.ClientObserver;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.ObjectAccessException;
import com.threerings.presents.dobj.Subscriber;
import com.threerings.presents.net.UsernamePasswordCreds;
import robodj.client.DJService;
import robodj.data.DJObject;
import robodj.util.ErrorUtil;
import robodj.util.RDJPrefs;
import robodj.util.RDJPrefsPanel;
/**
* Handles the common client business for RoboDJ.
*/
public abstract class DJClient
implements Client.Invoker, ClientObserver, Subscriber
{
public static Client client;
public static DJObject djobj;
/**
* Creates our {@link Client} instance and connects to the RoboDJ
* server.
*
* @return true if the client was created successfully and the
* connection process was initiated, false if it was not due to
* missing or bogus configuration.
*/
public boolean connectToServer ()
{
try {
String host = RDJPrefs.getMusicDaemonHost();
if (StringUtil.blank(host)) {
throw new IOException(
"No music server host specified in configuration.");
}
// establish a connection with the music server
Name name = new Name(RDJPrefs.getUser());
client = new Client(new UsernamePasswordCreds(name, ""), this);
client.addClientObserver(this);
client.setServer(host, Client.DEFAULT_SERVER_PORT);
client.logon();
return true;
} catch (IOException ioe) {
String errmsg = "Unable to communicate with music " +
"server on host '" + RDJPrefs.getMusicDaemonHost() + "' ";
if (ErrorUtil.reportError(errmsg, ioe)) {
System.exit(-1);
}
return false;
}
}
// documentation inherited from interface
public void clientDidLogon (Client lclient)
{
// subscribe to our DJObject
DJService djsvc = (DJService)client.requireService(DJService.class);
djsvc.getDJOid(client, new DJService.ResultListener() {
public void requestProcessed (Object result) {
int djoid = ((Integer)result).intValue();
client.getDObjectManager().subscribeToObject(
djoid, DJClient.this);
}
public void requestFailed (String cause) {
ErrorUtil.reportError("Failed to fetch DJ oid: " + cause, null);
System.exit(-1);
}
});
}
// documentation inherited from interface
public void clientObjectDidChange (Client client)
{
}
// documentation inherited from interface
public void clientDidLogoff (Client client)
{
System.exit(-1);
}
// documentation inherited from interface
public void clientFailedToLogon (Client lclient, Exception cause)
{
if (ErrorUtil.reportError("Failed to connect to server", cause)) {
System.exit(-1);
} else {
Thread t = new Thread() {
public void run () {
RDJPrefsPanel.display(true);
client.setServer(RDJPrefs.getMusicDaemonHost(),
Client.DEFAULT_SERVER_PORT);
client.logon();
}
};
t.start();
}
}
// documentation inherited from interface
public void clientConnectionFailed (Client client, Exception cause)
{
ErrorUtil.reportError("Lost connection to server", cause);
System.exit(-1);
}
// documentation inherited from interface
public boolean clientWillLogoff (Client client)
{
return true;
}
// documentation inherited from interface
public void objectAvailable (DObject object)
{
djobj = (DJObject)object;
didInit();
}
// documentation inherited from interface
public void requestFailed (int oid, ObjectAccessException cause)
{
ErrorUtil.reportError("Failed to fetch DJ object", cause);
System.exit(-1);
}
/**
* Called when we have successfully connected to the server and
* subscribed to our {@link DJObject}.
*/
protected void didInit ()
{
}
public static void exit ()
{
client.logoff(false);
}
}
@@ -0,0 +1,63 @@
//
// $Id$
package robodj.chooser;
import com.samskivert.util.Queue;
/**
* Provides the ability to perform some basic RoboDJ commands from the
* command line.
*/
public class Remote extends DJClient
{
public void invoke (String command)
{
_command = command;
connectToServer();
}
protected void didInit ()
{
super.didInit();
if (_command.equals("play")) {
djobj.play();
} else if (_command.equals("pause")) {
djobj.pause();
} else if (_command.equals("stop")) {
djobj.stop();
} else {
System.err.println("Unknown command: " + _command);
}
exit();
}
// documentation inherited from interface
public void invokeLater (Runnable run)
{
_queue.append(run);
}
public static void main (String[] args)
{
if (args.length == 0) {
System.out.println("Usage: Remote [play|pause|stop]");
System.exit(-1);
}
Remote remote = new Remote();
remote.invoke(args[0]);
Runnable run;
while ((run = (Runnable)_queue.get()) != null) {
run.run();
}
}
protected String _command;
protected static Queue _queue = new Queue();
}