]>
Commit | Line | Data |
---|---|---|
9a1955a7 | 1 | /* |
bb9dabf9 | 2 | * Copyright (c) 2015-2019 Red Hat, Inc. |
9a1955a7 JF |
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 | ||
bb9dabf9 | 35 | #include "log.h" |
9a1955a7 JF |
36 | #include "qnetd-client-algo-timer.h" |
37 | #include "qnetd-client-send.h" | |
38 | #include "qnetd-algorithm.h" | |
39 | #include "timer-list.h" | |
40 | ||
41 | static int | |
42 | qnetd_client_algo_timer_callback(void *data1, void *data2) | |
43 | { | |
44 | struct qnetd_client *client; | |
45 | enum tlv_vote result_vote; | |
46 | int send_vote; | |
47 | int reschedule_timer; | |
48 | enum tlv_reply_error_code reply_error_code; | |
49 | ||
50 | client = (struct qnetd_client *)data1; | |
51 | ||
52 | result_vote = TLV_VOTE_WAIT_FOR_REPLY; | |
53 | send_vote = 0; | |
54 | reschedule_timer = 0; | |
55 | ||
56 | reply_error_code = qnetd_algorithm_timer_callback(client, &reschedule_timer, | |
57 | &send_vote, &result_vote); | |
58 | ||
59 | if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) { | |
bb9dabf9 | 60 | log(LOG_ERR, "Algorithm for client %s returned error code. " |
9a1955a7 JF |
61 | "Sending error reply.", client->addr_str); |
62 | ||
63 | if (qnetd_client_send_err(client, 0, 0, reply_error_code) != 0) { | |
64 | client->schedule_disconnect = 1; | |
65 | return (0); | |
66 | } | |
67 | ||
68 | return (0); | |
69 | } else { | |
bb9dabf9 | 70 | log(LOG_DEBUG, "Algorithm for client %s decided to %s timer and %s vote " |
9a1955a7 JF |
71 | "with value %s", client->addr_str, |
72 | (reschedule_timer ? "reschedule" : "not reschedule"), | |
73 | (send_vote ? "send" : "not send"), | |
74 | tlv_vote_to_str(result_vote)); | |
75 | } | |
76 | ||
77 | if (send_vote) { | |
78 | client->algo_timer_vote_info_msq_seq_number++; | |
79 | ||
80 | if (qnetd_client_send_vote_info(client, | |
81 | client->algo_timer_vote_info_msq_seq_number, &client->last_ring_id, | |
82 | result_vote) != 0) { | |
83 | client->schedule_disconnect = 1; | |
84 | return (0); | |
85 | } | |
86 | } | |
87 | ||
88 | if (reschedule_timer) { | |
89 | /* | |
90 | * Timer list makes sure to schedule callback again | |
91 | */ | |
92 | return (-1); | |
93 | } | |
94 | ||
95 | client->algo_timer = NULL; | |
96 | return (0); | |
97 | } | |
98 | ||
99 | int | |
100 | qnetd_client_algo_timer_is_scheduled(struct qnetd_client *client) | |
101 | { | |
102 | ||
103 | return (client->algo_timer != NULL); | |
104 | } | |
105 | ||
106 | int | |
107 | qnetd_client_algo_timer_schedule_timeout(struct qnetd_client *client, uint32_t timeout) | |
108 | { | |
109 | ||
110 | if (qnetd_client_algo_timer_is_scheduled(client)) { | |
111 | if (qnetd_client_algo_timer_abort(client) != 0) { | |
bb9dabf9 | 112 | log(LOG_ERR, "Can't abort algo timer"); |
9a1955a7 JF |
113 | |
114 | return (-1); | |
115 | } | |
116 | } | |
117 | ||
118 | client->algo_timer = timer_list_add(client->main_timer_list, timeout, | |
119 | qnetd_client_algo_timer_callback, (void *)client, NULL); | |
120 | if (client->algo_timer == NULL) { | |
bb9dabf9 | 121 | log(LOG_ERR, "Can't schedule algo timer"); |
9a1955a7 JF |
122 | |
123 | return (-1); | |
124 | } | |
125 | ||
126 | return (0); | |
127 | } | |
128 | ||
129 | int | |
130 | qnetd_client_algo_timer_schedule(struct qnetd_client *client) | |
131 | { | |
132 | ||
133 | return (qnetd_client_algo_timer_schedule_timeout(client, client->heartbeat_interval / 4)); | |
134 | } | |
135 | ||
136 | int | |
137 | qnetd_client_algo_timer_abort(struct qnetd_client *client) | |
138 | { | |
139 | ||
140 | if (qnetd_client_algo_timer_is_scheduled(client)) { | |
af4f8826 | 141 | timer_list_entry_delete(client->main_timer_list, client->algo_timer); |
9a1955a7 JF |
142 | client->algo_timer = NULL; |
143 | } | |
144 | ||
145 | return (0); | |
146 | } |