diff --git a/nxtransport/jar/nxtransport.jar b/nxtransport/jar/nxtransport.jar index c859f30c..8d3496ac 100644 Binary files a/nxtransport/jar/nxtransport.jar and b/nxtransport/jar/nxtransport.jar differ diff --git a/nxtransport/src/es/virtualcable/nx/LinuxApplet.java b/nxtransport/src/es/virtualcable/nx/LinuxApplet.java index d85e504b..d57f6426 100644 --- a/nxtransport/src/es/virtualcable/nx/LinuxApplet.java +++ b/nxtransport/src/es/virtualcable/nx/LinuxApplet.java @@ -13,7 +13,7 @@ public class LinuxApplet implements OsApplet { private Hashtable params; private String tmpDir = ""; - // private String baseUrl = ""; + private String baseUrl = ""; private String nxFileName = ""; private String scrWidth; private String scrHeight; @@ -26,6 +26,9 @@ public class LinuxApplet implements OsApplet { String height = params.get("height"); boolean fullScreen = false; + // Notifies to broker the hostname/ip + util.notifyHostname(baseUrl, params.get("is")); + if( width.equals("-1")) { width = scrWidth; @@ -96,7 +99,7 @@ public class LinuxApplet implements OsApplet { public void setParameters(Hashtable parameters, String urlBase, int screenWidth, int screenHeight) { params = parameters; - // baseUrl = urlBase; + baseUrl = urlBase; scrWidth = Integer.toString(screenWidth); scrHeight = Integer.toString(screenHeight); } diff --git a/nxtransport/src/es/virtualcable/nx/MacApplet.java b/nxtransport/src/es/virtualcable/nx/MacApplet.java index 67891f36..36ce7092 100644 --- a/nxtransport/src/es/virtualcable/nx/MacApplet.java +++ b/nxtransport/src/es/virtualcable/nx/MacApplet.java @@ -13,7 +13,7 @@ public class MacApplet implements OsApplet { private Hashtable params; private String tmpDir = ""; - // private String baseUrl = ""; + private String baseUrl = ""; private String nxFileName = ""; private String scrWidth; private String scrHeight; @@ -26,6 +26,9 @@ public class MacApplet implements OsApplet { String height = params.get("height"); boolean fullScreen = false; + // Notifies to broker the hostname/ip + util.notifyHostname(baseUrl, params.get("is")); + if( width.equals("-1")) { width = scrWidth; @@ -96,7 +99,7 @@ public class MacApplet implements OsApplet { public void setParameters(Hashtable parameters, String urlBase, int screenWidth, int screenHeight) { params = parameters; - // baseUrl = urlBase; + baseUrl = urlBase; scrWidth = Integer.toString(screenWidth); scrHeight = Integer.toString(screenHeight); } diff --git a/nxtransport/src/es/virtualcable/nx/WindowsApplet.java b/nxtransport/src/es/virtualcable/nx/WindowsApplet.java index 2588cea2..75cf8bd6 100644 --- a/nxtransport/src/es/virtualcable/nx/WindowsApplet.java +++ b/nxtransport/src/es/virtualcable/nx/WindowsApplet.java @@ -11,7 +11,7 @@ public class WindowsApplet implements OsApplet { private Hashtable params; private String tmpDir = ""; - // private String baseUrl = ""; + private String baseUrl = ""; private String nxFileName = ""; private String scrWidth; private String scrHeight; @@ -19,7 +19,7 @@ public class WindowsApplet implements OsApplet { public void setParameters(Hashtable parameters, String urlBase, int screenWidth, int screenHeight) { params = parameters; - // baseUrl = urlBase; + baseUrl = urlBase; scrWidth = Integer.toString(screenWidth); scrHeight = Integer.toString(screenHeight); } @@ -34,6 +34,9 @@ public class WindowsApplet implements OsApplet { String height = params.get("height"); boolean fullScreen = false; + // Notifies to broker the hostname/ip + util.notifyHostname(baseUrl, params.get("is")); + if( width.equals("-1")) { width = scrWidth; diff --git a/nxtransport/src/es/virtualcable/nx/util.java b/nxtransport/src/es/virtualcable/nx/util.java new file mode 100644 index 00000000..83d5ed56 --- /dev/null +++ b/nxtransport/src/es/virtualcable/nx/util.java @@ -0,0 +1,98 @@ +package es.virtualcable.nx; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URLEncoder; + +public class util { + + public static boolean download(String baseUrl, String id, String outputFileName) + { + try { + java.net.URL u = new java.net.URL(baseUrl + id); + java.net.URLConnection uc = u.openConnection(); + String contentType = uc.getContentType(); + int contentLength = uc.getContentLength(); + if (contentType.startsWith("text/") || contentLength == -1) { + throw new IOException("This is not a binary file."); + } + InputStream raw = uc.getInputStream(); + InputStream in = new BufferedInputStream(raw); + byte[] data = new byte[contentLength]; + int bytesRead = 0; + int offset = 0; + while (offset < contentLength) { + bytesRead = in.read(data, offset, data.length - offset); + if (bytesRead == -1) + break; + offset += bytesRead; + } + in.close(); + + if (offset != contentLength) { + throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); + } + + java.io.FileOutputStream out = new java.io.FileOutputStream(outputFileName); + out.write(data); + out.flush(); + out.close(); + + } catch(Exception e) { + System.out.println("Unable to download file, already present or network error? " + e.getMessage()); + return false; + } + return true; + } + + + public static String getUrl(String url) { + try { + java.net.URL u = new java.net.URL(url); + BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream())); + StringBuilder data = new StringBuilder(); + + String inputLine; + while ((inputLine = in.readLine()) != null) { + data.append(inputLine); + data.append("\n"); + } + + in.close(); + return data.toString(); + + } catch(Exception e) { + System.out.println("Unable to get url. Network error? " + e.getMessage()); + return null; + } + + } + + @SuppressWarnings("deprecation") + public static void notifyHostname(String baseUrl, String serviceId) { + String[] urlComponents = baseUrl.split("/"); + String hostname; + String ip; + String url=""; + + try { + hostname = java.net.InetAddress.getLocalHost().getHostName(); + ip = java.net.InetAddress.getLocalHost().getHostAddress(); + } catch(Exception e) { + hostname = "unknown"; + ip = "0.0.0.0"; + } + + try { + // An url is "http[s]://.....:/, + url = urlComponents[0] + "//" + urlComponents[2] + "/sernotify/" + serviceId + "/hostname?hostname="+URLEncoder.encode(hostname)+"&ip="+URLEncoder.encode(ip); + getUrl(url); + } catch(Exception e) { + System.out.println("Unable to get url? " + e.getMessage()); + } + } + +}