View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.net;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.Socket;
25  
26  import org.xnap.util.QuotedStringTokenizer;
27  
28  public class DownloadSocket extends IncomingSocket {
29  
30      //--- Data Field(s) ---
31      
32      public String filename;
33      public long filesize;
34      public String nick;
35  
36      //--- Constructor(s) ---
37  
38      public DownloadSocket(Socket socket, InputStream in) throws IOException
39      {
40  	super(socket, in);
41  
42  	byte data[] = new byte[2048];
43  	int i = in.read(data);
44  	if (i > 0) {
45  	    String response = new String(data, 0, i);
46  		    
47  	    QuotedStringTokenizer t = new QuotedStringTokenizer(response);
48  		    
49  	    if (t.countTokens() < 3) {
50  		throw new IOException("invalid request: " + response);
51  	    }
52  
53  	    nick = t.nextToken();
54  	    filename = t.nextToken();
55  	    try {
56  		filesize = Long.parseLong(t.nextToken());
57  	    }
58  	    catch (NumberFormatException e) {
59  		throw new IOException("invalid request: " + response);
60  	    }
61  
62  	    logger.info("download response from " + nick + " for " + filename);
63  	}
64  	else {
65  	    throw new IOException("empty request");
66  	}
67      }
68  
69      public DownloadSocket(String nick, String filename, long filesize)
70      {
71  	this.nick = nick;
72  	this.filename = filename;
73  	this.filesize = filesize;
74      }
75  
76      //--- Method(s) ---
77  
78      public boolean equals(Object obj)
79      {
80  	if (obj != null) {
81  	    if (obj instanceof DownloadSocket) {
82  		DownloadSocket d = (DownloadSocket)obj;
83  		return (nick.equals(d.nick) && filename.equals(d.filename)
84  			&& filesize == d.filesize);
85  	    }
86  	}
87  
88  	return false;
89      }
90  }
91  
92