/*
*========================================================================
*
*   SYSTEM   : AZN_PLAZA_LANE LIBRARY
*   FILE     : lb_fileq.c
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.09.05
*   Modified :
*   Remark   :
 *              
 *========================================================================
*/

#if 0
#include "file_ctrl.h"
#else
#include "lb_func_list.h"
#endif
#include "file_lb.h"

/*
 *========================================================================
 *
 *   Function : lb_file_que_chk()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : File Queue존재 및 Size로 유효한 Queue인지 check.
 *
 *========================================================================
*/

int lb_file_que_chk(que_name)
char    *que_name;
{
    struct    stat    st;

    if (lstat (que_name, &st) < 0)
    {
        return(QUE_STATFAIL);
    }
    else 
    {
        return(SUCCESS);
    }
}

/*
 *========================================================================
 *
 *   Function : lb_readchk_fileq_dt()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : File Queue에 읽어올 Data가 있는지 Check
 *
 *========================================================================
*/

int lb_readchk_fileq_dt(que_fd)
int        que_fd;
{
    int            ret;
    long        read_cnt;
    FILE_QUE    que_head;


    lseek(que_fd, 0, 0);
    if((ret = read(que_fd, (char *)&que_head, QUE_HEADSIZE)) != QUE_HEADSIZE)
    { 

        return(QUE_READFAIL);
    }

    read_cnt = que_head.rear - que_head.front;

    if(read_cnt > 0)
        return(read_cnt);
    else if (read_cnt < 0)
        return(QUE_STATFAIL);
    else
        return(QUE_NODATA);
}

/*
 *========================================================================
 *
 *   Function : lb_poschk_fileq_dt()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : File Queue에 읽어올 Data의 위치를 가져온다.
 *              int     Que_fd        : Queue fd     
 *              long    QueReadPos    : 읽어올 위치
 *              long    QueReadCnt    : 읽어올 갯수
 *
 *========================================================================
*/

int lb_poschk_fileq_dt(que_fd, que_read_pos, que_read_cnt)
int        que_fd;
long    *que_read_pos;
long    *que_read_cnt;
{
    int            ret;
    FILE_QUE    que_head;

    lseek(que_fd, 0, 0);

    if((ret = read(que_fd, (char *)&que_head, QUE_HEADSIZE)) != QUE_HEADSIZE)
        return(QUE_READFAIL);
/*   printf("fileq ret= [%d]\n", ret);  */
    *que_read_cnt = que_head.rear - que_head.front;
/*   printf("que_head.rear - que_head.front = [%d][%d]\n", que_head.rear, que_head.front);    */
/*   printf("fileq cnt= [%d]\n", *que_read_cnt);   */
    if(*que_read_cnt > 0)
    {
        *que_read_pos = que_head.front;
        return(SUCCESS);
    }
    else
        return(QUE_NODATA);
}

/*
 *========================================================================
 *
 *   Function : lb_poschk_fileq_dt2()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : File Queue에 읽어올 Data의 위치를 가져온다.
 *              int     Que_fd        : Queue fd     
 *              long    QueReadPos    : 읽어올 위치
 *              long    QueReadCnt    : 읽어올 갯수
 *
 *========================================================================
*/

int lb_poschk_fileq_dt2(que_fd, que_read_pos, que_read_cnt)
int        que_fd;
long    *que_read_pos;
long    *que_read_cnt;
{
    int            ret;
    FILE_QUE    que_head;


    lseek(que_fd, 0, 0);
    if((ret = read(que_fd, (char *)&que_head, QUE_HEADSIZE)) != QUE_HEADSIZE)
        return(QUE_READFAIL);

    que_head.front = 0;
    *que_read_cnt = que_head.rear - que_head.front;

    if(*que_read_cnt > 0)
    {  
        *que_read_pos = que_head.front;
        return(SUCCESS);
    }
    else
        return(QUE_NODATA);
}

/*
 *========================================================================
 *
 *   Function : lb_read_fileq_dt()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : File Queue의 Data를 읽는다.
 *              int      Que_fd        : Queue fd         
 *              char    *QueBuffer     : Data 저장 Buffer
 *              long     QueReadPos    : 읽어올 위치     
 *              long     QueReadCnt    : 읽어올 개수     
 *              int      FrameSize     : Frame Size         
 *
 *========================================================================
*/

int lb_read_fileq_dt(que_fd, que_buffer, que_read_pos, que_read_cnt, frame_size)
int      que_fd;
char    *que_buffer;
long     que_read_pos;
long     que_read_cnt;
int      frame_size;
{
    int     ret;
    long    read_pos;
    long    read_length;
#if 0
    int     skret;
#endif

    read_pos = QUE_HEADSIZE + (que_read_pos * frame_size);
    read_length = frame_size * que_read_cnt;
#if 0
    skret =lseek(que_fd, read_pos, 0);
#else
    lseek(que_fd, read_pos, 0);
#endif

    if((ret = read(que_fd, que_buffer, read_length)) != read_length)
    {
        return(QUE_READFAIL);
    }

    return(SUCCESS);
}

