Send our own status periodically
This commit is contained in:
parent
541dd28a64
commit
f678c5054f
1 changed files with 37 additions and 1 deletions
38
ethermess.c
38
ethermess.c
|
@ -5,6 +5,7 @@
|
|||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <linux/if_packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <net/if.h>
|
||||
|
@ -19,6 +20,7 @@
|
|||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define EM_PROTOCOL_VERSION 0
|
||||
|
@ -43,6 +45,7 @@ unsigned char broadcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
|||
unsigned char own_status = EMS_AVAILABLE;
|
||||
unsigned char own_nick[256] = {'n', 'o', 'r', 't', 't', 'i'};
|
||||
unsigned char own_nick_length = 6;
|
||||
time_t next_status_broadcast;
|
||||
|
||||
bool own_message_queued = false;
|
||||
unsigned char own_message_destination_mac[6];
|
||||
|
@ -101,6 +104,15 @@ unsigned char random_byte(void) {
|
|||
return randomness;
|
||||
}
|
||||
|
||||
time_t monotonic_time(void) {
|
||||
struct timespec now;
|
||||
errno = 0;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
|
||||
err(1, "clock_gettime");
|
||||
}
|
||||
return now.tv_sec;
|
||||
}
|
||||
|
||||
char hexify(int nybble) {
|
||||
assert(0 <= nybble && nybble <= 16);
|
||||
return "0123456789abcdef"[nybble];
|
||||
|
@ -451,8 +463,26 @@ void eventloop(void) {
|
|||
pollfds[1].events = POLLIN;
|
||||
|
||||
while (running) {
|
||||
// Figure out how many ms to wait
|
||||
int wait_ms;
|
||||
time_t now = monotonic_time();
|
||||
if (next_status_broadcast <= now) {
|
||||
// The time has come to send the status broadcast
|
||||
send_status(broadcast_mac);
|
||||
// Do next one in 5 minutes
|
||||
next_status_broadcast = now + 5 * 60;
|
||||
} else {
|
||||
if (INT_MAX / 1000 >= next_status_broadcast - now) {
|
||||
// Wail until next status broadcast is due
|
||||
wait_ms = (next_status_broadcast - now) * 1000;
|
||||
} else {
|
||||
// Would overflow, wait INT_MAX ms
|
||||
wait_ms = INT_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
int ready = poll(pollfds, sizeof(pollfds) / sizeof(*pollfds), -1);
|
||||
int ready = poll(pollfds, sizeof(pollfds) / sizeof(*pollfds), wait_ms);
|
||||
if (ready == -1) {
|
||||
err(1, "poll");
|
||||
}
|
||||
|
@ -545,6 +575,12 @@ int main(int argc, char **argv) {
|
|||
// Initialize the message id cache
|
||||
memset(msgid_cache, 0, sizeof(msgid_cache));
|
||||
|
||||
// Broadcast our status to the network to let them know we're here
|
||||
send_status(broadcast_mac);
|
||||
|
||||
// Schedule next broadcast of our status 5 min in the future
|
||||
next_status_broadcast = monotonic_time() + 5 * 60;
|
||||
|
||||
// Request status from everyone, so that we can get an idea of who is on the network
|
||||
send_status_request(broadcast_mac);
|
||||
|
||||
|
|
Loading…
Reference in a new issue