client.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/time.h>
  14. #include <unistd.h>
  15. #include <libubox/ustream.h>
  16. #include "libubus.h"
  17. #include "count.h"
  18. static struct ubus_context *ctx;
  19. static struct blob_buf b;
  20. static void test_client_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
  21. {
  22. fprintf(stderr, "Subscribers active: %d\n", obj->has_subscribers);
  23. }
  24. static struct ubus_object test_client_object = {
  25. .subscribe_cb = test_client_subscribe_cb,
  26. };
  27. static void test_client_notify_cb(struct uloop_timeout *timeout)
  28. {
  29. static int counter = 0;
  30. int err;
  31. struct timeval tv1, tv2;
  32. int max = 1000;
  33. long delta;
  34. int i = 0;
  35. blob_buf_init(&b, 0);
  36. blobmsg_add_u32(&b, "counter", counter++);
  37. gettimeofday(&tv1, NULL);
  38. for (i = 0; i < max; i++)
  39. err = ubus_notify(ctx, &test_client_object, "ping", b.head, 1000);
  40. gettimeofday(&tv2, NULL);
  41. if (err)
  42. fprintf(stderr, "Notify failed: %s\n", ubus_strerror(err));
  43. delta = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
  44. fprintf(stderr, "Avg time per iteration: %ld usec\n", delta / max);
  45. uloop_timeout_set(timeout, 1000);
  46. }
  47. enum {
  48. RETURN_CODE,
  49. __RETURN_MAX,
  50. };
  51. static const struct blobmsg_policy return_policy[__RETURN_MAX] = {
  52. [RETURN_CODE] = { .name = "rc", .type = BLOBMSG_TYPE_INT32 },
  53. };
  54. static void test_count_data_cb(struct ubus_request *req,
  55. int type, struct blob_attr *msg)
  56. {
  57. struct blob_attr *tb[__RETURN_MAX];
  58. int rc;
  59. uint32_t count_to = *(uint32_t *)req->priv;
  60. blobmsg_parse(return_policy, __RETURN_MAX, tb, blob_data(msg), blob_len(msg));
  61. if (!tb[RETURN_CODE]) {
  62. fprintf(stderr, "No return code received from server\n");
  63. return;
  64. }
  65. rc = blobmsg_get_u32(tb[RETURN_CODE]);
  66. if (rc)
  67. fprintf(stderr, "Corruption of data with count up to '%u'\n", count_to);
  68. else
  69. fprintf(stderr, "Server validated our count up to '%u'\n", count_to);
  70. }
  71. static void test_count(struct uloop_timeout *timeout)
  72. {
  73. enum {
  74. COUNT_TO_MIN = 10000,
  75. COUNT_TO_MAX = 1000000,
  76. PROGRESSION = 100,
  77. };
  78. uint32_t id;
  79. static uint32_t count_to = 100000;
  80. static int count_progression = PROGRESSION;
  81. char *s;
  82. if (count_to <= COUNT_TO_MIN)
  83. count_progression = PROGRESSION;
  84. else if (count_to >= COUNT_TO_MAX)
  85. count_progression = -PROGRESSION;
  86. count_to += count_progression;
  87. s = count_to_number(count_to);
  88. if (!s) {
  89. fprintf(stderr, "Could not allocate memory to count up to '%u'\n", count_to);
  90. return;
  91. }
  92. fprintf(stderr, "Sending count up to '%u'; string has length '%u'\n",
  93. count_to, (uint32_t)strlen(s));
  94. blob_buf_init(&b, 0);
  95. blobmsg_add_u32(&b, "to", count_to);
  96. blobmsg_add_string(&b, "string", s);
  97. if (ubus_lookup_id(ctx, "test", &id)) {
  98. free(s);
  99. fprintf(stderr, "Failed to look up test object\n");
  100. return;
  101. }
  102. ubus_invoke(ctx, id, "count", b.head, test_count_data_cb, &count_to, 5000);
  103. free(s);
  104. uloop_timeout_set(timeout, 2000);
  105. }
  106. static struct uloop_timeout notify_timer = {
  107. .cb = test_client_notify_cb,
  108. };
  109. static struct uloop_timeout count_timer = {
  110. .cb = test_count,
  111. };
  112. static void test_client_fd_data_cb(struct ustream *s, int bytes)
  113. {
  114. char *data, *sep;
  115. int len;
  116. data = ustream_get_read_buf(s, &len);
  117. if (len < 1)
  118. return;
  119. sep = strchr(data, '\n');
  120. if (!sep)
  121. return;
  122. *sep = 0;
  123. fprintf(stderr, "Got line: %s\n", data);
  124. ustream_consume(s, sep + 1 - data);
  125. }
  126. static void test_client_fd_cb(struct ubus_request *req, int fd)
  127. {
  128. static struct ustream_fd test_fd;
  129. fprintf(stderr, "Got fd from the server, watching...\n");
  130. test_fd.stream.notify_read = test_client_fd_data_cb;
  131. ustream_fd_init(&test_fd, fd);
  132. }
  133. static void test_client_complete_cb(struct ubus_request *req, int ret)
  134. {
  135. fprintf(stderr, "completed request, ret: %d\n", ret);
  136. }
  137. static void client_main(void)
  138. {
  139. static struct ubus_request req;
  140. uint32_t id;
  141. int ret;
  142. ret = ubus_add_object(ctx, &test_client_object);
  143. if (ret) {
  144. fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
  145. return;
  146. }
  147. if (ubus_lookup_id(ctx, "test", &id)) {
  148. fprintf(stderr, "Failed to look up test object\n");
  149. return;
  150. }
  151. blob_buf_init(&b, 0);
  152. blobmsg_add_u32(&b, "id", test_client_object.id);
  153. ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
  154. test_client_notify_cb(&notify_timer);
  155. blob_buf_init(&b, 0);
  156. blobmsg_add_string(&b, "msg", "blah");
  157. ubus_invoke_async(ctx, id, "hello", b.head, &req);
  158. req.fd_cb = test_client_fd_cb;
  159. req.complete_cb = test_client_complete_cb;
  160. ubus_complete_request_async(ctx, &req);
  161. uloop_timeout_set(&count_timer, 2000);
  162. uloop_run();
  163. }
  164. int main(int argc, char **argv)
  165. {
  166. const char *ubus_socket = NULL;
  167. int ch;
  168. while ((ch = getopt(argc, argv, "cs:")) != -1) {
  169. switch (ch) {
  170. case 's':
  171. ubus_socket = optarg;
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. uloop_init();
  178. ctx = ubus_connect(ubus_socket);
  179. if (!ctx) {
  180. fprintf(stderr, "Failed to connect to ubus\n");
  181. return -1;
  182. }
  183. ubus_add_uloop(ctx);
  184. client_main();
  185. ubus_free(ctx);
  186. uloop_done();
  187. return 0;
  188. }