Fixed some things
This commit is contained in:
parent
c21b229882
commit
41c474d2a5
8 changed files with 239 additions and 138 deletions
13
src/zutil/Dumpable.java
Normal file
13
src/zutil/Dumpable.java
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
package zutil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If an class implements this interface and runs it through
|
||||||
|
* MultiPrintStream.dump then all the values in the object
|
||||||
|
* will be written out.
|
||||||
|
*
|
||||||
|
* @author Ziver
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface Dumpable {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -199,7 +199,7 @@ public class MultiPrintStream extends PrintStream {
|
||||||
* @param o is the Object to dump
|
* @param o is the Object to dump
|
||||||
*/
|
*/
|
||||||
public void dump( Object o ){
|
public void dump( Object o ){
|
||||||
dumpToString( o );
|
println(dumpToString( o ));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -212,24 +212,45 @@ public class MultiPrintStream extends PrintStream {
|
||||||
* <br>- Instance variables of a Object
|
* <br>- Instance variables of a Object
|
||||||
*
|
*
|
||||||
* @param o is the Object to dump
|
* @param o is the Object to dump
|
||||||
* @param print is if the method should print the data or just return it
|
* @return A String with all the printed data
|
||||||
|
*/
|
||||||
|
public String dumpToString( Object o) {
|
||||||
|
return dumpToString(o, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dumps the content of:
|
||||||
|
* <br>- Array content
|
||||||
|
* <br>- Map content (HashMap etc.)
|
||||||
|
* <br>- List content (ArrayList, LinkedList etc.)
|
||||||
|
* <br>- InputStream content (Prints out until the end of the stream)
|
||||||
|
* <br>- Reader content (Prints out until the end of the reader)
|
||||||
|
* <br>- Instance variables of a Object
|
||||||
|
*
|
||||||
|
* @param o is the Object to dump
|
||||||
|
* @param head is the string that will be put in front of every line
|
||||||
* @return A String with all the printed data
|
* @return A String with all the printed data
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public String dumpToString( Object o ) {
|
private String dumpToString( Object o , String head) {
|
||||||
if(o == null) return "NULL";
|
if(o == null) return "NULL";
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
Class oClass = o.getClass();
|
Class oClass = o.getClass();
|
||||||
buffer.append( oClass.getName() );
|
buffer.append( oClass.getName() );
|
||||||
|
String nextHead = head + "\t";
|
||||||
// Prints out Arrays
|
// Prints out Arrays
|
||||||
if ( oClass.isArray() ) {
|
if ( oClass.isArray() ) {
|
||||||
buffer.append( "[" );
|
buffer.append( "[" );
|
||||||
for ( int i=0; i<Array.getLength(o) ;i++ ) {
|
for ( int i=0; i<Array.getLength(o) ;i++ ) {
|
||||||
if ( i > 0 )
|
|
||||||
buffer.append( ", " );
|
|
||||||
Object value = Array.get(o,i);
|
Object value = Array.get(o,i);
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
|
buffer.append("\n");
|
||||||
|
buffer.append(nextHead);
|
||||||
|
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
||||||
|
if ( i+1<Array.getLength(o) )
|
||||||
|
buffer.append( "," );
|
||||||
}
|
}
|
||||||
|
buffer.append( "\n" );
|
||||||
|
buffer.append(head);
|
||||||
buffer.append( "]" );
|
buffer.append( "]" );
|
||||||
}
|
}
|
||||||
// Prints out a list
|
// Prints out a list
|
||||||
|
|
@ -238,10 +259,14 @@ public class MultiPrintStream extends PrintStream {
|
||||||
buffer.append( "{" );
|
buffer.append( "{" );
|
||||||
while(it.hasNext()){
|
while(it.hasNext()){
|
||||||
Object value = it.next();
|
Object value = it.next();
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
|
buffer.append("\n");
|
||||||
|
buffer.append(nextHead);
|
||||||
|
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
||||||
if(it.hasNext())
|
if(it.hasNext())
|
||||||
buffer.append( ", " );
|
buffer.append( "," );
|
||||||
}
|
}
|
||||||
|
buffer.append( "\n" );
|
||||||
|
buffer.append(head);
|
||||||
buffer.append( "}" );
|
buffer.append( "}" );
|
||||||
}
|
}
|
||||||
// Prints out a Map
|
// Prints out a Map
|
||||||
|
|
@ -251,43 +276,53 @@ public class MultiPrintStream extends PrintStream {
|
||||||
while(it.hasNext()){
|
while(it.hasNext()){
|
||||||
Object key = it.next();
|
Object key = it.next();
|
||||||
Object value = ((Map)o).get(key);
|
Object value = ((Map)o).get(key);
|
||||||
|
buffer.append("\n");
|
||||||
|
buffer.append(nextHead);
|
||||||
buffer.append( key );
|
buffer.append( key );
|
||||||
buffer.append( "=>" );
|
buffer.append( "=>" );
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
|
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
||||||
if(it.hasNext())
|
if(it.hasNext())
|
||||||
buffer.append( ", " );
|
buffer.append( "," );
|
||||||
}
|
}
|
||||||
|
buffer.append( "\n" );
|
||||||
|
buffer.append(head);
|
||||||
buffer.append( "}" );
|
buffer.append( "}" );
|
||||||
}
|
}
|
||||||
// Prints out data from InputStream
|
// Prints out data from InputStream
|
||||||
else if(o instanceof InputStream){
|
else if(o instanceof InputStream){
|
||||||
buffer.append( " =>{ \n" );
|
buffer.append( " =>{\n" );
|
||||||
try {
|
try {
|
||||||
InputStream in = (InputStream)o;
|
InputStream in = (InputStream)o;
|
||||||
int tmp;
|
int tmp;
|
||||||
while((tmp = in.read()) != -1){
|
while((tmp = in.read()) != -1){
|
||||||
|
buffer.append(nextHead);
|
||||||
buffer.append( (char)tmp );
|
buffer.append( (char)tmp );
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace(this);
|
e.printStackTrace(this);
|
||||||
}
|
}
|
||||||
buffer.append( "\n}" );
|
buffer.append( "\n" );
|
||||||
|
buffer.append(head);
|
||||||
|
buffer.append( "}" );
|
||||||
}
|
}
|
||||||
// Prints out data from InputStream
|
// Prints out data from InputStream
|
||||||
else if(o instanceof Reader){
|
else if(o instanceof Reader){
|
||||||
buffer.append( " =>{ \n" );
|
buffer.append( " =>{\n" );
|
||||||
try {
|
try {
|
||||||
Reader in = (Reader)o;
|
Reader in = (Reader)o;
|
||||||
int tmp;
|
int tmp;
|
||||||
while((tmp = in.read()) != -1){
|
while((tmp = in.read()) != -1){
|
||||||
|
buffer.append(nextHead);
|
||||||
buffer.append( (char)tmp );
|
buffer.append( (char)tmp );
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace(this);
|
e.printStackTrace(this);
|
||||||
}
|
}
|
||||||
buffer.append( "\n}" );
|
buffer.append( "\n" );
|
||||||
|
buffer.append(head);
|
||||||
|
buffer.append( "}" );
|
||||||
}
|
}
|
||||||
// Prints out Object properties
|
// Prints out Object properties
|
||||||
else{
|
else{
|
||||||
|
|
@ -295,20 +330,24 @@ public class MultiPrintStream extends PrintStream {
|
||||||
while ( oClass != null ) {
|
while ( oClass != null ) {
|
||||||
Field[] fields = oClass.getDeclaredFields();
|
Field[] fields = oClass.getDeclaredFields();
|
||||||
for ( int i=0; i<fields.length; i++ ) {
|
for ( int i=0; i<fields.length; i++ ) {
|
||||||
if ( buffer.length() > 1 )
|
|
||||||
buffer.append( ", " );
|
|
||||||
fields[i].setAccessible( true );
|
fields[i].setAccessible( true );
|
||||||
|
buffer.append("\n");
|
||||||
|
buffer.append(head);
|
||||||
buffer.append( fields[i].getName() );
|
buffer.append( fields[i].getName() );
|
||||||
buffer.append( "=" );
|
buffer.append( "=" );
|
||||||
try {
|
try {
|
||||||
Object value = fields[i].get(o);
|
Object value = fields[i].get(o);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
|
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
||||||
}
|
}
|
||||||
} catch ( IllegalAccessException e ) {}
|
} catch ( IllegalAccessException e ) {}
|
||||||
|
if ( i+1<fields.length )
|
||||||
|
buffer.append( "," );
|
||||||
}
|
}
|
||||||
oClass = oClass.getSuperclass();
|
oClass = oClass.getSuperclass();
|
||||||
}
|
}
|
||||||
|
buffer.append( "\n" );
|
||||||
|
buffer.append(head);
|
||||||
buffer.append( "}" );
|
buffer.append( "}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -321,8 +360,11 @@ public class MultiPrintStream extends PrintStream {
|
||||||
private boolean dumbCapable(Object o){
|
private boolean dumbCapable(Object o){
|
||||||
if(o != null){
|
if(o != null){
|
||||||
if(o.getClass().isArray()) return true;
|
if(o.getClass().isArray()) return true;
|
||||||
else if(o instanceof Collection)return true;
|
else if(o instanceof Collection<?>)return true;
|
||||||
else if(o instanceof Map)return true;
|
else if(o instanceof Map<?,?>)return true;
|
||||||
|
else if(o instanceof InputStream)return true;
|
||||||
|
else if(o instanceof Reader)return true;
|
||||||
|
else if(o instanceof Dumpable)return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ public class MySQLQueue<E> implements Queue<E>{
|
||||||
return poll();
|
return poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addAll(Collection arg0) {
|
public boolean addAll(Collection<? extends E> arg0) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -119,7 +119,7 @@ public class MySQLQueue<E> implements Queue<E>{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsAll(Collection arg0) {
|
public boolean containsAll(Collection<?> arg0) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -128,7 +128,7 @@ public class MySQLQueue<E> implements Queue<E>{
|
||||||
return (peek() != null);
|
return (peek() != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterator iterator() {
|
public Iterator<E> iterator() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -144,12 +144,12 @@ public class MySQLQueue<E> implements Queue<E>{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean removeAll(Collection arg0) {
|
public synchronized boolean removeAll(Collection<?> arg0) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean retainAll(Collection arg0) {
|
public boolean retainAll(Collection<?> arg0) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,9 @@ import java.io.InputStreamReader;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.security.auth.login.AccountException;
|
import javax.security.auth.login.AccountException;
|
||||||
|
|
||||||
|
|
@ -51,7 +53,8 @@ public class FTPClient extends Thread{
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
try {
|
try {
|
||||||
FTPClient client = new FTPClient("koc.se", 21, "ziver", "****", FTP_PASSIVE);
|
FTPClient client = new FTPClient("213.180.86.135", 21, "administrator", "geineZ2K", FTP_PASSIVE);
|
||||||
|
/*
|
||||||
client.createDir("./ziver/lol");
|
client.createDir("./ziver/lol");
|
||||||
client.removeDir("./ziver/lol");
|
client.removeDir("./ziver/lol");
|
||||||
|
|
||||||
|
|
@ -67,7 +70,12 @@ public class FTPClient extends Thread{
|
||||||
MultiPrintStream.out.dump(client.getFileList("./ziver"));
|
MultiPrintStream.out.dump(client.getFileList("./ziver"));
|
||||||
client.removeFile("./ziver/test.txt");
|
client.removeFile("./ziver/test.txt");
|
||||||
MultiPrintStream.out.dump(client.getFileList("./ziver"));
|
MultiPrintStream.out.dump(client.getFileList("./ziver"));
|
||||||
|
*/
|
||||||
|
ArrayList<String[]> tmp = client.getFileInfo("");
|
||||||
|
MultiPrintStream.out.println("****************");
|
||||||
|
MultiPrintStream.out.dump(tmp);
|
||||||
|
MultiPrintStream.out.println(tmp.size());
|
||||||
|
MultiPrintStream.out.println("****************");
|
||||||
client.close();
|
client.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
@ -150,7 +158,7 @@ public class FTPClient extends Thread{
|
||||||
* @return The input line
|
* @return The input line
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private synchronized String readCommand(boolean print) throws IOException{
|
public synchronized String readCommand(boolean print) throws IOException{
|
||||||
String tmp = in.readLine();
|
String tmp = in.readLine();
|
||||||
if(print)System.out.println(tmp);
|
if(print)System.out.println(tmp);
|
||||||
if(parseReturnCode(tmp) >= 400 ) throw new IOException(tmp);
|
if(parseReturnCode(tmp) >= 400 ) throw new IOException(tmp);
|
||||||
|
|
@ -161,7 +169,7 @@ public class FTPClient extends Thread{
|
||||||
/**
|
/**
|
||||||
* Reads from the command channel until there are nothing
|
* Reads from the command channel until there are nothing
|
||||||
* left to read and returns the last line
|
* left to read and returns the last line
|
||||||
* Multiple
|
*
|
||||||
* @param print To print out the received lines
|
* @param print To print out the received lines
|
||||||
* @return The last received line
|
* @return The last received line
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
|
|
@ -225,28 +233,28 @@ public class FTPClient extends Thread{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns information about the file or directory
|
* Returns information about the file or directory
|
||||||
* (This is system specific information)
|
|
||||||
*
|
*
|
||||||
* TODO:
|
* @deprecated
|
||||||
* @deprecated DOSENT WORK!!!!
|
|
||||||
* @param path The path and filename of a file or a directory
|
* @param path The path and filename of a file or a directory
|
||||||
* @return A String with information
|
* @return A List of Strings with information
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public String getFileInfo(String path) throws IOException{
|
public ArrayList<String[]> getFileInfo(String path) throws IOException{
|
||||||
StringBuffer info = new StringBuffer("");
|
Pattern regex = Pattern.compile("\\s{1,}");
|
||||||
|
ArrayList<String[]> info = new ArrayList<String[]>();
|
||||||
|
|
||||||
BufferedReader data_in = getDataInputStream();
|
BufferedReader data_in = getDataInputStream();
|
||||||
sendCommand("LIST "+path, true);
|
sendCommand("LIST "+path, DEBUG);
|
||||||
|
|
||||||
String tmp = "";
|
String tmp = "";
|
||||||
while((tmp = data_in.readLine()) != null){
|
while((tmp = data_in.readLine()) != null){
|
||||||
info.append(tmp);
|
System.err.println(tmp);
|
||||||
|
info.add(regex.split(tmp));
|
||||||
}
|
}
|
||||||
|
|
||||||
data_in.close();
|
data_in.close();
|
||||||
readCommand(DEBUG);
|
readCommand(DEBUG);
|
||||||
return info.toString();
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -32,11 +32,11 @@ public abstract class NioNetwork implements Runnable {
|
||||||
/**
|
/**
|
||||||
* Debug level
|
* Debug level
|
||||||
* 0 = nothing
|
* 0 = nothing
|
||||||
* 1 = connection debug
|
* 1 = connection info
|
||||||
* 2 = message debug
|
* 2 = message debug
|
||||||
* 3 = selector debug
|
* 3 = selector debug
|
||||||
*/
|
*/
|
||||||
public static final int DEBUG = 2;
|
public static int DEBUG = 2;
|
||||||
public static enum NetworkType {SERVER, CLIENT};
|
public static enum NetworkType {SERVER, CLIENT};
|
||||||
|
|
||||||
private NetworkType type;
|
private NetworkType type;
|
||||||
|
|
@ -139,7 +139,7 @@ public abstract class NioNetwork implements Runnable {
|
||||||
* @param data The data to send
|
* @param data The data to send
|
||||||
*/
|
*/
|
||||||
protected void queueSend(SocketChannel socket, byte[] data){
|
protected void queueSend(SocketChannel socket, byte[] data){
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("Sending Queue...");
|
if(DEBUG>=3) MultiPrintStream.out.println("Sending Queue...");
|
||||||
// And queue the data we want written
|
// And queue the data we want written
|
||||||
synchronized (pendingWriteData) {
|
synchronized (pendingWriteData) {
|
||||||
List<ByteBuffer> queue = pendingWriteData.get(socket);
|
List<ByteBuffer> queue = pendingWriteData.get(socket);
|
||||||
|
|
@ -157,7 +157,7 @@ public abstract class NioNetwork implements Runnable {
|
||||||
// Indicate we want the interest ops set changed
|
// Indicate we want the interest ops set changed
|
||||||
pendingChanges.add(new ChangeRequest(socket, ChangeRequest.CHANGEOPS, SelectionKey.OP_WRITE));
|
pendingChanges.add(new ChangeRequest(socket, ChangeRequest.CHANGEOPS, SelectionKey.OP_WRITE));
|
||||||
}
|
}
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("selector.wakeup();");
|
if(DEBUG>=3) MultiPrintStream.out.println("selector.wakeup();");
|
||||||
// Finally, wake up our selecting thread so it can make the required changes
|
// Finally, wake up our selecting thread so it can make the required changes
|
||||||
selector.wakeup();
|
selector.wakeup();
|
||||||
}
|
}
|
||||||
|
|
@ -175,11 +175,11 @@ public abstract class NioNetwork implements Runnable {
|
||||||
case ChangeRequest.CHANGEOPS:
|
case ChangeRequest.CHANGEOPS:
|
||||||
SelectionKey key = change.socket.keyFor(selector);
|
SelectionKey key = change.socket.keyFor(selector);
|
||||||
key.interestOps(change.ops);
|
key.interestOps(change.ops);
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("change.ops "+change.ops);
|
if(DEBUG>=3) MultiPrintStream.out.println("change.ops "+change.ops);
|
||||||
break;
|
break;
|
||||||
case ChangeRequest.REGISTER:
|
case ChangeRequest.REGISTER:
|
||||||
change.socket.register(selector, change.ops);
|
change.socket.register(selector, change.ops);
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("register socket ");
|
if(DEBUG>=3) MultiPrintStream.out.println("register socket ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -188,31 +188,31 @@ public abstract class NioNetwork implements Runnable {
|
||||||
|
|
||||||
// Wait for an event one of the registered channels
|
// Wait for an event one of the registered channels
|
||||||
selector.select();
|
selector.select();
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("selector is awake");
|
if(DEBUG>=3) MultiPrintStream.out.println("selector is awake");
|
||||||
|
|
||||||
// Iterate over the set of keys for which events are available
|
// Iterate over the set of keys for which events are available
|
||||||
Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
|
Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
|
||||||
while (selectedKeys.hasNext()) {
|
while (selectedKeys.hasNext()) {
|
||||||
SelectionKey key = (SelectionKey) selectedKeys.next();
|
SelectionKey key = (SelectionKey) selectedKeys.next();
|
||||||
selectedKeys.remove();
|
selectedKeys.remove();
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("KeyOP: "+key.interestOps()+" isAcceptable: "+SelectionKey.OP_ACCEPT+" isConnectable: "+SelectionKey.OP_CONNECT+" isWritable: "+SelectionKey.OP_WRITE+" isReadable: "+SelectionKey.OP_READ);
|
if(DEBUG>=3) MultiPrintStream.out.println("KeyOP: "+key.interestOps()+" isAcceptable: "+SelectionKey.OP_ACCEPT+" isConnectable: "+SelectionKey.OP_CONNECT+" isWritable: "+SelectionKey.OP_WRITE+" isReadable: "+SelectionKey.OP_READ);
|
||||||
|
|
||||||
if (key.isValid()) {
|
if (key.isValid()) {
|
||||||
// Check what event is available and deal with it
|
// Check what event is available and deal with it
|
||||||
if (key.isAcceptable()) {
|
if (key.isAcceptable()) {
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("Accepting Connection!!");
|
if(DEBUG>=3) MultiPrintStream.out.println("Accepting Connection!!");
|
||||||
accept(key);
|
accept(key);
|
||||||
}
|
}
|
||||||
else if (key.isConnectable()) {
|
else if (key.isConnectable()) {
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("Finnishing Connection!!");
|
if(DEBUG>=3) MultiPrintStream.out.println("Finnishing Connection!!");
|
||||||
finishConnection(key);
|
finishConnection(key);
|
||||||
}
|
}
|
||||||
else if (key.isWritable()) {
|
else if (key.isWritable()) {
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("Writing");
|
if(DEBUG>=3) MultiPrintStream.out.println("Writing");
|
||||||
write(key);
|
write(key);
|
||||||
}
|
}
|
||||||
else if (key.isReadable()) {
|
else if (key.isReadable()) {
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("Reading");
|
if(DEBUG>=3) MultiPrintStream.out.println("Reading");
|
||||||
read(key);
|
read(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -269,7 +269,7 @@ public abstract class NioNetwork implements Runnable {
|
||||||
clients.remove(remoteAdr);
|
clients.remove(remoteAdr);
|
||||||
pendingReadData.remove(socketChannel);
|
pendingReadData.remove(socketChannel);
|
||||||
pendingWriteData.remove(socketChannel);
|
pendingWriteData.remove(socketChannel);
|
||||||
if(DEBUG>=1)MultiPrintStream.out.println("Connection Forced Close("+remoteAdr+")!!! Connection Count: "+clients.size());
|
if(DEBUG>=1) MultiPrintStream.out.println("Connection Forced Close("+remoteAdr+")!!! Connection Count: "+clients.size());
|
||||||
if(type == NetworkType.CLIENT) throw new IOException("Server Closed The Connection!!!");
|
if(type == NetworkType.CLIENT) throw new IOException("Server Closed The Connection!!!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -282,7 +282,7 @@ public abstract class NioNetwork implements Runnable {
|
||||||
clients.remove(remoteAdr);
|
clients.remove(remoteAdr);
|
||||||
pendingReadData.remove(socketChannel);
|
pendingReadData.remove(socketChannel);
|
||||||
pendingWriteData.remove(socketChannel);
|
pendingWriteData.remove(socketChannel);
|
||||||
if(DEBUG>=1)MultiPrintStream.out.println("Connection Close("+remoteAdr+")!!! Connection Count: "+clients.size());
|
if(DEBUG>=1) MultiPrintStream.out.println("Connection Close("+remoteAdr+")!!! Connection Count: "+clients.size());
|
||||||
if(type == NetworkType.CLIENT) throw new IOException("Server Closed The Connection!!!");
|
if(type == NetworkType.CLIENT) throw new IOException("Server Closed The Connection!!!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -293,26 +293,25 @@ public abstract class NioNetwork implements Runnable {
|
||||||
System.arraycopy(readBuffer.array(), 0, rspByteData, 0, numRead);
|
System.arraycopy(readBuffer.array(), 0, rspByteData, 0, numRead);
|
||||||
if(encrypter != null)// Encryption
|
if(encrypter != null)// Encryption
|
||||||
rspByteData = encrypter.decrypt(rspByteData);
|
rspByteData = encrypter.decrypt(rspByteData);
|
||||||
|
|
||||||
/*
|
// Message Count 1m: 36750
|
||||||
|
// Message Count 1s: 612
|
||||||
if(!pendingReadData.containsKey(socketChannel)){
|
if(!pendingReadData.containsKey(socketChannel)){
|
||||||
pendingReadData.put(socketChannel, new DynamicByteArrayStream());
|
pendingReadData.put(socketChannel, new DynamicByteArrayStream());
|
||||||
}
|
}
|
||||||
if(encrypter != null)// Encryption
|
DynamicByteArrayStream dynBuf = pendingReadData.get(socketChannel);
|
||||||
rspByteData = encrypter.decrypt(rspByteData);
|
dynBuf.add(rspByteData);
|
||||||
|
|
||||||
pendingReadData.get(socketChannel).add(rspByteData);
|
|
||||||
*/
|
|
||||||
|
|
||||||
Object rspData = null;
|
Object rspData = null;
|
||||||
try{
|
try{
|
||||||
rspData = Converter.toObject(rspByteData);
|
//rspData = Converter.toObject(rspByteData);
|
||||||
//rspData = Converter.toObject(pendingReadData.get(socketChannel));
|
rspData = Converter.toObject(dynBuf);
|
||||||
handleRecivedMessage(socketChannel, rspData);
|
handleRecivedMessage(socketChannel, rspData);
|
||||||
//pendingReadData.get(socketChannel).clear();
|
dynBuf.clear();
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
//pendingReadData.get(socketChannel).reset();
|
dynBuf.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -326,18 +325,16 @@ public abstract class NioNetwork implements Runnable {
|
||||||
List<ByteBuffer> queue = pendingWriteData.get(socketChannel);
|
List<ByteBuffer> queue = pendingWriteData.get(socketChannel);
|
||||||
if(queue == null){
|
if(queue == null){
|
||||||
queue = new ArrayList<ByteBuffer>();
|
queue = new ArrayList<ByteBuffer>();
|
||||||
|
pendingWriteData.put(socketChannel, queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
// Write until there's not more data ...
|
// Write until there's not more data ...
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
ByteBuffer buf = queue.get(0);
|
ByteBuffer buf = queue.get(0);
|
||||||
i += buf.remaining();
|
|
||||||
socketChannel.write(buf);
|
socketChannel.write(buf);
|
||||||
i -= buf.remaining();
|
|
||||||
if (buf.remaining() > 0) {
|
if (buf.remaining() > 0) {
|
||||||
// ... or the socket's buffer fills up
|
// ... or the socket's buffer fills up
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("Write Buffer Full!!");
|
if(DEBUG>=3) MultiPrintStream.out.println("Write Buffer Full!!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
queue.remove(0);
|
queue.remove(0);
|
||||||
|
|
@ -347,31 +344,32 @@ public abstract class NioNetwork implements Runnable {
|
||||||
// We wrote away all data, so we're no longer interested
|
// We wrote away all data, so we're no longer interested
|
||||||
// in writing on this socket. Switch back to waiting for
|
// in writing on this socket. Switch back to waiting for
|
||||||
// data.
|
// data.
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("No more Data to write!!");
|
if(DEBUG>=3) MultiPrintStream.out.println("No more Data to write!!");
|
||||||
key.interestOps(SelectionKey.OP_READ);
|
key.interestOps(SelectionKey.OP_READ);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleRecivedMessage(SocketChannel socketChannel, Object rspData){
|
private void handleRecivedMessage(SocketChannel socketChannel, Object rspData){
|
||||||
if(DEBUG>=2)MultiPrintStream.out.println("Handling incomming message...");
|
if(DEBUG>=2) MultiPrintStream.out.println("Handling incomming message...");
|
||||||
|
|
||||||
if(rspData instanceof SystemMessage){
|
if(rspData instanceof SystemMessage){
|
||||||
if(systemWorker != null){
|
if(systemWorker != null){
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("System Message!!!");
|
if(DEBUG>=3) MultiPrintStream.out.println("System Message!!!");
|
||||||
systemWorker.processData(this, socketChannel, rspData);
|
systemWorker.processData(this, socketChannel, rspData);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if(DEBUG>=2)MultiPrintStream.out.println("Unhandled System Message!!!");
|
if(DEBUG>=2) MultiPrintStream.out.println("Unhandled System Message!!!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
// Hand the data off to our worker thread
|
// Hand the data off to our worker thread
|
||||||
if(worker != null){
|
if(worker != null){
|
||||||
if(DEBUG>=3)MultiPrintStream.out.println("Worker Message!!!");
|
if(DEBUG>=3) MultiPrintStream.out.println("Worker Message!!!");
|
||||||
worker.processData(this, socketChannel, rspData);
|
worker.processData(this, socketChannel, rspData);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if(DEBUG>=1)MultiPrintStream.out.println("Unhandled Worker Message!!!");
|
if(DEBUG>=1) MultiPrintStream.out.println("Unhandled Worker Message!!!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,19 +34,19 @@ public class SystemWorker extends ThreadedEventWorker {
|
||||||
@Override
|
@Override
|
||||||
public void messageEvent(WorkerDataEvent event) {
|
public void messageEvent(WorkerDataEvent event) {
|
||||||
try {
|
try {
|
||||||
if(NioNetwork.DEBUG>=2)MultiPrintStream.out.println("System Message: "+event.data.getClass().getName());
|
if(NioNetwork.DEBUG>=2) MultiPrintStream.out.println("System Message: "+event.data.getClass().getName());
|
||||||
if(event.data instanceof Message){
|
if(event.data instanceof Message){
|
||||||
if(event.data instanceof EchoMessage && ((EchoMessage)event.data).echo()){
|
if(event.data instanceof EchoMessage && ((EchoMessage)event.data).echo()){
|
||||||
// Echos back the recived message
|
// Echos back the recived message
|
||||||
((EchoMessage)event.data).recived();
|
((EchoMessage)event.data).recived();
|
||||||
if(NioNetwork.DEBUG>=3)MultiPrintStream.out.println("Echoing Message: "+event.data);
|
if(NioNetwork.DEBUG>=3) MultiPrintStream.out.println("Echoing Message: "+event.data);
|
||||||
nio.send(event.socket, event.data);
|
nio.send(event.socket, event.data);
|
||||||
}
|
}
|
||||||
else if(event.data instanceof ResponseRequestMessage &&
|
else if(event.data instanceof ResponseRequestMessage &&
|
||||||
rspEvents.get(((ResponseRequestMessage)event.data).getResponseId()) != null){
|
rspEvents.get(((ResponseRequestMessage)event.data).getResponseId()) != null){
|
||||||
// Handle the response
|
// Handle the response
|
||||||
handleResponse(((ResponseRequestMessage)event.data).getResponseId(), event.data);
|
handleResponse(((ResponseRequestMessage)event.data).getResponseId(), event.data);
|
||||||
if(NioNetwork.DEBUG>=3)MultiPrintStream.out.println("Response Request Message: "+event.data);
|
if(NioNetwork.DEBUG>=3) MultiPrintStream.out.println("Response Request Message: "+event.data);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
//Services
|
//Services
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ public class Torrent {
|
||||||
private boolean is_private;
|
private boolean is_private;
|
||||||
|
|
||||||
public Torrent(File torrent) throws IOException{
|
public Torrent(File torrent) throws IOException{
|
||||||
this(FileFinder.getFileContent(FileFinder.find("C:\\Users\\Ziver\\Desktop\\test.torrent")));
|
this(FileFinder.getFileContent( torrent ));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Torrent(String data){
|
public Torrent(String data){
|
||||||
|
|
@ -51,8 +51,9 @@ public class Torrent {
|
||||||
is_private = false;
|
is_private = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private void decode(String data){
|
private void decode(String data){
|
||||||
HashMap dataMap = (HashMap)TorrentParser.decode(data);
|
HashMap<?,?> dataMap = (HashMap<?,?>)TorrentParser.decode(data);
|
||||||
|
|
||||||
name = (String)dataMap.get("name");
|
name = (String)dataMap.get("name");
|
||||||
comment = (String)dataMap.get("comment");
|
comment = (String)dataMap.get("comment");
|
||||||
|
|
@ -65,4 +66,37 @@ public class Torrent {
|
||||||
info_hash = (HashMap)dataMap.get("info");
|
info_hash = (HashMap)dataMap.get("info");
|
||||||
is_private = (((Integer)dataMap.get("private")) != 0);
|
is_private = (((Integer)dataMap.get("private")) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ************** GETTER **************
|
||||||
|
public String getName(){
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public String getComments(){
|
||||||
|
return comment;
|
||||||
|
}
|
||||||
|
public long getDate(){
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
public ArrayList<String> getFileList(){
|
||||||
|
return file_list;
|
||||||
|
}
|
||||||
|
public long getSize(){
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
public String getAuthor(){
|
||||||
|
return created_by;
|
||||||
|
}
|
||||||
|
public String getMainTracker(){
|
||||||
|
return main_tracker;
|
||||||
|
}
|
||||||
|
public ArrayList<String> getTrackerList(){
|
||||||
|
return tracker_list;
|
||||||
|
}
|
||||||
|
public HashMap<String,Object> getInfoHash(){
|
||||||
|
return info_hash;
|
||||||
|
}
|
||||||
|
public boolean isPrivate(){
|
||||||
|
return is_private;
|
||||||
|
}
|
||||||
|
// ************************************
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,12 @@ public class DynamicByteArrayStream extends InputStream{
|
||||||
private ArrayList<byte[]> bytes;
|
private ArrayList<byte[]> bytes;
|
||||||
/** The current size of the stream */
|
/** The current size of the stream */
|
||||||
private int size;
|
private int size;
|
||||||
/** points to the current index in the Arraylist */
|
/** points to the current index in the ArrayList */
|
||||||
private int globalPointer;
|
private int byteArrayIndex;
|
||||||
/** points localy in the current index in the ArrayList */
|
/** points locally in the current index in the ArrayList */
|
||||||
private int localPointer;
|
private int localPointer;
|
||||||
/** The current position */
|
/** The current position */
|
||||||
private int currentPos;
|
private int pos;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of DynamicByteArrayStream
|
* Create a new instance of DynamicByteArrayStream
|
||||||
|
|
@ -22,9 +22,9 @@ public class DynamicByteArrayStream extends InputStream{
|
||||||
public DynamicByteArrayStream(){
|
public DynamicByteArrayStream(){
|
||||||
bytes = new ArrayList<byte[]>();
|
bytes = new ArrayList<byte[]>();
|
||||||
size = 0;
|
size = 0;
|
||||||
globalPointer = 0;
|
byteArrayIndex = 0;
|
||||||
localPointer = 0;
|
localPointer = 0;
|
||||||
currentPos = 0;
|
pos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -36,71 +36,77 @@ public class DynamicByteArrayStream extends InputStream{
|
||||||
size += b.length;
|
size += b.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized int read() throws IOException {
|
||||||
|
if(pos >= size) return -1;
|
||||||
|
|
||||||
|
int ret = bytes.get(byteArrayIndex)[localPointer] & 0xff;
|
||||||
|
pos++;
|
||||||
|
localPointer++;
|
||||||
|
if(localPointer >= bytes.get(byteArrayIndex).length){
|
||||||
|
byteArrayIndex++;
|
||||||
|
localPointer = 0;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized int read(byte b[], int off, int len) {
|
||||||
|
//System.out.println("*****************************************************");
|
||||||
|
//System.out.println("off: "+off+" len: "+len);
|
||||||
|
//System.out.println("size: "+size+" arraylen: "+bytes.size());
|
||||||
|
//System.out.println("pos: "+pos+" localPointer: "+localPointer);
|
||||||
|
if(len <= 0) return 0;
|
||||||
|
if(pos >= size) return -1;
|
||||||
|
|
||||||
|
int bytes_read = 0;
|
||||||
|
if(pos+len >= size) len = size - pos;
|
||||||
|
for(int i=0; i<len ;i++){
|
||||||
|
byte[] src = bytes.get(byteArrayIndex);
|
||||||
|
if(localPointer+len-i >= src.length){
|
||||||
|
//System.out.println("1");
|
||||||
|
int length = src.length-localPointer;
|
||||||
|
System.arraycopy(src, localPointer, b, off+i, length);
|
||||||
|
|
||||||
|
localPointer = 0;
|
||||||
|
byteArrayIndex++;
|
||||||
|
bytes_read += length;
|
||||||
|
i += length;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//System.out.println("2");
|
||||||
|
int length = len-i;
|
||||||
|
System.arraycopy(src, localPointer, b, off+i, length);
|
||||||
|
|
||||||
|
localPointer += length;
|
||||||
|
bytes_read += length;
|
||||||
|
i += length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos += len;
|
||||||
|
//System.out.println("new_pos: "+pos+" read: "+bytes_read);
|
||||||
|
return bytes_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized int available() {
|
||||||
|
return size - pos;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears this stream from the byte arrays
|
* Clears this stream from the byte arrays
|
||||||
*/
|
*/
|
||||||
public synchronized void clear(){
|
public synchronized void clear(){
|
||||||
size = 0;
|
size = 0;
|
||||||
globalPointer = 0;
|
reset();
|
||||||
localPointer = 0;
|
|
||||||
currentPos = 0;
|
|
||||||
|
|
||||||
bytes.clear();
|
bytes.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized int read() throws IOException {
|
|
||||||
if(currentPos >= size){
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
int ret = bytes.get(globalPointer)[localPointer] & 0xff;
|
|
||||||
currentPos++;
|
|
||||||
localPointer++;
|
|
||||||
if(localPointer >= bytes.get(globalPointer).length){
|
|
||||||
globalPointer++;
|
|
||||||
localPointer = 0;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
public synchronized int read(byte b[], int off, int len) {
|
|
||||||
System.out.println("read off:"+off+" len: "+len);
|
|
||||||
if(currentPos+off >= size){
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
off += localPointer;
|
|
||||||
while(off>0){
|
|
||||||
if(bytes.get(globalPointer).length < off){
|
|
||||||
globalPointer++;
|
|
||||||
off -= bytes.get(globalPointer).length;
|
|
||||||
}
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
|
|
||||||
int length;
|
|
||||||
int oldLen = len;
|
|
||||||
while(len > 0){
|
|
||||||
length = bytes.get(globalPointer).length;
|
|
||||||
System.arraycopy(b, 0, bytes.get(globalPointer), 0, (length<len ? length : len));
|
|
||||||
len -= length;
|
|
||||||
if(len > 0) globalPointer++;
|
|
||||||
if(bytes.size() <= globalPointer) break;
|
|
||||||
}
|
|
||||||
localPointer = 0;
|
|
||||||
currentPos += ( len<0 ? oldLen : oldLen-len);
|
|
||||||
return ( len<0 ? oldLen : oldLen-len);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
public synchronized int available() {
|
|
||||||
return size - currentPos;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void reset() {
|
public synchronized void reset() {
|
||||||
globalPointer = 0;
|
byteArrayIndex = 0;
|
||||||
localPointer = 0;
|
localPointer = 0;
|
||||||
currentPos = 0;
|
pos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
|
//bytes = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue