001: // ICPSender.java
002: // /$Id: ICPSender.java,v 1.4 2000/08/16 21:38:04 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // please first read the full copyright statement in file COPYRIGHT.HTML
005:
006: package org.w3c.www.protocol.http.icp;
007:
008: import java.net.DatagramPacket;
009: import java.net.DatagramSocket;
010: import java.net.InetAddress;
011: import java.net.SocketException;
012: import java.net.URL;
013:
014: import java.io.IOException;
015:
016: class ICPSender {
017: /**
018: * The filter we are attached to.
019: */
020: protected ICPFilter filter = null;
021: /**
022: * Our target host address.
023: */
024: protected InetAddress addr = null;
025: /**
026: * Our target port number.
027: */
028: protected int port = -1;
029: /**
030: * The HTTP service provided by the target ICP host.
031: */
032: protected URL proxy = null;
033: /**
034: * The buffer to emit messages.
035: */
036: protected byte buffer[] = null;
037:
038: /**
039: * Return a String representation of this sender.
040: * @return A String.
041: */
042:
043: public String toString() {
044: return getAddress() + "/" + getPort();
045: }
046:
047: /**
048: * Get the HTTP service location of that sender.
049: * @return The HTTP service location, as a URL.
050: */
051:
052: public final URL getProxyLocation() {
053: return proxy;
054: }
055:
056: /**
057: * Get the ICP address for this neighbor.
058: * @return An InetAddress instance.
059: */
060:
061: public final InetAddress getAddress() {
062: return addr;
063: }
064:
065: /**
066: * Get the port number for that sender target.
067: * @return An integer port number.
068: */
069:
070: public final int getPort() {
071: return port;
072: }
073:
074: /**
075: * Send the given ICP message to our target host.
076: * @param msg The ICP message to send.
077: * @return A boolean, <strong>true</strong> if message was emitted.
078: */
079:
080: public boolean send(ICPMessage msg) {
081: // Encode the message in a byte array:
082: int len = msg.toByteArray(buffer);
083: if (len < 0) {
084: buffer = new byte[-len + 1];
085: len = msg.toByteArray(buffer);
086: }
087: // Wrap it up in a datagram:
088: DatagramPacket p = new DatagramPacket(buffer, len, addr, port);
089: try {
090: filter.getSocket().send(p);
091: return true;
092: } catch (IOException ex) {
093: }
094: // FIXME Should mark that host down
095: return false;
096: }
097:
098: /**
099: * Create a ICPSender to query the given host.
100: * @param addr The InetAddress of the ICP host.
101: * @param port The port number it is listening on.
102: * @exception SocketException If we couldn't create the datagram socket.
103: */
104:
105: ICPSender(ICPFilter filter, int srcport, InetAddress addr,
106: int dstport, URL proxy) throws SocketException {
107: this .filter = filter;
108: this .addr = addr;
109: this .port = dstport;
110: this .proxy = proxy;
111: this .buffer = new byte[512];
112: }
113:
114: }
|