Changeset 8928


Ignore:
Timestamp:
2005-01-24T16:13:34+13:00 (19 years ago)
Author:
sjboddie
Message:

Altered phind so it works correctly over an https connection
(DataInputStream.available() seems always to return 0 for https connections
under Sun java, though it works ok with Microsoft's VM).

Location:
trunk/gsdl/src/java/org/nzdl/gsdl/Phind
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/java/org/nzdl/gsdl/Phind/Makefile

    r2687 r8928  
    1616
    1717%.class : %.java
    18     javac -d . -classpath ../../../.. $<
     18    javac -target 1.1 -d . -classpath ../../../.. $<
  • trunk/gsdl/src/java/org/nzdl/gsdl/Phind/Phind.java

    r7788 r8928  
    8787
    8888import java.net.URL;
     89import java.net.URLConnection;
    8990import java.io.DataInputStream;
     91import java.io.InputStream;
    9092
    9193import java.net.Socket;
     
    207209    mode = initMode;
    208210   
     211        System.out.println("Java vendor: " + System.getProperty("java.vendor"));
     212        System.out.println("Java version: " + System.getProperty("java.version"));
     213
    209214    // Read applet parameters
    210215    getParameters();
     
    473478    try {
    474479        URL phindcgi = new URL(query);
    475         DataInputStream in = new DataInputStream(phindcgi.openStream());
     480            //            DataInputStream in = new DataInputStream(phindcgi.openStream());
     481
     482            URLConnection conn = phindcgi.openConnection();
     483            DataInputStream in = new DataInputStream(conn.getInputStream());
     484
    476485        byte[] buffer;
    477486        int availableBytes = 0;
    478487
     488       
    479489        while (!area.finished) {
     490        // Lovely old method, which would work just fine if Sun supported their own API and made
     491        // in.available() work.
     492
     493        /*
    480494        availableBytes = in.available();
    481495        if (availableBytes == 0) {
     
    489503             *  DLConsulting 12-07-2004
    490504             *  Start...
    491              */
     505             
     506                    System.err.println("No bytes available");
    492507            if(time >= timeout) {
    493508            throw new InterruptedIOException(
     
    503518            }
    504519            time++;
    505             /** ...End */
     520            /** ...End
    506521        } else {
     522                    System.err.println("Preparing to read " + availableBytes + " bytes");
    507523            buffer = new byte[availableBytes];
    508524            in.read(buffer);
     525                    System.err.println("Done reading");
    509526            area.parseBytes(buffer);
    510527        }
    511         }
     528        */
     529
     530        // New - and hopefully safer - method.
     531
     532        // Create a new SafeReader, and then wait while it reads
     533        // we can watch for timeouts as necessary
     534        int timeout = 0;
     535        SafeReader reader = new SafeReader(in);
     536        reader.start();
     537        while(!reader.isComplete()) {
     538            // Wait timeout time.
     539            try {
     540            Thread.sleep(1000);
     541            }
     542            catch(InterruptedException exception) {
     543            System.err.println("test.init(): Unexpected InterruptedException. Non-fatal.");
     544            exception.printStackTrace();
     545            }
     546            // Then see if the readers read anything
     547            if(reader.poll() == 0) {
     548            timeout++;
     549            // Have we reached timeout?
     550            if(timeout > reader.getTimeout()) {
     551                // Nothing been read. Kill the reader
     552                reader.interrupt();
     553                // And throw a hissy fit
     554                throw new InterruptedIOException("No data recieved in " + reader.getTimeout() +
     555                                 " seconds. Connection timed-out.");
     556            }
     557            // Otherwise, wait a little longer.
     558            }
     559            // Otherwise we can continue waiting and polling
     560        }
     561        // Process the buffer
     562        System.err.println("Done reading");
     563        area.parseBytes(reader.getBuffer());
     564            }
    512565       
    513566        in.close();
     
    9541007    }
    9551008
    956 
    957 
    958 
    959 
     1009    public class SafeReader
     1010    extends Thread {
     1011    // Has the reader finished reading?
     1012    private boolean complete;
     1013    // The buffer in where the data read is stored
     1014    private byte[] buffer;
     1015    // The stream this reader reads from
     1016    private DataInputStream in;
     1017    // If the bytes_read hasn't changed in this time, then stop this thread
     1018    // and timeout
     1019    private int timeout;
     1020    // The current buffer size
     1021    private int buffer_current_size;
     1022    // The maximum buffer size
     1023    private int buffer_max_size;
     1024    // The number of bytes read through this reader since the last poll
     1025    private int bytes_read;
     1026
     1027    /** Default constructor
     1028     *  @param in the DataInputStream to read from
     1029     */
     1030    public SafeReader(DataInputStream in) {
     1031        complete = false;
     1032        buffer_current_size = 0;
     1033        buffer_max_size = 16;
     1034        buffer = new byte[buffer_max_size]; // Default size
     1035        timeout = 60; // Sixty seconds
     1036        bytes_read = 0;
     1037       
     1038        this.in = in;
     1039    }
     1040
     1041    /** Construct a new SafeReader
     1042     *  @param in the DataInputStream to read from
     1043     *  @param timeout how long this thread should wait when reading data
     1044     */
     1045    public SafeReader(DataInputStream in, int timeout) {
     1046        this(in);
     1047        this.timeout = timeout;
     1048    }
     1049
     1050    /** Retrieve the current buffer
     1051     *  @return a byte[] buffer trimmed to the appropriate size
     1052     */
     1053    public byte[] getBuffer() {
     1054        byte[] final_buffer = new byte[buffer_current_size];
     1055        System.arraycopy(buffer, 0, final_buffer, 0, buffer_current_size);
     1056        return final_buffer;
     1057    }
     1058
     1059    /** Retrieve the current timeout
     1060     *  @return the timeout as an int
     1061     */
     1062    public int getTimeout() {
     1063        return timeout;
     1064    }
     1065
     1066    /** See whether the reader has finished reading
     1067     *  @return true if the read is complete (-1 bytes read!) or false otherwise
     1068     */
     1069    public synchronized boolean isComplete() {
     1070        return complete;
     1071    }
     1072
     1073    /** Poll the reader to determine if it has read any bytes recently
     1074     *  @return the number of bytes read
     1075     */
     1076    public long poll() {
     1077        // Maybe should test if complete - but its only one second right?
     1078        long result = bytes_read;
     1079        bytes_read = 0;
     1080        return result;
     1081    }
     1082
     1083    /** Try to read bytes from the input stream. Note the caller is responsible for closing the datainputstream
     1084     */
     1085    public void run() {
     1086        try {
     1087        int i = 0;
     1088        byte[] read_buffer = new byte[8];
     1089        while ((i = in.read(read_buffer)) != -1) {
     1090            // Check our storage buffer is big enough
     1091            if(buffer_max_size - (buffer_current_size + i) <= 0) {
     1092            // Double the size
     1093            buffer_max_size = 2 * buffer_max_size;
     1094            byte[] new_buffer = new byte[buffer_max_size];
     1095            System.arraycopy(buffer, 0, new_buffer, 0, buffer_current_size);
     1096            buffer = new_buffer;
     1097            }
     1098            // Then copy in the read buffer
     1099            System.arraycopy(read_buffer, 0, buffer, buffer_current_size, i);
     1100            buffer_current_size += i;
     1101            bytes_read += i;
     1102        }
     1103        setComplete(true);
     1104        }
     1105        catch(IOException exception) {
     1106        System.err.println("SafeReader.run(): Unexpected IOException. Fatal.");
     1107        exception.printStackTrace();
     1108        }
     1109    }
     1110
     1111    /** Set the complete boolean to the desired state, synchronized to avoid race with isComplete()
     1112     *  @param value the desired state
     1113     */
     1114    private synchronized void setComplete(boolean value) {
     1115        complete = value;
     1116    }
     1117    }
    9601118}
    9611119
Note: See TracChangeset for help on using the changeset viewer.