winfo_doc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "winfo_doc.h"
  2. #include <cds/dstring.h>
  3. #include <cds/sstr.h>
  4. static int doc_add_watcher(dstring_t *buf, watcher_t *w)
  5. {
  6. char tmp[64];
  7. dstr_append_zt(buf, "\t\t<watcher status=\"");
  8. dstr_append_str(buf, &watcher_status_names[w->status]);
  9. dstr_append_zt(buf, "\" event=\"");
  10. dstr_append_str(buf, &watcher_event_names[w->event]);
  11. dstr_append_zt(buf, "\" id=\"");
  12. if (w->id.len < 1) {
  13. sprintf(tmp, "%p", w);
  14. dstr_append_zt(buf, tmp);
  15. }
  16. else dstr_append_str(buf, &w->id);
  17. dstr_append_zt(buf, "\">");
  18. dstr_append_str(buf, &w->uri);
  19. dstr_append_zt(buf, "</watcher>\r\n");
  20. return 0;
  21. }
  22. static int doc_add_internal_watcher(dstring_t *buf, internal_pa_subscription_t *iw)
  23. {
  24. char tmp[64];
  25. dstr_append_zt(buf, "\t\t<watcher status=\"");
  26. dstr_append_str(buf, &watcher_status_names[iw->status]);
  27. dstr_append_zt(buf, "\" event=\"");
  28. dstr_append_str(buf, &watcher_event_names[0]);
  29. dstr_append_zt(buf, "\" id=\"");
  30. sprintf(tmp, "%pi", iw);
  31. dstr_append_zt(buf, tmp);
  32. dstr_append_zt(buf, "\">");
  33. dstr_append_str(buf, get_subscriber_id(iw->subscription));
  34. dstr_append_zt(buf, "</watcher>\r\n");
  35. return 0;
  36. }
  37. static int doc_add_watcher_list(dstring_t *buf, struct presentity* p)
  38. {
  39. watcher_t *watcher = p->first_watcher;
  40. internal_pa_subscription_t *subscription = p->first_qsa_subscription;
  41. dstr_append_zt(buf, "\t<watcher-list resource=\"");
  42. dstr_append_str(buf, &p->data.uri);
  43. dstr_append_zt(buf, "\" package=\"presence\">\r\n");
  44. while (watcher) {
  45. doc_add_watcher(buf, watcher);
  46. watcher = watcher->next;
  47. }
  48. while (subscription) {
  49. doc_add_internal_watcher(buf, subscription);
  50. subscription = subscription->next;
  51. }
  52. /* FIXME: for testing - create too big document to be sent
  53. * and trace memory where occurs the leak !!!*/
  54. /* if (1) {
  55. int i;
  56. for (i = 0; i < 1000; i++) {
  57. dstr_append_zt(buf, "<!--This is a very very very very long text,"
  58. "which will be appended to watcher information to disable"
  59. " sending it and trace memory leaks which I had"
  60. " seen before -->\r\n");
  61. }
  62. }
  63. */
  64. dstr_append_zt(buf, "\t</watcher-list>\r\n");
  65. return 0;
  66. }
  67. static int doc_add_winfo(dstring_t *buf, struct presentity* p, struct watcher* w)
  68. {
  69. char tmp[256];
  70. dstr_append_zt(buf, "<watcherinfo xmlns=\"urn:ietf:params:xml:ns:watcherinfo\" version=\"");
  71. sprintf(tmp, "%d", w->document_index);
  72. dstr_append_zt(buf, tmp);
  73. dstr_append_zt(buf, "\" state=\"full\">\r\n");
  74. doc_add_watcher_list(buf, p);
  75. dstr_append_zt(buf, "</watcherinfo>\r\n");
  76. return 0;
  77. }
  78. int create_winfo_document(struct presentity* p, struct watcher* w, str *dst, str *dst_content_type)
  79. {
  80. dstring_t buf;
  81. int err;
  82. if (!dst) return -1;
  83. str_clear(dst);
  84. if (dst_content_type) str_clear(dst_content_type);
  85. if ((!p) || (!w)) return -1;
  86. if (dst_content_type)
  87. if (str_dup_zt(dst_content_type, "application/watcherinfo+xml") < 0) {
  88. return -1;
  89. }
  90. /* if (!p->first_tuple) return 0;*/ /* no tuples => nothing to say */
  91. dstr_init(&buf, 2048);
  92. dstr_append_zt(&buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
  93. doc_add_winfo(&buf, p, w);
  94. err = dstr_get_str(&buf, dst);
  95. dstr_destroy(&buf);
  96. if (err != 0) {
  97. str_free_content(dst);
  98. if (dst_content_type) str_free_content(dst_content_type);
  99. }
  100. return err;
  101. }
  102. static int doc_add_watcher_offline(dstring_t *buf, offline_winfo_t *w)
  103. {
  104. char tmp[64];
  105. char *wevent = "subscribe";
  106. dstr_append_zt(buf, "\t\t<watcher status=\"");
  107. dstr_append_str(buf, &w->status);
  108. dstr_append_zt(buf, "\" event=\"");
  109. dstr_append_zt(buf, wevent);
  110. dstr_append_zt(buf, "\" id=\"");
  111. sprintf(tmp, "ol%p%x", w, rand());
  112. dstr_append_zt(buf, tmp);
  113. dstr_append_zt(buf, "\">");
  114. dstr_append_str(buf, &w->watcher);
  115. dstr_append_zt(buf, "</watcher>\r\n");
  116. return 0;
  117. }
  118. static int doc_add_watcher_list_offline(dstring_t *buf, struct presentity* p, offline_winfo_t *info)
  119. {
  120. offline_winfo_t *i = info;
  121. dstr_append_zt(buf, "\t<watcher-list resource=\"");
  122. dstr_append_str(buf, &p->data.uri);
  123. dstr_append_zt(buf, "\" package=\"presence\">\r\n");
  124. while (i) {
  125. doc_add_watcher_offline(buf, i);
  126. i = i->next;
  127. }
  128. dstr_append_zt(buf, "\t</watcher-list>\r\n");
  129. return 0;
  130. }
  131. static int doc_add_winfo_offline(dstring_t *buf, struct presentity* p, struct watcher* w, offline_winfo_t *info)
  132. {
  133. char tmp[256];
  134. dstr_append_zt(buf, "<watcherinfo xmlns=\"urn:ietf:params:xml:ns:watcherinfo\" version=\"");
  135. sprintf(tmp, "%d", w->document_index);
  136. dstr_append_zt(buf, tmp);
  137. dstr_append_zt(buf, "\" state=\"partial\">\r\n"); /* !!! only partial notification !!! */
  138. doc_add_watcher_list_offline(buf, p, info);
  139. dstr_append_zt(buf, "</watcherinfo>\r\n");
  140. return 0;
  141. }
  142. /* create the NOTIFY from given infos */
  143. int create_winfo_document_offline(struct presentity* p, struct watcher* w,
  144. offline_winfo_t *infos, str *dst, str *dst_content_type)
  145. {
  146. dstring_t buf;
  147. if (!dst) return -1;
  148. str_clear(dst);
  149. if (dst_content_type) str_clear(dst_content_type);
  150. if ((!p) || (!w)) return -1;
  151. if (dst_content_type)
  152. str_dup_zt(dst_content_type, "application/watcherinfo+xml");
  153. /* if (!p->first_tuple) return 0;*/ /* no tuples => nothing to say */
  154. dstr_init(&buf, 2048);
  155. dstr_append_zt(&buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
  156. doc_add_winfo_offline(&buf, p, w, infos);
  157. dstr_get_str(&buf, dst);
  158. dstr_destroy(&buf);
  159. return 0;
  160. }