This repository was archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxaptx.c
74 lines (49 loc) · 2.11 KB
/
xaptx.c
1
2
3
4
5
6
7
8
9
10
11
12
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
48
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
#include "Fichier.h"
#include "xapdef.h"
int xap_send_tick(int a_interval) {
time_t i_timenow;
static time_t i_sendtick=0;
i_timenow = time((time_t*)0);
// Check timer for heartbeat send
if ((i_timenow-i_sendtick>=a_interval)||(i_sendtick==0)) {
i_sendtick=i_timenow;
return 1;
} // send tick
return 0;
}
int xap_heartbeat_tick(int a_interval) {
// Call this function periodically.
// Every HBEAT_INTERVAL, it will send a heartbeat
time_t i_timenow;
static time_t i_heartbeattick=0;
i_timenow = time((time_t*)0);
// Check timer for heartbeat send
if ((i_timenow-i_heartbeattick>=a_interval)||(i_heartbeattick==0)) {
i_heartbeattick=i_timenow;
// Send heartbeat to all external listeners
xap_broadcast_heartbeat(g_xap_sender_sockfd, &g_xap_sender_address,a_interval);
return 1;
} // heartbeat tick
return 0;
}
// Send a heartbeat to the real world
int xap_broadcast_heartbeat(int a_sock, struct sockaddr_in* a_addr, int interval) {
int i;
char i_buff[1500];
// Construct the heartbeat message (to say we are alive)
// If using a hub, this must be sent on startup, because
// that is how the hub knows who we are
sprintf(i_buff, "xap-hbeat\n{\nv=12\nhop=1\nuid=%s\nclass=xap-hbeat.alive\nsource=%s.%s.%s\ninterval=%d\nport=%d\n}\n", g_uid, XAP_ME, XAP_SOURCE, g_instance, interval, ntohs(g_xap_receiver_address.sin_port));
if (g_debuglevel_xap>=DEBUG_VERBOSE) Flog_Ecrire("Heartbeat source=%s, instance=%s, interval=%d, port=%d",XAP_SOURCE, g_instance, interval, ntohs(g_xap_receiver_address.sin_port) );
// Send it...
i = sendto(a_sock, i_buff, (int)strlen(i_buff), 0, (struct sockaddr *)a_addr, sizeof(struct sockaddr));
if (g_debuglevel_xap>=DEBUG_VERBOSE) Flog_Ecrire("Broadcasting heartbeat");
return i;
}
// Build an outgoing xap (broadcast) message
int xap_send_message(const char* a_buff) {
// Send it...
sendto(g_xap_sender_sockfd, a_buff, (int)strlen(a_buff), 0, (struct sockaddr *)&g_xap_sender_address, sizeof(struct sockaddr));
if (g_debuglevel_xap>=DEBUG_VERBOSE) Flog_Ecrire("Outgoing xAP message sent");
return 1;
}