Fixed some things

This commit is contained in:
Ziver Koc 2009-08-22 18:18:54 +00:00
parent c21b229882
commit 41c474d2a5
8 changed files with 239 additions and 138 deletions

View file

@ -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);
while((tmp = data_in.readLine()) != null){
System.err.println(tmp);
info.add(regex.split(tmp));
}
data_in.close();
readCommand(DEBUG);
return info.toString();
return info;
}
/**

View file

@ -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;
}
@ -293,26 +293,25 @@ public abstract class NioNetwork implements Runnable {
System.arraycopy(readBuffer.array(), 0, rspByteData, 0, numRead);
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);
pendingReadData.get(socketChannel).add(rspByteData);
*/
DynamicByteArrayStream dynBuf = pendingReadData.get(socketChannel);
dynBuf.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!!!");
}
}
}

View file

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

View file

@ -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;
}
// ************************************
}