repository_class Class Reference

repository class More...

#include <repository.h>

Collaboration diagram for repository_class:
[legend]

List of all members.

Classes

struct  file
 a particular file More...
struct  location
 location of a file More...
struct  ltstr
 comparison operator for standard template library map More...
struct  section
 a section in the repository More...

Public Member Functions

 repository_class ()
 constructor
void add_file (string file_name, int sock, string ip_address, int port, int max_clients)
 member function to add a file
int remove_file (string file_name, int sock)
 member function to remove a file
int obtain_file (string file_name, string *ip_address, int *port, int cli_sock)
 member function to obtain location info on a file
int release_file (string file_name, int cli_sock)
 member function to release a previously obtained file
void wait_file (string file_name)
 member function to wait for a particular file to become available

Private Member Functions

int section_number (char c)
 produce the index in the archive

Private Attributes

section archive [SEC_NUM]
 the real repository is this archive


Detailed Description

repository class

this class describes the structure and methods of a repository the repository has the following structure:

Definition at line 26 of file repository.h.


Constructor & Destructor Documentation

repository_class::repository_class (  ) 

constructor

the repository is always initialized empty

Definition at line 6 of file repository.cpp.

References archive, and SEC_NUM.


Member Function Documentation

void repository_class::add_file ( string  file_name,
int  sock,
string  ip_address,
int  port,
int  max_clients 
)

member function to add a file

it adds a file to the repository, working as follows:

  • if file already exists, add something only if the location specified for that file does not exists
  • if file does not exists, add a new file structure in the section with the new location in the list if something is changed in the repository, then some client waiting for notification on a condition variable associated with the file can be signaled, and that's what we do: we signal ALL the threads currently waiting on the condition variable, the reason to do that is explained in the specifics of release_file()

See also:
release_file()
A FS can store in the DFR one file location at a time, if more (and maybe different) locations are specified the repository will simply do nothing

Parameters:
file_name | name of the file to add
sock | socket associated to the FS on the DFR
ip_address | ip_address of a FS that holds the file
port | port on which the FS wants to be contacted
max_clients | maximum number of clients for this file on the FS

Definition at line 20 of file repository.cpp.

References archive, repository_class::repository_class::location::cur_clients, DEB, repository_class::repository_class::section::files, repository_class::repository_class::file::free, repository_class::repository_class::file::locations, repository_class::repository_class::location::max_clients, repository_class::repository_class::location::port, section_number(), repository_class::repository_class::location::serv_ip_address, repository_class::repository_class::location::serv_sock, and repository_class::repository_class::file::waiting_clients.

Referenced by FS_req_s_body().

int repository_class::obtain_file ( string  file_name,
string *  ip_address,
int *  port,
int  cli_sock 
)

member function to obtain location info on a file

it works as follows:

  • if file is not found it returns -1 (error)
  • if file is found but maximum total number of clients is reached then it returns 1 (error)
  • if file is found and there exists a location with free client slots, then FS informations are returned with a 0 int value
    Parameters:
    file_name | name of the file the CL needs to get
    ip_address | pointer to return the ip_address of the FS
    port | pointer to return the port on the FS
    cli_sock | socket identifying the client on the DFR
    Returns:
    0 if ok, -1 if file not found, 1 if FSs at full capacity

Definition at line 101 of file repository.cpp.

References archive, DEB, repository_class::repository_class::section::files, repository_class::repository_class::file::locations, and section_number().

Referenced by serve_start_file_transfer().

int repository_class::release_file ( string  file_name,
int  cli_sock 
)

member function to release a previously obtained file

it works as follows:

  • if the file is not found returns -1 (error)
  • if the file is found but the client socket is not present in the active clients, then the function returns -1 (error)
  • if the file is found and the client socket is present in the active clients, then the client is removed from the list, and the allocated FS slot is freed (this means the condition must also be signaled)

the condition may be signaled, but we have to signal ALL the threads currently waiting for that condition variable, the reason to do that can be explained with an example:

  • the first client registers a notify and crashes
  • the second client registers a notify on the same file and crashes
  • if we make just one signal when it's freed or created then the first or the second client related threads is awaken
  • this thread goes on sending the notify message to the client
  • the client is down, so an error is shown
  • the thread of the crashed client cannot signal another client because it hasn't got the file in it's used files, so it can release the file

note that this function returns an error if the client needs to free a file but the File Server has unregistered that file... this is no problem... but the client needs to be prepared to receive an error.

Parameters:
file_name | name of the file to be released
cli_sock | socket of the client on the DFR
Returns:
0 if ok, -1 if file not found or client not in active clients

Definition at line 130 of file repository.cpp.

References archive, DEB, repository_class::repository_class::section::files, repository_class::repository_class::file::free, repository_class::repository_class::file::locations, and section_number().

Referenced by CL_req_s_body(), and serve_end_file_transfer().

int repository_class::remove_file ( string  file_name,
int  sock 
)

member function to remove a file

it removes a specified file location, as follows:

  • if file does not exists or specified location is not found on the particular file, the function returns with a -1 (error)
  • if the specified file and location are found than the location is deleted, if the deleted location was the last location for that file than the file structure is NOT ALWAYS deleted, infact we some thread MAY still waiting on the condition variable for the file to become available (which could happen in the future). To delete the file structure, we need to be sure that no clients are waiting on the condition variable (free)... if so we can safely remove the structure.

Parameters:
file_name | name of the file to remove
sock | socket on the DFR of the FS that holds the file
Returns:
0 if removal was successful, -1 otherwise

Definition at line 68 of file repository.cpp.

References archive, DEB, repository_class::repository_class::section::files, repository_class::repository_class::file::locations, section_number(), and repository_class::repository_class::file::waiting_clients.

Referenced by FS_req_s_body().

int repository_class::section_number ( char  c  )  [private]

produce the index in the archive

using the first character of the filename, this function produces the index (in the archive array) of the appropriate section

Parameters:
c | first character of the filename
Returns:
the index of the appropriate section for the file

Definition at line 12 of file repository.cpp.

References SEC_NUM.

Referenced by add_file(), obtain_file(), release_file(), remove_file(), and wait_file().

void repository_class::wait_file ( string  file_name  ) 

member function to wait for a particular file to become available

if a file does not exist yet or if there is no FS slots left for a client, the client can use the wait_file() function to wait for a particular file to become available, FS creating files, or clients releasing files can signal the waiting clients, the awaken clients must test again the condition and block again if necessary

Parameters:
file_name | name of the file the client wants to be notified about

Definition at line 161 of file repository.cpp.

References archive, DEB, repository_class::repository_class::section::files, repository_class::repository_class::file::free, repository_class::repository_class::file::locations, repository_class::repository_class::section::mutex, section_number(), and repository_class::repository_class::file::waiting_clients.

Referenced by serve_reg_notification().


Member Data Documentation

section repository_class::archive[SEC_NUM] [private]

the real repository is this archive

Definition at line 84 of file repository.h.

Referenced by add_file(), obtain_file(), release_file(), remove_file(), repository_class(), and wait_file().


The documentation for this class was generated from the following files:

Generated on Sat Jun 13 21:06:30 2009 for DistributedFileRepository by  doxygen 1.5.8