static NEOERR* aux_cmd_memoryget(struct aux_entry *e, QueueEntry *q) { unsigned char *val = NULL; size_t vsize = 0; NEOERR *err; int id = 0; mdb_conn *db = e->db; struct cache *cd = e->cd; REQ_FETCH_PARAM_INT(q->hdfrcv, "id", id); if (m_memory_maxid < 1) return nerr_raise(NERR_ASSERT, "empty memory"); /* * neo_rand() may get 0, so, +1 */ if (id == 0) id = neo_rand(m_memory_maxid) + 1; if (cache_getf(cd, &val, &vsize, PREFIX_MEMORY"%d", id)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { MDB_QUERY_RAW(db, "memory", _COL_MEMORY, "id<=%d AND statu=%d ORDER BY id DESC LIMIT 1", NULL, id, MEMORY_ST_OK); err = mdb_set_row(q->hdfsnd, db, _COL_MEMORY, NULL); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, MEMORY_CC_SEC, PREFIX_MEMORY"%d", id); } return STATUS_OK; }
/* * input : aname(STR) * return: NORMAL * reply : [appfoo: ["aid": "222", "aname": "appfoo", ...] */ static NEOERR* aic_cmd_appusers(struct queue_entry *q, struct cache *cd, mdb_conn *db) { unsigned char *val = NULL; size_t vsize = 0; int aid, lm = 0; char *aname; NEOERR *err; REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname); REQ_FETCH_PARAM_INT(q->hdfrcv, "limit", lm); aid = hash_string(aname); if (cache_getf(cd, &val, &vsize, PREFIX_USERLIST"%d", aid)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { if (lm > 0) { MDB_QUERY_RAW(db, "userinfo", USERINFO_COL, "aid=%d ORDER BY uptime DESC LIMIT %d;", NULL, aid, lm); } else { MDB_QUERY_RAW(db, "userinfo", USERINFO_COL, "aid=%d ORDER BY uptime DESC;", NULL, aid); } err = mdb_set_rows(q->hdfsnd, db, USERINFO_COL, "userlist", "1"); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, AIC_CC_SEC, PREFIX_USERLIST"%d", aid); } return STATUS_OK; }
static NEOERR* aux_cmd_impget(struct queue_entry *q, struct cache *cd, mdb_conn *db) { unsigned char *val = NULL; size_t vsize = 0; NEOERR *err; char *aname; int aid, count, offset; REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname); aid = hash_string(aname); mmisc_pagediv(q->hdfrcv, NULL, &count, &offset, NULL, q->hdfsnd); if (cache_getf(cd, &val, &vsize, PREFIX_IMP"%d_%d", aid, offset)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { MMISC_PAGEDIV_SET_N(q->hdfsnd, db, "improve", "aid=%d", NULL, aid); MDB_QUERY_RAW(db, "improve", IMP_COL, "aid=%d ORDER BY id DESC LIMIT " " %d OFFSET %d", NULL, aid, count, offset); err = mdb_set_rows(q->hdfsnd, db, IMP_COL, "imps", "0"); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, IMP_CC_SEC, PREFIX_IMP"%d_%d", aid, offset); } return STATUS_OK; }
static NEOERR* city_cmd_id(struct city_entry *e, QueueEntry *q) { unsigned char *val = NULL; size_t vsize = 0; int id; NEOERR *err; mdb_conn *db = e->db; struct cache *cd = e->cd; REQ_GET_PARAM_INT(q->hdfrcv, "id", id); if (cache_getf(cd, &val, &vsize, PREFIX_CITY"%d", id)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { MDB_QUERY_RAW(db, "city", _COL_CITY, "id=%d", NULL, id); err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys", NULL, MDB_FLAG_Z); if (err != STATUS_OK) return nerr_pass(err); /* * get city's parents */ int cnt = 0; char *pid = hdf_get_valuef(q->hdfsnd, "citys.0.pid"); while (pid && atoi(pid) > 0) { MDB_QUERY_RAW(db, "city", _COL_CITY, "id=%d", NULL, atoi(pid)); err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys", NULL, MDB_FLAG_Z); TRACE_NOK(err); pid = hdf_get_valuef(q->hdfsnd, "citys.%d.pid", ++cnt); } CACHE_HDF(q->hdfsnd, CITY_CC_SEC, PREFIX_CITY"%d", id); } return STATUS_OK; }
static NEOERR* place_cmd_get(struct city_entry *e, QueueEntry *q) { unsigned char *val = NULL; size_t vsize = 0; int count = 0; char *ip, tok[64]; REQ_GET_PARAM_STR(q->hdfrcv, "ip", ip); struct cache *cd = e->cd; if (cache_getf(cd, &val, &vsize, PREFIX_PLACE"%s", ip)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { char *s = strdup(ip); char *dupip = s, *p = s; while (*p != '\0') { if (*p == ',') { *p = '\0'; sprintf(tok, "%d", count); ip2place(q->hdfsnd, s, tok); count++; s = p+1; } p++; } sprintf(tok, "%d", count); ip2place(q->hdfsnd, s, tok); free(dupip); CACHE_HDF(q->hdfsnd, 0, PREFIX_PLACE"%s", ip); } return STATUS_OK; }
static NEOERR* dyn_cmd_getadmin(struct queue_entry *q, struct cache *cd, mdb_conn *db) { unsigned char *val = NULL; size_t vsize = 0; char *uname, *aname; int uid, aid; NEOERR *err; REQ_GET_PARAM_STR(q->hdfrcv, "uname", uname); REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname); uid = hash_string(uname); aid = hash_string(aname); if (cache_getf(cd, &val, &vsize, PREFIX_ADMIN"%d_%d", uid, aid)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { MDB_QUERY_RAW(db, "track", TRACK_COL, "uid=%d AND aid=%d AND type=%d " " ORDER BY id DESC LIMIT 1;", NULL, uid, aid, TYPE_JOIN); err = mdb_set_row(q->hdfsnd, db, TRACK_COL, NULL); nerr_handle(&err, NERR_NOT_FOUND); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, ONE_MINUTE, PREFIX_ADMIN"%d_%d", uid, aid); } return STATUS_OK; }
static NEOERR* aux_cmd_impdetail(struct queue_entry *q, struct cache *cd, mdb_conn *db) { unsigned char *val = NULL; size_t vsize = 0; NEOERR *err; int id; REQ_GET_PARAM_INT(q->hdfrcv, "id", id); if (cache_getf(cd, &val, &vsize, PREFIX_IMP_DETAIL"%d", id)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { MDB_QUERY_RAW(db, "improve", IMP_COL, "id=%d", NULL, id); err = mdb_set_row(q->hdfsnd, db, IMP_COL, NULL); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, IMP_CC_SEC, PREFIX_IMP_DETAIL"%d", id); } return STATUS_OK; }
int main(int argc, char **argv, char **envp) { unsigned char buf[2048]; int blen = 2048, len; char *s; HDF *hdf; //mconfig_parse_file("/tpl/oms.hdf", &g_cfg); mtimer_start(); for (int i = 0; i < 100000; i++) { memset(buf, 2048, 0x0); len = pack_hdf(g_cfg, buf, blen); unpack_hdf(buf, len, &hdf); s = hdf_get_value(hdf, "manual.Layout", NULL); hdf_destroy(&hdf); } mtimer_stop(NULL); return 0; }
static NEOERR* aic_cmd_appousers(struct queue_entry *q, struct cache *cd, mdb_conn *db) { unsigned char *val = NULL; size_t vsize = 0; int count, offset; int pid; char *pname, *aname; NEOERR *err; REQ_GET_PARAM_STR(q->hdfrcv, "pname", pname); pid = hash_string(pname); mmisc_pagediv(q->hdfrcv, NULL, &count, &offset, NULL, q->hdfsnd); if (cache_getf(cd, &val, &vsize, PREFIX_APPOUSER"%d_%d", pid, offset)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { MMISC_PAGEDIV_SET_N(q->hdfsnd, db, "appinfo", "pid=%d OR aid=%d", NULL, pid, pid); MDB_QUERY_RAW(db, "appinfo", APPINFO_COL, "pid=%d OR aid=%d ORDER BY uptime DESC LIMIT %d OFFSET %d", NULL, pid, pid, count, offset); err = mdb_set_rows(q->hdfsnd, db, APPINFO_COL, "users", "1"); if (err != STATUS_OK) return nerr_pass(err); HDF *node = hdf_get_child(q->hdfsnd, "users"); while (node) { /* numcamer */ aname = hdf_get_value(node, "aname", NULL); if (aname) { MDB_QUERY_RAW(db, "userinfo", " COUNT(*) AS numcamer ", "aid=%d", NULL, hash_string(aname)); err = mdb_set_row(node, db, " numcamer ", NULL); if (err != STATUS_OK) return nerr_pass(err); } node = hdf_obj_next(node); } CACHE_HDF(q->hdfsnd, AIC_CC_SEC, PREFIX_APPOUSER"%d_%d", pid, offset); } return STATUS_OK; }
static NEOERR* city_cmd_ip(struct city_entry *e, QueueEntry *q) { unsigned char *val = NULL; size_t vsize = 0; char *ip, *c, *a, *s; NEOERR *err; struct cache *cd = e->cd; REQ_GET_PARAM_STR(q->hdfrcv, "ip", ip); if (cache_getf(cd, &val, &vsize, PREFIX_CITY"%s", ip)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { if (!ip2place(q->hdfsnd, ip, NULL)) return nerr_raise(NERR_ASSERT, "%s not ip", ip); c = hdf_get_value(q->hdfsnd, "c", NULL); a = hdf_get_value(q->hdfsnd, "a", NULL); s = strstr(c, "省"); if (!s) s = strstr(c, "区"); if (s) { s += 3; char tok[64] = {0}; strncpy(tok, c, s-c); hdf_set_value(q->hdfrcv, "p", tok); hdf_set_value(q->hdfrcv, "c", s); } else { mtc_err("%s doesn't contain 省/区(%s)", c, a); hdf_set_value(q->hdfrcv, "c", c); } err = city_cmd_s(e, q); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, CITY_CC_SEC, PREFIX_CITY"%s", ip); } return STATUS_OK; }
/* * input : aname(STR) * return: NORMAL * reply : ["state": 0, ...] OR [] */ static NEOERR* aic_cmd_appinfo(struct queue_entry *q, struct cache *cd, mdb_conn *db) { unsigned char *val = NULL; size_t vsize = 0; int aid, pid; char *aname; NEOERR *err; REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname); aid = hash_string(aname); if (cache_getf(cd, &val, &vsize, PREFIX_APPINFO"%d", aid)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { hdf_set_value(q->hdfsnd, "pname", aname); MDB_QUERY_RAW(db, "appinfo", APPINFO_COL, "aid=%d", NULL, aid); err = mdb_set_row(q->hdfsnd, db, APPINFO_COL, NULL); if (nerr_handle(&err, NERR_NOT_FOUND)) return nerr_raise(REP_ERR_NREGIST, "%s %d hasn't regist", aname, aid); if (err != STATUS_OK) return nerr_pass(err); pid = hdf_get_int_value(q->hdfsnd, "pid", 0); if (pid != 0) { MDB_QUERY_RAW(db, "appinfo", "aname", "aid=%d", NULL, pid); err = mdb_set_row(q->hdfsnd, db, "pname", NULL); nerr_handle(&err, NERR_NOT_FOUND); if (err != STATUS_OK) return nerr_pass(err); } MDB_QUERY_RAW(db, "appinfo", "COUNT(*)+1 AS numuser", "pid=%d", NULL, aid); err = mdb_set_row(q->hdfsnd, db, "numuser", NULL); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, AIC_CC_SEC, PREFIX_APPINFO"%d", aid); } return STATUS_OK; }
static NEOERR* aic_cmd_app_getsecy(struct queue_entry *q, struct cache *cd, mdb_conn *db) { unsigned char *val = NULL; size_t vsize = 0; char *aname; int aid; NEOERR *err; REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname); aid = hash_string(aname); if (cache_getf(cd, &val, &vsize, PREFIX_SECY"%d", aid)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { MDB_QUERY_RAW(db, "appinfo", " aname ", " (aid=%d OR pid=%d) AND tune & %d = %d ", NULL, aid, aid, LCS_TUNE_SECY, LCS_TUNE_SECY); err = mdb_set_row(q->hdfsnd, db, " aname ", NULL); nerr_handle(&err, NERR_NOT_FOUND); if (err != STATUS_OK) return nerr_pass(err); CACHE_HDF(q->hdfsnd, AIC_CC_SEC, PREFIX_SECY"%d", aid); } return STATUS_OK; }
/* * ids=0:1 * { "0": { "1": { "ntt": "0", "nst": "0" } }, "success": "1" } * * ids=0:1,0:20 * { "0": { "1": { "ntt": "0", "nst": "0" }, "20": { "ntt": "0", "nst": "0" } }, "success": "1" } * * ids=0:1,1:20 * { "0": { "1": { "ntt": "0", "nst": "0" } }, "1": { "20": { "ntt": "0", "nst": "0" } }, "success": "1" } */ static NEOERR* aux_cmd_cmtget(struct aux_entry *e, QueueEntry *q) { unsigned char *val = NULL; size_t vsize = 0; int count, offset; char *ids, *idsdump, tok[128]; int type = -1, oid = -1; NEOERR *err; REQ_GET_PARAM_STR(q->hdfrcv, "ids", ids); mdb_conn *db = e->db; struct cache *cd = e->cd; mdb_pagediv(q->hdfrcv, NULL, &count, &offset, NULL, q->hdfsnd); if (cache_getf(cd, &val, &vsize, PREFIX_COMMENT"%s_%d", ids, offset)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { idsdump = strdup(ids); char *p = ids; while (*p) { if (*p == ':') { *p = '\0'; type = atoi(ids); ids = p+1; } if (*p == ',') { *p = '\0'; oid = atoi(ids); if (type >= 0 && oid >= 0) { sprintf(tok, "%d.%d", type, oid); MDB_PAGEDIV_SET(q->hdfsnd, tok, db, "comment", "type=%d AND statu=%d AND oid=%d", NULL, type, CMT_ST_NORMAL, oid); MDB_QUERY_RAW(db, "comment", _COL_CMT, "type=%d AND statu=%d AND oid=%d " " ORDER BY intime DESC LIMIT %d OFFSET %d", NULL, type, CMT_ST_NORMAL, oid, count, offset); sprintf(tok, "%d.%d.cmts", type, oid); err = mdb_set_rows(q->hdfsnd, db, _COL_CMT, tok, NULL); nerr_handle(&err, NERR_NOT_FOUND); if (err != STATUS_OK) return nerr_pass(err); mstr_html_escape(hdf_get_child(q->hdfsnd, tok), "content"); type = oid = -1; } ids = p+1; } p++; } oid = atoi(ids); if (type >= 0 && oid >=0) { sprintf(tok, "%d.%d", type, oid); MDB_PAGEDIV_SET(q->hdfsnd, tok, db, "comment", "type=%d AND statu=%d AND oid=%d", NULL, type, CMT_ST_NORMAL, oid); MDB_QUERY_RAW(db, "comment", _COL_CMT, "type=%d AND statu=%d AND oid=%d " " ORDER BY intime DESC LIMIT %d OFFSET %d", NULL, type, CMT_ST_NORMAL, oid, count, offset); sprintf(tok, "%d.%d.cmts", type, oid); err = mdb_set_rows(q->hdfsnd, db, _COL_CMT, tok, NULL); nerr_handle(&err, NERR_NOT_FOUND); if (err != STATUS_OK) return nerr_pass(err); mstr_html_escape(hdf_get_child(q->hdfsnd, tok), "content"); } CACHE_HDF(q->hdfsnd, CMT_CC_SEC, PREFIX_COMMENT"%s_%d", idsdump, offset); free(idsdump); } return STATUS_OK; }
static NEOERR* city_cmd_s(struct city_entry *e, QueueEntry *q) { unsigned char *val = NULL; size_t vsize = 0; char *p = NULL, *c; NEOERR *err; mdb_conn *db = e->db; struct cache *cd = e->cd; REQ_GET_PARAM_STR(q->hdfrcv, "c", c); REQ_FETCH_PARAM_STR(q->hdfrcv, "p", p); if (cache_getf(cd, &val, &vsize, PREFIX_CITY"%s.%s", p, c)) { unpack_hdf(val, vsize, &q->hdfsnd); } else { if (p) { char tok[64] = {0}; strncpy(tok, p, 64); if (!strstr(p, "省") && !strstr(p, "区")) snprintf(tok, sizeof(tok), "%s省", p); MDB_QUERY_RAW(db, "city", _COL_CITY, "s=$1", "s", tok); mdb_set_row(q->hdfsnd, db, _COL_CITY, "province", MDB_FLAG_NO_ERR); } MDB_QUERY_RAW(db, "city", _COL_CITY, "s=$1", "s", c); err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys", NULL, MDB_FLAG_Z); if (nerr_handle(&err, NERR_NOT_FOUND)) { if (strstr(c, "市")) c[strlen(c)-3] = '\0'; MDB_QUERY_RAW(db, "city", _COL_CITY, "s=$1", "s", c); err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys", NULL, MDB_FLAG_EMPTY_OK); } if (err != STATUS_OK) return nerr_pass(err); /* * get city's parents */ int cnt = 0; char *pid = hdf_get_valuef(q->hdfsnd, "citys.0.pid"); while (pid && atoi(pid) > 0) { MDB_QUERY_RAW(db, "city", _COL_CITY, "id=%d", NULL, atoi(pid)); err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys", NULL, MDB_FLAG_Z); TRACE_NOK(err); pid = hdf_get_valuef(q->hdfsnd, "citys.%d.pid", ++cnt); } /* * 北京市 has no subcities */ /* int id = hdf_get_int_value(q->hdfsnd, "city.id", 0); MDB_QUERY_RAW(db, "city", _COL_CITY, "pid=%d", NULL, id); err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "subcities", NULL, MDB_FLAG_EMPTY_OK); if (err != STATUS_OK) return nerr_pass(err); */ CACHE_HDF(q->hdfsnd, CITY_CC_SEC, PREFIX_CITY"%s.%s", p, c); } return STATUS_OK; }
/* * application logic message, called by msparse_buf() */ static NEOERR* msparse_msg(moc_srv *srv, unsigned char *buf, size_t len, moc_arg *arg) { uint32_t id, reply; unsigned char *payload; size_t psize, rv; MOC_NOT_NULLB(arg, arg->evth); if (!srv || !buf || len < 8) return nerr_raise(NERR_ASSERT, "illegal packet"); //MSG_DUMP("recv: ", buf, len); /* The header is: * 4 bytes ID * 4 bytes Reply Code * Variable Payload */ id = ntohl(* ((uint32_t *) buf)); reply = ntohl(* ((uint32_t *) buf + 1)); payload = buf + 8; psize = len - 8; if (id == 0 && reply == 10000) { /* * server push */ if (psize < 4) return nerr_raise(NERR_ASSERT, "server pushed empty message"); struct msqueue_entry *e = msqueue_entry_create(); if (!e) return nerr_raise(NERR_NOMEM, "alloc msqueue entry"); rv = unpack_hdf(payload + 4, psize - 4, &(e->hdfrcv)); if (rv <= 0) return nerr_raise(NERR_ASSERT, "server pushed illegal message"); //TRACE_HDF(e->hdfrcv); char *cmd = NULL; HDF_ATTR *attr = hdf_get_attr(e->hdfrcv, "_Reserve"); while (attr != NULL) { if (!strcmp(attr->key, "cmd")) cmd = attr->value; attr = attr->next; } if (!cmd) return nerr_raise(NERR_ASSERT, "cmd not supplied"); e->ename = strdup(srv->evt->ename); e->cmd = strdup(cmd); mtc_dbg("receive cmd %s", cmd); hdf_remove_tree(e->hdfrcv, "_Reserve"); mssync_lock(&arg->callbacksync); msqueue_put(arg->callbackqueue, e); mssync_unlock(&arg->callbacksync); /* * notify callback thread */ mssync_signal(&arg->callbacksync); } else { /* * server response */ if (id < g_reqid) return nerr_raise(NERR_ASSERT, "id not match %d %d", g_reqid, id); if (psize >= 4) { mssync_lock(&(arg->mainsync)); rv = unpack_hdf(payload + 4, psize - 4, &(srv->evt->hdfrcv)); mssync_unlock(&(arg->mainsync)); if (rv <= 0) return nerr_raise(NERR_ASSERT, "server responsed illegal message"); //TRACE_HDF(srv->evt->hdfrcv); } /* * notify main thread */ mssync_signal(&(arg->mainsync)); } return STATUS_OK; }