Browse Source

rudimentary redis handling and some die message fixes

git.mgm/mediaproxy-ng/2.0
Richard Fuchs 15 years ago
parent
commit
cffb385bcf
5 changed files with 106 additions and 4 deletions
  1. +1
    -1
      daemon/Makefile
  2. +2
    -0
      daemon/call.h
  3. +24
    -3
      daemon/main.c
  4. +49
    -0
      daemon/redis.c
  5. +30
    -0
      daemon/redis.h

+ 1
- 1
daemon/Makefile View File

@ -9,7 +9,7 @@ CFLAGS+= -I$(LIBREDISDIR)
LDFLAGS= `pkg-config --libs glib-2.0` `pcre-config --libs`
LDFLAGS+= -L$(LIBREDISDIR) -lhiredis
SRCS= main.c kernel.c poller.c aux.c control.c streambuf.c call.c control_udp.c
SRCS= main.c kernel.c poller.c aux.c control.c streambuf.c call.c control_udp.c redis.c
OBJS= $(SRCS:.c=.o)


+ 2
- 0
daemon/call.h View File

@ -18,6 +18,7 @@ struct peer;
struct callstream;
struct call;
struct callmaster;
struct redis;
@ -72,6 +73,7 @@ struct callmaster {
struct mediaproxy_stats stats;
struct poller *poller;
struct redis *redis;
int kernelfd;
unsigned int kernelid;
u_int32_t ip;


+ 24
- 3
daemon/main.c View File

@ -14,6 +14,7 @@
#include "log.h"
#include "call.h"
#include "kernel.h"
#include "redis.h"
@ -38,6 +39,9 @@ static int timeout;
static int silent_timeout;
static int port_min;
static int port_max;
static u_int32_t redis_ip;
static u_int16_t redis_port;
static char *redis_key;
@ -104,6 +108,8 @@ static void options(int *argc, char ***argv) {
static char *adv_ips;
static char *listenps;
static char *listenudps;
static char *redisps;
static GOptionEntry e[] = {
{ "table", 't', 0, G_OPTION_ARG_INT, &table, "Kernel table to use", "INT" },
{ "ip", 'i', 0, G_OPTION_ARG_STRING, &ips, "Local IP address for RTP", "IP" },
@ -117,6 +123,8 @@ static void options(int *argc, char ***argv) {
{ "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Don't fork to background", NULL },
{ "port-min", 'm', 0, G_OPTION_ARG_INT, &port_min, "Lowest port to use for RTP", "INT" },
{ "port-max", 'M', 0, G_OPTION_ARG_INT, &port_max, "Highest port to use for RTP", "INT" },
{ "redis", 'r', 0, G_OPTION_ARG_STRING, &redisps, "Connect to Redis database", "IP:PORT" },
{ "redis-key", 'R', 0, G_OPTION_ARG_STRING, &redis_key, "Namespace key for Redis", "STRING" },
{ NULL, }
};
@ -145,20 +153,27 @@ static void options(int *argc, char ***argv) {
if (listenps) {
if (parse_ip_port(&listenp, &listenport, listenps))
die("Invalid IP or port (--listen)");
die("Invalid IP or port (--listen)\n");
}
if (listenudps) {
if (parse_ip_port(&udp_listenp, &udp_listenport, listenudps))
die("Invalid IP or port (--listen-udp)");
die("Invalid IP or port (--listen-udp)\n");
}
if (tos < 0 || tos > 255)
die("Invalid TOS value");
die("Invalid TOS value\n");
if (timeout <= 0)
timeout = 60;
if (silent_timeout <= 0)
silent_timeout = 3600;
if (redisps) {
if (parse_ip_port(&redis_ip, &redis_port, redisps) || !redis_ip)
die("Invalid IP or port (--redis)\n");
if (!redis_key)
die("Must specify Redis namespace key (--redis-key) when using Redis\n");
}
}
@ -236,6 +251,12 @@ int main(int argc, char **argv) {
die("Failed to open UDP control connection port\n");
}
if (redis_ip) {
m->redis = redis_new(redis_ip, redis_port, redis_key);
if (!m->redis)
die("Failed to connect to Redis database\n");
}
mylog(LOG_INFO, "Startup complete");
if (!foreground)


+ 49
- 0
daemon/redis.c View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <hiredis.h>
#include <sys/types.h>
#include <sys/time.h>
#include "redis.h"
#include "aux.h"
#include "log.h"
struct redis *redis_new(u_int32_t ip, u_int16_t port, char *key) {
struct redis *r;
struct timeval tv;
redisReply *rp;
r = malloc(sizeof(*r));
ZERO(*r);
sprintf(r->host, IPF, IPP(ip));
r->port = port;
r->key = key;
tv.tv_sec = 1;
tv.tv_usec = 0;
r->ctx = redisConnectWithTimeout(r->host, r->port, tv);
if (!r->ctx)
goto err;
if (r->ctx->err)
goto err;
rp = redisCommand(r->ctx, "PING");
if (!rp)
goto err;
freeReplyObject(rp);
return r;
err:
if (r->ctx && r->ctx->errstr)
mylog(LOG_CRIT, "Redis error: %s\n", r->ctx->errstr);
if (r->ctx)
redisFree(r->ctx);
free(r);
return NULL;
}

+ 30
- 0
daemon/redis.h View File

@ -0,0 +1,30 @@
#ifndef __REDIS_H__
#define __REDIS_H__
#include <sys/types.h>
#include <hiredis.h>
struct redis {
char host[32];
int port;
redisContext *ctx;
char *key;
int active:1;
};
struct redis *redis_new(u_int32_t, u_int16_t, char *);
#endif

Loading…
Cancel
Save