Changeset 38908 for main/trunk


Ignore:
Timestamp:
2024-04-06T22:41:44+13:00 (2 months ago)
Author:
anupama
Message:

Tried to replace calls to deprecated and dangerous applet stop(). But one problem remains constant between using stop() and the new, custom stopRunning() when GsdlCollageApplet is run in webswing: Unless the application is manually exited (when run as an application in webswing, the exit button is available), I don't see stop() or stopRunning() called in the log at packages/tomcat/bin/logs/webswing.log. Yet is webswing always relaunching the app and calling its init() method, which would start creating new threads and downloading and other things? What happens to the objects never terminated and garbage collected unless explicitly exited? How do I in Java detect that the webswing browser window has been redirected to another page? TODO: Maybe this can be resolved with a Java-JavaScript communication between webswing and the app. TODO2: MyAffineTransform line 39, why is there a division by zero error at times when the app window dimensions ought to always be positive now? TODO3: I want the status bar gone. It is not being used anyway and at present, sometimes the outlines of its dimensions appear, even though they shouldn't. This occasional behaviour (whether the MyAffineTransform error or status bar) is another bother for debugging.

Location:
main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/CURL.java

    r38871 r38908  
    135135    public void readAll() {
    136136    int value;
    137     while((value = read()) != -1) {
     137    //while((value = read()) != -1) {
     138    while(!Thread.currentThread().isInterrupted() && (value = read()) != -1) { 
     139    }
     140    try {
     141        input.close();
     142    } catch(IOException ioex) {
     143        System.err.println("CURL tried to close the inputstream, but got exception " + ioex);
     144    } finally {
     145        if(Thread.currentThread().isInterrupted()) {
     146        System.err.println("**** CURL.readAll() had been interrupted.");
     147        }       
    138148    }
    139149    }
     
    159169    else {
    160170        try {
    161         value = input.read();
     171        value = input.read(); // what if this blocks?? How can this be made interruptible?
    162172        } catch (Exception e) {
    163173        e.printStackTrace();
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/Display.java

    r38853 r38908  
    1010 *  @author Katrina Edgar */
    1111public class Display extends Thread {
    12 
     12   
    1313    GsdlCollageApplet app_ = null;
    1414
     
    1616    super("Display");
    1717    app_ = app;
     18
     19    System.err.println("@@@@ Someone called new Display()");
    1820   
    1921    }
    2022
    21 
     23    // Do we even use this class? If so, maybe a separate stopRunning() method is better
     24    // As I'm not sure if there needs to be synchronisation done around app_.isStopping()?   
    2225    /** Runs display thread by repeatedly calling the next frame,
    2326     *  waiting for a specified delay period between calls */
     
    2932        Thread curr_thread = Thread.currentThread();
    3033       
    31         while (curr_thread == this) {
     34        while (!app_.isStopping() && curr_thread == this) {
    3235        app_.repaint();
    3336        // can't make it zero or it hogs the whole processor
     
    3639        }
    3740
     41        if(app_.isStopping() && app_.verbosity() >= 3) {
     42        System.err.println("*** Display.run() thread has been told to stop.");
     43        }
    3844        System.err.println("Stopped display thread.");
    3945       
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/DisplayImages.java

    r38889 r38908  
    1818 *  to create movement when no new images have been downloaded. */
    1919public class DisplayImages extends Thread {
    20 
    21 
     20   
    2221    /** Will fire a removal operation when 4 images in the collage overlap a single area */
    2322    static final int NO_IMAGES_OF_OVERLAP = 4;
     23
     24    private boolean stop_running = false;
    2425   
    2526    /** To determine if there are several images overlapped, can remove the
     
    141142            Thread curr_thread = Thread.currentThread();
    142143           
    143             while (curr_thread == next_frame_) {
     144            while (!isStopping() && curr_thread == next_frame_) {
    144145            try {       
    145146                Runtime rt = Runtime.getRuntime();
     
    158159            }
    159160                   
    160            
     161            if(isStopping() && DisplayImages.this.app_.verbosity() >= 3) {
     162            System.err.println("*** DisplayImages inner thread next_frame has been told to stop.");
     163            }
    161164        }
    162165
     
    342345   
    343346    }
     347
     348    // some other thread can call this method to tell this thread to stop running
     349    public void stopRunning() {
     350    stop_running = true;
     351    if (app_.verbosity()>=3) {
     352        System.err.println("**** DisplayImages.stopRunning() called");
     353    }
     354    }
    344355 
    345  
     356    public boolean isStopping() {
     357    return stop_running;
     358    }
    346359
    347360    /** Paints the applet on the screen */
     
    421434        Thread curr_thread = Thread.currentThread();
    422435       
    423         while (curr_thread == this) {
     436        while (!isStopping() && curr_thread == this) {
    424437        if (inuse_ready_.size() >= 1){
    425438            Thread.sleep(1000);         
     
    438451           System.out.println("Display images thread is interrupted");
    439452    }
    440 
    441     System.out.println("Display images thread stoped");
     453   
     454    if(isStopping() && app_.verbosity() >= 3) {
     455        System.err.println("*** DisplayImages thread has been told to stop.");
     456    }
     457    System.out.println("DisplayImages thread stopped");
    442458         
    443459    }
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/DownloadUrls.java

    r38885 r38908  
    88import javax.swing.ImageIcon; //****
    99
     10//import org.apache.log4j.*;
    1011
    1112/**
     
    2122
    2223public class DownloadUrls extends Thread {
     24   
    2325    // for GS3   
    2426    String baseURL = null;
     
    3537    String document_root_ = null;
    3638
    37 
     39    /** When this thread is asked to stop running, this variable will be set to true */
     40    private boolean stop_running = false;   
     41
     42    /** When this thread is asked to stop downloading, this variable will be set to true.
     43     * For now the behaviour is the same as stop_running=true on this thread,
     44     * but in case it changes in the future, we have a separate variable.
     45     * Also, calling stopRunning() is not the same as setting stop_running = true, so
     46     * to be careful, a separate variable for stop_downloading could be safer when coding.
     47     */
     48    private boolean stop_downloading = false;
     49   
    3850    /** CHRIS - Holds the contents of the collection's assoc directory */
    3951    //    File[] assocDir_ = null;
     
    181193    }
    182194
    183    
    184 
     195    // some other thread can call this method to tell this thread to stop running
     196    public void stopRunning() {
     197    if (verbosity_ >= 3) {
     198        System.err.println("**** DownloadUrls.stopRunning() called");
     199    }
     200   
     201    stop_running = true;
     202    // Interrupt this thread, even if it's not the one running
     203    // Just want to make sure the DownloadURls' thread the CURL object runs in
     204    // gets interrupted if it's what's currently running
     205    if(!this.isInterrupted()) {
     206        this.interrupt();
     207    }
     208    if(!Thread.currentThread().isInterrupted()) {
     209        Thread.currentThread().interrupt();
     210    }
     211    }
     212
     213    public boolean isStopping() {
     214    return stop_running;
     215    }
    185216
    186217
     
    390421       try{
    391422           // if have completed the maximum number of downloads for the
    392            // application then stop
     423           // application then stop downloading
    393424           if (size == app_.maxDownloads()) {
    394            stop();
     425           // NOTE: the app can continue displaying images forever after download is
     426           // finished, until interrupted/stopped.
     427           // So don't set stop_running=false just because downloads have finished.
     428           //stop_running = true; // Don't do this!
     429           //thread_running = false;
     430           //thread.currentThread().interrupt();
     431
     432           stop_downloading = true;
     433           //stop(); // TODO, remove this, replacing with above
     434           
    395435           }
    396436           
     
    398438       catch (Exception e) {
    399439           thread_running_ = false;
    400            stop();
     440           //stop(); // TODO
     441           stop_downloading = true;
    401442           e.printStackTrace();
    402443       }
     
    412453    public void rec_add_images(String new_url, int depth)
    413454    {
    414 
    415455    if (verbosity_ >= 2) {
    416456        System.err.println("*** Inspecting url: " + new_url);
     
    424464
    425465    // connect to the url
     466    // stopRunning would have set the interrupted flag, and
     467    // CURL checks for that in its loop, *outside* its potentially-blocking read() call
    426468    CURL curl = (app_.gsdlversion == 3) ? new CURL(new_url, app_.baseURL) : new CURL(new_url);
    427469
     
    445487        }
    446488        // process each of the image links according to the parameters given.
    447         for (int i = 0; i < src_links.size(); i++)
     489        for (int i = 0; i < src_links.size() && !stop_running && !stop_downloading; i++)
    448490        {
    449491        URL url = (URL)src_links.get(i);
     
    491533        }
    492534
     535        if(stop_running && verbosity_ >= 3) {
     536        System.err.println("*** DownloadUrls.rec_add_images() - Asked to stop running");
     537        return;
     538        }
     539       
    493540        // get all the <code><a href=</code> links into a vector
    494541        Vector href_links = curl.getHrefLinks();
     
    500547
    501548        // process each of the href links according to the parameters given.
    502         for (int i = 0; i < href_links.size(); i++)
     549        for (int i = 0; i < href_links.size() && !stop_running && !stop_downloading; i++)
    503550        {
    504551           
     
    546593        System.err.println("Unable able to download "+new_url);
    547594    }
     595
     596    if(stop_running && verbosity_ >= 3) {
     597        System.err.println("*** DownloadUrls.rec_add_images() thread has been told to stop.");
     598    }
    548599    }
    549600
     
    566617        return;
    567618        }
    568        
     619
    569620        // open a url to the file written
    570621        URL u = new URL(starting_url_ + "externallinks");
     
    576627        String l = r.readLine();
    577628        // split the line of the space, first part is the image, second part the link
    578         while (l != null) {
     629        while (l != null && !stop_running && !stop_downloading) {
    579630   
    580631        String tmp1 = new String();
     
    600651        r.close();
    601652
     653        if(stop_running && verbosity_ >= 3) {
     654            System.err.println("*** DownloadUrls.externalLinks(): Asked to stop running");
     655        }
     656       
    602657    } catch (Exception e) {
    603658        e.printStackTrace();
     
    612667    visited_url_ = new Hashtable();
    613668    visited_images_ = new Hashtable();
    614 
    615         rec_add_images(starting_url_,1);
     669   
     670    rec_add_images(starting_url_,1);
    616671    download_images_.stopDownload();
    617     System.err.println("Download thread finished.");
     672    System.err.println("DownloadUrls.run() - download thread finished.");
    618673    }
    619674}
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/GsdlCollageApplet.java

    r38890 r38908  
    4848    int gsdlversion = 2;
    4949    String baseURL = null;
     50
     51    /** When asked to stop running, this variable will be set to true */
     52    private boolean stop_running = false;   
    5053   
    5154    /** Amount of error checking output produced <br>
     
    222225    this.X_DIM = (w <= 0) ? X_DIM : w;
    223226    this.Y_DIM = (h <= 0) ? Y_DIM : h;
     227
     228    // Attempting hack to circumvent division by zero error in MyAffineTransform line 39
     229    //DisplayImages.app_x_dim_ = X_DIM;
     230    //DisplayImages.app_y_dim_ = Y_DIM;
    224231   
    225232    try {
     
    459466    }
    460467
     468    // TODO: Do I need to follow
     469    // https://stackoverflow.com/questions/5861894/how-to-synchronize-or-lock-upon-variables-in-java
     470    public void stopRunning() {
     471    if(verbosity_ >= 3) {
     472        System.err.println("**** GsdlCollageApplet.stopRunning() called");
     473    }
     474   
     475    stop_running = true;
     476    if(download_thread_ != null) {
     477        download_thread_.stopRunning();
     478    }
     479    if(display_thread_ != null) {
     480        display_thread_.stopRunning();
     481    }
     482
     483    // The Display Thread?
     484    //if(!Thread.currentThread().isInterrupted()) {
     485    //    Thread.currentThread().interrupt();
     486    //}
     487    }
     488   
     489    public boolean isStopping() {
     490    return stop_running;
     491    }
     492
    461493    protected class CollageMouseAdapter extends MouseAdapter {
    462494 
     
    547579    }
    548580
    549     /** Stops threads for downloading and displaying images */
     581    /** Stops threads for downloading and displaying images */   
    550582    public void stop()
    551583    {
    552     download_thread_.stop();
    553     display_thread_.stop();
     584    //download_thread_.stop();
     585    //display_thread_.stop();
     586    stopRunning();
    554587   
    555588    download_thread_ = null;
     
    696729            collageApp.showStatus("Stopping threads");
    697730            System.err.println("\n\n*** Closing applet: stopping threads...");
    698             collageApp.stop();
    699             collageApp.destroy();
     731            collageApp.stopRunning();
     732            //collageApp.stop();
     733            //collageApp.destroy();
    700734        }
    701735        });
Note: See TracChangeset for help on using the changeset viewer.