mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2026-04-23 16:56:41 +00:00
Commit of common limelight core with bindings based on HEAD of RenderScript-Renderer
This commit is contained in:
31
moonlight-common/src/com/limelight/nvstream/http/NvApp.java
Normal file
31
moonlight-common/src/com/limelight/nvstream/http/NvApp.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.limelight.nvstream.http;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
145
moonlight-common/src/com/limelight/nvstream/http/NvHTTP.java
Normal file
145
moonlight-common/src/com/limelight/nvstream/http/NvHTTP.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package com.limelight.nvstream.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
import org.xmlpull.v1.XmlPullParserFactory;
|
||||
|
||||
|
||||
public class NvHTTP {
|
||||
private String macAddress;
|
||||
private String deviceName;
|
||||
|
||||
public static final int PORT = 47989;
|
||||
public static final int CONNECTION_TIMEOUT = 5000;
|
||||
|
||||
public String baseUrl;
|
||||
|
||||
public NvHTTP(InetAddress host, String macAddress, String deviceName) {
|
||||
this.macAddress = macAddress;
|
||||
this.deviceName = deviceName;
|
||||
this.baseUrl = "http://" + host.getHostAddress() + ":" + PORT;
|
||||
}
|
||||
|
||||
private String getXmlString(InputStream in, String tagname)
|
||||
throws XmlPullParserException, IOException {
|
||||
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
|
||||
factory.setNamespaceAware(true);
|
||||
XmlPullParser xpp = factory.newPullParser();
|
||||
|
||||
xpp.setInput(new InputStreamReader(in));
|
||||
int eventType = xpp.getEventType();
|
||||
Stack<String> currentTag = new Stack<String>();
|
||||
|
||||
while (eventType != XmlPullParser.END_DOCUMENT) {
|
||||
switch (eventType) {
|
||||
case (XmlPullParser.START_TAG):
|
||||
currentTag.push(xpp.getName());
|
||||
break;
|
||||
case (XmlPullParser.END_TAG):
|
||||
currentTag.pop();
|
||||
break;
|
||||
case (XmlPullParser.TEXT):
|
||||
if (currentTag.peek().equals(tagname)) {
|
||||
return xpp.getText();
|
||||
}
|
||||
break;
|
||||
}
|
||||
eventType = xpp.next();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private InputStream openHttpConnection(String url) throws IOException {
|
||||
URLConnection conn = new URL(url).openConnection();
|
||||
conn.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||
conn.setDefaultUseCaches(false);
|
||||
conn.connect();
|
||||
return conn.getInputStream();
|
||||
}
|
||||
|
||||
public String getAppVersion() throws XmlPullParserException, IOException {
|
||||
InputStream in = openHttpConnection(baseUrl + "/appversion");
|
||||
return getXmlString(in, "appversion");
|
||||
}
|
||||
|
||||
public boolean getPairState() throws IOException, XmlPullParserException {
|
||||
InputStream in = openHttpConnection(baseUrl + "/pairstate?mac=" + macAddress);
|
||||
String paired = getXmlString(in, "paired");
|
||||
return Integer.valueOf(paired) != 0;
|
||||
}
|
||||
|
||||
public int getSessionId() throws IOException, XmlPullParserException {
|
||||
InputStream in = openHttpConnection(baseUrl + "/pair?mac=" + macAddress
|
||||
+ "&devicename=" + deviceName);
|
||||
String sessionId = getXmlString(in, "sessionid");
|
||||
return Integer.parseInt(sessionId);
|
||||
}
|
||||
|
||||
public int getSteamAppId(int sessionId) throws IOException,
|
||||
XmlPullParserException {
|
||||
LinkedList<NvApp> appList = getAppList(sessionId);
|
||||
for (NvApp app : appList) {
|
||||
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
|
||||
public int launchApp(int sessionId, int appId) throws IOException,
|
||||
XmlPullParserException {
|
||||
InputStream in = openHttpConnection(baseUrl + "/launch?session="
|
||||
+ sessionId + "&appid=" + appId);
|
||||
String gameSession = getXmlString(in, "gamesession");
|
||||
return Integer.parseInt(gameSession);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user