提交 b17b843b authored 作者: Joao Mesquita's avatar Joao Mesquita

fs_eventsd to make new nice things

上级 01f957e2
PWD=$(shell pwd)
INCS=-I$(PWD)/src/include
LIBEDIT_DIR=../../libs/libedit
DEBUG=-g -ggdb
BASE_FLAGS=$(INCS) -DHAVE_EDITLINE $(DEBUG) -I$(LIBEDIT_DIR)/src/ -fPIC
PICKY=-O2 -ffast-math -Wall -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes
CFLAGS=$(BASE_FLAGS) $(PICKY)
CXXFLAGS=$(BASE_FLAGS) -Wall -Wno-unused-variable
MYLIB=libesl.a
LIBS=-lncurses -lpthread -lesl
LDFLAGS=-L.
OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o
SRC=src/esl.c src/esl_event.c src/esl_threadmutex.c src/esl_config.c src/esl_oop.cpp
HEADERS=src/include/esl_config.h src/include/esl_event.h src/include/esl.h src/include/esl_threadmutex.h src/include/esl_oop.h
SOLINK=-shared -Xlinker -x
# comment the next line to disable c++ (no swig mods for you then)
OBJS += src/esl_oop.o
all: $(MYLIB) fs_cli testclient testserver ivrd eventsd
$(MYLIB): $(OBJS) $(HEADERS) $(SRC)
ar rcs $(MYLIB) $(OBJS)
ranlib $(MYLIB)
testserver: $(MYLIB) testserver.c
$(CC) $(CC_CFLAGS) $(CFLAGS) testserver.c -o testserver $(LDFLAGS) $(LIBS)
ivrd: $(MYLIB) ivrd.c
$(CC) $(CC_CFLAGS) $(CFLAGS) ivrd.c -o ivrd $(LDFLAGS) $(LIBS)
eventsd: $(MYLIB) eventsd.c
$(CC) $(CC_CFLAGS) $(CFLAGS) eventsd.c -o fs_eventsd $(LDFLAGS) $(LIBS)
testclient: $(MYLIB) testclient.c
$(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient $(LDFLAGS) $(LIBS)
fs_cli: $(MYLIB) fs_cli.c
$(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli $(LDFLAGS) -L$(LIBEDIT_DIR)/src/.libs $(LIBS) -ledit
%.o: %.c
$(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
%.o: %.cpp
$(CXX) $(CXX_CFLAGS) $(CXXFLAGS) -c $< -o $@
clean:
rm -f *.o src/*.o testclient testserver ivrd fs_cli libesl.a *~ src/*~ src/include/*~
$(MAKE) -C perl clean
$(MAKE) -C php clean
$(MAKE) -C lua clean
$(MAKE) -C python clean
$(MAKE) -C ruby clean
$(MAKE) -C java clean
$(MAKE) -C managed clean
reswig: swigclean
$(MAKE) -C perl reswig
$(MAKE) -C php reswig
$(MAKE) -C lua reswig
$(MAKE) -C python reswig
$(MAKE) -C ruby reswig
$(MAKE) -C java reswig
$(MAKE) -C managed reswig
swigclean: clean
$(MAKE) -C perl swigclean
$(MAKE) -C php swigclean
$(MAKE) -C lua swigclean
$(MAKE) -C python swigclean
$(MAKE) -C ruby swigclean
$(MAKE) -C java swigclean
$(MAKE) -C managed swigclean
perlmod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C perl
phpmod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C php
luamod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C lua
pymod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C python
tclmod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C tcl
rubymod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C ruby
javamod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C java
managedmod: $(MYLIB)
$(MAKE) MYLIB="../$(MYLIB)" SOLINK="$(SOLINK)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CXX_CFLAGS="$(CXX_CFLAGS)" -C managed
phpmod-install: phpmod
$(MAKE) -C php install
everymod: perlmod phpmod luamod pymod rubymod javamod managedmod
Copy all those files to SRC/libs/esl and hit do make
This will spawn a new process with a script indicated on action tag to every new event received filtered by header and matched by category.
/*
* Copyright (c) 2010, Anthony Minessale II
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* Contributed by João Mesquita <jmesquita@gmail.com>
*
*
* Sponsored by Gabpark
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <esl.h>
#include <getopt.h>
typedef struct {
char e_name[128];
char e_header[128];
char action[128];
} inbound_event_profile_t;
static inbound_event_profile_t profiles[128] = {{{0}}};
static int pcount = 0;
static void mycallback(esl_event_t *event, char * path)
{
int pipefd[2];
int pid;
char * serialized = NULL;
/*make a pipe (fds go in pipefd[0] and pipefd[1])*/
pipe(pipefd);
pid = fork();
if (pid == 0)
{
/* Wire fd to stdin */
dup2(pipefd[0], 0);
// close unused hald of pipe
close(pipefd[1]);
// execute grep
execl(path, path, (char *)NULL);
_exit(EXIT_SUCCESS);
}
else /* Parent */
{
// close unused unput half of pipe
close(pipefd[0]);
esl_event_serialize(event, &serialized, ESL_FALSE);
write(pipefd[1], serialized, strlen(serialized));
close(pipefd[1]);
esl_safe_free(serialized);
}
return;
}
static int usage(char *name){
printf("Usage: %s [-H <host>] [-P <port>] [-p <secret>] [-d <level>] [-c configuration]\n\n", name);
printf(" -?,-h --help Usage Information\n");
printf(" -H, --host=hostname Host to connect\n");
printf(" -P, --port=port Port to connect (1 - 65535)\n");
printf(" -u, --user=user@domain user@domain\n");
printf(" -p, --password=password Password\n");
printf(" -c, --conf=command Config file to use.\n");
printf(" -d, --debug=level Debug Level (0 - 7)\n\n");
return 1;
}
int main(int argc, char *argv[])
{
char host[128] = "127.0.0.1", conf[128] = "/etc/fs_eventsd.conf", pass[128] = "ClueCon";
int port = 8021;
esl_handle_t handle = {{0}};
esl_config_t cfg;
int rv = 0;
/* Vars for optargs */
int opt;
static struct option options[] = {
{"help", 0, 0, 'h'},
{"host", 1, 0, 'H'},
{"port", 1, 0, 'P'},
{"user", 1, 0, 'u'},
{"password", 1, 0, 'p'},
{"debug", 1, 0, 'd'},
{"conf", 1, 0, 'c'},
{0, 0, 0, 0}
};
char temp_host[128];
char temp_conf[128];
int argv_host = 0;
char temp_pass[128];
int argv_pass = 0 ;
int temp_port = 0;
int argv_port = 0;
int temp_log = -1;
int argv_error = 0;
int argv_conf = 0;
esl_global_set_default_logger(6);
for(;;) {
int option_index = 0;
opt = getopt_long(argc, argv, "H:U:P:S:p:d:c:h?", options, &option_index);
if (opt == -1) break;
switch (opt)
{
case 'H':
esl_set_string(temp_host, optarg);
argv_host = 1;
break;
case 'c':
esl_set_string(temp_conf, optarg);
argv_conf = 1;
break;
case 'P':
temp_port= atoi(optarg);
if (temp_port > 0 && temp_port < 65536){
argv_port = 1;
} else {
printf("ERROR: Port must be in range 1 - 65535\n");
argv_error = 1;
}
break;
case 'p':
esl_set_string(temp_pass, optarg);
argv_pass = 1;
break;
case 'd':
temp_log=atoi(optarg);
if (temp_log < 0 || temp_log > 7){
printf("ERROR: Debug level should be 0 - 7.\n");
argv_error = 1;
} else {
esl_global_set_default_logger(temp_log);
}
break;
case 'h':
case '?':
usage(argv[0]);
return 0;
default:
opt = 0;
}
}
if (argv_host){
esl_set_string(host, temp_host);
}
if (argv_pass){
esl_set_string(pass, temp_pass);
}
if (argv_port){
port = temp_port;
}
if (argv_conf){
esl_set_string(conf, temp_conf);
}
if (argv_error){
printf("\n");
return usage(argv[0]);
}
if (!(rv = esl_config_open_file(&cfg, conf)))
{
esl_log(ESL_LOG_ERROR, "No config file, nothing to be done then.\n");
return -1;
}
if (rv)
{
char *var, *val;
char cur_cat[128] = "";
while (esl_config_next_pair(&cfg, &var, &val))
{
if (strcmp(cur_cat, cfg.category))
{
esl_set_string(cur_cat, cfg.category);
esl_set_string(profiles[pcount].e_name, cur_cat);
pcount++;
}
if (!strcasecmp(var, "action"))
{
esl_set_string(profiles[pcount-1].action, val);
}
if (!strcasecmp(var, "header"))
{
esl_set_string(profiles[pcount-1].e_header, val);
}
}
}
esl_log(ESL_LOG_INFO, "Connecting...\n");
if (esl_connect(&handle, host, port, NULL, pass) != ESL_SUCCESS)
{
esl_log(ESL_LOG_ERROR, "Error Connecting [%s]\n", handle.err);
return -1;
}
esl_log(ESL_LOG_INFO, "OK!\n");
esl_events(&handle, ESL_EVENT_TYPE_PLAIN ,"all");
while (handle.connected)
{
if(esl_recv_event(&handle, 1, NULL) == ESL_SUCCESS)
{
int i;
for(i = 0; i < pcount; i++)
{
if (!strcasecmp(profiles[i].e_name,
esl_event_get_header(handle.last_ievent,
profiles[i].e_header)))
{
mycallback(handle.last_ievent,
profiles[i].action);
}
}
}
}
esl_log(ESL_LOG_INFO, "Disconnecting...\n");
esl_disconnect(&handle);
return 0;
}
[HEARTBEAT]
action=./test.py
header=Event-Name
[CHANNEL_CREATE]
action=./test.py
header=Event-Name
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论