| 1 |
/* |
| 2 |
* clmtest.c: AIS membership service client application |
| 3 |
* |
| 4 |
* Copyright (c) 2003 Intel Corp. |
| 5 |
* Author: Zhu Yi ([email protected]) |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "lha_internal.h" |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <signal.h> |
| 26 |
#include <strings.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/time.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <saf/ais.h> |
| 32 |
|
| 33 |
#define MAX_ITEMS 5 /* for a max 5-nodes cluster */ |
| 34 |
|
| 35 |
/* global variables */ |
| 36 |
static SaClmHandleT hd; |
| 37 |
static SaClmClusterNotificationT nbuf[MAX_ITEMS]; |
| 38 |
static SaClmClusterNodeT cn; |
| 39 |
|
| 40 |
static void |
| 41 |
track_start(int sig) |
| 42 |
{ |
| 43 |
int ret; |
| 44 |
SaUint8T flag = SA_TRACK_CHANGES; |
| 45 |
|
| 46 |
signal(SIGUSR1, &track_start); |
| 47 |
printf("-------------------------------------------------\n"); |
| 48 |
fprintf(stderr, "Start to Track Cluster Membership\n"); |
| 49 |
if ((ret = saClmClusterTrackStart(&hd, flag, nbuf |
| 50 |
, MAX_ITEMS)) != SA_OK) { |
| 51 |
fprintf(stderr, "saClmClusterTrackStart error, errno [%d]\n" |
| 52 |
, ret); |
| 53 |
exit(1); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
track_stop(int sig) |
| 59 |
{ |
| 60 |
int ret; |
| 61 |
|
| 62 |
signal(SIGUSR2, &track_start); |
| 63 |
fprintf(stderr, "Stop to Track Cluster Membership\n"); |
| 64 |
if ((ret = saClmClusterTrackStop(&hd)) != SA_OK) { |
| 65 |
fprintf(stderr, "saClmClusterTrackStop error, errno [%d]\n" |
| 66 |
, ret); |
| 67 |
exit(1); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
track_callback(SaClmClusterNotificationT *nbuf, SaUint32T nitem |
| 73 |
, SaUint32T nmem, SaUint64T nview, SaErrorT error) |
| 74 |
{ |
| 75 |
uint i; |
| 76 |
|
| 77 |
if (error != SA_OK) { |
| 78 |
fprintf(stderr, "Track Callback failed [%d]\n", error); |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
printf("-------------------------------------------------\n"); |
| 82 |
printf("SA CLM Track Callback BEGIN\n"); |
| 83 |
printf("viewNumber = %llu\n", nview); |
| 84 |
printf("numberOfItems = %lu\n", nitem); |
| 85 |
printf("numberOfMembers = %lu\n", nmem); |
| 86 |
|
| 87 |
for (i = 0; i < nitem; i++) { |
| 88 |
printf("\n"); |
| 89 |
printf("\tclusterChanges = %s [%d]\n" |
| 90 |
, nbuf[i].clusterChanges == 1 ? "SA_CLM_NODE_NO_CHANGE" |
| 91 |
: nbuf[i].clusterChanges == 2 ? "SA_CLM_NODE_JOINED" |
| 92 |
: nbuf[i].clusterChanges == 3 ? "SA_CLM_NODE_LEFT":"ERROR" |
| 93 |
, nbuf[i].clusterChanges); |
| 94 |
printf("\tnodeId = %ld\n", nbuf[i].clusterNode.nodeId); |
| 95 |
printf("\tnodeAddress = %s\n" |
| 96 |
, nbuf[i].clusterNode.nodeAddress.length > 0 |
| 97 |
? (char *)nbuf[i].clusterNode.nodeAddress.value : "N/A"); |
| 98 |
printf("\tnodeName = %s\n" |
| 99 |
, nbuf[i].clusterNode.nodeName.length > 0 |
| 100 |
? (char *)nbuf[i].clusterNode.nodeName.value : "N/A"); |
| 101 |
printf("\tclusterName = %s\n" |
| 102 |
, nbuf[i].clusterNode.clusterName.length > 0 |
| 103 |
? (char *)nbuf[i].clusterNode.clusterName.value : "N/A"); |
| 104 |
printf("\tmember = %d\n", nbuf[i].clusterNode.member); |
| 105 |
printf("\tbootTimestamp = %lld\n" |
| 106 |
, nbuf[i].clusterNode.bootTimestamp); |
| 107 |
} |
| 108 |
printf("\nSA CLM Track Callback END\n"); |
| 109 |
} |
| 110 |
|
| 111 |
static void |
| 112 |
dump_nodeinfo(SaClmClusterNodeT *cn) |
| 113 |
{ |
| 114 |
printf("Dump information from SaClmClusterNodeGet\n"); |
| 115 |
printf("\n"); |
| 116 |
printf("nodeId = %ld\n", cn->nodeId); |
| 117 |
printf("nodeAddress = %s\n" |
| 118 |
, cn->nodeAddress.length > 0 |
| 119 |
? (char *)cn->nodeAddress.value : "N/A"); |
| 120 |
printf("nodeName = %s\n" |
| 121 |
, cn->nodeName.length > 0 |
| 122 |
? (char *)cn->nodeName.value : "N/A"); |
| 123 |
printf("clusterName = %s\n" |
| 124 |
, cn->clusterName.length > 0 |
| 125 |
? (char *)cn->clusterName.value : "N/A"); |
| 126 |
printf("member = %d\n", cn->member); |
| 127 |
printf("bootTimestamp = %lld\n", cn->bootTimestamp); |
| 128 |
printf("\n"); |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
getnode_callback(SaInvocationT invocation, SaClmClusterNodeT *clusterNode |
| 133 |
, SaErrorT error) |
| 134 |
{ |
| 135 |
if (error != SA_OK) { |
| 136 |
fprintf(stderr, "Get Node Callback failed [%d]\n", error); |
| 137 |
exit(1); |
| 138 |
} |
| 139 |
fprintf(stderr, "Invocation [%d]\n", invocation); |
| 140 |
dump_nodeinfo(clusterNode); |
| 141 |
} |
| 142 |
|
| 143 |
int main(void) |
| 144 |
{ |
| 145 |
SaSelectionObjectT st; |
| 146 |
SaErrorT ret; |
| 147 |
SaClmNodeIdT nid; |
| 148 |
|
| 149 |
SaClmCallbacksT my_callbacks = { |
| 150 |
.saClmClusterTrackCallback |
| 151 |
= (SaClmClusterTrackCallbackT)track_callback, |
| 152 |
.saClmClusterNodeGetCallback |
| 153 |
= (SaClmClusterNodeGetCallbackT)getnode_callback |
| 154 |
}; |
| 155 |
|
| 156 |
if ((ret = saClmInitialize(&hd, &my_callbacks, NULL)) != SA_OK) { |
| 157 |
fprintf(stderr, "saClmInitialize error, errno [%d]\n",ret); |
| 158 |
return 1; |
| 159 |
} |
| 160 |
if ((ret = saClmSelectionObjectGet(&hd, &st)) != SA_OK) { |
| 161 |
fprintf(stderr, "saClmSelectionObjectGet error, errno [%d]\n" |
| 162 |
, ret); |
| 163 |
return 1; |
| 164 |
} |
| 165 |
|
| 166 |
nid = 0; |
| 167 |
|
| 168 |
/* Synchronously get nodeId information */ |
| 169 |
printf("-------------------------------------------------\n"); |
| 170 |
printf("Get nodeId [%lu] info by SaClmClusterNodeGet\n", nid); |
| 171 |
if ((ret = saClmClusterNodeGet(nid, 10, &cn)) != SA_OK) { |
| 172 |
if (ret == SA_ERR_INVALID_PARAM) { |
| 173 |
fprintf(stderr, "NodeId [%lu] record not found!\n",nid); |
| 174 |
} else { |
| 175 |
fprintf(stderr |
| 176 |
, "saClmClusterNodeGet error, errno [%d]\n" |
| 177 |
, ret); |
| 178 |
return 1; |
| 179 |
} |
| 180 |
} else { |
| 181 |
dump_nodeinfo(&cn); |
| 182 |
} |
| 183 |
|
| 184 |
/* Asynchronously get my nodeId information */ |
| 185 |
nid = 1; |
| 186 |
printf("-------------------------------------------------\n"); |
| 187 |
printf("Get nodeId [%lu] info by SaClmClusterNodeGetAsync\n", nid); |
| 188 |
if ((ret = saClmClusterNodeGetAsync(&hd, 1234, nid, &cn)) != SA_OK) { |
| 189 |
if (ret == SA_ERR_INVALID_PARAM) { |
| 190 |
fprintf(stderr, "NodeId [%lu] record not found!\n",nid); |
| 191 |
} else { |
| 192 |
fprintf(stderr |
| 193 |
, "saClmClusterNodeGet error, errno [%d]\n" |
| 194 |
, ret); |
| 195 |
return 1; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/* Start to track cluster membership changes events */ |
| 200 |
track_start(SIGUSR1); |
| 201 |
signal(SIGUSR2, &track_stop); |
| 202 |
|
| 203 |
for (;;) { |
| 204 |
fd_set rset; |
| 205 |
|
| 206 |
FD_ZERO(&rset); |
| 207 |
FD_SET(st, &rset); |
| 208 |
|
| 209 |
if (select(st + 1, &rset, NULL, NULL, NULL) == -1) { |
| 210 |
/* TODO should we use pselect here? */ |
| 211 |
if (errno == EINTR) |
| 212 |
continue; |
| 213 |
else { |
| 214 |
perror("select"); |
| 215 |
return 1; |
| 216 |
} |
| 217 |
} |
| 218 |
if ((ret = saClmDispatch(&hd, SA_DISPATCH_ALL)) != SA_OK) { |
| 219 |
if (ret == SA_ERR_LIBRARY) { |
| 220 |
fprintf(stderr, "I am evicted!\n"); |
| 221 |
return 1; |
| 222 |
} |
| 223 |
fprintf(stderr, "saClmDispatch error, errno [%d]\n" |
| 224 |
, ret); |
| 225 |
return 1; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return 0; |
| 230 |
} |