qnetd-client-algo-timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "qnetd-log.h"
  35. #include "qnetd-client-algo-timer.h"
  36. #include "qnetd-client-send.h"
  37. #include "qnetd-algorithm.h"
  38. #include "timer-list.h"
  39. static int
  40. qnetd_client_algo_timer_callback(void *data1, void *data2)
  41. {
  42. struct qnetd_client *client;
  43. enum tlv_vote result_vote;
  44. int send_vote;
  45. int reschedule_timer;
  46. enum tlv_reply_error_code reply_error_code;
  47. client = (struct qnetd_client *)data1;
  48. result_vote = TLV_VOTE_WAIT_FOR_REPLY;
  49. send_vote = 0;
  50. reschedule_timer = 0;
  51. reply_error_code = qnetd_algorithm_timer_callback(client, &reschedule_timer,
  52. &send_vote, &result_vote);
  53. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  54. qnetd_log(LOG_ERR, "Algorithm for client %p returned error code. "
  55. "Sending error reply.", client);
  56. if (qnetd_client_send_err(client, 0, 0, reply_error_code) != 0) {
  57. client->schedule_disconnect = 1;
  58. return (0);
  59. }
  60. return (0);
  61. } else {
  62. qnetd_log(LOG_DEBUG, "Algorithm for client %p decided to %s timer and %s vote "
  63. "with value %s", client,
  64. (reschedule_timer ? "reschedule" : "not reschedule"),
  65. (send_vote ? "send" : "not send"),
  66. tlv_vote_to_str(result_vote));
  67. }
  68. if (send_vote) {
  69. client->algo_timer_vote_info_msq_seq_number++;
  70. if (qnetd_client_send_vote_info(client,
  71. client->algo_timer_vote_info_msq_seq_number, result_vote) != 0) {
  72. client->schedule_disconnect = 1;
  73. return (0);
  74. }
  75. }
  76. if (reschedule_timer) {
  77. /*
  78. * Timer list makes sure to schedule callback again
  79. */
  80. return (-1);
  81. }
  82. client->algo_timer = NULL;
  83. return (0);
  84. }
  85. int
  86. qnetd_client_algo_timer_is_scheduled(struct qnetd_client *client)
  87. {
  88. return (client->algo_timer != NULL);
  89. }
  90. int
  91. qnetd_client_algo_timer_schedule_timeout(struct qnetd_client *client, uint32_t timeout)
  92. {
  93. if (qnetd_client_algo_timer_is_scheduled(client)) {
  94. if (qnetd_client_algo_timer_abort(client) != 0) {
  95. qnetd_log(LOG_ERR, "Can't abort algo timer");
  96. return (-1);
  97. }
  98. }
  99. client->algo_timer = timer_list_add(client->main_timer_list, timeout,
  100. qnetd_client_algo_timer_callback, (void *)client, NULL);
  101. if (client->algo_timer == NULL) {
  102. qnetd_log(LOG_ERR, "Can't schedule algo timer");
  103. return (-1);
  104. }
  105. return (0);
  106. }
  107. int
  108. qnetd_client_algo_timer_schedule(struct qnetd_client *client)
  109. {
  110. return (qnetd_client_algo_timer_schedule_timeout(client, client->heartbeat_interval / 4));
  111. }
  112. int
  113. qnetd_client_algo_timer_abort(struct qnetd_client *client)
  114. {
  115. if (qnetd_client_algo_timer_is_scheduled(client)) {
  116. timer_list_delete(client->main_timer_list, client->algo_timer);
  117. client->algo_timer = NULL;
  118. }
  119. return (0);
  120. }