Bug fixes and other stuff

This commit is contained in:
Ziver Koc 2014-11-06 20:21:29 +00:00
parent a7e6324a10
commit e822a4b35c
7 changed files with 80 additions and 63 deletions

View file

@ -57,7 +57,7 @@ public class StringOutputStream extends OutputStream{
} }
/** /**
* Same as {@link clear()} * Same as {@link OutputStream:clear()}
*/ */
@Override @Override
public void close() { public void close() {

View file

@ -38,7 +38,7 @@ import java.util.zip.ZipFile;
import zutil.io.InputStreamCloser; import zutil.io.InputStreamCloser;
public class FileSearch implements Iterable<FileSearchItem>{ public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
// Constants // Constants
private static final List<String> compressedFileExtensions = Arrays.asList(new String[]{ private static final List<String> compressedFileExtensions = Arrays.asList(new String[]{
"jar", "zip" "jar", "zip"
@ -72,7 +72,7 @@ public class FileSearch implements Iterable<FileSearchItem>{
} }
/** /**
* 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){ public void setExtension(String ext){
@ -180,10 +180,10 @@ public class FileSearch implements Iterable<FileSearchItem>{
} }
} }
}
interface FileSearchItem{
public interface FileSearchItem{
/** @return a file or folder name **/ /** @return a file or folder name **/
public String getName(); 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 **/ /** @return a URL to the file or folder, in case of a compressed file the URL to the package will be returned **/
@ -197,10 +197,10 @@ interface FileSearchItem{
public InputStream getInputStream() throws IOException; public InputStream getInputStream() throws IOException;
/** @return an String array with all files if this is a folder otherwise null **/ /** @return an String array with all files if this is a folder otherwise null **/
public String[] listFiles(); public String[] listFiles();
} }
class FileSearchFileItem implements FileSearchItem{ public class FileSearchFileItem implements FileSearchItem{
private File file; private File file;
protected FileSearchFileItem(File file){ protected FileSearchFileItem(File file){
@ -217,9 +217,9 @@ class FileSearchFileItem implements FileSearchItem{
public InputStream getInputStream() throws IOException { return new FileInputStream(file); } public InputStream getInputStream() throws IOException { return new FileInputStream(file); }
public String[] listFiles() { return file.list(); } public String[] listFiles() { return file.list(); }
} }
class FileSearchZipItem implements FileSearchItem{ public class FileSearchZipItem implements FileSearchItem{
private String file; private String file;
private ZipEntry entry; private ZipEntry entry;
@ -241,4 +241,6 @@ class FileSearchZipItem implements FileSearchItem{
} }
public String[] listFiles() { return null; } public String[] listFiles() { return null; }
}
} }

View file

@ -104,7 +104,7 @@ public class HttpHeaderParser {
httpCode = Integer.parseInt( line.substring( 9, 12 )); httpCode = Integer.parseInt( line.substring( 9, 12 ));
} }
// Client Request // Client Request
else{ else if(line.contains("HTTP/")){
type = (line.substring(0, line.indexOf(" "))).trim(); type = (line.substring(0, line.indexOf(" "))).trim();
version = Float.parseFloat( line.substring(line.lastIndexOf("HTTP/")+5 , line.length()).trim() ); version = Float.parseFloat( line.substring(line.lastIndexOf("HTTP/")+5 , line.length()).trim() );
line = (line.substring(type.length()+1, line.lastIndexOf("HTTP/"))).trim(); line = (line.substring(type.length()+1, line.lastIndexOf("HTTP/"))).trim();

View file

@ -49,6 +49,8 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
private HashMap<String, LinkedList<SSDPServiceInfo>> services_st; private HashMap<String, LinkedList<SSDPServiceInfo>> services_st;
private HashMap<String, SSDPServiceInfo> services_usn; private HashMap<String, SSDPServiceInfo> services_usn;
private SSDPServiceListener listener;
public static void main(String[] args) throws IOException{ public static void main(String[] args) throws IOException{
System.out.println(LogUtil.getCalingClass()); System.out.println(LogUtil.getCalingClass());
@ -104,8 +106,8 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
http.setHeader("ST", st ); http.setHeader("ST", st );
http.setHeader("Man", "\"ssdp:discover\"" ); http.setHeader("Man", "\"ssdp:discover\"" );
http.setHeader("MX", "3" ); http.setHeader("MX", "3" );
http.flush();
http.close();
logger.log(Level.FINEST, "***** REQUEST: \n"+msg); logger.log(Level.FINEST, "***** REQUEST: \n"+msg);
byte[] data = msg.toString().getBytes(); byte[] data = msg.toString().getBytes();
DatagramPacket packet = new DatagramPacket( DatagramPacket packet = new DatagramPacket(
@ -113,6 +115,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
InetAddress.getByName( SSDPServer.SSDP_MULTICAST_ADDR ), InetAddress.getByName( SSDPServer.SSDP_MULTICAST_ADDR ),
SSDPServer.SSDP_PORT ); SSDPServer.SSDP_PORT );
super.send( packet ); super.send( packet );
http.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -173,10 +176,11 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
*/ */
public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network) { public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network) {
HttpHeaderParser header = new HttpHeaderParser( new String( packet.getData() ) ); 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 usn = header.getHeader("USN");
String st = header.getHeader("ST"); String st = header.getHeader("ST");
boolean newService = false;
StandardSSDPInfo service; StandardSSDPInfo service;
// Get existing service // Get existing service
if( services_usn.containsKey( usn )){ if( services_usn.containsKey( usn )){
@ -184,6 +188,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
} }
// Add new service // Add new service
else{ else{
newService = true;
service = new StandardSSDPInfo(); service = new StandardSSDPInfo();
services_usn.put( usn, service); services_usn.put( usn, service);
if( !services_st.containsKey(st) ) if( !services_st.containsKey(st) )
@ -197,7 +202,10 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
service.setExpirationTime( service.setExpirationTime(
System.currentTimeMillis() + System.currentTimeMillis() +
1000 * getCacheTime(header.getHeader("Cache-Control")) ); 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){ private long getCacheTime(String cache_control){
@ -212,4 +220,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
return ret; return ret;
} }
public interface SSDPServiceListener{
public void newService(StandardSSDPInfo service);
}
} }

View file

@ -79,6 +79,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
public static void main(String[] args) throws IOException{ public static void main(String[] args) throws IOException{
LogUtil.setGlobalLevel(Level.FINEST);
SSDPServer ssdp = new SSDPServer(); SSDPServer ssdp = new SSDPServer();
StandardSSDPInfo service = new StandardSSDPInfo(); StandardSSDPInfo service = new StandardSSDPInfo();
service.setLocation("nowhere"); service.setLocation("nowhere");
@ -101,8 +102,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
/** /**
* Adds an service that will be announced. * Adds an service that will be announced.
* *
* @param searchTarget is the ST value in SSDP * @param service add a new service to be announced
* @param location is the location of the service
*/ */
public void addService(SSDPServiceInfo service){ public void addService(SSDPServiceInfo service){
services.put( service.getSearchTarget(), service ); services.put( service.getSearchTarget(), service );
@ -178,11 +178,11 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
String msg = new String( packet.getData() ); String msg = new String( packet.getData() );
HttpHeaderParser header = new HttpHeaderParser( msg ); HttpHeaderParser header = new HttpHeaderParser( msg );
logger.log(Level.FINEST, "**** Received:\n"+header); logger.log(Level.FINEST, "#### Received:\n"+header);
// ******* Respond // ******* Respond
// Check that the message is an ssdp discovery message // 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 man = header.getHeader("Man").replace("\"", "");
String st = header.getHeader("ST"); String st = header.getHeader("ST");
// Check that its the correct URL and that its an ssdp:discover message // 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("EXT", "" );
http.setHeader("Cache-Control", "max-age = "+cache_time ); http.setHeader("Cache-Control", "max-age = "+cache_time );
http.setHeader("USN", services.get(st).getUSN() ); http.setHeader("USN", services.get(st).getUSN() );
http.flush();
http.close(); String strData = response.toString();
logger.log(Level.FINEST, "********** Response:\n"+response); logger.log(Level.FINEST, "#### Response:\n"+strData);
byte[] data = response.toString().getBytes(); byte[] data = strData.getBytes();
packet = new DatagramPacket( packet = new DatagramPacket(
data, data.length, data, data.length,
packet.getAddress(), packet.getAddress(),
packet.getPort()); packet.getPort());
network.send( packet ); network.send( packet );
http.close();
} }
} }
} }
@ -267,7 +269,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
http.setHeader("USN", services.get(searchTarget).getUSN() ); http.setHeader("USN", services.get(searchTarget).getUSN() );
http.close(); http.close();
logger.log(Level.FINEST, "******** Notification:\n"+msg); logger.log(Level.FINEST, "#### Notification:\n"+msg);
byte[] data = msg.toString().getBytes(); byte[] data = msg.toString().getBytes();
DatagramPacket packet = new DatagramPacket( DatagramPacket packet = new DatagramPacket(
data, data.length, data, data.length,

View file

@ -24,6 +24,8 @@ package zutil.plugin;
import zutil.parser.DataNode; import zutil.parser.DataNode;
import java.net.URLClassLoader;
/** /**
* This class contains information about a plugin * This class contains information about a plugin
* and implementation instances of the plugin interfaces * and implementation instances of the plugin interfaces
@ -53,9 +55,9 @@ public class PluginData<T> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T getObject() throws InstantiationException, IllegalAccessException, ClassNotFoundException{ public T getObject() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
if(obj == null) //if(obj == null)
new URLClassLoader(urls); // new URLClassLoader(pluginClass);
//obj = (T) Class.forName(pluginClass).newInstance(); // //obj = (T) Class.forName(pluginClass).newInstance();
return obj; return obj;
} }

View file

@ -54,8 +54,8 @@ public class PluginManager<T> implements Iterable<PluginData<T>>{
search.searchFolders(false); search.searchFolders(false);
search.setFileName("plugin.json"); search.setFileName("plugin.json");
for(File file : search){ for(FileSearch.FileSearchItem file : search){
DataNode node = JSONParser.read(FileUtil.getFileContent(file)); DataNode node = JSONParser.read(FileUtil.getContent(file.getUrl()));
PluginData<T> plugin = new PluginData<T>(intfClass.getName(), node); PluginData<T> plugin = new PluginData<T>(intfClass.getName(), node);
if(node.get("interfaces").getString(intfClass.getName()) != null){ if(node.get("interfaces").getString(intfClass.getName()) != null){