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
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public String dumpToString( Object o ) {
|
||||
private String dumpToString( Object o , String head) {
|
||||
if(o == null) return "NULL";
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
Class oClass = o.getClass();
|
||||
buffer.append( oClass.getName() );
|
||||
String nextHead = head + "\t";
|
||||
// Prints out Arrays
|
||||
if ( oClass.isArray() ) {
|
||||
buffer.append( "[" );
|
||||
for ( int i=0; i<Array.getLength(o) ;i++ ) {
|
||||
if ( i > 0 )
|
||||
buffer.append( ", " );
|
||||
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( "]" );
|
||||
}
|
||||
// Prints out a list
|
||||
|
|
@ -238,10 +259,14 @@ public class MultiPrintStream extends PrintStream {
|
|||
buffer.append( "{" );
|
||||
while(it.hasNext()){
|
||||
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())
|
||||
buffer.append( ", " );
|
||||
buffer.append( "," );
|
||||
}
|
||||
buffer.append( "\n" );
|
||||
buffer.append(head);
|
||||
buffer.append( "}" );
|
||||
}
|
||||
// Prints out a Map
|
||||
|
|
@ -251,43 +276,53 @@ public class MultiPrintStream extends PrintStream {
|
|||
while(it.hasNext()){
|
||||
Object key = it.next();
|
||||
Object value = ((Map)o).get(key);
|
||||
buffer.append("\n");
|
||||
buffer.append(nextHead);
|
||||
buffer.append( key );
|
||||
buffer.append( "=>" );
|
||||
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
|
||||
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
||||
if(it.hasNext())
|
||||
buffer.append( ", " );
|
||||
buffer.append( "," );
|
||||
}
|
||||
buffer.append( "\n" );
|
||||
buffer.append(head);
|
||||
buffer.append( "}" );
|
||||
}
|
||||
// Prints out data from InputStream
|
||||
else if(o instanceof InputStream){
|
||||
buffer.append( " =>{ \n" );
|
||||
buffer.append( " =>{\n" );
|
||||
try {
|
||||
InputStream in = (InputStream)o;
|
||||
int tmp;
|
||||
while((tmp = in.read()) != -1){
|
||||
buffer.append(nextHead);
|
||||
buffer.append( (char)tmp );
|
||||
}
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(this);
|
||||
}
|
||||
buffer.append( "\n}" );
|
||||
buffer.append( "\n" );
|
||||
buffer.append(head);
|
||||
buffer.append( "}" );
|
||||
}
|
||||
// Prints out data from InputStream
|
||||
else if(o instanceof Reader){
|
||||
buffer.append( " =>{ \n" );
|
||||
buffer.append( " =>{\n" );
|
||||
try {
|
||||
Reader in = (Reader)o;
|
||||
int tmp;
|
||||
while((tmp = in.read()) != -1){
|
||||
buffer.append(nextHead);
|
||||
buffer.append( (char)tmp );
|
||||
}
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(this);
|
||||
}
|
||||
buffer.append( "\n}" );
|
||||
buffer.append( "\n" );
|
||||
buffer.append(head);
|
||||
buffer.append( "}" );
|
||||
}
|
||||
// Prints out Object properties
|
||||
else{
|
||||
|
|
@ -295,20 +330,24 @@ public class MultiPrintStream extends PrintStream {
|
|||
while ( oClass != null ) {
|
||||
Field[] fields = oClass.getDeclaredFields();
|
||||
for ( int i=0; i<fields.length; i++ ) {
|
||||
if ( buffer.length() > 1 )
|
||||
buffer.append( ", " );
|
||||
fields[i].setAccessible( true );
|
||||
buffer.append("\n");
|
||||
buffer.append(head);
|
||||
buffer.append( fields[i].getName() );
|
||||
buffer.append( "=" );
|
||||
try {
|
||||
Object value = fields[i].get(o);
|
||||
if (value != null) {
|
||||
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
|
||||
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
||||
}
|
||||
} catch ( IllegalAccessException e ) {}
|
||||
if ( i+1<fields.length )
|
||||
buffer.append( "," );
|
||||
}
|
||||
oClass = oClass.getSuperclass();
|
||||
}
|
||||
buffer.append( "\n" );
|
||||
buffer.append(head);
|
||||
buffer.append( "}" );
|
||||
}
|
||||
|
||||
|
|
@ -321,8 +360,11 @@ public class MultiPrintStream extends PrintStream {
|
|||
private boolean dumbCapable(Object o){
|
||||
if(o != null){
|
||||
if(o.getClass().isArray()) return true;
|
||||
else if(o instanceof Collection)return true;
|
||||
else if(o instanceof Map)return true;
|
||||
else if(o instanceof Collection<?>)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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ public class MySQLQueue<E> implements Queue<E>{
|
|||
return poll();
|
||||
}
|
||||
|
||||
public boolean addAll(Collection arg0) {
|
||||
public boolean addAll(Collection<? extends E> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
|
@ -119,7 +119,7 @@ public class MySQLQueue<E> implements Queue<E>{
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean containsAll(Collection arg0) {
|
||||
public boolean containsAll(Collection<?> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ public class MySQLQueue<E> implements Queue<E>{
|
|||
return (peek() != null);
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
public Iterator<E> iterator() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
|
@ -144,12 +144,12 @@ public class MySQLQueue<E> implements Queue<E>{
|
|||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean removeAll(Collection arg0) {
|
||||
public synchronized boolean removeAll(Collection<?> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection arg0) {
|
||||
public boolean retainAll(Collection<?> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,9 @@ import java.io.InputStreamReader;
|
|||
import java.io.PrintStream;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.security.auth.login.AccountException;
|
||||
|
||||
|
|
@ -51,7 +53,8 @@ public class FTPClient extends Thread{
|
|||
|
||||
public static void main(String[] args){
|
||||
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.removeDir("./ziver/lol");
|
||||
|
||||
|
|
@ -67,7 +70,12 @@ public class FTPClient extends Thread{
|
|||
MultiPrintStream.out.dump(client.getFileList("./ziver"));
|
||||
client.removeFile("./ziver/test.txt");
|
||||
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();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
@ -150,7 +158,7 @@ public class FTPClient extends Thread{
|
|||
* @return The input line
|
||||
* @throws IOException
|
||||
*/
|
||||
private synchronized String readCommand(boolean print) throws IOException{
|
||||
public synchronized String readCommand(boolean print) throws IOException{
|
||||
String tmp = in.readLine();
|
||||
if(print)System.out.println(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
|
||||
* left to read and returns the last line
|
||||
* Multiple
|
||||
*
|
||||
* @param print To print out the received lines
|
||||
* @return The last received line
|
||||
* @throws IOException
|
||||
|
|
@ -225,28 +233,28 @@ public class FTPClient extends Thread{
|
|||
|
||||
/**
|
||||
* Returns information about the file or directory
|
||||
* (This is system specific information)
|
||||
*
|
||||
* TODO:
|
||||
* @deprecated DOSENT WORK!!!!
|
||||
* @deprecated
|
||||
* @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
|
||||
*/
|
||||
public String getFileInfo(String path) throws IOException{
|
||||
StringBuffer info = new StringBuffer("");
|
||||
public ArrayList<String[]> getFileInfo(String path) throws IOException{
|
||||
Pattern regex = Pattern.compile("\\s{1,}");
|
||||
ArrayList<String[]> info = new ArrayList<String[]>();
|
||||
|
||||
BufferedReader data_in = getDataInputStream();
|
||||
sendCommand("LIST "+path, true);
|
||||
sendCommand("LIST "+path, DEBUG);
|
||||
|
||||
String tmp = "";
|
||||
while((tmp = data_in.readLine()) != null){
|
||||
info.append(tmp);
|
||||
System.err.println(tmp);
|
||||
info.add(regex.split(tmp));
|
||||
}
|
||||
|
||||
data_in.close();
|
||||
readCommand(DEBUG);
|
||||
return info.toString();
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -32,11 +32,11 @@ public abstract class NioNetwork implements Runnable {
|
|||
/**
|
||||
* Debug level
|
||||
* 0 = nothing
|
||||
* 1 = connection debug
|
||||
* 1 = connection info
|
||||
* 2 = message debug
|
||||
* 3 = selector debug
|
||||
*/
|
||||
public static final int DEBUG = 2;
|
||||
public static int DEBUG = 2;
|
||||
public static enum NetworkType {SERVER, CLIENT};
|
||||
|
||||
private NetworkType type;
|
||||
|
|
@ -139,7 +139,7 @@ public abstract class NioNetwork implements Runnable {
|
|||
* @param data The data to send
|
||||
*/
|
||||
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
|
||||
synchronized (pendingWriteData) {
|
||||
List<ByteBuffer> queue = pendingWriteData.get(socket);
|
||||
|
|
@ -157,7 +157,7 @@ public abstract class NioNetwork implements Runnable {
|
|||
// Indicate we want the interest ops set changed
|
||||
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
|
||||
selector.wakeup();
|
||||
}
|
||||
|
|
@ -175,11 +175,11 @@ public abstract class NioNetwork implements Runnable {
|
|||
case ChangeRequest.CHANGEOPS:
|
||||
SelectionKey key = change.socket.keyFor(selector);
|
||||
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;
|
||||
case ChangeRequest.REGISTER:
|
||||
change.socket.register(selector, change.ops);
|
||||
if(DEBUG>=3)MultiPrintStream.out.println("register socket ");
|
||||
if(DEBUG>=3) MultiPrintStream.out.println("register socket ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -188,31 +188,31 @@ public abstract class NioNetwork implements Runnable {
|
|||
|
||||
// Wait for an event one of the registered channels
|
||||
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
|
||||
Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
|
||||
while (selectedKeys.hasNext()) {
|
||||
SelectionKey key = (SelectionKey) selectedKeys.next();
|
||||
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()) {
|
||||
// Check what event is available and deal with it
|
||||
if (key.isAcceptable()) {
|
||||
if(DEBUG>=3)MultiPrintStream.out.println("Accepting Connection!!");
|
||||
if(DEBUG>=3) MultiPrintStream.out.println("Accepting Connection!!");
|
||||
accept(key);
|
||||
}
|
||||
else if (key.isConnectable()) {
|
||||
if(DEBUG>=3)MultiPrintStream.out.println("Finnishing Connection!!");
|
||||
if(DEBUG>=3) MultiPrintStream.out.println("Finnishing Connection!!");
|
||||
finishConnection(key);
|
||||
}
|
||||
else if (key.isWritable()) {
|
||||
if(DEBUG>=3)MultiPrintStream.out.println("Writing");
|
||||
if(DEBUG>=3) MultiPrintStream.out.println("Writing");
|
||||
write(key);
|
||||
}
|
||||
else if (key.isReadable()) {
|
||||
if(DEBUG>=3)MultiPrintStream.out.println("Reading");
|
||||
if(DEBUG>=3) MultiPrintStream.out.println("Reading");
|
||||
read(key);
|
||||
}
|
||||
}
|
||||
|
|
@ -269,7 +269,7 @@ public abstract class NioNetwork implements Runnable {
|
|||
clients.remove(remoteAdr);
|
||||
pendingReadData.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!!!");
|
||||
return;
|
||||
}
|
||||
|
|
@ -282,7 +282,7 @@ public abstract class NioNetwork implements Runnable {
|
|||
clients.remove(remoteAdr);
|
||||
pendingReadData.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!!!");
|
||||
return;
|
||||
}
|
||||
|
|
@ -294,25 +294,24 @@ public abstract class NioNetwork implements Runnable {
|
|||
if(encrypter != null)// Encryption
|
||||
rspByteData = encrypter.decrypt(rspByteData);
|
||||
|
||||
/*
|
||||
// Message Count 1m: 36750
|
||||
// Message Count 1s: 612
|
||||
if(!pendingReadData.containsKey(socketChannel)){
|
||||
pendingReadData.put(socketChannel, new DynamicByteArrayStream());
|
||||
}
|
||||
if(encrypter != null)// Encryption
|
||||
rspByteData = encrypter.decrypt(rspByteData);
|
||||
DynamicByteArrayStream dynBuf = pendingReadData.get(socketChannel);
|
||||
dynBuf.add(rspByteData);
|
||||
|
||||
pendingReadData.get(socketChannel).add(rspByteData);
|
||||
*/
|
||||
|
||||
Object rspData = null;
|
||||
try{
|
||||
rspData = Converter.toObject(rspByteData);
|
||||
//rspData = Converter.toObject(pendingReadData.get(socketChannel));
|
||||
//rspData = Converter.toObject(rspByteData);
|
||||
rspData = Converter.toObject(dynBuf);
|
||||
handleRecivedMessage(socketChannel, rspData);
|
||||
//pendingReadData.get(socketChannel).clear();
|
||||
dynBuf.clear();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
//pendingReadData.get(socketChannel).reset();
|
||||
dynBuf.reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -326,18 +325,16 @@ public abstract class NioNetwork implements Runnable {
|
|||
List<ByteBuffer> queue = pendingWriteData.get(socketChannel);
|
||||
if(queue == null){
|
||||
queue = new ArrayList<ByteBuffer>();
|
||||
pendingWriteData.put(socketChannel, queue);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
// Write until there's not more data ...
|
||||
while (!queue.isEmpty()) {
|
||||
ByteBuffer buf = queue.get(0);
|
||||
i += buf.remaining();
|
||||
socketChannel.write(buf);
|
||||
i -= buf.remaining();
|
||||
if (buf.remaining() > 0) {
|
||||
// ... 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;
|
||||
}
|
||||
queue.remove(0);
|
||||
|
|
@ -347,31 +344,32 @@ public abstract class NioNetwork implements Runnable {
|
|||
// We wrote away all data, so we're no longer interested
|
||||
// in writing on this socket. Switch back to waiting for
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(systemWorker != null){
|
||||
if(DEBUG>=3)MultiPrintStream.out.println("System Message!!!");
|
||||
if(DEBUG>=3) MultiPrintStream.out.println("System Message!!!");
|
||||
systemWorker.processData(this, socketChannel, rspData);
|
||||
}
|
||||
else{
|
||||
if(DEBUG>=2)MultiPrintStream.out.println("Unhandled System Message!!!");
|
||||
if(DEBUG>=2) MultiPrintStream.out.println("Unhandled System Message!!!");
|
||||
}
|
||||
}
|
||||
else{
|
||||
// Hand the data off to our worker thread
|
||||
if(worker != null){
|
||||
if(DEBUG>=3)MultiPrintStream.out.println("Worker Message!!!");
|
||||
if(DEBUG>=3) MultiPrintStream.out.println("Worker Message!!!");
|
||||
worker.processData(this, socketChannel, rspData);
|
||||
}
|
||||
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
|
||||
public void messageEvent(WorkerDataEvent event) {
|
||||
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 EchoMessage && ((EchoMessage)event.data).echo()){
|
||||
// Echos back the recived message
|
||||
((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);
|
||||
}
|
||||
else if(event.data instanceof ResponseRequestMessage &&
|
||||
rspEvents.get(((ResponseRequestMessage)event.data).getResponseId()) != null){
|
||||
// Handle the response
|
||||
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{
|
||||
//Services
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public class Torrent {
|
|||
private boolean is_private;
|
||||
|
||||
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){
|
||||
|
|
@ -51,8 +51,9 @@ public class Torrent {
|
|||
is_private = false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void decode(String data){
|
||||
HashMap dataMap = (HashMap)TorrentParser.decode(data);
|
||||
HashMap<?,?> dataMap = (HashMap<?,?>)TorrentParser.decode(data);
|
||||
|
||||
name = (String)dataMap.get("name");
|
||||
comment = (String)dataMap.get("comment");
|
||||
|
|
@ -65,4 +66,37 @@ public class Torrent {
|
|||
info_hash = (HashMap)dataMap.get("info");
|
||||
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;
|
||||
/** The current size of the stream */
|
||||
private int size;
|
||||
/** points to the current index in the Arraylist */
|
||||
private int globalPointer;
|
||||
/** points localy in the current index in the ArrayList */
|
||||
/** points to the current index in the ArrayList */
|
||||
private int byteArrayIndex;
|
||||
/** points locally in the current index in the ArrayList */
|
||||
private int localPointer;
|
||||
/** The current position */
|
||||
private int currentPos;
|
||||
private int pos;
|
||||
|
||||
/**
|
||||
* Create a new instance of DynamicByteArrayStream
|
||||
|
|
@ -22,9 +22,9 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
public DynamicByteArrayStream(){
|
||||
bytes = new ArrayList<byte[]>();
|
||||
size = 0;
|
||||
globalPointer = 0;
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
currentPos = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -36,71 +36,77 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
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
|
||||
*/
|
||||
public synchronized void clear(){
|
||||
size = 0;
|
||||
globalPointer = 0;
|
||||
localPointer = 0;
|
||||
currentPos = 0;
|
||||
|
||||
reset();
|
||||
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() {
|
||||
globalPointer = 0;
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
currentPos = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
//bytes = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue