Read and Write Protocols


May be obsolete since discussion on Monday, Sept. 7.

FULL READ (filename, filepath)
    1. Open main.txt metadata file.
        1.1 Traverse to metadata file of the filepath directory.
    2. Enumerate list of mirrors (plus pointer to first mirror - to avoid parsing from top every time).
    3. Create new thread/process (listening for data).
    4. Send request to first mirror through CLIENT_PORT to Server:SERVER_PORT:
        Request Contents: Filename, Hash, new thread/process port FILEIO_PORT.
        4.1 Initiate timer.
    5. If reply timed-out OR (Server) reply with BUSY:
        5.1 Repeat from 4 with next mirror.
       Else if (Server) hash matches:
           5.2 (Server) Reply with ACK:
           5.3 (Server) Send file to Client:FILEIO_PORT.
        5.4 Save data in temporary file /filename.
        (5.5) On completion, is there any need to cross-acknowledge end of transmission?
    6. Close FILEIO_PORT.


WRITE (filename, filepath)
    1. Get MIRROR_LIST.
    2. Read LOOSE_THRESHOLD (max. no. of files per mirror allowed) value.
    3. Read mirror list and find first mirror with no_of_files_stored<LOOSE_THRESHOLD.
        If no mirror qualifies, find the mirror with smallest no_of_files_stored.
    4. Send request to chosen mirror:
        Request Contents: filesize
        4.1 (Mirror) Check disk space minus RESERVED_SPACE.
        4.2 (Mirror) If space available:
            4.2.1 (Mirror) Add filesize to RESERVED_SPACE.
            4.2.2 (Mirror) Create a new thread/process with port no. FILEIO_PORT.
            4.2.3 (Mirror) Send ACK to client containing FILEIO_PORT.
                4.2.4 (Mirror) Send a NO_SPACE_AVAILABLE response to client.
                4.2.5 Repeat from 3 for another mirror.
    5. Send filename, hash to Mirror:FILEIO_PORT.
    6. (Mirror) If another file with filename already present, find the lowest integer X for with filenameX is not already present.
        6.1 (Mirror) Save (filename,filenameX) association in FILENAME_ASSOCIATIONS.
        6.2 (Mirror) Send ACK.
    7. Send file to Mirror_FILEIO_PORT.
    8. (Mirror) Save file data under name filenameX.
    9. Repeat from 3 for 2 more mirrors.


Additional file suggested: MIRROR_LIST
    Contents: (id of mirror) | (no. of owned files currently being stored on the mirror)
        Mirror1 | no_of_files_stored
        Mirror2 | no_of_files_stored
        Add/Remove mirror
        Shuffle order
        Increment/Decrement file count

Additional data structure: RESERVED_SPACE
    Type: numeric
    Value: stores the disk space currently reserved for an expected Write request.
Additional file suggested: FILENAME_ASSOCIATION
    Contents: (original filename) | (filename on disk)
        filename | filenameX
        filename | filenameX
        Add/Remove (filename,filenameX) association        Rename filename