/*
 *========================================================================
 *
 *   Function : lb_set_fileq_front()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : Queue에 Front값 입력.
 *
 *========================================================================
*/

int lb_set_fileq_front(que_fd, front_pos)
int     que_fd;
long    front_pos;
{
    long   write_length;
    int    ret;

    write_length = 8; 
    lseek(que_fd, 8, 0);

    lockf(que_fd, F_LOCK, write_length);
    ret=write(que_fd, (char *)&front_pos, write_length);
    if( ret < write_length)
    {

        lockf(que_fd, F_ULOCK, 0);
        return(QUE_WRITEFAIL);
    }
    lockf(que_fd, F_ULOCK, 0);
    return(SUCCESS);
}

/*
 *========================================================================
 *
 *   Function : lb_set_fileq_pos()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : File Queue에 Data를 저장할 위치
 *
 *========================================================================
*/

int lb_set_fileq_pos(que_fd, que_write_pos)
int        que_fd;
long    *que_write_pos;
{
    int         ret;
    FILE_QUE    que_head;

    lseek(que_fd, 0, 0);
    if((ret = read(que_fd, (char *)&que_head, QUE_HEADSIZE)) != QUE_HEADSIZE)
    {
        return(QUE_READFAIL);
    }

    *que_write_pos = que_head.rear;
    return(SUCCESS);
}

/*
 *========================================================================
 *
 *   Function : lb_set_fileq()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : File Queue에 Data를 저장한다.
 *              int      Que_fd          : Queue fd        
 *              char    *QueBuffer       : Data 저장 Buffer
 *              long     QueWritePos     : 저장할 위치     
 *              long     QueWriteCnt     : 저장할 개수     
 *              int      FrameSize       : Frame Size      
 *
 *========================================================================
*/

int lb_set_fileq(que_fd, que_buffer, que_write_pos, que_write_cnt, frame_size)
int      que_fd;
char    *que_buffer;
long     que_write_pos;
long     que_write_cnt;
int      frame_size;
{
    long    write_pos;
    long    write_length;

    write_pos = QUE_HEADSIZE + (que_write_pos * frame_size);
    write_length = frame_size * que_write_cnt;

    lseek(que_fd, write_pos, 0);
    lockf(que_fd, F_LOCK, write_length);
    if (write(que_fd, que_buffer, write_length) < write_length)
    {
        lockf(que_fd, F_ULOCK, 0);
        return(QUE_WRITEFAIL);
    }
    lockf(que_fd, F_ULOCK, 0);
    return(SUCCESS);
}

/*
 *========================================================================
 *
 *   Function : lb_set_fileq_rear()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : Queue에 rear값 입력.
 *
 *========================================================================
*/

int lb_set_fileq_rear(que_fd, rear_pos)
int        que_fd;
long       rear_pos;
{
    long    write_length;

    write_length = 8;
    lseek(que_fd, 0, 0);

    lockf(que_fd, F_LOCK, write_length);
    if (write(que_fd, (char *)&rear_pos, write_length) < write_length)
    {
        lockf(que_fd, F_ULOCK, 0);
        return(QUE_WRITEFAIL);
    }
    lockf(que_fd, F_ULOCK, 0);
    return(SUCCESS);
}

/*
 *========================================================================
 *
 *   Function : fque_write()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : FILE QUEUE WRITE
 *
 *========================================================================
*/

int fque_write(isfd, record, size)
int        isfd;
char      *record;
int        size;
{
    long    wr_size = 0;
    long    rear = 0;
    long    write_pos = 0;

    lseek(isfd, 0, 0);
    rear = 0;
    read(isfd, (char *)&rear, 8);

    wr_size = size ;
    write_pos = QUE_HEADSIZE + (rear * wr_size);

/* printf("write_pos = [%d]\n", write_pos); */

    lseek(isfd, write_pos, 0);
    lockf(isfd, F_LOCK, wr_size);
    if (write(isfd, record, wr_size) < wr_size)
    {
        lockf(isfd, F_ULOCK, 0);
        return(FAIL);
    }

    rear += 1;
    lseek(isfd, 0, 0);
    if (write(isfd, (char *)&rear, 8) < 8)
    {
        lockf(isfd, F_ULOCK, 0);
        return(FAIL);
    }
    lockf(isfd, F_ULOCK, 0);

    return(SUCCESS);
}

