Discover computers running GFE

This commit is contained in:
Iwan Timmer
2014-08-16 15:22:18 +02:00
parent 149be52bbc
commit 0b014f1fe8

View File

@@ -21,6 +21,9 @@ import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.limelight.nvstream.http.PairingManager; import com.limelight.nvstream.http.PairingManager;
import com.limelight.nvstream.mdns.MdnsComputer;
import com.limelight.nvstream.mdns.MdnsDiscoveryAgent;
import com.limelight.nvstream.mdns.MdnsDiscoveryListener;
/** /**
* Main class for Limelight Pi * Main class for Limelight Pi
@@ -30,19 +33,22 @@ import com.limelight.nvstream.http.PairingManager;
*/ */
public class Limelight implements NvConnectionListener { public class Limelight implements NvConnectionListener {
private String host; private InetAddress host;
private NvConnection conn; private NvConnection conn;
private boolean connectionTerminating; private boolean connectionTerminating;
private Logger logger; private Logger logger;
public Limelight() {
}
/** /**
* Constructs a new instance based on the given host * Constructs a new instance based on the given host
* @param host can be hostname or IP address. * @param host can be hostname or IP address.
*/ */
public Limelight(String host) { public Limelight(InetAddress host) {
this.host = host; this.host = host;
} }
/* /*
* Creates a connection to the host and starts up the stream. * Creates a connection to the host and starts up the stream.
*/ */
@@ -68,7 +74,7 @@ public class Limelight implements NvConnectionListener {
} }
} }
conn = new NvConnection(host, "Pi", this, streamConfig, PlatformBinding.getCryptoProvider()); conn = new NvConnection(host.getHostAddress(), "Pi", this, streamConfig, PlatformBinding.getCryptoProvider());
GamepadMapping mapping = null; GamepadMapping mapping = null;
if (mappingFile!=null) { if (mappingFile!=null) {
@@ -102,7 +108,7 @@ public class Limelight implements NvConnectionListener {
* Creates a connection to the host and starts up the stream. * Creates a connection to the host and starts up the stream.
*/ */
private void startUpFake(StreamConfiguration streamConfig, String videoFile) { private void startUpFake(StreamConfiguration streamConfig, String videoFile) {
conn = new NvConnection(host, "Pi", this, streamConfig, PlatformBinding.getCryptoProvider()); conn = new NvConnection(host.getHostAddress(), "Pi", this, streamConfig, PlatformBinding.getCryptoProvider());
conn.start(PlatformBinding.getDeviceName(), null, conn.start(PlatformBinding.getDeviceName(), null,
VideoDecoderRenderer.FLAG_PREFER_QUALITY, VideoDecoderRenderer.FLAG_PREFER_QUALITY,
new FakeAudioRenderer(), new FakeAudioRenderer(),
@@ -115,33 +121,29 @@ public class Limelight implements NvConnectionListener {
private void pair() { private void pair() {
NvHTTP httpConn; NvHTTP httpConn;
httpConn = new NvHTTP(host,
"Pi", PlatformBinding.getDeviceName(), PlatformBinding.getCryptoProvider());
try { try {
httpConn = new NvHTTP(InetAddress.getByName(host), if (httpConn.getPairState() == PairingManager.PairState.PAIRED) {
"Pi", PlatformBinding.getDeviceName(), PlatformBinding.getCryptoProvider()); displayError("pair", "Already paired");
try { } else {
if (httpConn.getPairState() == PairingManager.PairState.PAIRED) { final String pinStr = PairingManager.generatePinString();
displayError("pair", "Already paired");
} else { displayMessage("Please enter the following PIN on the target PC: "+pinStr);
final String pinStr = PairingManager.generatePinString();
PairingManager.PairState pairState = httpConn.pair(pinStr);
displayMessage("Please enter the following PIN on the target PC: "+pinStr); if (pairState == PairingManager.PairState.PIN_WRONG) {
displayError("pair", "Incorrect PIN");
PairingManager.PairState pairState = httpConn.pair(pinStr); }
if (pairState == PairingManager.PairState.PIN_WRONG) { else if (pairState == PairingManager.PairState.FAILED) {
displayError("pair", "Incorrect PIN"); displayError("pair", "Pairing failed");
} }
else if (pairState == PairingManager.PairState.FAILED) { else if (pairState == PairingManager.PairState.PAIRED) {
displayError("pair", "Pairing failed"); displayError("pair", "Paired successfully");
}
else if (pairState == PairingManager.PairState.PAIRED) {
displayError("pair", "Paired successfully");
}
} }
} catch (Exception e) {
displayError("Pair", e.getMessage());
} }
} catch (UnknownHostException e1) { } catch (Exception e) {
displayError("Pair", "Failed to resolve host"); displayError("Pair", e.getMessage());
} }
} }
@@ -151,7 +153,7 @@ public class Limelight implements NvConnectionListener {
* @param args unused. * @param args unused.
*/ */
public static void main(String args[]) { public static void main(String args[]) {
String host = null; InetAddress host = null;
List<String> inputs = new ArrayList<String>(); List<String> inputs = new ArrayList<String>();
int width = 1280; int width = 1280;
int height = 720; int height = 720;
@@ -270,12 +272,17 @@ public class Limelight implements NvConnectionListener {
parse = false; parse = false;
} else if (action == null) { } else if (action == null) {
action = args[i].toLowerCase(); action = args[i].toLowerCase();
if (!action.equals("stream") || !action.equals("pair") || !action.equals("fake") || !action.equals("help")) { if (!action.equals("stream") && !action.equals("pair") && !action.equals("fake") && !action.equals("help") && !action.equals("discover")) {
System.out.println("Syntax error: invalid action specified"); System.out.println("Syntax error: invalid action specified");
System.exit(3); System.exit(3);
} }
} else if (host == null) { } else if (host == null) {
host = args[i]; try {
host = InetAddress.getByName(args[i]);
} catch (UnknownHostException ex) {
System.out.println("Failed to resolve host");
System.exit(3);
}
} else { } else {
System.out.println("Syntax Error: Unrecognized argument: " + args[i]); System.out.println("Syntax Error: Unrecognized argument: " + args[i]);
parse = false; parse = false;
@@ -285,7 +292,7 @@ public class Limelight implements NvConnectionListener {
if (action == null) { if (action == null) {
System.out.println("Syntax Error: Missing required action argument"); System.out.println("Syntax Error: Missing required action argument");
parse = false; parse = false;
} else if (action == "help") } else if (action.equals("help"))
parse = false; parse = false;
if (args.length == 0 || !parse) { if (args.length == 0 || !parse) {
@@ -295,6 +302,7 @@ public class Limelight implements NvConnectionListener {
System.out.println(); System.out.println();
System.out.println("\tpair\t\t\tPair device with computer"); System.out.println("\tpair\t\t\tPair device with computer");
System.out.println("\tstream\t\t\tStream computer to device"); System.out.println("\tstream\t\t\tStream computer to device");
System.out.println("\tdiscover\t\tList available computers");
System.out.println("\thelp\t\t\tShow this help"); System.out.println("\thelp\t\t\tShow this help");
System.out.println(); System.out.println();
System.out.println(" Streaming options:"); System.out.println(" Streaming options:");
@@ -316,13 +324,13 @@ public class Limelight implements NvConnectionListener {
System.out.println("Use ctrl-c to exit application"); System.out.println("Use ctrl-c to exit application");
System.exit(5); System.exit(5);
} }
if (host == null) {
System.out.println("Syntax Error: Missing required host argument");
System.exit(3);
}
Limelight limelight = new Limelight(host); Limelight limelight;
if (host == null || action.equals("discover")) {
limelight = new Limelight();
limelight.discover(!action.equals("discover"));
} else
limelight = new Limelight(host);
//Set debugging level //Set debugging level
limelight.setLevel(debug); limelight.setLevel(debug);
@@ -337,6 +345,34 @@ public class Limelight implements NvConnectionListener {
} else if (action.equals("pair")) } else if (action.equals("pair"))
limelight.pair(); limelight.pair();
} }
public void discover(final boolean first) {
final Object mutex = new Object();
new MdnsDiscoveryAgent(new MdnsDiscoveryListener() {
@Override
public void notifyComputerAdded(MdnsComputer computer) {
displayMessage("Discovered " + computer.getName() + " " + computer.getAddress().getHostAddress());
host = computer.getAddress();
if (first)
synchronized (mutex) {
mutex.notify();
}
}
@Override
public void notifyComputerRemoved(MdnsComputer computer) {
}
@Override
public void notifyDiscoveryFailure(Exception e) {
}
});
synchronized (mutex) {
try {
mutex.wait();
} catch (InterruptedException ex) { }
}
}
public void setLevel(Level level) { public void setLevel(Level level) {
if (logger==null) if (logger==null)