static void wurfld_fill_capabilities(wurfl_device_capability_enumerator_handle enumerator, char *label, fbuf_t *output) { int count = 0; fbuf_printf(output, "\"%s\":{", label); while(wurfl_device_capability_enumerator_is_valid(enumerator)) { char *name = (char *)wurfl_device_capability_enumerator_get_name(enumerator); char *val = (char *)wurfl_device_capability_enumerator_get_value(enumerator); if (name && val) { fbuf_printf(output, "%s\"%s\":" , count ? "," : "", name); if (strcmp(val, "true") == 0) { fbuf_add(output, "1"); } else if (strcmp(val, "false") == 0) { fbuf_add(output, "0"); } else { char *escaped_val = NULL; unsigned long escaped_len = 0; byte_escape('\"', '\\', val, strlen(val)+1, &escaped_val, &escaped_len); if (escaped_val) { fbuf_printf(output, "\"%s\"", escaped_val); free(escaped_val); } else { fbuf_add(output, "\"\""); } } count++; } wurfl_device_capability_enumerator_move_next(enumerator); } fbuf_add(output, "}"); DEBUG2("found %d %s", count, label); }
int fbuf_add_ln(fbuf_t *fbuf, const char *data) { int n1 = 0, n2 = 0; n1 = fbuf_add(fbuf, data); if (n1 == -1) return -1; n2 = fbuf_add(fbuf, "\n"); if (n2 == -1) return -1; return n1 + n2; }
static void wurfld_get_capabilities(char *useragent, fbuf_t *output) { wurfl_device_handle device = wurfl_lookup_useragent(ATOMIC_READ(wurfl), useragent); if (device) { fbuf_printf(output, "{\"match_type\":\"%d\",\"matcher_name\":\"%s\",\"device\":\"%s\",", wurfl_device_get_match_type(device), wurfl_device_get_matcher_name(device), wurfl_device_get_id(device) ); wurfl_device_capability_enumerator_handle enumerator = wurfl_device_get_capability_enumerator(device); wurfld_fill_capabilities(enumerator, "capabilities", output); wurfl_device_capability_enumerator_destroy(enumerator); fbuf_add(output, ","); enumerator = wurfl_device_get_virtual_capability_enumerator(device); wurfld_fill_capabilities(enumerator, "virtual_capabilities", output); wurfl_device_capability_enumerator_destroy(enumerator); fbuf_add(output, "}\n"); wurfl_device_destroy(device); } }
int shardcache_client_migration_begin(shardcache_client_t *c, shardcache_node_t **nodes, int num_nodes) { fbuf_t mgb_message = FBUF_STATIC_INITIALIZER; int i; for (i = 0; i < num_nodes; i++) { if (i > 0) fbuf_add(&mgb_message, ","); fbuf_printf(&mgb_message, "%s:%s", shardcache_node_get_string(nodes[i])); } for (i = 0; i < c->num_shards; i++) { char *addr = shardcache_node_get_address(c->shards[i]); int fd = connections_pool_get(c->connections, addr); if (fd < 0) { c->errno = SHARDCACHE_CLIENT_ERROR_NETWORK; snprintf(c->errstr, sizeof(c->errstr), "Can't connect to '%s'", addr); fbuf_destroy(&mgb_message); return -1; } int rc = migrate_peer(addr, (char *)c->auth, SHC_HDR_SIGNATURE_SIP, fbuf_data(&mgb_message), fbuf_used(&mgb_message), fd); if (rc != 0) { close(fd); c->errno = SHARDCACHE_CLIENT_ERROR_NODE; snprintf(c->errstr, sizeof(c->errstr), "Node '%s' (%s) didn't aknowledge the migration\n", shardcache_node_get_label(c->shards[i]), addr); fbuf_destroy(&mgb_message); // XXX - should we abort migration on peers that have been notified (if any)? return -1; } connections_pool_add(c->connections, addr, fd); } fbuf_destroy(&mgb_message); c->errno = SHARDCACHE_CLIENT_OK; c->errstr[0] = 0; return 0; }
static inline void expression_dump_internal(expression_t *expr, fbuf_t *buf) { int i = 0; expression_operand_t *operand; expression_t *subexpr; while (expr->operands[i]) { operand = expr->operands[i]; if (expr->operation->minOperands == 1 && expr->operation->maxOperands == 1) { if (expr->operation->op >= EXPR_OP_SIN && expr->operation->op <= EXPR_OP_ATAN) { fbuf_printf(buf, " %s(", expr->operation->label); } else { fbuf_printf(buf, " %s ", expr->operation->label); } } switch(operand->type) { case EXPR_OPTYPE_EXPRESSION: subexpr = operand->expr; if (subexpr->numOperands > 1) fbuf_printf(buf, "( "); expression_dump_internal(operand->expr, buf); if(subexpr->numOperands > 1) fbuf_printf(buf, " )"); break; case EXPR_OPTYPE_INTEGER: if (expr->operation->op >= EXPR_OP_SIN && expr->operation->op <= EXPR_OP_ATAN) { fbuf_printf(buf, "%lfπ", operand->inum/3.14); } else { fbuf_printf(buf, "%d", operand->inum); } break; case EXPR_OPTYPE_FLOAT: if (expr->operation->op >= EXPR_OP_SIN && expr->operation->op <= EXPR_OP_ATAN) { fbuf_printf(buf, "%lfπ", operand->fnum/3.14); } else { fbuf_printf(buf, "%lf", operand->fnum); } break; case EXPR_OPTYPE_STRING: fbuf_printf(buf, "%s", operand->string); break; case EXPR_OPTYPE_CALLBACK: if (operand->callback.label) fbuf_printf(buf, "callback:%s", operand->callback.label); else fbuf_printf(buf, "callback:0x%p", operand->callback.cb); break; } if(expr->operands[++i] && expr->operation->op != EXPR_OP_NOT) { if((expr->operation->minOperands > 1 && expr->operation->maxOperands != 1) || expr->operation->op == EXPR_OP_CHANGE) { fbuf_printf(buf, " %s ", expr->operation->label); } } else if (i == 0 && expr->operation->op == EXPR_OP_CHANGE) { fbuf_printf(buf, " %s 1", expr->operation->label); } } if(expr->operation->minOperands == 1 && expr->operation->maxOperands == 1 && expr->operation->op >= EXPR_OP_SIN && expr->operation->op <= EXPR_OP_ATAN) { fbuf_add(buf, ")"); } }