2009-02-17 00:27:28 +00:00
|
|
|
#include <esl.h>
|
|
|
|
#include <esl_oop.h>
|
|
|
|
|
2009-02-17 23:18:28 +00:00
|
|
|
#define connection_construct_common() memset(&handle, 0, sizeof(handle)); last_event_obj = NULL
|
2009-03-02 20:52:46 +00:00
|
|
|
#define event_construct_common() event = NULL; serialized_string = NULL; mine = 0; hp = NULL
|
2009-02-17 00:27:28 +00:00
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
void eslSetLogLevel(int level)
|
|
|
|
{
|
|
|
|
esl_global_set_default_logger(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
ESLconnection::ESLconnection(const char *host, const char *port, const char *password)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
2009-02-17 23:18:28 +00:00
|
|
|
connection_construct_common();
|
2009-02-17 00:27:28 +00:00
|
|
|
int x_port = atoi(port);
|
|
|
|
|
|
|
|
esl_connect(&handle, host, x_port, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLconnection::ESLconnection(int socket)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
2009-02-17 23:18:28 +00:00
|
|
|
connection_construct_common();
|
2009-02-17 00:27:28 +00:00
|
|
|
memset(&handle, 0, sizeof(handle));
|
|
|
|
esl_attach_handle(&handle, (esl_socket_t)socket, NULL);
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLconnection::~ESLconnection()
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
if (handle.connected) {
|
|
|
|
esl_disconnect(&handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
int ESLconnection::connected()
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
return handle.connected;
|
|
|
|
}
|
|
|
|
|
2009-02-20 23:03:06 +00:00
|
|
|
int ESLconnection::send(const char *cmd)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
return esl_send(&handle, cmd);
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLevent *ESLconnection::sendRecv(const char *cmd)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
if (esl_send_recv(&handle, cmd) == ESL_SUCCESS) {
|
|
|
|
esl_event_t *event;
|
|
|
|
esl_event_dup(&event, handle.last_sr_event);
|
2009-02-17 20:13:45 +00:00
|
|
|
return new ESLevent(event, 1);
|
2009-02-17 00:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-20 16:52:11 +00:00
|
|
|
ESLevent *ESLconnection::api(const char *cmd, const char *arg)
|
|
|
|
{
|
2009-02-20 17:16:36 +00:00
|
|
|
size_t len;
|
2009-02-20 16:52:11 +00:00
|
|
|
char *cmd_buf;
|
|
|
|
|
2009-02-20 17:16:36 +00:00
|
|
|
if (!cmd) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(cmd) + (arg ? strlen(arg) : 0) + 5;
|
|
|
|
|
2009-02-20 16:52:11 +00:00
|
|
|
cmd_buf = (char *) malloc(len);
|
|
|
|
assert(cmd_buf);
|
|
|
|
|
2009-02-20 17:16:36 +00:00
|
|
|
snprintf(cmd_buf, len, "api %s %s", cmd, arg ? arg : "");
|
2009-03-02 22:19:36 +00:00
|
|
|
*(cmd_buf + (len + 1)) = '\0';
|
|
|
|
|
2009-02-20 16:52:11 +00:00
|
|
|
|
|
|
|
if (esl_send_recv(&handle, cmd_buf) == ESL_SUCCESS) {
|
|
|
|
esl_event_t *event;
|
|
|
|
esl_event_dup(&event, handle.last_sr_event);
|
|
|
|
return new ESLevent(event, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cmd_buf);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESLevent *ESLconnection::bgapi(const char *cmd, const char *arg)
|
|
|
|
{
|
2009-02-20 17:16:36 +00:00
|
|
|
size_t len;
|
2009-02-20 16:52:11 +00:00
|
|
|
char *cmd_buf;
|
|
|
|
|
2009-02-20 17:16:36 +00:00
|
|
|
if (!cmd) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(cmd) + (arg ? strlen(arg) : 0) + 5;
|
|
|
|
|
2009-02-20 16:52:11 +00:00
|
|
|
cmd_buf = (char *) malloc(len);
|
|
|
|
assert(cmd_buf);
|
|
|
|
|
2009-02-20 17:16:36 +00:00
|
|
|
snprintf(cmd_buf, len, "bgapi %s %s", cmd, arg ? arg : "");
|
2009-03-04 15:26:39 +00:00
|
|
|
*(cmd_buf + (len + 1)) = '\0';
|
2009-02-20 16:52:11 +00:00
|
|
|
|
|
|
|
if (esl_send_recv(&handle, cmd_buf) == ESL_SUCCESS) {
|
|
|
|
esl_event_t *event;
|
|
|
|
esl_event_dup(&event, handle.last_sr_event);
|
|
|
|
return new ESLevent(event, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cmd_buf);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLevent *ESLconnection::getInfo()
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
if (handle.connected && handle.info_event) {
|
|
|
|
esl_event_t *event;
|
|
|
|
esl_event_dup(&event, handle.info_event);
|
2009-02-17 20:13:45 +00:00
|
|
|
return new ESLevent(event, 1);
|
2009-02-17 00:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
int ESLconnection::setBlockingExecute(const char *val)
|
2009-02-17 14:13:32 +00:00
|
|
|
{
|
|
|
|
if (val) {
|
|
|
|
handle.blocking_execute = esl_true(val);
|
|
|
|
}
|
|
|
|
return handle.blocking_execute;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
int ESLconnection::setEventLock(const char *val)
|
2009-02-17 14:13:32 +00:00
|
|
|
{
|
|
|
|
if (val) {
|
|
|
|
handle.event_lock = esl_true(val);
|
|
|
|
}
|
|
|
|
return handle.event_lock;
|
|
|
|
}
|
|
|
|
|
2009-02-20 23:03:06 +00:00
|
|
|
int ESLconnection::execute(const char *app, const char *arg, const char *uuid)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
return esl_execute(&handle, app, arg, uuid);
|
|
|
|
}
|
|
|
|
|
2009-02-20 23:03:06 +00:00
|
|
|
int ESLconnection::sendEvent(ESLevent *send_me)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
return esl_sendevent(&handle, send_me->event);
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLevent *ESLconnection::recvEvent()
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
if (last_event_obj) {
|
|
|
|
delete last_event_obj;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
if (esl_recv_event(&handle, NULL) == ESL_SUCCESS) {
|
2009-02-20 16:52:11 +00:00
|
|
|
esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
|
|
|
|
if (e) {
|
2009-02-17 20:13:45 +00:00
|
|
|
esl_event_t *event;
|
2009-02-20 16:52:11 +00:00
|
|
|
esl_event_dup(&event, e);
|
2009-02-17 20:13:45 +00:00
|
|
|
last_event_obj = new ESLevent(event, 1);
|
|
|
|
return last_event_obj;
|
|
|
|
}
|
2009-02-17 00:27:28 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 23:19:58 +00:00
|
|
|
last_event_obj = new ESLevent("server_disconnected");
|
|
|
|
|
|
|
|
return last_event_obj;
|
2009-02-17 00:27:28 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLevent *ESLconnection::recvEventTimed(int ms)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
if (last_event_obj) {
|
|
|
|
delete last_event_obj;
|
|
|
|
last_event_obj = NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
if (esl_recv_event_timed(&handle, ms, NULL) == ESL_SUCCESS) {
|
2009-02-20 16:52:11 +00:00
|
|
|
esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
|
|
|
|
if (e) {
|
2009-02-17 20:13:45 +00:00
|
|
|
esl_event_t *event;
|
2009-02-20 16:52:11 +00:00
|
|
|
esl_event_dup(&event, e);
|
2009-02-17 20:13:45 +00:00
|
|
|
last_event_obj = new ESLevent(event, 1);
|
|
|
|
return last_event_obj;
|
|
|
|
}
|
2009-02-17 00:27:28 +00:00
|
|
|
}
|
|
|
|
|
2009-03-02 23:59:04 +00:00
|
|
|
return NULL;
|
2009-02-17 00:27:28 +00:00
|
|
|
}
|
|
|
|
|
2009-02-20 23:03:06 +00:00
|
|
|
int ESLconnection::filter(const char *header, const char *value)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
return esl_filter(&handle, header, value);
|
|
|
|
}
|
|
|
|
|
2009-02-20 23:03:06 +00:00
|
|
|
int ESLconnection::events(const char *etype, const char *value)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
esl_event_type_t type_id = ESL_EVENT_TYPE_PLAIN;
|
|
|
|
|
|
|
|
if (!strcmp(etype, "xml")) {
|
|
|
|
type_id = ESL_EVENT_TYPE_XML;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esl_events(&handle, type_id, value);
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
// ESLevent
|
2009-02-17 00:27:28 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLevent::ESLevent(const char *type, const char *subclass_name)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
esl_event_types_t event_id;
|
|
|
|
|
2009-02-17 23:18:28 +00:00
|
|
|
event_construct_common();
|
|
|
|
|
2009-02-17 00:27:28 +00:00
|
|
|
if (esl_name_event(type, &event_id) != ESL_SUCCESS) {
|
|
|
|
event_id = ESL_EVENT_MESSAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!esl_strlen_zero(subclass_name) && event_id != ESL_EVENT_CUSTOM) {
|
|
|
|
esl_log(ESL_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
|
|
|
|
event_id = ESL_EVENT_CUSTOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (esl_event_create_subclass(&event, event_id, subclass_name) != ESL_SUCCESS) {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Failed to create event!\n");
|
|
|
|
event = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
serialized_string = NULL;
|
|
|
|
mine = 1;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
ESLevent::ESLevent(esl_event_t *wrap_me, int free_me)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
2009-02-17 23:18:28 +00:00
|
|
|
event_construct_common();
|
2009-02-17 00:27:28 +00:00
|
|
|
event = wrap_me;
|
|
|
|
mine = free_me;
|
|
|
|
serialized_string = NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 23:18:28 +00:00
|
|
|
|
|
|
|
ESLevent::ESLevent(ESLevent *me)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
2009-02-17 23:18:28 +00:00
|
|
|
/* workaround for silly php thing */
|
|
|
|
event = me->event;
|
|
|
|
mine = me->mine;
|
|
|
|
serialized_string = NULL;
|
|
|
|
me->event = NULL;
|
|
|
|
me->mine = 0;
|
|
|
|
esl_safe_free(me->serialized_string);
|
|
|
|
}
|
2009-02-17 00:27:28 +00:00
|
|
|
|
2009-02-17 23:18:28 +00:00
|
|
|
ESLevent::~ESLevent()
|
|
|
|
{
|
|
|
|
|
2009-02-17 00:27:28 +00:00
|
|
|
if (serialized_string) {
|
|
|
|
free(serialized_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event && mine) {
|
|
|
|
esl_event_destroy(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-02 20:52:46 +00:00
|
|
|
const char *ESLevent::nextHeader(void)
|
|
|
|
{
|
|
|
|
const char *name = NULL;
|
|
|
|
|
|
|
|
if (hp) {
|
|
|
|
name = hp->name;
|
|
|
|
hp = hp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ESLevent::firstHeader(void)
|
|
|
|
{
|
|
|
|
if (event) {
|
|
|
|
hp = event->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextHeader();
|
|
|
|
}
|
2009-02-17 00:27:28 +00:00
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
const char *ESLevent::serialize(const char *format)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
int isxml = 0;
|
|
|
|
|
|
|
|
this_check("");
|
|
|
|
|
|
|
|
esl_safe_free(serialized_string);
|
|
|
|
|
|
|
|
if (!event) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (esl_event_serialize(event, &serialized_string, ESL_TRUE) == ESL_SUCCESS) {
|
|
|
|
return serialized_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
bool ESLevent::setPriority(esl_priority_t priority)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
this_check(false);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
esl_event_set_priority(event, priority);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Trying to setPriority an event that does not exist!\n");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
const char *ESLevent::getHeader(char *header_name)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
this_check("");
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
return esl_event_get_header(event, header_name);
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Trying to getHeader an event that does not exist!\n");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
bool ESLevent::addHeader(const char *header_name, const char *value)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
this_check(false);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
return esl_event_add_header_string(event, ESL_STACK_BOTTOM, header_name, value) == ESL_SUCCESS ? true : false;
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
bool ESLevent::delHeader(const char *header_name)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
this_check(false);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
return esl_event_del_header(event, header_name) == ESL_SUCCESS ? true : false;
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Trying to delHeader an event that does not exist!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
bool ESLevent::addBody(const char *value)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
this_check(false);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
return esl_event_add_body(event, "%s", value) == ESL_SUCCESS ? true : false;
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Trying to addBody an event that does not exist!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
char *ESLevent::getBody(void)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
this_check((char *)"");
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
return esl_event_get_body(event);
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Trying to getBody an event that does not exist!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:13:45 +00:00
|
|
|
const char *ESLevent::getType(void)
|
2009-02-17 00:27:28 +00:00
|
|
|
{
|
|
|
|
this_check("");
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
return esl_event_name(event->event_id);
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Trying to getType an event that does not exist!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char *) "invalid";
|
|
|
|
}
|