数据存储示例

数据存储和补发,最多存储7天数据

#include <iostream>
#include <unistd.h>
#include <fcntl.h>

#define MAX_STORE_FREAM			12/*120960*/  //5S每帧,7天 (7 * 60 * 24 * 60/5) = 120960//604800
#define   MSG_PER_FREAM_VEHI		500
#define   MAX_STORE_SIZE_VEHI		(MAX_STORE_FREAM * MSG_PER_FREAM_VEHI) //60480 帧

pthread_mutex_t vehiLocker;
static const char* vehiFileName="vehiMsgStorage.txt";  //BMPro
int vehiWriteIndex;
int vehiReadIndex;
pthread_cond_t vehiNotEmpty;

bool vehiIsFull()
{
    if((vehiWriteIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI == vehiReadIndex)
    {
        return true;
    }
    return false;
}

int vehiStoreMsg(uint8_t* msg)
{
    if(!msg)
    {
        return -1;
    }
    pthread_mutex_lock(&vehiLocker);
    int fd = open(vehiFileName, O_RDWR|O_CREAT, 0777);
    if(fd < 0)
    {
        goto FAILED;
    }
    printf("vehiStoreMsg,write---%d \n", vehiWriteIndex);
    if(vehiIsFull())
    {
        printf("isFull read--%d write-- %d\n", vehiReadIndex, vehiWriteIndex);
        vehiReadIndex = (vehiReadIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI;
    }
    if(lseek(fd,vehiWriteIndex, SEEK_SET) < 0)
    {
        goto FAILED;
    }
    if(write(fd, msg, MSG_PER_FREAM_VEHI) < MSG_PER_FREAM_VEHI)
    {
        goto FAILED;
    }
    vehiWriteIndex = (vehiWriteIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI;   //下一个写指针位置!!!
    printf("vehiStoreMsg is OK read = %ld  write = %ld \n",vehiReadIndex,vehiWriteIndex);
    close(fd);
    pthread_cond_signal(&vehiNotEmpty);
    pthread_mutex_unlock(&vehiLocker);
    return 0;
    FAILED:
    if(fd >= 0)
    {
        close(fd);
    }
    printf("[ERROR]vehiStoreMsg read--%d write-- %d\n", vehiReadIndex,vehiWriteIndex);
    pthread_mutex_unlock(&vehiLocker);
    return -1;
}

bool vehiIsEmpty()
{
    if(vehiWriteIndex == vehiReadIndex)
    {
        return true;
    }
    return false;
}
int vehiRestoreMsg(uint8_t* msg)
{
    if(!msg)
    {
        return -1;
    }
    pthread_mutex_lock(&vehiLocker);
    while(vehiIsEmpty())
    {
        printf("wait the signal at vehiIsEmpty  \n");
        pthread_cond_wait(&vehiNotEmpty, &vehiLocker);
    }
    int fd = open(vehiFileName, O_RDONLY,0777);
    if(fd < 0)
    {
        goto FAILED;
    }
    if(lseek(fd,vehiReadIndex, SEEK_SET) < 0)
    {
        goto FAILED;
    }

    if(read(fd, msg, MSG_PER_FREAM_VEHI ) < MSG_PER_FREAM_VEHI)
    {
        goto FAILED;
    }
    close(fd);
    //20200923 应该是去整除的结果
    if (vehiReadIndex % MSG_PER_FREAM_VEHI > 0)
        //if(vehiReadIndex / MAX_STORE_SIZE_VEHI > 0)
    {
        vehiReadIndex = 0;
    }
    if (vehiWriteIndex % MSG_PER_FREAM_VEHI > 0)
        //if(vehiWriteIndex / MAX_STORE_SIZE_VEHI > 0)
    {
        vehiWriteIndex = 0;
    }
    printf("vehiRestoreMsg is OK ;read = %d  write = %d \n",vehiReadIndex,vehiWriteIndex);
    //printRawData(ELOG_LVL_DEBUG,TAG_NONE," read repush data is ",msg,MSG_PER_FREAM_VEHI);
    pthread_mutex_unlock(&vehiLocker);
    return 0;

    FAILED:
    if(fd >= 0)
    {
        close(fd);
    }
    printf("vehiRestoreMsg is fail,close fd  \n");
    pthread_mutex_unlock(&vehiLocker);
    return -1;
}

bool vehiPopMsg()
{
    pthread_mutex_lock(&vehiLocker);
    vehiReadIndex = (vehiReadIndex + MSG_PER_FREAM_VEHI)%MAX_STORE_SIZE_VEHI;    //将读指针指向下一个位置
    printf("vehiPopMsg,manager.vehiInfo.readIndex is---%d // writeIndex is %d,\n", vehiReadIndex,vehiWriteIndex);
    pthread_mutex_unlock(&vehiLocker);
    return true;
}

int main() {
    pthread_mutex_init(&vehiLocker,NULL);
    pthread_cond_init(&vehiNotEmpty,NULL);
    const char *data = "11111111111111";
    unsigned char tmpBuf[MSG_PER_FREAM_VEHI] = {0};
    while (true){
        sleep(1);
        vehiStoreMsg((uint8_t *) data);
        vehiRestoreMsg(tmpBuf);
        vehiPopMsg();
    }
    return 0;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值