mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-14 13:28:31 +00:00
add c++ wrapper for esl and a perl module using swig (might need more work)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12074 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
1 parent
2980d25a14
commit
10ce6f6bbd
13 files changed
+4073
-10
No files matched your search
+5
-3
@@ -390,10 +390,12 @@ static void sock_setup(esl_handle_t *handle)
|
||||
#endif
|
||||
}
|
||||
|
||||
ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in addr)
|
||||
ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in *addr)
|
||||
{
|
||||
handle->sock = socket;
|
||||
handle->addr = addr;
|
||||
if (addr) {
|
||||
handle->addr = *addr;
|
||||
}
|
||||
|
||||
if (handle->sock == ESL_SOCK_INVALID) {
|
||||
return ESL_FAIL;
|
||||
@@ -558,7 +560,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
|
||||
goto end;
|
||||
}
|
||||
|
||||
callback(server_sock, client_sock, echoClntAddr);
|
||||
callback(server_sock, client_sock, &echoClntAddr);
|
||||
}
|
||||
|
||||
end:
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
#include <esl.h>
|
||||
#include <esl_oop.h>
|
||||
|
||||
#define construct_common() memset(&handle, 0, sizeof(handle)); last_event_obj = NULL; last_event = NULL;
|
||||
|
||||
eslConnection::eslConnection(const char *host, const char *port, const char *password)
|
||||
{
|
||||
construct_common();
|
||||
int x_port = atoi(port);
|
||||
|
||||
esl_connect(&handle, host, x_port, password);
|
||||
}
|
||||
|
||||
|
||||
eslConnection::eslConnection(int socket)
|
||||
{
|
||||
construct_common();
|
||||
memset(&handle, 0, sizeof(handle));
|
||||
esl_attach_handle(&handle, (esl_socket_t)socket, NULL);
|
||||
}
|
||||
|
||||
eslConnection::~eslConnection()
|
||||
{
|
||||
if (handle.connected) {
|
||||
esl_disconnect(&handle);
|
||||
}
|
||||
|
||||
esl_event_safe_destroy(&last_event);
|
||||
}
|
||||
|
||||
int eslConnection::connected()
|
||||
{
|
||||
return handle.connected;
|
||||
}
|
||||
|
||||
esl_status_t eslConnection::send(const char *cmd)
|
||||
{
|
||||
return esl_send(&handle, cmd);
|
||||
}
|
||||
|
||||
eslEvent *eslConnection::sendRecv(const char *cmd)
|
||||
{
|
||||
if (esl_send_recv(&handle, cmd) == ESL_SUCCESS) {
|
||||
esl_event_t *event;
|
||||
esl_event_dup(&event, handle.last_sr_event);
|
||||
return new eslEvent(event, 1);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
eslEvent *eslConnection::getInfo()
|
||||
{
|
||||
if (handle.connected && handle.info_event) {
|
||||
esl_event_t *event;
|
||||
esl_event_dup(&event, handle.info_event);
|
||||
return new eslEvent(event, 1);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esl_status_t eslConnection::execute(const char *app, const char *arg, const char *uuid)
|
||||
{
|
||||
return esl_execute(&handle, app, arg, uuid);
|
||||
}
|
||||
|
||||
esl_status_t eslConnection::sendEvent(eslEvent *send_me)
|
||||
{
|
||||
return esl_sendevent(&handle, send_me->event);
|
||||
}
|
||||
|
||||
eslEvent *eslConnection::recvEvent()
|
||||
{
|
||||
if (last_event_obj) {
|
||||
delete last_event_obj;
|
||||
}
|
||||
|
||||
if (esl_recv_event(&handle, &last_event) == ESL_SUCCESS) {
|
||||
esl_event_t *event;
|
||||
esl_event_dup(&event, last_event);
|
||||
last_event_obj = new eslEvent(event, 1);
|
||||
return last_event_obj;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
eslEvent *eslConnection::recvEventTimed(int ms)
|
||||
{
|
||||
if (last_event_obj) {
|
||||
delete last_event_obj;
|
||||
last_event_obj = NULL;
|
||||
}
|
||||
|
||||
if (esl_recv_event_timed(&handle, ms, &last_event) == ESL_SUCCESS) {
|
||||
esl_event_t *event;
|
||||
esl_event_dup(&event, last_event);
|
||||
last_event_obj = new eslEvent(event, 1);
|
||||
return last_event_obj;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esl_status_t eslConnection::filter(const char *header, const char *value)
|
||||
{
|
||||
return esl_filter(&handle, header, value);
|
||||
}
|
||||
|
||||
esl_status_t eslConnection::events(const char *etype, const char *value)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
// eslEvent
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
eslEvent::eslEvent(const char *type, const char *subclass_name)
|
||||
{
|
||||
esl_event_types_t event_id;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
eslEvent::eslEvent(esl_event_t *wrap_me, int free_me)
|
||||
{
|
||||
event = wrap_me;
|
||||
mine = free_me;
|
||||
serialized_string = NULL;
|
||||
}
|
||||
|
||||
eslEvent::~eslEvent()
|
||||
{
|
||||
|
||||
if (serialized_string) {
|
||||
free(serialized_string);
|
||||
}
|
||||
|
||||
if (event && mine) {
|
||||
esl_event_destroy(&event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char *eslEvent::serialize(const char *format)
|
||||
{
|
||||
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 "";
|
||||
|
||||
}
|
||||
|
||||
bool eslEvent::setPriority(esl_priority_t priority)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
const char *eslEvent::getHeader(char *header_name)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
bool eslEvent::addHeader(const char *header_name, const char *value)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
bool eslEvent::delHeader(const char *header_name)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
bool eslEvent::addBody(const char *value)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
char *eslEvent::getBody(void)
|
||||
{
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const char *eslEvent::getType(void)
|
||||
{
|
||||
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";
|
||||
}
|
||||
@@ -321,9 +321,9 @@ ESL_DECLARE(int) esl_tolower(int c);
|
||||
ESL_DECLARE(int) esl_snprintf(char *buffer, size_t count, const char *fmt, ...);
|
||||
|
||||
|
||||
typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in addr);
|
||||
typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr);
|
||||
|
||||
ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in addr);
|
||||
ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in *addr);
|
||||
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback);
|
||||
ESL_DECLARE(esl_status_t) esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid);
|
||||
ESL_DECLARE(esl_status_t) esl_sendevent(esl_handle_t *handle, esl_event_t *event);
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 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.
|
||||
*/
|
||||
|
||||
#define EXTERN_C extern "C" {
|
||||
#ifndef _ESL_OOP_H_
|
||||
#define _ESL_OOP_H_
|
||||
#include <esl.h>
|
||||
#ifdef __cplusplus
|
||||
EXTERN_C
|
||||
#endif
|
||||
|
||||
#define this_check(x) do { if (!this) { esl_log(ESL_LOG_ERROR, "object is not initalized\n"); return x;}} while(0)
|
||||
#define this_check_void() do { if (!this) { esl_log(ESL_LOG_ERROR, "object is not initalized\n"); return;}} while(0)
|
||||
|
||||
|
||||
class eslEvent {
|
||||
protected:
|
||||
public:
|
||||
esl_event_t *event;
|
||||
char *serialized_string;
|
||||
int mine;
|
||||
|
||||
eslEvent(const char *type, const char *subclass_name = NULL);
|
||||
eslEvent(esl_event_t *wrap_me, int free_me = 0);
|
||||
virtual ~eslEvent();
|
||||
const char *serialize(const char *format = NULL);
|
||||
bool setPriority(esl_priority_t priority = ESL_PRIORITY_NORMAL);
|
||||
const char *getHeader(char *header_name);
|
||||
char *getBody(void);
|
||||
const char *getType(void);
|
||||
bool addBody(const char *value);
|
||||
bool addHeader(const char *header_name, const char *value);
|
||||
bool delHeader(const char *header_name);
|
||||
};
|
||||
|
||||
|
||||
|
||||
class eslConnection {
|
||||
private:
|
||||
esl_handle_t handle;
|
||||
esl_event_t *last_event;
|
||||
eslEvent *last_event_obj;
|
||||
public:
|
||||
eslConnection(const char *host, const char *port, const char *password);
|
||||
eslConnection(int socket);
|
||||
virtual ~eslConnection();
|
||||
int connected();
|
||||
eslEvent *getInfo();
|
||||
esl_status_t send(const char *cmd);
|
||||
eslEvent *sendRecv(const char *cmd);
|
||||
esl_status_t sendEvent(eslEvent *send_me);
|
||||
eslEvent *recvEvent();
|
||||
eslEvent *recvEventTimed(int ms);
|
||||
esl_status_t filter(const char *header, const char *value);
|
||||
esl_status_t events(const char *etype, const char *value);
|
||||
esl_status_t execute(const char *app, const char *arg, const char *uuid);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user