diff --git a/src/zutil/io/StringOutputStream.java b/src/zutil/io/StringOutputStream.java index 9ef837f..f69397e 100644 --- a/src/zutil/io/StringOutputStream.java +++ b/src/zutil/io/StringOutputStream.java @@ -57,7 +57,7 @@ public class StringOutputStream extends OutputStream{ } /** - * Same as {@link clear()} + * Same as {@link OutputStream:clear()} */ @Override public void close() { diff --git a/src/zutil/io/file/FileSearch.java b/src/zutil/io/file/FileSearch.java index 1b9e75b..785f767 100644 --- a/src/zutil/io/file/FileSearch.java +++ b/src/zutil/io/file/FileSearch.java @@ -38,7 +38,7 @@ import java.util.zip.ZipFile; import zutil.io.InputStreamCloser; -public class FileSearch implements Iterable{ +public class FileSearch implements Iterable{ // Constants private static final List compressedFileExtensions = Arrays.asList(new String[]{ "jar", "zip" @@ -72,7 +72,7 @@ public class FileSearch implements Iterable{ } /** - * Sets the file extensions to search for (should not include . at the beggining) + * Sets the file extensions to search for (should not include . at the beginning) */ public void setExtension(String ext){ @@ -180,65 +180,67 @@ public class FileSearch implements Iterable{ } } -} -interface FileSearchItem{ - /** @return a file or folder name **/ - public String getName(); - /** @return a URL to the file or folder, in case of a compressed file the URL to the package will be returned **/ - public URL getUrl() throws MalformedURLException ; - public boolean isCompressed(); - public boolean isFile(); - public boolean isDirectory(); + public interface FileSearchItem{ + /** @return a file or folder name **/ + public String getName(); + /** @return a URL to the file or folder, in case of a compressed file the URL to the package will be returned **/ + public URL getUrl() throws MalformedURLException ; - /** @return an InputStream if this is a file otherwise null **/ - public InputStream getInputStream() throws IOException; - /** @return an String array with all files if this is a folder otherwise null **/ - public String[] listFiles(); -} + public boolean isCompressed(); + public boolean isFile(); + public boolean isDirectory(); - -class FileSearchFileItem implements FileSearchItem{ - private File file; - - protected FileSearchFileItem(File file){ - this.file = file; + /** @return an InputStream if this is a file otherwise null **/ + public InputStream getInputStream() throws IOException; + /** @return an String array with all files if this is a folder otherwise null **/ + public String[] listFiles(); } - public String getName() { return file.getName(); } - public URL getUrl() throws MalformedURLException { return new URL(file.getAbsolutePath()); } - public boolean isCompressed() { return false; } - public boolean isFile() { return file.isFile(); } - public boolean isDirectory() { return file.isDirectory(); } + public class FileSearchFileItem implements FileSearchItem{ + private File file; - public InputStream getInputStream() throws IOException { return new FileInputStream(file); } - public String[] listFiles() { return file.list(); } + protected FileSearchFileItem(File file){ + this.file = file; + } -} + public String getName() { return file.getName(); } + public URL getUrl() throws MalformedURLException { return new URL(file.getAbsolutePath()); } -class FileSearchZipItem implements FileSearchItem{ - private String file; - private ZipEntry entry; + public boolean isCompressed() { return false; } + public boolean isFile() { return file.isFile(); } + public boolean isDirectory() { return file.isDirectory(); } + + public InputStream getInputStream() throws IOException { return new FileInputStream(file); } + public String[] listFiles() { return file.list(); } - protected FileSearchZipItem(String file, ZipEntry entry){ - this.file = file; - this.entry = entry; } - public String getName() { return entry.getName(); } - public URL getUrl() throws MalformedURLException { return new URL(file); } + public class FileSearchZipItem implements FileSearchItem{ + private String file; + private ZipEntry entry; - public boolean isCompressed() { return true; } - public boolean isFile() { return !entry.isDirectory(); } - public boolean isDirectory() { return entry.isDirectory(); } + protected FileSearchZipItem(String file, ZipEntry entry){ + this.file = file; + this.entry = entry; + } + + public String getName() { return entry.getName(); } + public URL getUrl() throws MalformedURLException { return new URL(file); } + + public boolean isCompressed() { return true; } + public boolean isFile() { return !entry.isDirectory(); } + public boolean isDirectory() { return entry.isDirectory(); } + + public InputStream getInputStream() throws IOException { + ZipFile zip = new ZipFile(file); + return new InputStreamCloser(zip.getInputStream(entry), zip); + } + public String[] listFiles() { return null; } - public InputStream getInputStream() throws IOException { - ZipFile zip = new ZipFile(file); - return new InputStreamCloser(zip.getInputStream(entry), zip); } - public String[] listFiles() { return null; } } \ No newline at end of file diff --git a/src/zutil/net/http/HttpHeaderParser.java b/src/zutil/net/http/HttpHeaderParser.java index d78a550..c69ee2d 100644 --- a/src/zutil/net/http/HttpHeaderParser.java +++ b/src/zutil/net/http/HttpHeaderParser.java @@ -104,7 +104,7 @@ public class HttpHeaderParser { httpCode = Integer.parseInt( line.substring( 9, 12 )); } // Client Request - else{ + else if(line.contains("HTTP/")){ type = (line.substring(0, line.indexOf(" "))).trim(); version = Float.parseFloat( line.substring(line.lastIndexOf("HTTP/")+5 , line.length()).trim() ); line = (line.substring(type.length()+1, line.lastIndexOf("HTTP/"))).trim(); diff --git a/src/zutil/net/ssdp/SSDPClient.java b/src/zutil/net/ssdp/SSDPClient.java index ad1fd03..6668ec0 100644 --- a/src/zutil/net/ssdp/SSDPClient.java +++ b/src/zutil/net/ssdp/SSDPClient.java @@ -48,6 +48,8 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork // Contains all the received services private HashMap> services_st; private HashMap services_usn; + + private SSDPServiceListener listener; public static void main(String[] args) throws IOException{ @@ -104,8 +106,8 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork http.setHeader("ST", st ); http.setHeader("Man", "\"ssdp:discover\"" ); http.setHeader("MX", "3" ); + http.flush(); - http.close(); logger.log(Level.FINEST, "***** REQUEST: \n"+msg); byte[] data = msg.toString().getBytes(); DatagramPacket packet = new DatagramPacket( @@ -113,6 +115,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork InetAddress.getByName( SSDPServer.SSDP_MULTICAST_ADDR ), SSDPServer.SSDP_PORT ); super.send( packet ); + http.close(); } catch (Exception e) { e.printStackTrace(); } @@ -173,10 +176,11 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork */ public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network) { HttpHeaderParser header = new HttpHeaderParser( new String( packet.getData() ) ); - logger.log(Level.FINEST, "*********** Recived\n"+header); + logger.log(Level.FINEST, "Recived: \n"+header); String usn = header.getHeader("USN"); String st = header.getHeader("ST"); + boolean newService = false; StandardSSDPInfo service; // Get existing service if( services_usn.containsKey( usn )){ @@ -184,6 +188,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork } // Add new service else{ + newService = true; service = new StandardSSDPInfo(); services_usn.put( usn, service); if( !services_st.containsKey(st) ) @@ -197,7 +202,10 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork service.setExpirationTime( System.currentTimeMillis() + 1000 * getCacheTime(header.getHeader("Cache-Control")) ); - logger.log(Level.FINEST, "*********** Recived\n"+service); + + logger.log(Level.FINEST, "Recived:\n"+service); + if(listener != null && newService) + listener.newService(service); } private long getCacheTime(String cache_control){ @@ -212,4 +220,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork return ret; } + public interface SSDPServiceListener{ + public void newService(StandardSSDPInfo service); + } } diff --git a/src/zutil/net/ssdp/SSDPServer.java b/src/zutil/net/ssdp/SSDPServer.java index 1804e59..ffb5732 100644 --- a/src/zutil/net/ssdp/SSDPServer.java +++ b/src/zutil/net/ssdp/SSDPServer.java @@ -79,6 +79,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork public static void main(String[] args) throws IOException{ + LogUtil.setGlobalLevel(Level.FINEST); SSDPServer ssdp = new SSDPServer(); StandardSSDPInfo service = new StandardSSDPInfo(); service.setLocation("nowhere"); @@ -101,8 +102,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork /** * Adds an service that will be announced. * - * @param searchTarget is the ST value in SSDP - * @param location is the location of the service + * @param service add a new service to be announced */ public void addService(SSDPServiceInfo service){ services.put( service.getSearchTarget(), service ); @@ -178,11 +178,11 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork String msg = new String( packet.getData() ); HttpHeaderParser header = new HttpHeaderParser( msg ); - logger.log(Level.FINEST, "**** Received:\n"+header); + logger.log(Level.FINEST, "#### Received:\n"+header); // ******* Respond // Check that the message is an ssdp discovery message - if( header.getRequestType().equalsIgnoreCase("M-SEARCH") ){ + if( header.getRequestType() != null && header.getRequestType().equalsIgnoreCase("M-SEARCH") ){ String man = header.getHeader("Man").replace("\"", ""); String st = header.getHeader("ST"); // Check that its the correct URL and that its an ssdp:discover message @@ -199,15 +199,17 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork http.setHeader("EXT", "" ); http.setHeader("Cache-Control", "max-age = "+cache_time ); http.setHeader("USN", services.get(st).getUSN() ); + http.flush(); - http.close(); - logger.log(Level.FINEST, "********** Response:\n"+response); - byte[] data = response.toString().getBytes(); + String strData = response.toString(); + logger.log(Level.FINEST, "#### Response:\n"+strData); + byte[] data = strData.getBytes(); packet = new DatagramPacket( data, data.length, packet.getAddress(), packet.getPort()); network.send( packet ); + http.close(); } } } @@ -267,7 +269,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork http.setHeader("USN", services.get(searchTarget).getUSN() ); http.close(); - logger.log(Level.FINEST, "******** Notification:\n"+msg); + logger.log(Level.FINEST, "#### Notification:\n"+msg); byte[] data = msg.toString().getBytes(); DatagramPacket packet = new DatagramPacket( data, data.length, diff --git a/src/zutil/plugin/PluginData.java b/src/zutil/plugin/PluginData.java index c18e400..aa6989d 100644 --- a/src/zutil/plugin/PluginData.java +++ b/src/zutil/plugin/PluginData.java @@ -24,6 +24,8 @@ package zutil.plugin; import zutil.parser.DataNode; +import java.net.URLClassLoader; + /** * This class contains information about a plugin * and implementation instances of the plugin interfaces @@ -53,9 +55,9 @@ public class PluginData { @SuppressWarnings("unchecked") public T getObject() throws InstantiationException, IllegalAccessException, ClassNotFoundException{ - if(obj == null) - new URLClassLoader(urls); - //obj = (T) Class.forName(pluginClass).newInstance(); + //if(obj == null) + // new URLClassLoader(pluginClass); + // //obj = (T) Class.forName(pluginClass).newInstance(); return obj; } diff --git a/src/zutil/plugin/PluginManager.java b/src/zutil/plugin/PluginManager.java index d149941..820cfb9 100644 --- a/src/zutil/plugin/PluginManager.java +++ b/src/zutil/plugin/PluginManager.java @@ -54,8 +54,8 @@ public class PluginManager implements Iterable>{ search.searchFolders(false); search.setFileName("plugin.json"); - for(File file : search){ - DataNode node = JSONParser.read(FileUtil.getFileContent(file)); + for(FileSearch.FileSearchItem file : search){ + DataNode node = JSONParser.read(FileUtil.getContent(file.getUrl())); PluginData plugin = new PluginData(intfClass.getName(), node); if(node.get("interfaces").getString(intfClass.getName()) != null){