Downloading Pages

    Hello Internet! I am Christopher Chianelli, the programmer for Book of Fame. These blog posts are to describe what I've been up to lately with the project. If you want to see what I've been up to before this blog post, see the design document.

    Since version 1.0, there has been a few changes with how the images from the server are downloaded. In version 1.0, we downloaded pages one at a time, and freed them whenever we weren't rendering them (when the images do not correspond to the previous two pages, the current two pages, or the next two pages). When an image is freed, it needs to be downloaded again.

Code wise, it looks like this:

Start:
    1. Download pages 1 through 6 in order

Whenever the user turns to the next page:
    1. Copy image from page n to page (n - 2) for n = 3 to 6 (the current two pages are now the previous two pages, and the next two pages are now the current two pages)
    2. Download the next two pages

Whenever the user turns to the previous page:
    1. Copy image from page n to page (n + 2) for n = 4 to 1 (the current two pages are now the next two pages, and the previous two pages are now the current two pages)
    2. Download the previous two pages

    This method has severals issues:

1. The user has to wait for the two pages to be completely downloaded before they can turn the page (otherwise the not completed download would override the correct image when it is completed)

2. Since we are downloading the entire image, it takes a while to download

3. The user needs to re-download pages they already visited after going four pages ahead or behind

    To remedy this, I made a few changes to how the images are downloaded:

1. I created a new class, ImageBuffer, which handles the downloading of images

2. The PageImages class no longer handles the downloading of images, but instead handles displaying the image on the proper page

3. Instead of downloading the entire image at once, we download a 25% quality one followed by a 100% quality one, quadrupling the speed at which a user can view a page

4. Instead of keeping just the six pages required for rendering, we keep an additional six (three before and three after) in a buffer

5. Instead of downloading the images one at a time, we download them all at once

6. We use a Dictionary ADT to tell the downloader subroutines where to put the image so the user can turn the pages when the images are downloading

    A Dictionary ADT (Abstract Data Type) is a data structure that allows us to lookup a value using a key. I will use my cable company's channel listing as an allegory for a Dictionary. With my cable company's channel listing, I can find out what channel number corresponds to my favourite channel (You can lookup what value correspond to what key). If my cable company decides that channel number should be used for another channel, they can change the channel listing (keys can take on new values, in this case, channels). My cable company could get an upgrade allowing them to host more channels, causing them to add them to the listing (you can insert keys into a Dictionary). Perhaps a competing cable company could buy exclusive rights to channel 1, causing my cable company to remove it from their listing (you can remove keys from a Dictionary).

    Now that you have a better idea of what a Dictionary is, I'll explain what my dictionary is used for. My Dictionary uses page numbers as keys and buffer slots as values. A downloader needs to know what buffer slot to send the downloaded image to. To accomplish this, the downloader consults the Dictionary after the image (both the 25% and 100% ones) is downloaded and sends the downloaded image to the buffer slot that corresponds with the image's page number. Whenever the user turns to the next/previous page, the Dictionary is updated so that the page numbers are connected to the correct buffer slots. If a page is too far ahead/behind, it is removed from the dictionary, which tells the corresponding downloader to stop downloading and to free its image.

    These changes added significant improvements to the loading times of Book of Fame, allowing users to turn pages as fast as they want. I'm currently working on mini-games, so you have that to look forward to when I post next. That's all for now! See ya!