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:
Anthony Minessale 2009-02-17 00:27:28 +00:00
parent 2980d25a14
commit 10ce6f6bbd
13 changed files with 4074 additions and 11 deletions

8
libs/esl/ESL.i Normal file
View File

@ -0,0 +1,8 @@
%{
#include "esl.h"
#include "esl_oop.h"
%}
%include esl_oop.h

View File

@ -1,14 +1,24 @@
INCS=-Isrc/include
LIBEDIT_DIR=../../libs/libedit
DEBUG=-g -ggdb
BASE_FLAGS=$(INCS) -DHAVE_EDITLINE $(DEBUG) -I$(LIBEDIT_DIR)/src/ -fPIC
PICKY=-O2 -ffast-math -Wall -Werror -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes
CFLAGS=$(INCS) -DHAVE_EDITLINE $(DEBUG) -I$(LIBEDIT_DIR)/src/ $(PICKY)
CFLAGS=$(BASE_FLAGS) $(PICKY)
CXXFLAGS=$(BASE_FLAGS)
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
HEADERS=src/include/esl_config.h src/include/esl_event.h src/include/esl.h src/include/esl_threadmutex.h
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
PERL = $(shell which perl)
PERL_LIBDIR =-L$(shell perl -MConfig -e 'print $$Config{archlib}')/CORE
PERL_LIBS =$(shell perl -MConfig -e 'print $$Config{libs}')
LOCAL_CFLAGS= -w -DMULTIPLICITY $(shell $(PERL) -MExtUtils::Embed -e ccopts) -DEMBED_PERL
LOCAL_LDFLAGS=$(shell $(PERL) -MExtUtils::Embed -e ldopts) $(shell $(PERL) -MConfig -e 'print $$Config{libs}')
# comment the next line to disable c++ (no swig mods for you then)
OBJS += src/esl_oop.o
all: $(MYLIB) fs_cli testclient testserver
@ -28,6 +38,33 @@ fs_cli: $(MYLIB) fs_cli.c
%.o: %.c
$(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o src/*.o testclient testserver fs_cli libesl.a *~ src/*~ src/include/*~
%.o: %.cpp
$(CXX) $(CXX_CFLAGS) $(CXXFLAGS) -c $< -o $@
clean:
rm -f *.o src/*.o testclient testserver fs_cli libesl.a *~ src/*~ src/include/*~ perl/*.o perl/*.so
reswig: swigclean perl/esl_wrap.cpp perl/perlxsi.c
swigclean: clean
rm -f perl/esl_wrap.* perl/ESL.so perl/ESL.pm perl/perlxsi.*
perl/esl_wrap.cpp:
swig -module ESL -shadow -perl5 -c++ -DMULTIPLICITY -Isrc/include -o perl/esl_wrap.cpp ESL.i
perl/perlxsi.c:
$(PERL) -MExtUtils::Embed -e xsinit -- -o perl/perlxsi.c
perl/perlxsi.o: perl/perlxsi.c
$(CC) $(CC_CFLAGS) $(CFLAGS) $(LOCAL_CFLAGS) -c perl/perlxsi.c -o perl/perlxsi.o
perl/esl_wrap.o: perl/esl_wrap.cpp
$(CXX) $(CXX_CFLAGS) $(CXXFLAGS) $(LOCAL_CFLAGS) -c perl/esl_wrap.cpp -o perl/esl_wrap.o
perl/ESL.so: $(MYLIB) perl/esl_wrap.o perl/perlxsi.o
$(CXX) $(SOLINK) perl/esl_wrap.o perl/perlxsi.o $(LOCAL_LDFLAGS) -o perl/ESL.so -L. $(LIBS)
perlmod: perl/ESL.so

155
libs/esl/perl/ESL.pm Normal file
View File

@ -0,0 +1,155 @@
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 1.3.35
#
# Don't modify this file, modify the SWIG interface instead.
package ESL;
require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
package ESLc;
bootstrap ESL;
package ESL;
@EXPORT = qw( );
# ---------- BASE METHODS -------------
package ESL;
sub TIEHASH {
my ($classname,$obj) = @_;
return bless $obj, $classname;
}
sub CLEAR { }
sub FIRSTKEY { }
sub NEXTKEY { }
sub FETCH {
my ($self,$field) = @_;
my $member_func = "swig_${field}_get";
$self->$member_func();
}
sub STORE {
my ($self,$field,$newval) = @_;
my $member_func = "swig_${field}_set";
$self->$member_func($newval);
}
sub this {
my $ptr = shift;
return tied(%$ptr);
}
# ------- FUNCTION WRAPPERS --------
package ESL;
############# Class : ESL::eslEvent ##############
package ESL::eslEvent;
use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS);
@ISA = qw( ESL );
%OWNER = ();
%ITERATORS = ();
*swig_event_get = *ESLc::eslEvent_event_get;
*swig_event_set = *ESLc::eslEvent_event_set;
*swig_serialized_string_get = *ESLc::eslEvent_serialized_string_get;
*swig_serialized_string_set = *ESLc::eslEvent_serialized_string_set;
*swig_mine_get = *ESLc::eslEvent_mine_get;
*swig_mine_set = *ESLc::eslEvent_mine_set;
sub new {
my $pkg = shift;
my $self = ESLc::new_eslEvent(@_);
bless $self, $pkg if defined($self);
}
sub DESTROY {
return unless $_[0]->isa('HASH');
my $self = tied(%{$_[0]});
return unless defined $self;
delete $ITERATORS{$self};
if (exists $OWNER{$self}) {
ESLc::delete_eslEvent($self);
delete $OWNER{$self};
}
}
*serialize = *ESLc::eslEvent_serialize;
*setPriority = *ESLc::eslEvent_setPriority;
*getHeader = *ESLc::eslEvent_getHeader;
*getBody = *ESLc::eslEvent_getBody;
*getType = *ESLc::eslEvent_getType;
*addBody = *ESLc::eslEvent_addBody;
*addHeader = *ESLc::eslEvent_addHeader;
*delHeader = *ESLc::eslEvent_delHeader;
sub DISOWN {
my $self = shift;
my $ptr = tied(%$self);
delete $OWNER{$ptr};
}
sub ACQUIRE {
my $self = shift;
my $ptr = tied(%$self);
$OWNER{$ptr} = 1;
}
############# Class : ESL::eslConnection ##############
package ESL::eslConnection;
use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS);
@ISA = qw( ESL );
%OWNER = ();
%ITERATORS = ();
sub new {
my $pkg = shift;
my $self = ESLc::new_eslConnection(@_);
bless $self, $pkg if defined($self);
}
sub DESTROY {
return unless $_[0]->isa('HASH');
my $self = tied(%{$_[0]});
return unless defined $self;
delete $ITERATORS{$self};
if (exists $OWNER{$self}) {
ESLc::delete_eslConnection($self);
delete $OWNER{$self};
}
}
*connected = *ESLc::eslConnection_connected;
*getInfo = *ESLc::eslConnection_getInfo;
*send = *ESLc::eslConnection_send;
*sendRecv = *ESLc::eslConnection_sendRecv;
*sendEvent = *ESLc::eslConnection_sendEvent;
*recvEvent = *ESLc::eslConnection_recvEvent;
*recvEventTimed = *ESLc::eslConnection_recvEventTimed;
*filter = *ESLc::eslConnection_filter;
*events = *ESLc::eslConnection_events;
*execute = *ESLc::eslConnection_execute;
sub DISOWN {
my $self = shift;
my $ptr = tied(%$self);
delete $OWNER{$ptr};
}
sub ACQUIRE {
my $self = shift;
my $ptr = tied(%$self);
$OWNER{$ptr} = 1;
}
# ------- VARIABLE STUBS --------
package ESL;
1;

3416
libs/esl/perl/esl_wrap.cpp Normal file

File diff suppressed because it is too large Load Diff

17
libs/esl/perl/events.pl Normal file
View File

@ -0,0 +1,17 @@
require ESL;
my $con = new ESL::eslConnection("localhost", "8021", "ClueCon");
$con->events("plain", "all");
for(;;) {
#my $e = $con->recvEventTimed(100);
my $e = $con->recvEvent();
if ($e) {
print $e->serialize();
}
}

16
libs/esl/perl/perlxsi.c Normal file
View File

@ -0,0 +1,16 @@
#include <EXTERN.h>
#include <perl.h>
EXTERN_C void xs_init (pTHX);
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void
xs_init(pTHX)
{
char *file = __FILE__;
dXSUB_SYS;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}

32
libs/esl/perl/server.pl Normal file
View File

@ -0,0 +1,32 @@
require ESL;
use IO::Socket::INET;
my $ip = "127.0.0.1";
my $sock = new IO::Socket::INET ( LocalHost => $ip, LocalPort => '8040', Proto => 'tcp', Listen => 1, Reuse => 1 );
die "Could not create socket: $!\n" unless $sock;
for(;;) {
my $new_sock = $sock->accept();
my $pid = fork();
if ($pid) {
close($new_sock);
next;
}
my $host = $new_sock->sockhost();
my $fd = fileno($new_sock);
my $con = new ESL::eslConnection($fd);
my $info = $con->getInfo();
print $info->serialize();
my $uuid = $info->getHeader("unique-id");
$con->execute("answer", "", $uuid);
$con->execute("playback", "/ram/swimp.raw", $uuid);
close($new_sock);
}

View File

@ -0,0 +1,6 @@
require ESL;
my $command = join(" ", @ARGV);
my $con = new ESL::eslConnection("localhost", "8021", "ClueCon");
my $e = $con->sendRecv("api $command");
print $e->getBody();

View File

@ -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:

277
libs/esl/src/esl_oop.cpp Normal file
View File

@ -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";
}

View File

@ -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);

View File

@ -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

View File

@ -2,7 +2,7 @@
#include <stdlib.h>
#include <esl.h>
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in addr)
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr)
{
esl_handle_t handle = {{0}};
int done = 0;