qnetd-client-algo-timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 %s returned error code. "
  55. "Sending error reply.", client->addr_str);
  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 %s decided to %s timer and %s vote "
  63. "with value %s", client->addr_str,
  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, &client->last_ring_id,
  72. result_vote) != 0) {
  73. client->schedule_disconnect = 1;
  74. return (0);
  75. }
  76. }
  77. if (reschedule_timer) {
  78. /*
  79. * Timer list makes sure to schedule callback again
  80. */
  81. return (-1);
  82. }
  83. client->algo_timer = NULL;
  84. return (0);
  85. }
  86. int
  87. qnetd_client_algo_timer_is_scheduled(struct qnetd_client *client)
  88. {
  89. return (client->algo_timer != NULL);
  90. }
  91. int
  92. qnetd_client_algo_timer_schedule_timeout(struct qnetd_client *client, uint32_t timeout)
  93. {
  94. if (qnetd_client_algo_timer_is_scheduled(client)) {
  95. if (qnetd_client_algo_timer_abort(client) != 0) {
  96. qnetd_log(LOG_ERR, "Can't abort algo timer");
  97. return (-1);
  98. }
  99. }
  100. client->algo_timer = timer_list_add(client->main_timer_list, timeout,
  101. qnetd_client_algo_timer_callback, (void *)client, NULL);
  102. if (client->algo_timer == NULL) {
  103. qnetd_log(LOG_ERR, "Can't schedule algo timer");
  104. return (-1);
  105. }
  106. return (0);
  107. }
  108. int
  109. qnetd_client_algo_timer_schedule(struct qnetd_client *client)
  110. {
  111. return (qnetd_client_algo_timer_schedule_timeout(client, client->heartbeat_interval / 4));
  112. }
  113. int
  114. qnetd_client_algo_timer_abort(struct qnetd_client *client)
  115. {
  116. if (qnetd_client_algo_timer_is_scheduled(client)) {
  117. timer_list_delete(client->main_timer_list, client->algo_timer);
  118. client->algo_timer = NULL;
  119. }
  120. return (0);
  121. }