Skip to content
Snippets Groups Projects
Commit 224152f7 authored by Olivier Dion's avatar Olivier Dion Committed by Adrien Béraud
Browse files

libguile-jami: Expect user to call jami:init and jami:fini

Exit hooks are only called when in a REPL.  Thus, provide the user with bindings
for initialization and finalization of the daemon.  The syntax `with-jami' can be
used to ensure that finalization happens if initialization succeed.

Change-Id: I2f84a76844866a1de06300cde26618c8576045e5
parent 6fb7210f
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ lib_LTLIBRARIES = libguile-jami.la
libguile_jami_la_SOURCES = \
src/main.cpp \
src/utils.h \
src/bindings/jami.h \
src/bindings/bindings.cpp \
src/bindings/bindings.h \
src/bindings/account.h \
......
(define-module (jami)
#:use-module (system foreign-library))
#:use-module (system foreign-library)
#:export (init
initialized
fini
logging
platform
start
version
JAMI_FLAG_DEBUG
JAMI_FLAG_CONSOLE_LOG
JAMI_FLAG_AUTOANSWER
JAMI_FLAG_IOS_EXTENSION)
#:export-syntax (with-jami))
;;; Call DRing::init so that every other bindings work.
;;;
;;; Since Dring::fini is not safe in atexit callback, we also register a exit
;;; hook for finalizing Jami with DRing::fini.
(let* ((libjami (load-foreign-library "libguile-jami"))
(init (foreign-library-function libjami "init"))
(fini (foreign-library-function libjami "fini")))
(init)
(add-hook! exit-hook fini))
(bootstrap (foreign-library-function libjami "bootstrap")))
(bootstrap))
(define-syntax with-jami
(syntax-rules ()
((_ config-file (init-flags ...) thunk)
(dynamic-wind
(lambda ()
(init (logior init-flags ...))
(start config-file))
thunk
fini))
((_ (init-flags ...) thunk)
(with-jami "" (init-flags ...) thunk))
((_ thunk)
(with-jami "" (0) thunk))))
(define JAMI_FLAG_DEBUG (ash 1 0))
(define JAMI_FLAG_CONSOLE_LOG (ash 1 1))
(define JAMI_FLAG_AUTOANSWER (ash 1 2))
(define JAMI_FLAG_IOS_EXTENSION (ash 1 3))
......@@ -26,6 +26,7 @@
(ice-9 match)
(ice-9 threads)
((agent) #:prefix agent:)
((jami) #:select (with-jami JAMI_FLAG_DEBUG))
((jami account) #:prefix account:)
((jami call) #:prefix call:)
((jami signal) #:prefix jami:)
......@@ -119,14 +120,19 @@
(define (main args)
(match (cdr args)
(("alice" bob-id) (alice bob-id))
(("bob") (bob))
(_
(jami:error "Invalid arguments: ~a" args)
(jami:error "Usage: ~a alice|bob (ARG)\n" (car args))
(exit EXIT_FAILURE)))
(jami:info "bye bye")
(with-jami (JAMI_FLAG_DEBUG)
(match (cdr args)
(("alice" bob-id)
(lambda ()
(alice bob-id)
(jami:info "bye bye")))
(("bob")
(lambda ()
(bob)
(jami:info "bye bye")))
(_
(jami:error "Invalid arguments: ~a" args)
(jami:error "Usage: ~a alice|bob (ARG)\n" (car args))
(exit EXIT_FAILURE))))
(exit EXIT_SUCCESS))
......@@ -25,6 +25,7 @@
#include "bindings/account.h"
#include "bindings/call.h"
#include "bindings/conversation.h"
#include "bindings/jami.h"
#include "bindings/logger.h"
#include "bindings/signal.h"
......@@ -36,6 +37,7 @@ install_scheme_primitives()
scm_c_define_module(name, init, NULL);
};
load_module("jami", install_jami_primitives);
load_module("jami account", install_account_primitives);
load_module("jami call", install_call_primitives);
load_module("jami conversation", install_conversation_primitives);
......
/*
* Copyright (C) 2022 Savoir-faire Linux Inc.
*
* Author: Olivier Dion <olivier.dion@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "jami/jami.h"
#include "utils.h"
static SCM init_binding(SCM flags)
{
LOG_BINDING();
unsigned int flags_cast = from_guile(flags);
return to_guile(DRing::init(static_cast<DRing::InitFlag>(flags_cast)));
}
static SCM fini_binding()
{
LOG_BINDING();
DRing::fini();
return SCM_UNDEFINED;
}
static SCM initialized_binding()
{
LOG_BINDING();
return to_guile(DRing::initialized());
}
static SCM logging_binding(SCM whom, SCM action)
{
LOG_BINDING();
DRing::logging(from_guile(whom), from_guile(action));
return SCM_UNDEFINED;
}
static SCM platform_binding()
{
LOG_BINDING();
return to_guile(DRing::platform());
}
static SCM start_binding(SCM config_file)
{
LOG_BINDING();
return to_guile(DRing::start(from_guile(config_file)));
}
static SCM version_binding()
{
LOG_BINDING();
return to_guile(DRing::version());
}
static void
install_jami_primitives(void *)
{
define_primitive("init", 1, 0, 0, (void*) init_binding);
define_primitive("initialized", 0, 0, 0, (void*) initialized_binding);
define_primitive("fini", 0, 0, 0, (void*) fini_binding);
define_primitive("logging", 2, 0, 0, (void*) logging_binding);
define_primitive("platform", 0, 0, 0, (void*) platform_binding);
define_primitive("start", 1, 0, 0, (void*) start_binding);
define_primitive("version", 0, 0, 0, (void*) version_binding);
}
......@@ -25,24 +25,11 @@
#include <libguile.h>
extern "C" {
DRING_PUBLIC void init();
DRING_PUBLIC void fini();
DRING_PUBLIC void bootstrap();
}
void
init()
bootstrap()
{
DRing::init(DRing::InitFlag(DRing::DRING_FLAG_DEBUG));
if (not DRing::start("")) {
scm_misc_error("Dring::start", NULL, 0);
}
install_scheme_primitives();
}
void
fini()
{
DRing::fini();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment