Better XML parsing in NvHTTP

Cleans up some unnecessary code in `NvHTTP` and DRY's it up a bit.
Adds in a `getAppList` function which returns a list of `NvApp`, a class
used to represent applications. This is then used by `getSteamAppId` so
it actually opens up Steam instead of whatever the first app is.
This commit is contained in:
Aaron Neyer 2013-11-04 03:03:25 -05:00
parent e50f668aaf
commit f179010c7e
2 changed files with 134 additions and 65 deletions

View File

@ -0,0 +1,31 @@
package com.limelight.nvstream;
public class NvApp {
private String appName;
private int appId;
private boolean isRunning;
public void setAppName(String appName) {
this.appName = appName;
}
public void setAppId(String appId) {
this.appId = Integer.parseInt(appId);
}
public void setIsRunning(String isRunning) {
this.isRunning = isRunning.equals("1");
}
public String getAppName() {
return this.appName;
}
public int getAppId() {
return this.appId;
}
public boolean getIsRunning() {
return this.isRunning;
}
}

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.LinkedList;
import java.util.Stack; import java.util.Stack;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -11,19 +12,18 @@ import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
public class NvHTTP { public class NvHTTP {
private String host;
private String macAddress; private String macAddress;
public static final int PORT = 47989; public static final int PORT = 47989;
public String baseUrl;
public NvHTTP(String host, String macAddress) public NvHTTP(String host, String macAddress) {
{
this.host = host;
this.macAddress = macAddress; this.macAddress = macAddress;
this.baseUrl = "http://" + host + ":" + PORT;
} }
private String getXmlString(InputStream in, String attribute) throws XmlPullParserException, IOException private String getXmlString(InputStream in, String tagname)
{ throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser(); XmlPullParser xpp = factory.newPullParser();
@ -32,21 +32,19 @@ public class NvHTTP {
int eventType = xpp.getEventType(); int eventType = xpp.getEventType();
Stack<String> currentTag = new Stack<String>(); Stack<String> currentTag = new Stack<String>();
while (eventType != XmlPullParser.END_DOCUMENT) while (eventType != XmlPullParser.END_DOCUMENT) {
{ switch (eventType) {
if (eventType == XmlPullParser.START_TAG) { case (XmlPullParser.START_TAG):
currentTag.push(xpp.getName()); currentTag.push(xpp.getName());
for (int i = 0; i < xpp.getAttributeCount(); i++) break;
{ case (XmlPullParser.END_TAG):
if (xpp.getAttributeName(i).equals(attribute))
return xpp.getAttributeValue(i);
}
} else if (eventType == XmlPullParser.END_TAG) {
currentTag.pop(); currentTag.pop();
} else if (eventType == XmlPullParser.TEXT) { break;
if (currentTag.peek().equals(attribute)) { case (XmlPullParser.TEXT):
if (currentTag.peek().equals(tagname)) {
return xpp.getText(); return xpp.getText();
} }
break;
} }
eventType = xpp.next(); eventType = xpp.next();
} }
@ -54,46 +52,86 @@ public class NvHTTP {
return null; return null;
} }
private InputStream openHttpConnection(String url) throws IOException private InputStream openHttpConnection(String url) throws IOException {
{
return new URL(url).openConnection().getInputStream(); return new URL(url).openConnection().getInputStream();
} }
public String getAppVersion() throws XmlPullParserException, IOException public String getAppVersion() throws XmlPullParserException, IOException {
{ InputStream in = openHttpConnection(baseUrl + "/appversion");
InputStream in = openHttpConnection("http://"+host+":"+PORT+"/appversion");
return getXmlString(in, "appversion"); return getXmlString(in, "appversion");
} }
public boolean getPairState() throws IOException, XmlPullParserException public boolean getPairState() throws IOException, XmlPullParserException {
{ InputStream in = openHttpConnection(baseUrl + "/pairstate?mac=" + macAddress);
InputStream in = openHttpConnection("http://"+host+":"+PORT+"/pairstate?mac="+macAddress);
String paired = getXmlString(in, "paired"); String paired = getXmlString(in, "paired");
return Integer.valueOf(paired) != 0; return Integer.valueOf(paired) != 0;
} }
public int getSessionId() throws IOException, XmlPullParserException public int getSessionId() throws IOException, XmlPullParserException {
{
/* Pass the model (minus spaces) as the device name */ /* Pass the model (minus spaces) as the device name */
String deviceName = android.os.Build.MODEL; String deviceName = android.os.Build.MODEL;
deviceName = deviceName.replace(" ", ""); deviceName = deviceName.replace(" ", "");
InputStream in = openHttpConnection("http://"+host+":"+PORT+"/pair?mac="+macAddress+"&devicename="+deviceName); InputStream in = openHttpConnection(baseUrl + "/pair?mac=" + macAddress
+ "&devicename=" + deviceName);
String sessionId = getXmlString(in, "sessionid"); String sessionId = getXmlString(in, "sessionid");
return Integer.valueOf(sessionId); return Integer.parseInt(sessionId);
} }
public int getSteamAppId(int sessionId) throws IOException, XmlPullParserException public int getSteamAppId(int sessionId) throws IOException,
{ XmlPullParserException {
InputStream in = openHttpConnection("http://"+host+":"+PORT+"/applist?session="+sessionId); LinkedList<NvApp> appList = getAppList(sessionId);
String appId = getXmlString(in, "ID"); for (NvApp app : appList) {
return Integer.valueOf(appId); if (app.getAppName().equals("Steam")) {
return app.getAppId();
}
}
return 0;
}
public LinkedList<NvApp> getAppList(int sessionId) throws IOException, XmlPullParserException {
InputStream in = openHttpConnection(baseUrl + "/applist?session=" + sessionId);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new InputStreamReader(in));
int eventType = xpp.getEventType();
LinkedList<NvApp> appList = new LinkedList<NvApp>();
Stack<String> currentTag = new Stack<String>();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case (XmlPullParser.START_TAG):
currentTag.push(xpp.getName());
if (xpp.getName().equals("App")) {
appList.addLast(new NvApp());
}
break;
case (XmlPullParser.END_TAG):
currentTag.pop();
break;
case (XmlPullParser.TEXT):
NvApp app = appList.getLast();
if (currentTag.peek().equals("AppTitle")) {
app.setAppName(xpp.getText());
} else if (currentTag.peek().equals("ID")) {
app.setAppId(xpp.getText());
} else if (currentTag.peek().equals("IsRunning")) {
app.setIsRunning(xpp.getText());
}
break;
}
eventType = xpp.next();
}
return appList;
} }
// Returns gameSession XML attribute // Returns gameSession XML attribute
public int launchApp(int sessionId, int appId) throws IOException, XmlPullParserException public int launchApp(int sessionId, int appId) throws IOException,
{ XmlPullParserException {
InputStream in = openHttpConnection("http://"+host+":"+PORT+"/launch?session="+sessionId+"&appid="+appId); InputStream in = openHttpConnection(baseUrl + "/launch?session="
+ sessionId + "&appid=" + appId);
String gameSession = getXmlString(in, "gamesession"); String gameSession = getXmlString(in, "gamesession");
return Integer.valueOf(gameSession); return Integer.parseInt(gameSession);
} }
} }