将参数传递给来自C类的HTTP请求

时间:2013-03-05 11:32:57

标签: c http endianness

我的问题很难说,但我试试。

我有一个模块ztc_config,用于读取网络中某些传感器的响应。为了阅读这个回复,我写了一个C类,query.c就是这样:

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

#include "data.h"
#include "modules.h"

void query(hash_t* params) {
    record_t* rec;
    timestamp_t ts;
    timestamp_t rec_ts;
    uint32_t sec;
    uint32_t usec;
    timestamp_t now;
    timestamp_t start;
    timestamp_t end;
    char* startime;
    char* endtime;
    // Coordinator, opgroup and  Opcode  specified by the client
    uint64_t netid = 0;
    uint8_t opcode_group = 0;
    uint8_t opcode = 0;
    char* par;
    now = mdl_now();
    /* set the start and end time based on query parameters */
    startime = hash_get(params, "start");
    if(startime != NULL) 
        start = mdl_parse_timestr(startime, now);
    else
        start = now;
    endtime = hash_get(params, "end");
    if(endtime != NULL) 
        end = mdl_parse_timestr(endtime, now);
    else 
        end = ~0;
    // Network id is required
    par = hash_get(params, "netid");
    if(par == NULL) {
        mdl_print("netid required\n");
        exit(1);
    }
    netid = strtoull(par, NULL, 10);
    par = hash_get(params, "opcode_group");
    if(par == NULL) {
        mdl_print("opcode_group required\n");
        exit(1);
    }
    opcode_group = strtoul(par, NULL, 10);
    par = hash_get(params, "opcode");
    if(par == NULL) {
        mdl_print("opcode required\n");
        exit(1);
    }
    opcode = strtoul(par, NULL, 10);
    /* seek in the bytestream */
    mdl_seek(start);
    while((rec = (record_t* ) mdl_next(&ts)) != NULL) {
        uint64_t val;
        //float fval;
        /* reached end time ? */
        if(ts > end) break;
        rec_ts = NTOHLL(rec->ts);
        sec = TS2SEC(rec_ts);
        usec = TS2USEC(rec_ts);
        // Only print records with the wanted net and opcodes
        if((netid != 0) && (netid != NTOHLL(rec->net_id))) 
            continue;
        if((opcode_group != 0) && (opcode_group != ntohl(rec->opcode_group))) 
            continue;
        if((opcode != 0) && (opcode != ntohl(rec->opcode)))
            continue;
        val = NTOHLL(rec->payload);
        mdl_print("%u %llu %u %u %u %d\n", sec, NTOHLL(rec->net_id), rec->op_code_group, rec->op_code, rec->payloadlength, val);
    }
}

当我访问此链接时,我会这样做:

http://localhost:44444/ztc_config?netid=2459546910990453036&opcode_group=164&opcode=224&start=-5m&end=-1s

在页面中我只看到来自netid 2459546910990453036的记录 使用opcode_group 164(hex A4) 带操作码224(十六进制E0) 在最后5分钟。

但是我的query.c类有问题。所以,如果我访问此链接

http://localhost:44444/ztc_config?netid=0&opcode_group=0&opcode=0&start=-5m&end=-1s

我在过去5分钟内收到了所有记录。

但是如果我改变任何opcode_group或操作码值,我都没有获得任何东西......

有什么建议吗?错误在哪里?

非常感谢。

修改

我试图改变两个变量opcode_group和opcode的名称,如下所示:

....
uint8_t op_code_group =0;
uint8_t op_code =0;
....
....
op_code_group = strtoul(par, NULL, 10);
....
op_code = strtoul(par, NULL, 10);

通过这种方式我写道:

if ((op_code_group != 0) && (op_code_group != ntohl(rec->opcode_group))) continue;
if ((op_code != 0) && (op_code != ntohl(rec->opcode))) continue;

这应该是一个过滤器。在另一种情况下,使用相同的变量名称,条件为真,但值始终是初始化值= 0.或者不是?

此解决方案无论如何都不起作用...... :(请帮助我!

0 个答案:

没有答案