Skip to content

Latest commit

 

History

History
187 lines (137 loc) · 3.9 KB

hbtest2.c

File metadata and controls

187 lines (137 loc) · 3.9 KB
 
Apr 21, 2011
Apr 21, 2011
1
2
3
4
5
6
7
8
9
10
11
/**
@brief Test utility #2 for IP Heartbeat service
@file hbtest2.c
This is the test utility for IP Heartbeat service.
It tries to emulate fixed-sync applications.
<p>
May 12, 2011
May 12, 2011
12
Copyright (C) 2008-2011 Nokia Corporation.
Apr 21, 2011
Apr 21, 2011
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@author Raimo Vuonnala <raimo.vuonnala@nokia.com>
This file is part of Dsme.
Dsme is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Dsme is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Dsme. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
/* socket transport */
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
May 30, 2013
May 30, 2013
48
#include "../src/libiphb.h"
Apr 21, 2011
Apr 21, 2011
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
static volatile int run = 1;
#define ME "hbtest2: "
static int debugmode = 0;
static void
sig_handler(int signo)
{
switch (signo) {
case SIGQUIT:
case SIGTERM:
case SIGINT:
run = 0;
break;
default:
fprintf(stderr, ME "\aERROR, unknown signal %d\n", signo);
}
}
int
main (int argc, char *argv[])
{
iphb_t hb;
int hbsock = -1;
int period;
if (argc < 2) {
printf("Usage: %s period_in_secs [-d]\n", argv[0]);
exit(1);
}
period = atoi(argv[1]);
if (argc >= 3 && strcmp(argv[2], "-d") == 0)
debugmode = 1;
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGPIPE, SIG_IGN);
printf(ME "running\n");
hb = iphb_open(0);
if (!hb) {
perror(ME "\aERROR, iphb_open()");
run = 0;
}
else {
struct iphb_stats stats;
printf(ME "iphb service opened\n");
if (iphb_get_stats(hb, &stats) == -1)
fprintf(stderr, ME "\aERROR, iphb_get_stats() failed %s\n", strerror(errno));
else
printf(ME "iphb_get_stats(): clients=%u, waiting=%u, next hb=%u secs\n", stats.clients, stats.waiting, stats.next_hb);
}
if (run) {
hbsock = iphb_get_fd(hb);
if (hbsock == -1) {
perror(ME "\aERROR, iphb_get_fd()");
run = 0;
}
}
for (;run;) {
time_t now;
time_t went_to_sleep;
struct timeval timeout;
fd_set readfds;
int st;
went_to_sleep = time(0);
if (iphb_wait(hb, period, period, 0) != 0) {
perror(ME "\aERROR, iphb_wait()");
run = 0;
continue;
}
printf(ME "waiting for iphbd wakeup...\n");
timeout.tv_sec = period + 2;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(hbsock, &readfds);
st = select(hbsock + 1, &readfds, NULL, NULL, &timeout);
now = time(0);
if (st == -1) {
if (errno == EINTR)
continue;
else {
perror(ME "\aERROR, select()");
run = 0;
}
}
else
if (st >= 0) {
if (now - went_to_sleep > period + 1) /* allow 1 sec slippage */
fprintf(stderr, ME "\aERROR, select() did not fire as expected, took %d secs\n",
(int)(now - went_to_sleep));
if (debugmode) printf(ME "slept %d secs\n", (int)(now - went_to_sleep));
if (FD_ISSET(hbsock, &readfds)) {
printf(ME "select() woken by iphbd, waited %d secs\n",
(int)(now - went_to_sleep));
}
if (st == 0) {
fprintf(stderr, ME "\aERROR, select() did not fire at all!\n");
}
Apr 21, 2011
Apr 21, 2011
175
176
177
printf(ME "woke up, last heartbeat happened %d secs ago, now is %s",
(int)(now - went_to_sleep),
ctime(&now));
Apr 21, 2011
Apr 21, 2011
178
179
180
181
182
183
184
185
186
}
}
printf(ME "bye\n");
if (hb)
iphb_close(hb);
return 0;
}