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
public void close() {

View file

@ -38,7 +38,7 @@ import java.util.zip.ZipFile;
import zutil.io.InputStreamCloser;
public class FileSearch implements Iterable<FileSearchItem>{
public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
// Constants
private static final List<String> compressedFileExtensions = Arrays.asList(new String[]{
"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){
@ -180,65 +180,67 @@ public class FileSearch implements Iterable<FileSearchItem>{
}
}
}
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; }
}

View file

@ -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();

View file

@ -48,6 +48,8 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
// Contains all the received services
private HashMap<String, LinkedList<SSDPServiceInfo>> services_st;
private HashMap<String, SSDPServiceInfo> 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);
}
}

View file

@ -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,

View file

@ -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<T> {
@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;
}

View file

@ -54,8 +54,8 @@ public class PluginManager<T> implements Iterable<PluginData<T>>{
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<T> plugin = new PluginData<T>(intfClass.getName(), node);
if(node.get("interfaces").getString(intfClass.getName()) != null){