/* 
 * BookShare Protocol
 * 
 * bs_proto.h
 *
 * BookShare protocol specification used both by the BookShare library
 * and the BookShare server. Namely it describes the format of the
 * PDU's exchanged between client-server: the request and the response
 * messages.
 * 
 */

#ifndef _BS_PROTO_H_
#define _BS_PROTO_H_

#include <config.h>


/*
 * BookShare PDU Codes
 *  - pdu identifier - bs_pdu_type_t
 *  - status returned by server - snfs_msg_res_status_t
 */


typedef enum {
	REQ_GIVE = 0,
	REQ_SEARCH = 1,
	REQ_CANCEL = 2,
	REQ_TAKE = 3,
	REQ_RELEASE = 4,
	REQ_RETURN = 5
} bs_pdu_type_t;


typedef enum {
	RES_OK = 1,
	RES_ERROR = 0
} bs_pdu_res_status_t;


/*
 * PDU Give
 *   - request message: bs_pdu_req_give_t
 */


typedef struct {
	char titles[MAX_TITLES_SIZE];
} bs_pdu_req_give_t;


/*
 * PDU Search
 *   - request message: bs_pdu_req_search_t
 *   - response message: bs_pdu_res_search_t
 */


typedef struct {
	char username[MAX_USERNAME_SIZE];
	char password[MAX_PASSWORD_SIZE];
	char titles[MAX_TITLES_SIZE];
	int weight;
} bs_pdu_req_search_t;


typedef struct {
	char title[MAX_BOOKNAME_SIZE];
	char counter[MAX_COUNTER_SIZE];
	int weight;
	char neighbours[MAX_NEIGHBOURS_SIZE];
	char reason[MAX_REASON_SIZE];
} bs_pdu_res_search_t;


/*
 * PDU Cancel
 *   - request message: bs_pdu_req_cancel_t
 */


typedef struct {
	char username[MAX_USERNAME_SIZE];
	char titles[MAX_TITLES_SIZE];
} bs_pdu_req_cancel_t;


/*
 * PDU Take
 *   - request message: bs_pdu_req_take_t
 *   - response message: bs_pdu_res_take_t
 */


typedef struct {
	char username[MAX_USERNAME_SIZE];
	char titlecs[MAX_TITLES_SIZE];
} bs_pdu_req_take_t;


typedef struct {
	char title[MAX_BOOKNAME_SIZE];
	char counter[MAX_COUNTER_SIZE];
	char reason[MAX_REASON_SIZE];
	int size;
	char* cover;
} bs_pdu_res_take_t;


/*
 * PDU Release
 *   - request message: bs_pdu_req_release_t
 *   - response message: bs_pdu_res_release_t
 */


typedef struct {
	char username[MAX_USERNAME_SIZE];
	char titles[MAX_TITLES_SIZE];
	int makeavailable;
} bs_pdu_req_release_t;


typedef struct {
	char title[MAX_BOOKNAME_SIZE];
	char counter[MAX_COUNTER_SIZE];
	char reason[MAX_REASON_SIZE];
} bs_pdu_res_release_t;


/*
 * PDU Return
 *   - request message: bs_pdu_req_return_t
 *   - response message: bs_pdu_res_return_t
 */


typedef struct {
	char titles[MAX_TITLES_SIZE];
} bs_pdu_req_return_t;


typedef struct {

} bs_pdu_res_return_t;


/*
 * BookShare PDUs
 *
 * PDUs have a common field ('type') indicating the type of
 * PDU that the request/response refers to; the union contains
 * all the possible PDU formats both referring to requests and
 * responses. Response PDUs have also the 'status' field
 * indicating if the service succeeded.
 * 
 *   - request message: bs_pdu_req_t
 *   - response message: bs_pdu_res_t
 */


typedef struct {
   bs_pdu_type_t type;
   union {
      bs_pdu_req_give_t give;
      bs_pdu_req_search_t search;
      bs_pdu_req_cancel_t cancel;
      bs_pdu_req_take_t take;
      bs_pdu_req_release_t release;
      bs_pdu_req_return_t returns;
   } body;
} bs_pdu_req_t;


typedef struct {
   bs_pdu_type_t type;
   bs_pdu_res_status_t status;
   union {
      bs_pdu_res_search_t search;
      bs_pdu_res_take_t take;
      bs_pdu_res_release_t release;
      bs_pdu_res_return_t returns;
   } body;
} bs_pdu_res_t;


#endif
