execute.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2018-2020 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "swclt_test.h"
  23. static void __on_session_state_provider(swclt_sess_t *sess, void *condV)
  24. {
  25. ks_cond_t *cond = (ks_cond_t *)condV;
  26. if (sess->state == SWCLT_STATE_ONLINE) {
  27. ks_json_t *channels = ks_json_create_array();
  28. swclt_sess_protocol_provider_add(sess,
  29. "test",
  30. BLADE_ACL_SYSTEM, // rpc execute
  31. BLADE_ACL_SYSTEM, // channel subscribe
  32. BLADE_ACL_SYSTEM, // channel broadcast
  33. NULL, // method access overrides
  34. &channels,
  35. 3,
  36. NULL,
  37. NULL);
  38. swclt_sess_metric_register(sess, "test", 30, 9);
  39. } else if (sess->state == SWCLT_STATE_OFFLINE) {
  40. // Disconnected
  41. }
  42. /* Notify the waiting test of the state change */
  43. ks_cond_lock(cond);
  44. ks_cond_broadcast(cond);
  45. ks_cond_unlock(cond);
  46. }
  47. static void __on_session_state(swclt_sess_t *sess, void *condV)
  48. {
  49. /* Notify the waiting test of the state change */
  50. ks_cond_t *cond = (ks_cond_t *)condV;
  51. ks_cond_lock(cond);
  52. ks_cond_broadcast(cond);
  53. ks_cond_unlock(cond);
  54. }
  55. static ks_status_t __on_incoming_test_execute_rqu(swclt_sess_t *sess, swclt_cmd_t *cmd, const blade_execute_rqu_t *rqu, void *data)
  56. {
  57. /* Formulate a response */
  58. ks_cond_t *cond = (ks_cond_t *)data;
  59. ks_json_t *result = ks_json_create_object();
  60. ks_json_add_string_to_object(result, "reply", "i got it!");
  61. ks_json_t *cmd_result = BLADE_EXECUTE_RPL_MARSHAL(
  62. &(blade_execute_rpl_t){
  63. rqu->requester_nodeid,
  64. rqu->responder_nodeid,
  65. result});
  66. REQUIRE(!swclt_cmd_set_result(cmd, &cmd_result));
  67. return KS_STATUS_SUCCESS;
  68. }
  69. static void __on_outgoing_test_execute_rpl(swclt_cmd_reply_t *reply, void *cond)
  70. {
  71. ks_cond_broadcast((ks_cond_t *)cond);
  72. }
  73. void test_execute(ks_pool_t *pool)
  74. {
  75. swclt_sess_t *sess1 = NULL, *sess2 = NULL;
  76. char *nodeid1, *nodeid2;
  77. ks_cond_t *cond;
  78. const char *ident;
  79. REQUIRE(!ks_cond_create(&cond, pool));
  80. ks_cond_lock(cond);
  81. /* Create two sessions to blade */
  82. REQUIRE(!swclt_sess_create(&sess1, g_target_ident_str, g_certified_config));
  83. REQUIRE(!swclt_sess_create(&sess2, g_target_ident_str, g_certified_config));
  84. /* Get called back when they're connected */
  85. REQUIRE(!swclt_sess_set_state_change_cb(sess1, __on_session_state, cond));
  86. REQUIRE(!swclt_sess_set_state_change_cb(sess2, __on_session_state_provider, cond));
  87. /* On the second session register a execute handler we'll communicate with */
  88. REQUIRE(!swclt_sess_register_protocol_method(sess2, "test", "test.method", __on_incoming_test_execute_rqu, cond));
  89. /* Initiate the connect */
  90. REQUIRE(!swclt_sess_connect(sess1));
  91. REQUIRE(!swclt_sess_connect(sess2));
  92. int i = 15;
  93. while (i-- > 0 && (!swclt_sess_connected(sess1) || !swclt_sess_connected(sess2))) {
  94. ks_cond_timedwait(cond, 1000);
  95. }
  96. REQUIRE(swclt_sess_connected(sess1));
  97. REQUIRE(swclt_sess_connected(sess2));
  98. ks_cond_unlock(cond);
  99. /* Load our nodeids for both */
  100. REQUIRE(!swclt_sess_info(sess1, pool, NULL, &nodeid1, NULL));
  101. REQUIRE(!swclt_sess_info(sess2, pool, NULL, &nodeid2, NULL));
  102. /* Now execute from the first session to the second session */
  103. ks_json_t *params = ks_json_create_object();
  104. REQUIRE(params);
  105. ks_json_add_string_to_object(params, "arg", "value");
  106. swclt_cmd_future_t *future = NULL;
  107. REQUIRE(!swclt_sess_execute_async(sess1, nodeid2, "test", "test.method", &params, NULL, NULL, &future));
  108. REQUIRE(future);
  109. swclt_cmd_reply_t *reply = NULL;
  110. REQUIRE(!swclt_cmd_future_get(future, &reply));
  111. swclt_cmd_future_destroy(&future);
  112. swclt_cmd_reply_destroy(&reply);
  113. /* repeat, but this time don't wait for any response */
  114. params = ks_json_create_object();
  115. REQUIRE(params);
  116. ks_json_add_string_to_object(params, "arg", "value");
  117. REQUIRE(!swclt_sess_execute_async(sess1, nodeid2, "test", "test.method", &params, NULL, NULL, NULL));
  118. ks_sleep_ms(5000);
  119. swclt_sess_destroy(&sess1);
  120. swclt_sess_destroy(&sess2);
  121. ks_cond_destroy(&cond);
  122. }