Skip to content

Commit

Permalink
freeswitch: Add box referencing from multiple modules
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Jan 31, 2017
1 parent b1f3dde commit 37109ce
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 14 deletions.
118 changes: 115 additions & 3 deletions modules/freeswitch/fs_api.c
Expand Up @@ -25,17 +25,129 @@
*/

#include "../../parser/parse_uri.h"
#include "../../proxy.h"
#include "../../resolve.h"
#include "../../forward.h"
#include "../../ut.h"

#include "fs_api.h"

add_fs_evs_f add_fs_event_sock(str *evs_str, char *tag, enum fs_evs_types typ,
struct list_head *fs_boxes;

typedef struct _fs_mod_ref {
str tag;
struct list_head list;
} fs_mod_ref;

static fs_mod_ref *mk_fs_mod_ref(str *tag);
static void free_fs_mod_ref(fs_mod_ref *mod_tag);
static fs_evs *find_fs_evs(union sockaddr_union *sock);

static fs_mod_ref *mk_fs_mod_ref(str *tag)
{
fs_mod_ref *fs_tag = NULL;

fs_tag = shm_malloc(sizeof *fs_tag + tag->len);
if (!fs_tag) {
LM_ERR("out of mem\n");
return NULL;
}

fs_tag->tag.s = (char *)(fs_tag + 1);
fs_tag->tag.len = tag->len;
memcpy(fs_tag->tag.s, tag->s, tag->len);

return fs_tag;
}

static void free_fs_mod_ref(fs_mod_ref *mod_tag)
{
mod_tag->tag.s = NULL;
shm_free(mod_tag);
}

static int has_tag(fs_evs *evs, str *tag)
{
struct list_head *ele;
fs_mod_ref *mtag;

list_for_each(ele, &evs->modlist) {
mtag = list_entry(ele, fs_mod_ref, list);

if (str_strcmp(&mtag->tag, tag) == 0) {
return 0;
}
}

return -1;
}

static fs_evs *find_fs_evs(union sockaddr_union *sock)
{
struct list_head *ele;
fs_evs *evs;

list_for_each(ele, fs_boxes) {
evs = list_entry(ele, fs_evs, list);

if (su_cmp(sock, &evs->su) == 1) {
return evs;
}
}

return NULL;
}

fs_evs *add_fs_event_sock(str *evs_str, str *tag, enum fs_evs_types type,
ev_hrbeat_cb_f scb, void *info)
{
fs_evs *evs;
fs_mod_ref *mtag;
union sockaddr_union su;

if (!evs_str->s || evs_str->len == 0 || !tag) {
LM_ERR("bad params: '%.*s', %.*s\n", evs_str->len, evs_str->s,
tag->len, tag->s);
return NULL;
}

if (resolve_hostport(evs_str, FS_DEFAULT_EVS_PORT, &su) != 0) {
LM_ERR("bad ip[:port] string! (%.*s)\n", evs_str->len, evs_str->s);
return NULL;
}

evs = find_fs_evs(&su);
if (evs) {
if (!has_tag(evs, tag)) {
mtag = mk_fs_mod_ref(tag);
if (!mtag) {
LM_ERR("mk tag failed\n");
return NULL;
}

list_add(&mtag->list, &evs->modlist);
}
} else {
evs = shm_malloc(sizeof *evs);
if (!evs) {
LM_ERR("out of mem\n");
return NULL;
}
memset(evs, 0, sizeof *evs);

evs->type = type;
evs->su = su;

list_add(&evs->list, fs_boxes);
}

return evs;

out_free:
shm_free(evs);
return NULL;
}

del_fs_evs_f del_fs_event_sock(fs_evs *evs, char *tag)
int del_fs_event_sock(fs_evs *evs, str *tag)
{
return 0;
}
26 changes: 15 additions & 11 deletions modules/freeswitch/fs_api.h
Expand Up @@ -28,18 +28,20 @@
#define __FREESWITCH_API__

#include "../../lib/list.h"
#include "../../ip_addr.h"

#define FS_DEFAULT_EVS_PORT 8021

enum fs_evs_types {
FS_GW_STAT,
FS_GW_STATS,
};

typedef struct _fs_evs {
enum fs_evs_types typ;
struct ip_addr ip;
unsigned short int port;
enum fs_evs_types type;
union sockaddr_union su;

struct list_head list;
struct list_head taglist;
struct list_head list; /* distinct FS boxes */
struct list_head modlist; /* distinct module references to the same box */
} fs_evs;

/* statistics contained within a FreeSWITCH "HEARTBEAT" event */
Expand All @@ -51,16 +53,16 @@ typedef struct _fs_ev_hrbeat {

typedef struct _fs_api_t fs_api_t;
/* host[:port] (8021) struct/lock/etc. */
typedef int (*ev_hrbeat_cb_f) (fs_evs *evs, char *tag, fs_ev_hrbeat *hb, void *info);
typedef fs_evs* (*add_fs_evs_f) (str *evs_str, char *tag, enum fs_evs_types typ,
typedef int (*ev_hrbeat_cb_f) (fs_evs *evs, str *tag, fs_ev_hrbeat *hb, void *info);
typedef fs_evs* (*add_fs_evs_f) (str *evs_str, str *tag, enum fs_evs_types type,
ev_hrbeat_cb_f scb, void *info);

typedef int (*del_fs_evs_f) (fs_evs *evs, char *tag);
typedef int (*del_fs_evs_f) (fs_evs *evs, str *tag);

// XXX remove after dev
add_fs_evs_f add_fs_event_sock(str *evs_str, char *tag, enum fs_evs_types typ,
fs_evs *add_fs_event_sock(str *evs_str, str *tag, enum fs_evs_types type,
ev_hrbeat_cb_f scb, void *info);
del_fs_evs_f del_fs_event_sock(fs_evs *evs, char *tag);
int del_fs_event_sock(fs_evs *evs, str *tag);

struct _fs_api_t {
/*
Expand All @@ -80,6 +82,8 @@ struct _fs_api_t {

};

extern struct list_head *fs_boxes;

int fs_bind(fs_api_t *fapi);

#endif /* __FREESWITCH_API__ */

0 comments on commit 37109ce

Please sign in to comment.