001: /*
002: * Copyright (c) 2000 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.sandStorm.lib.Gnutella;
026:
027: import java.util.*;
028: import java.io.*;
029: import java.net.*;
030:
031: /**
032: * A Gnutella query hits packet.
033: */
034: public class GnutellaQueryHitsPacket extends GnutellaPacket {
035:
036: private static final boolean DEBUG = false;
037:
038: int port;
039: InetAddress address;
040: private static InetAddress localAddress;
041: int speed;
042: private GnutellaQueryHit hits[];
043: private GnutellaGUID hitsGUID;
044:
045: static {
046: try {
047: localAddress = InetAddress.getLocalHost();
048: } catch (UnknownHostException e) {
049: System.err
050: .println("GnutellaQueryHitsPacket: WARNING: Cannot obtain local IP address: "
051: + e.getMessage());
052: localAddress = null;
053: }
054: }
055:
056: /**
057: * Create a GnutellaQueryHitsPacket from the given payload.
058: */
059: public GnutellaQueryHitsPacket(byte[] payload)
060: throws UnknownHostException {
061: super (GNUTELLA_FN_QUERYHITS, payload);
062: //parsePayload();
063: }
064:
065: /**
066: * Create a GnutellaQueryHitsPacket from the given payload with the
067: * given GUID, ttl, and hops.
068: */
069: public GnutellaQueryHitsPacket(GnutellaGUID guid, int ttl,
070: int hops, byte[] payload) throws UnknownHostException {
071: super (guid, GNUTELLA_FN_QUERYHITS, ttl, hops, payload);
072: //parsePayload();
073: }
074:
075: /**
076: * Create a GnutellaQueryHitsPacket from the given GnutellaQueryHit
077: * array.
078: */
079: public GnutellaQueryHitsPacket(GnutellaQueryHit hits[]) {
080: super (GNUTELLA_FN_QUERYHITS, null);
081: this .address = localAddress;
082: this .port = DEFAULT_DOWNLOAD_PORT;
083: this .speed = DEFAULT_SPEED;
084: this .hits = hits;
085: this .hitsGUID = new GnutellaGUID();
086: }
087:
088: /**
089: * Create a GnutellaQueryHitsPacket with a single hit with the given
090: * index, size, and filename.
091: */
092: public GnutellaQueryHitsPacket(int index, int size, String filename) {
093: this ((GnutellaQueryHit[]) null);
094: //this.hits = new GnutellaQueryHit[1];
095: //this.hits[0] = new GnutellaQueryHit(index, size, filename);
096: }
097:
098: private void parsePayload() throws UnknownHostException {
099: int num_hits = payload[0];
100: port = readLEShort(payload, 1);
101:
102: String addr = (int) (payload[2] & 0xff) + "."
103: + (int) (payload[3] & 0xff) + "."
104: + (int) (payload[4] & 0xff) + "."
105: + (int) (payload[5] & 0xff);
106: address = InetAddress.getByName(addr);
107:
108: speed = readLEInt(payload, 7);
109: int off = 8;
110:
111: hits = new GnutellaQueryHit[num_hits];
112: for (int i = 0; i < num_hits; i++) {
113: hits[i] = new GnutellaQueryHit(payload, off);
114: off += hits[i].getSize();
115: }
116:
117: hitsGUID = new GnutellaGUID(payload, off);
118: }
119:
120: protected void prepareForSend() {
121:
122: //int sz = 11+16; // header plus hitsGUID
123:
124: //int hitssz = 0;
125: //for (int i = 0; i < hits.length; i++) {
126: // hitssz += hits[i].getSize();
127: //}
128: //payload = new byte[sz+hitssz];
129:
130: //payload[0] = (byte)hits.length;
131: //writeLEShort(port, payload, 1);
132: //byte addr[] = address.getAddress();
133: //payload[3] = addr[0];
134: //payload[4] = addr[1];
135: //payload[5] = addr[2];
136: //payload[6] = addr[3];
137: //writeLEInt(speed, payload, 7);
138:
139: //int off = 11;
140: //for (int i = 0; i < hits.length; i++) {
141: // hits[i].dump(payload, off);
142: // off += hits[i].getSize();
143: //}
144: //hitsGUID.dump(payload, off);
145: }
146:
147: public String toString() {
148: return "GnutellaQueryHitsPacket";
149: }
150:
151: }
|