Applet input stream buffering delay, how to stop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolsti
    Contributor
    • Mar 2008
    • 310

    Applet input stream buffering delay, how to stop?

    I have an applet that accepts data as a stream from a php script using the following code snippet:

    Code:
               String dataendpoint = "http://phpfunction.php";
    
                URL url = new URL(dataendpoint);
                InputStream is = url.openStream();
                String line;
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr,0);
                while ((line = br.readLine()) != null) {
                   // my code here to handle each line of data which prepares it for
                   // showing on a line graph.
                }
    The variable dataendpoint actually comes into the applet as a parameter, I am just showing something here for clarity.

    The intention is to have PHP read existing datafiles and produce the initial data for this applet which is then plotted on a graph in the browser. After this initial data is shown, the data will then continue to be streamed as new data is produced. So the PHP code is like this (shown as pseudocode):

    Code:
       // open data files, read lines, and send to standard output using echo
       // flush data using ob_flush() and flush() calls
       while (1) {
          sleep(30);
          // get new data, if any, then send to standard output
          // flush buffers again
       }
    Here is the problem: When I leave off the while loop and just let the script spit out the contents of the datafiles and exit, everything works fine and my graph appears immediately in my applet.

    With the while loop, however, nothing appears for a very long time delay. After this time delay, everything works fine, and my graph is updated every 30 seconds when new data appears, just as I want it to.

    The problem is how to avoid this long delay time which is occurring? I have done a Wireshark trace and see that the data from the files is being sent immediately to the browser. Putting print statements in my Java code shows me that the code is blocking at this line:

    InputStream is = url.openStream( );

    Can anyone tell me what is happening here and what to do about it?

    Thanks for any help!!!

    Steve, Denmark
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Two remarks:

    1) what happens when you put that 'sleep(30)' at the end of your while loop?
    2) you can't instantiate a BufferedReader with a buffer size of zero (0).

    kind regards,

    Jos

    Comment

    • coolsti
      Contributor
      • Mar 2008
      • 310

      #3
      Hi Jos,
      yes you are correct, the code I showed above does not work because of the buffer size of 0. That was a cut and paste of the code during my experimentation . I found out afterwards that it doesn't work.

      Putting the sleep at the end of the infinite while loop is not easy to do as that would mean I have a steady amount of data to send out, which is not the case here (the data comes from another process and the PHP script will poll to see if there is any new data to send, otherwise sleep).

      It may be interesting, however, to try to produce fake data to see if the browser and applet reacts after a certain minimum of data has been fed to it. Since I can send comment lines to my applet this would then be one way to get around the problem, if it works (am home now, will try on Monday) but it would be nice if there was a real solution to the problem.

      Steve, Denmark

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Originally posted by coolsti
        Putting the sleep at the end of the infinite while loop is not easy to do as that would mean I have a steady amount of data to send out, which is not the case here (the data comes from another process and the PHP script will poll to see if there is any new data to send, otherwise sleep).
        ? I don't understand how this is the case, if you put the sleep as the last statement inside the while loop.
        With your current setup there is a mandatory 30 second wait even if it is unecessary. Since it isn't returning anything, this may be causing problems on the other side with opening the stream. With Josah's modification, wouldn't it output the data it has, then wait 30 seconds before it checks again?

        Comment

        • coolsti
          Contributor
          • Mar 2008
          • 310

          #5
          I may be missing something or maybe my explanation of the situation is not so clear. Let me try again. My PHP script opens a data file and dumps the contents of it out to the browser with echo statements. When it reaches the end of the data file the script does not finish and exit. Instead, I wait some time with the sleep() function, then I look to see if there is new data. If there is, I send it also out to the browser and wait again. This is why the sleep here is at the start of the while loop.

          The PHP script is just an input stream for the applet in the client browser. It supplies a chunk of data at once, and then some more data whenever data is ready. The applet plots this data as a scrolling time graph.

          Now, this works if I have the PHP script exit before doing the while loop, where I then send only the current data in the file to be plotted on the applet. It also works with the while loop, but only after a rather long delay time, where the applet just appears to be waiting for data. After the delay time I get the scrolling update of the applet plot as I want.

          My question here is why do I get this delay, which is apparently caused by the url.openstream( ) call not returning. I am flushing the PHP script data before and during the while loop. I can see from sniffing that the data is indeed sent to the browser. So there is something in the browser or in the Java within the browser that decides that it should wait before letting the Java program get past the openstream() call.

          I cannot see why this happens, unless something within the browser is causing a buffering of the input stream before letting the openstream() return.

          Can anyone help?

          Comment

          Working...