execute.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018 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(swclt_sess_t sess, const swclt_hstate_change_t *state_change_info, ks_cond_t *cond)
  24. {
  25. /* Notify the waiting test of the state change */
  26. ks_cond_lock(cond);
  27. ks_cond_broadcast(cond);
  28. ks_cond_unlock(cond);
  29. }
  30. 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)
  31. {
  32. /* Formulate a response */
  33. ks_cond_t *cond = (ks_cond_t *)data;
  34. ks_pool_t *cmd_pool = ks_handle_pool(cmd);
  35. ks_json_t *result = ks_json_pcreate_object(cmd_pool);
  36. ks_json_padd_string_to_object(cmd_pool, result, "reply", "i got it!");
  37. ks_json_t *cmd_result = BLADE_EXECUTE_RPL_MARSHAL(
  38. cmd_pool,
  39. &(blade_execute_rpl_t){
  40. rqu->requester_nodeid,
  41. rqu->responder_nodeid,
  42. result});
  43. REQUIRE(!swclt_cmd_set_result(cmd, &cmd_result));
  44. /* Notify the waiting test of the state change */
  45. ks_cond_lock(cond);
  46. ks_cond_broadcast(cond);
  47. ks_cond_unlock(cond);
  48. return KS_STATUS_SUCCESS;
  49. }
  50. static void __on_outgoing_test_execute_rpl(swclt_cmd_t cmd, ks_cond_t *cond)
  51. {
  52. ks_cond_lock(cond);
  53. ks_cond_broadcast(cond);
  54. ks_cond_unlock(cond);
  55. }
  56. void test_execute(ks_pool_t *pool)
  57. {
  58. swclt_sess_t sess1, sess2;
  59. swclt_cfg_t cfg;
  60. char *nodeid1, *nodeid2;
  61. swclt_hmon_t sess1_mon, sess2_mon;
  62. ks_cond_t *cond;
  63. const char *ident;
  64. REQUIRE(!ks_cond_create(&cond, pool));
  65. ks_cond_lock(cond);
  66. /* Create two sessions to blade */
  67. REQUIRE(!swclt_sess_create(&sess1, g_target_ident_str, g_certified_config));
  68. REQUIRE(!swclt_sess_create(&sess2, g_target_ident_str, g_certified_config));
  69. /* Get called back when they're connected */
  70. REQUIRE(!swclt_hmon_register(&sess1_mon, sess1, __on_session_state, cond));
  71. REQUIRE(!swclt_hmon_register(&sess2_mon, sess2, __on_session_state, cond));
  72. /* On the second session register a execute handler we'll communicate with */
  73. REQUIRE(!swclt_sess_register_protocol_method(sess2, "test", "test.method", __on_incoming_test_execute_rqu, cond));
  74. /* Initiate the connect */
  75. REQUIRE(!swclt_sess_connect(sess1));
  76. REQUIRE(!swclt_sess_connect(sess2));
  77. /* Wait for the state to change, twice */
  78. ks_cond_wait(cond);
  79. ks_cond_wait(cond);
  80. /* Load our nodeids for both */
  81. REQUIRE(!swclt_sess_info(sess1, pool, NULL, &nodeid1, NULL));
  82. REQUIRE(!swclt_sess_info(sess2, pool, NULL, &nodeid2, NULL));
  83. /* Now execute from the first session to the second session */
  84. ks_json_t *params = ks_json_create_object();
  85. swclt_cmd_t sess1_exec_cmd;
  86. REQUIRE(params);
  87. REQUIRE(ks_json_add_string_to_object(params, "arg", "value"));
  88. REQUIRE(!swclt_sess_execute_async(sess1, nodeid2, "test", "test.method", &params, (swclt_cmd_cb_t)__on_outgoing_test_execute_rpl, cond, &sess1_exec_cmd));
  89. ks_cond_wait(cond);
  90. ks_cond_wait(cond);
  91. SWCLT_CMD_TYPE type;
  92. REQUIRE(!swclt_cmd_type(sess1_exec_cmd, &type));
  93. ks_log(KS_LOG_INFO, "Final type is: %s", swclt_cmd_type_str(type));
  94. REQUIRE(type == SWCLT_CMD_TYPE_RESULT);
  95. ks_handle_destroy(&sess1_mon);
  96. ks_handle_destroy(&sess2_mon);
  97. ks_handle_destroy(&sess1);
  98. ks_handle_destroy(&sess2);
  99. ks_handle_destroy(&cfg);
  100. ks_cond_destroy(&cond);
  101. }