/* * Relay UA events as publish messages to the Broker */ static void ua_event_handler(struct ua *ua, enum ua_event ev, struct call *call, const char *prm, void *arg) { struct mqtt *mqtt = arg; struct odict *od = NULL; int err; err = odict_alloc(&od, 8); if (err) return; err = event_encode_dict(od, ua, ev, call, prm); if (err) goto out; err = mqtt_publish_message(mqtt, mqtt->pubtopic, "%H", json_encode_odict, od); if (err) { warning("mqtt: failed to publish message (%m)\n", err); goto out; } out: mem_deref(od); }
int webapp_chat_init(void) { int err = 0; err = message_init(message_handler, NULL); if (err) return err; err = odict_alloc(&messages, DICT_BSIZE); return err; }
int webapp_chat_add(const char *peer, const char *message, bool self) { char time_s[256] = {0}; int err = 0; struct odict *o; err = odict_alloc(&o, DICT_BSIZE); if (err) return ENOMEM; odict_entry_add(o, "message", ODICT_STRING, message); odict_entry_add(o, "peer", ODICT_STRING, peer); odict_entry_add(o, "self", ODICT_BOOL, self); (void)re_snprintf(time_s, sizeof(time_s), "%d", time(NULL)); odict_entry_add(messages, time_s, ODICT_OBJECT, o); mem_deref(o); return err; }
int json_decode_odict(struct odict **op, uint32_t hash_size, const char *str, size_t len, unsigned maxdepth) { struct odict *o; int err; if (!op || !str) return EINVAL; err = odict_alloc(&o, hash_size); if (err) return err; err = json_decode(str, len, maxdepth, object_handler, array_handler, object_entry_handler, array_entry_handler, o); if (err) mem_deref(o); else *op = o; return err; }
void webapp_odict_add(struct odict *og, const struct odict_entry *eg) { struct le *le; struct odict *o; int err = 0; char index[64]; size_t index_cnt=0; err = odict_alloc(&o, DICT_BSIZE); if (err) return; le = (void *)&eg->le; if (!le) return; for (le=le->list->head; le; le=le->next) { const struct odict_entry *e = le->data; if (!str_cmp(e->key, "command")) continue; odict_entry_add(o, e->key, e->type, e->u.str); } /* Limited Loop * No one will need more than 100 accounts for a personal computer */ for (int i=0; i<100; i++) { re_snprintf(index, sizeof(index), "%u", index_cnt); if (!odict_lookup(og, index)) { break; } index_cnt = index_cnt + 1; } odict_entry_add(og, index, ODICT_OBJECT, o); mem_deref(o); }
static int container_add(const char *name, unsigned idx, enum odict_type type, struct json_handlers *h) { struct odict *o = h->arg, *oc; char index[64]; int err; if (!name) { if (re_snprintf(index, sizeof(index), "%u", idx) < 0) return ENOMEM; name = index; } err = odict_alloc(&oc, hash_bsize(o->ht)); if (err) return err; err = odict_entry_add(o, name, type, oc); mem_deref(oc); h->arg = oc; return err; }