presence: implemented rpc command to list presententy records in memory
authorDaniel-Constantin Mierla <miconda@gmail.com>
Tue, 21 Apr 2020 07:57:30 +0000 (09:57 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Tue, 21 Apr 2020 07:57:30 +0000 (09:57 +0200)
- presence.presentity_list [mode] - mode is optional and can be 'full'
to print all attributes of the presentity record

src/modules/presence/hash.c
src/modules/presence/hash.h
src/modules/presence/presence.c

index 7a58fe5..6d868e1 100644 (file)
@@ -705,6 +705,11 @@ done:
 
 static ps_ptable_t *_ps_ptable = NULL;
 
+ps_ptable_t *ps_ptable_get(void)
+{
+       return _ps_ptable;
+}
+
 #define PS_PRESENTITY_FIELD_COPY(field) do { \
                if (pt->field.s) { \
                        ptn->field.s = p; \
@@ -945,6 +950,7 @@ void ps_ptable_destroy(void)
        }
        for(i=0; i<_ps_ptable->ssize; i++) {
                lock_destroy(&_ps_ptable->slots[i].lock);
+               pt = _ps_ptable->slots[i].plist;
                while(pt!=NULL) {
                        ptn = pt->next;
                        ps_presentity_free(pt, 0);
index 96621ef..4573242 100644 (file)
@@ -180,5 +180,6 @@ int ps_ptable_remove(ps_presentity_t *pt);
 ps_presentity_t *ps_ptable_get_list(str *user, str *domain);
 ps_presentity_t *ps_ptable_get_item(str *user, str *domain, str *event, str *etag);
 ps_presentity_t *ps_ptable_get_expired(int eval);
+ps_ptable_t *ps_ptable_get(void);
 
 #endif
index c0e1912..84997b4 100644 (file)
@@ -1847,6 +1847,83 @@ static const char *rpc_presence_cleanup_doc[3] = {
        0
 };
 
+/*! \brief
+ *  rpc cmd: presence.presentity_list
+ *                     \mode - output attributes control
+ *             * */
+void rpc_presence_presentity_list(rpc_t *rpc, void *ctx)
+{
+       str omode = {0, 0};
+       int imode = 0;
+       int i = 0;
+       ps_ptable_t *ptb = NULL;
+       ps_presentity_t *ptn = NULL;
+       void* th = NULL;
+       str pempty = str_init("");
+
+       LM_DBG("listing in memory presentity records\n");
+
+       imode = rpc->scan(ctx, "*S", &omode);
+       if(imode < 1) {
+               imode = 0;
+       } else {
+               if(omode.len == 4 && strncmp(omode.s, "full", 4)==0) {
+                       imode = 1;
+               } else {
+                       imode = 0;
+               }
+       }
+       ptb = ps_ptable_get();
+       if(ptb == NULL) {
+               return;
+       }
+
+       for(i=0; i<ptb->ssize; i++) {
+               lock_get(&ptb->slots[i].lock);
+               ptn = ptb->slots[i].plist;
+               while(ptn!=NULL) {
+                       /* add record node */
+                       if (rpc->add(ctx, "{", &th) < 0) {
+                               rpc->fault(ctx, 500, "Internal error creating rpc");
+                               lock_release(&ptb->slots[i].lock);
+                               return;
+                       }
+                       /* add common fields */
+                       if(rpc->struct_add(th, "SSSSSd",
+                                       "user",  &ptn->user,
+                                       "domain", &ptn->domain,
+                                       "event", &ptn->event,
+                                       "etag", &ptn->etag,
+                                       "sender", (ptn->sender.s)?&ptn->sender:&pempty,
+                                       "expires", ptn->expires)<0) {
+                               rpc->fault(ctx, 500, "Internal error adding item");
+                               lock_release(&ptb->slots[i].lock);
+                               return;
+                       }
+                       if(imode==1) {
+                               /* add extra fields */
+                               if(rpc->struct_add(th, "ddSSd",
+                                               "received_time",  ptn->received_time,
+                                               "priority", ptn->priority,
+                                               "ruid", (ptn->ruid.s)?&ptn->ruid:&pempty,
+                                               "body", (ptn->body.s)?&ptn->body:&pempty,
+                                               "hashid", ptn->hashid)<0) {
+                                       rpc->fault(ctx, 500, "Internal error adding item");
+                                       lock_release(&ptb->slots[i].lock);
+                                       return;
+                               }
+                       }
+                       ptn = ptn->next;
+               }
+               lock_release(&ptb->slots[i].lock);
+       }
+       return;
+}
+
+static const char *rpc_presence_presentity_list_doc[2] = {
+       "Trigger update of watchers",
+       0
+};
 
 rpc_export_t presence_rpc[] = {
        {"presence.cleanup", rpc_presence_cleanup, rpc_presence_cleanup_doc, 0},
@@ -1854,6 +1931,8 @@ rpc_export_t presence_rpc[] = {
                        rpc_presence_refresh_watchers_doc, 0},
        {"presence.updateWatchers", rpc_presence_update_watchers,
                        rpc_presence_update_watchers_doc, 0},
+       {"presence.presentity_list", rpc_presence_presentity_list,
+                       rpc_presence_presentity_list_doc, 0},
        {0, 0, 0, 0}
 };