/*
* File Name: data.h
* Author: Jade Cheng
* Date: March 29, 2009
* Course: ICS 451
* Assignment: Project 2
*/
#ifndef DATA_H_
#define DATA_H_
#include "common.h"
/** The maximum number of items that can exist in the data cache. */
#define MAX_DATA_CACHE_ITEMS 5
/** The maximum number of items that can exist in the content directory. */
#define MAX_CONTENT_DIR_ITEMS 16
/** The maximum number of requests that can exist at one time. */
#define MAX_REQUESTS 5
/** The maximum number of items that can exist in the local content. */
#define MAX_LOCAL_CONTENT_ITEMS (MAX_ARGS - 2)
/** The maximum number of peers that can be assigned from the command line. */
#define MAX_PEERS ((MAX_ARGS - 2) + MAX_CONTENT_DIR_ITEMS)
/** Data corresponding to one entry in the local content or the data cache. */
typedef struct content_item_t {
/** The name of the content. */
char name[NAME_BUFFER_SIZE];
/** The contents of the file. */
u_char buffer[MAX_CONTENT_LENGTH];
/** The length of the contents in bytes. */
size_t size;
} content_item_t;
/** Data corresponding to one item in the content directory. */
typedef struct content_dir_item_t {
/** The name of the file. */
char name[NAME_BUFFER_SIZE];
/** The peer that has the file. */
peer_t peer;
} content_dir_item_t;
/** Data corresponding to the content directory cache. */
typedef struct content_dir_t {
/** An array of items in the cache. */
content_dir_item_t items[MAX_CONTENT_DIR_ITEMS];
/**
* An array of values set to the time when a corresponding item in the
* items array has been accessed.
*/
timeval time[MAX_CONTENT_DIR_ITEMS];
/** The number of items in the content directory. */
size_t count;
} content_dir_t;
/** Data corresponding to the data cache. */
typedef struct data_cache_t {
/** An array of content items in the data cache. */
content_item_t items[MAX_DATA_CACHE_ITEMS];
/**
* An array of values set to the time when a corresponding item in the
* items array has been accessed.
*/
timeval time[MAX_DATA_CACHE_ITEMS];
/** The number of items in the data cache. */
size_t count;
} data_cache_t;
/** Data corresponding to the local content. */
typedef struct local_content_t {
/** An array of items in the local content. */
content_item_t items[MAX_LOCAL_CONTENT_ITEMS];
/** The number of items in the local content. */
size_t count;
} local_content_t;
/** Data corresponding to the cache of requests. */
typedef struct request_cache_t {
/** An array of requested file names. */
char names[MAX_REQUESTS][NAME_BUFFER_SIZE];
/**
* An array of values set to the time when a corresponding item in the
* names array has been accessed.
*/
timeval time[MAX_REQUESTS];
/** The number of items in the request cache. */
size_t count;
} request_cache_t;
/** Data corresponding to a collection of peers. */
typedef struct peers_t {
/** The array of peers. */
peer_t peers[MAX_PEERS];
/** The number of peers in the collection. */
size_t count;
} peers_t;
/**
* Adds an item to the content directory. The corresponding time for the item
* is set to the current time. If the item already exists, then the time is
* reset to the current time.
*
* @param dst The content directory.
* @param name The name of the item.
* @param peer The peer address and port.
*/
extern void content_dir_add(
content_dir_t * dst,
const char * name,
const peer_t * peer);
/**
* Removes all items from the content directory with a given name.
*
* @param dst The content directory.
* @param name The name of the items to remove.
*/
extern void content_dir_remove(content_dir_t * dst, const char * name);
/**
* Finds an item with a given name in the content directory. The index
* argument is used as both input and output. For input, it must be the
* starting index for the search (the first time the function is called, it
* should be zero). For output, it contains the index of an item that matches
* the name. If no item is found with the name, the function returns
* EXIT_FAILURE and the value of index should be ignored. When an item is
* found, its corresponding time is reset to the current time.
*
* @param src The content directory.
* @param name The name of the item to find.
* @param index The starting index and then the index of the found item.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int content_dir_find(
content_dir_t * src,
const char * name,
size_t * index);
/**
* Prints the content directory.
*
* @param src The content directory.
*/
extern void content_dir_print(const content_dir_t * src);
/**
* Adds an item to the data cache. The corresponding time for the item is
* set to the current time.
*
* @param dst The data cache.
* @param name The name of the item.
* @param buffer The buffer of data.
* @param size The number of valid bytes in the buffer.
*/
extern void data_cache_add(
data_cache_t * dst,
const char * name,
const u_char * buffer,
size_t size);
/**
* Finds an item in the data cache. If the item is found, the corresponding
* time for the item is reset to the current time. When the item is found,
* the function returns EXIT_SUCCESS, and the index of the item is assigned to
* the index parameter.
*
* @param src The data cache.
* @param name The name of the item.
* @param index The index of the item to find.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int data_cache_find(
data_cache_t * src,
const char * name,
size_t * index);
/**
* Prints the data cache.
*
* @param src The data cache.
*/
extern void data_cache_print(const data_cache_t * src);
/**
* Adds an item to the local content. The function fails if the item already
* exists in the local content or if the item cannot be loaded from the file
* system.
*
* @param dst The local content.
* @param name The name of the item.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int local_content_add(local_content_t * dst, const char * name);
/**
* Finds an item in the local content. If the item is found, the function
* returns EXIT_SUCCESS and assigns the index of the item to the index
* parameter.
*
* @param src The local content.
* @param name The name of the item to find.
* @param index The index of the item that is found.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int local_content_find(
const local_content_t * src,
const char * name,
size_t * index);
/**
* Prints the local content.
*
* @param src The local content.
*/
extern void local_content_print(const local_content_t * src);
/**
* Adds a peer to a collection. If the peer already exists in the collection,
* it is not added, and the function returns EXIT_FAILURE.
*
* @param dst The peer collection.
* @param src The peer to add.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int peers_add(peers_t * dst, const peer_t * src);
/**
* Combines peers from a content directory and a peer collection. Duplicate
* peers are not added.
*
* @param dst The peer collection.
* @param src1 The content directory.
* @param src2 The peer colleciton.
*/
extern void peers_combine(
peers_t * dst,
const content_dir_t * src1,
const peers_t * src2);
/**
* Prints the collection of peers.
*
* @param peers The collection of peers.
*/
extern void peers_print(const peers_t * src);
/**
* Adds an item to the request cache. If the item is found, the corresponding
* time for the item is reset to the current time.
*
* @param dst The request cache.
* @param name The name of the item.
*/
extern void request_add(request_cache_t * dst, const char * name);
/**
* Expires old requests from the request cache. When an item expires, all
* items in the content directory that contain the same name are also deleted.
* The function returns EXIT_SUCCESS if no items are expired.
*
* @param dst1 The request cache.
* @param dst2 The content directory.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int request_expire(request_cache_t * dst1, content_dir_t * dst2);
/**
* Finds an item in the request cache with the given name. If the item is
* found, then the corresponding time is reset to the current time. If the
* item is found, the function returns EXIT_SUCCESS and assigns the index of
* the item found to the index parameter.
*
* @param src The request cache.
* @param name The name of the file.
* @param index The index of request that matched the name.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int request_find(
request_cache_t * src,
const char * name,
size_t * index);
/**
* Gets the next request timeout.
*
* @param src The request cache.
* @param dst The time value.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
extern int request_get_next_timeout(request_cache_t * src, timeval * dst);
/**
* Removes the request with a given index from the request cache.
*
* @param dst The request cache.
* @param index The index of the item to remove.
*/
extern void request_remove(request_cache_t * dst, size_t index);
/**
* Prints the request cache.
*
* @param src The request cache.
*/
extern void request_print(const request_cache_t * src);
#endif /* DATA_H_ */