new: use the new serve callbacks

This commit is contained in:
ngn
2024-07-18 19:33:12 +03:00
parent ffd6449986
commit 8332d9f941
2 changed files with 84 additions and 22 deletions

View File

@ -22,10 +22,9 @@
// clang-format on
#include <arpa/inet.h>
#include <errno.h>
#include <libmp/all.h>
#include <libmp/ctx.h>
#include <libmp/error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -35,6 +34,23 @@
#include "intl.h"
#include "log.h"
void sockaddr_to_str(struct sockaddr *addr, char *str) {
struct sockaddr_in *ipv4;
struct sockaddr_in6 *ipv6;
switch (addr->sa_family) {
case AF_INET:
ipv4 = (struct sockaddr_in *)addr;
inet_ntop(AF_INET, &ipv4->sin_addr, str, INET_ADDRSTRLEN);
break;
case AF_INET6:
ipv6 = (struct sockaddr_in6 *)addr;
inet_ntop(AF_INET6, &ipv6->sin6_addr, str, INET6_ADDRSTRLEN);
break;
}
}
bool sync_callback(
lm_ctx_t *ctx, lm_pool_t *pool, lm_ctx_sync_state_t state, size_t current, size_t total, void *data) {
switch (state) {
@ -62,6 +78,32 @@ bool sync_callback(
return true;
}
bool serve_callback(lm_pool_t *pool, lm_mptp_t *packet, struct sockaddr *addr, void *data) {
char ipaddr[INET6_ADDRSTRLEN];
bzero(ipaddr, sizeof(ipaddr));
sockaddr_to_str(addr, ipaddr);
switch (MPTP_FLAGS_CODE(packet)) {
case MPTP_C2S_PING:
info(_("Request from %s: PING (%s)"), ipaddr, pool->name);
break;
case MPTP_C2S_INFO:
info(_("Request from %s: INFO (%s)"), ipaddr, pool->name);
break;
case MPTP_C2S_LIST:
info(_("Request from %s: LIST (%s)"), ipaddr, pool->name);
break;
case MPTP_C2S_PULL:
info(_("Request from %s: PULL (%s)"), ipaddr, pool->name);
break;
}
return true;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
error(_("Configuration file not specified"));
@ -148,7 +190,7 @@ int main(int argc, char *argv[]) {
info(pool_count == 1 ? _("Serving %lu pool on %s") : _("Serving %lu pools on %s"), pool_count, addr);
if (!lm_ctx_serve(&ctx, addr, config_get_integer("threads"))) {
if (!lm_ctx_serve(&ctx, addr, config_get_integer("threads"), serve_callback, NULL)) {
error(_("Failed to start the server: %s"), lm_strerror());
goto end;
}