messages.h

/*
 * File Name:     messages.h
 * Author:        Jade Cheng
 * Date:          March 29, 2009
 * Course:        ICS 451
 * Assignment:    Project 2
 */

#ifndef MESSAGES_H_
#define MESSAGES_H_

/** The maximum size in bytes for all messages.*/
#define MESSAGE_BUFFER_SIZE 1200

/** The maximum number of items in the listing messages.*/
#define MAX_LISTING_COUNT 56

/** The maximum number of items in the try messages. */
#define MAX_TRY_COUNT \
    ((MESSAGE_BUFFER_SIZE - (2 + NAME_BUFFER_SIZE + 2)) / \
    (sizeof(u_short) + sizeof(u_long)))

/** Data corresponding to a data message. */
typedef struct data_msg_t {

    /** The name of the file contained in the data message. */
    char name[NAME_BUFFER_SIZE];

    /** The contents of the file contained in the data message. */
    u_char data[MAX_CONTENT_LENGTH];

    /** The length of the contents in bytes of the data message. */
    u_short size;

} data_msg_t;

/** Data corresponding to a listing message. */
typedef struct listing_msg_t {

    /** The array of names contained in the listing message. */
    char entries[MAX_LISTING_COUNT][NAME_BUFFER_SIZE];

    /** The number of items in the listing message.*/
    u_short count;

} listing_msg_t;

/** The possible message types. */
typedef enum msg_type {

    msg_type_unknown,
    msg_type_data,
    msg_type_listing,
    msg_type_request,
    msg_type_try

} msg_type;

/** Data corresponding to a message buffer that is sent or received. */
typedef struct msg_buf_t {

    /** The contents of the message. */
    u_char buffer[MESSAGE_BUFFER_SIZE];

    /** The size of the message in bytes. */
    size_t size;

    /** An index indicating the current read position in the buffer. */
    size_t index;

} msg_buf_t;

/** Data corresponding to a request message. */
typedef struct request_msg_t {

    /** The name of file being requested. */
    char name[NAME_BUFFER_SIZE];

} request_msg_t;

/** Data corresponding to a try message. */
typedef struct try_msg_t {

    /** The name of the file. */
    char name[NAME_BUFFER_SIZE];

    /** A collection of peers to try. */
    peer_t peers[MAX_TRY_COUNT];

    /** The number of peers. */
    u_short count;

} try_msg_t;

/**
 * Parses a message buffer as a data message.
 *
 * @param src A message buffer.
 * @param dst A data message.
 *
 * @return EXIT_SUCCESS or EXIT_FAILURE.
 */
extern int data_msg_read(msg_buf_t * src, data_msg_t * dst);

/**
 * Writes a data message to a message buffer.  The message buffer can be used
 * for UDP sending.
 *
 * @param dst A message buffer.
 * @param src A data message.
 */
extern void data_msg_write(msg_buf_t * dst, const data_msg_t * src);

/**
 * Returns the message type from a message buffer based on the header.
 *
 * @param src A message buffer.
 *
 * @return The message type.
 */
extern msg_type get_msg_type(msg_buf_t * src);

/**
 * Parses a message buffer as a listing message.
 *
 * @param src A message buffer.
 * @param dst A listing message.
 *
 * @return EXIT_SUCCESS or EXIT_FAILURE.
 */
extern int listing_msg_read(msg_buf_t * src, listing_msg_t * dst);

/**
 * Writes a listing message to a message buffer.  The message bufer can be
 * used for UDP sending.
 *
 * @param dst A message buffer.
 * @param src A listing message.
 */
extern void listing_msg_write(msg_buf_t * dst, listing_msg_t * src);

/**
 * Parses a message buffer as a request message.
 *
 * @param src A message buffer.
 * @param dst A request message.
 *
 * @return EXIT_SUCCESS or EXIT_FAILURE.
 */
extern int request_msg_read(msg_buf_t * src, request_msg_t * dst);

/**
 * Writes a request message to a message buffer.  The message buffer can be
 * used for UDP sending.
 *
 * @param dst A message buffer.
 * @param src A request message.
 */
extern void request_msg_write(msg_buf_t * dst, request_msg_t * src);

/**
 * Parses a message buffer as a try message.
 *
 * @param src A message buffer.
 * @param dst A try message.
 *
 * @return EXIT_SUCCESS or EXIT_FAILURE.
 */
extern int try_msg_read(msg_buf_t * src, try_msg_t * dst);

/**
 * Writes a try message to a message buffer.  The message buffer can be used
 * for UDP sending.
 *
 * @param dst A message buffer.
 * @param src A try message.
 */
extern void try_msg_write(msg_buf_t * dst, try_msg_t * src);

#endif /* MESSAGES_H_ */
Valid HTML 4.01 Valid CSS