Exemplo n.º 1
0
int luasofia_nua_handle_create(lua_State *L, nua_t *nua)
{
    luasofia_nua_handle_t *lnh = NULL;
    nua_handle_t *nh = NULL;
    su_home_t *home = su_home_create();
    tagi_t *tags = luasofia_tags_table_to_taglist(L, 2, home);

    /* create a nua_handle object */
    lnh = (luasofia_nua_handle_t*)lua_newuserdata(L, sizeof(luasofia_nua_handle_t));

    /* create the nua_handle_t */
    nh = nua_handle(nua, lnh, TAG_NEXT(tags));
    if (!nh)
        luaL_error(L, "nua_handle failed!");

    /* set Lua state */
    lnh->L = L;
    lnh->nh = nh;

    /* set its metatable */
    luaL_getmetatable(L, NUA_HANDLE_MTABLE);
    lua_setmetatable(L, -2);

    /* store nua handle at luasofia userdata table */
    luasofia_userdata_table_set(L, nh);

    su_home_unref(home);
    return 1;
}
Exemplo n.º 2
0
static int luasofia_nua_create(lua_State *L)
{
    luasofia_su_root_t *lroot = NULL;
    luasofia_nua_t *lnua = NULL;
    nua_t *nua = NULL;
    tagi_t *tags = NULL;
    su_home_t *home = su_home_create();

    /* get and check first argument (should be a root_t) */
    lroot = (luasofia_su_root_t*)luaL_checkudata(L, 1, SU_ROOT_MTABLE);

    /* check the callback table */
    if (!lua_isnoneornil(L, 2))
        luaL_checktype(L, 2, LUA_TTABLE);

    tags = luasofia_tags_table_to_taglist(L, 4, home);

    /* create a nua object */
    lnua = (luasofia_nua_t*) lua_newuserdata(L, sizeof(luasofia_nua_t));

    /* create the nua_t */
    nua = nua_create(lroot->root, nua_event_callback, L, TAG_NEXT(tags));
    if (!nua) {
        luaL_error(L, "nua_create failed!");
    }
    /* save nua object */
    lnua->nua = nua;

    /* set its metatable */
    luaL_getmetatable(L, NUA_MTABLE);
    lua_setmetatable(L, -2);

    /* store nua at luasofia userdata table */
    luasofia_userdata_table_set(L, nua);

    /* create env table */
    lua_createtable(L, 2, 0);

    /* save second argument (callbacks) on env table */
    if (!lua_isnoneornil(L, 2)) {
        lua_pushvalue(L, 2);
        lua_rawseti(L, -2, ENV_CALLBACK_INDEX);
    }

    /* save third argument (magic) on env table */
    if (!lua_isnoneornil(L, 3)) {
        lua_pushvalue(L, 3);
        lua_rawseti(L, -2, ENV_MAGIC_INDEX);
    }

    /* set env table as environment for udata */
    lua_setfenv(L, -2);

    su_home_unref(home);
    return 1;
}
Exemplo n.º 3
0
static int luasofia_nua_get_params(lua_State *L)
{
    /* get and check first argument (should be a luasofia_nua_t) */
    su_home_t *home = su_home_create();
    luasofia_nua_t *lnua = (luasofia_nua_t*)luaL_checkudata(L, 1, NUA_MTABLE);
    tagi_t *tags = luasofia_tags_table_to_taglist(L, 2, home);

    nua_get_params(lnua->nua, TAG_NEXT(tags));
    su_home_unref(home);
    return 0;
}
Exemplo n.º 4
0
static int luasofia_nua_handle_invite(lua_State *L)
{
    /* get and check first argument (should be a luasofia_nua_handle_t) */
    luasofia_nua_handle_t *lnh = (luasofia_nua_handle_t*)luaL_checkudata(L, 1, NUA_HANDLE_MTABLE);
    if (lnh->nh) {
        su_home_t *home = su_home_create();
        tagi_t *tags = luasofia_tags_table_to_taglist(L, 2, home);
        nua_invite(lnh->nh, TAG_NEXT(tags));
        su_home_unref(home);
    }
    return 0;
}
Exemplo n.º 5
0
int luasofia_nta_agent_create(lua_State * L)
{
    luasofia_su_root_t *lroot = NULL;
    url_string_t * contact = NULL;
    luasofia_nta_agent_t* u_nta_agent = NULL;
    su_home_t *home = su_home_create();
    tagi_t *tags = NULL;

    /* get and check first argument (should be a root_t) */
    lroot = (luasofia_su_root_t*)luaL_checkudata(L, -4, SU_ROOT_MTABLE);

    if(lua_isuserdata (L, -3)) {
        //Since there is no metatable for url_t or url_string_t we cant perform a checkudata here.
        contact = (url_string_t *) lua_touserdata (L, -3);
    } else {
        contact = URL_STRING_MAKE(luaL_checkstring (L, -3));
    }

    /* check the callback function */
    if(!lua_isfunction(L, -2))
        luaL_error(L, "nta_agent_create failed!, expected a callback function !");

    /* check if there is tags */
    tags = luasofia_tags_table_to_taglist(L, -1, home);

    u_nta_agent           = (luasofia_nta_agent_t *) lua_newuserdata(L, sizeof(luasofia_nta_agent_t));
    u_nta_agent->L        = L;
    u_nta_agent->agent    = nta_agent_create (lroot->root,
                                              (url_string_t const *)contact,
                                              nta_agent_message_callback,
                                              (nta_agent_magic_t *)u_nta_agent,
                                              TAG_NEXT(tags));

    // lets hold the ref to the lua callback function.
    lua_pushvalue(L, -3);
    u_nta_agent->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);

    if (!u_nta_agent->agent)
        luaL_error(L, "nta_agent_create failed!");

    /* set its metatable */
    luaL_getmetatable(L, NTA_AGENT_MTABLE);
    lua_setmetatable(L, -2);

    /* store nta_agent at luasofia userdata table 
       userdata_table[nta_agent_lightudata] = nta_agent_fulludata */
    luasofia_userdata_table_set(L, u_nta_agent->agent);

    su_home_unref(home);
    return 1;
}
Exemplo n.º 6
0
static int luasofia_nua_handle_get_hparams(lua_State *L)
{
    /* get and check first argument (should be a luasofia_nua_handle_t) */
    luasofia_nua_handle_t *lnh = (luasofia_nua_handle_t*) luaL_checkudata(L, 1, NUA_HANDLE_MTABLE);
    if (lnh->nh) {
        su_home_t *home = su_home_create();
      
        /* get and check second argument (should be a tag table) */
        //tagi_t *tags = luasofia_tags_table_to_taglist(L, 2, home);

        nua_get_hparams(lnh->nh, TAG_ANY(), TAG_NULL()); //FIXME TAG_NEXT(tags));

        su_home_unref(home);
    }
    return 0;
}
Exemplo n.º 7
0
static int luasofia_nua_handle_respond(lua_State *L)
{
    /* get and check first argument (should be a luasofia_nua_handle_t) */
    luasofia_nua_handle_t *lnh = (luasofia_nua_handle_t*) luaL_checkudata(L, 1, NUA_HANDLE_MTABLE);
    if (lnh->nh) {
        char const* phrase = NULL;
        su_home_t *home = su_home_create();
        /* get and check second argument (should be a int) */
        int status = luaL_checkinteger(L, 2);
      
        /* get and check fourth argument (should be a tag table) */
        tagi_t *tags = luasofia_tags_table_to_taglist(L, 4, home);

        /* get and check third argument (should be a string) */
        if (!lua_isnoneornil(L, 3))
            phrase = luaL_checkstring (L, 3);

        nua_respond(lnh->nh, status, phrase, TAG_NEXT(tags));
        su_home_unref(home);
    }
    return 0;
}
Exemplo n.º 8
0
    void DrachtioController::run() {
        
        if( m_bDaemonize ) {
            daemonize() ;
        }

		/* now we can initialize logging */
		m_logger.reset( this->createLogger() );
		this->logConfig() ;

        DR_LOG(log_debug) << "Main thread id: " << boost::this_thread::get_id() << endl ;

       /* open stats connection */
        string adminAddress ;
        unsigned int adminPort = m_Config->getAdminPort( adminAddress ) ;
        if( 0 != adminPort ) {
            m_pClientController.reset( new ClientController( this, adminAddress, adminPort )) ;
        }

        string url ;
        m_Config->getSipUrl( url ) ;
        DR_LOG(log_notice) << "starting sip stack on " << url << endl ;
        
        int rv = su_init() ;
        if( rv < 0 ) {
            DR_LOG(log_error) << "Error calling su_init: " << rv << endl ;
            return ;
        }
        ::atexit(su_deinit);
        
        m_root = su_root_create( NULL ) ;
        if( NULL == m_root ) {
            DR_LOG(log_error) << "Error calling su_root_create: " << endl ;
            return  ;
        }
        m_home = su_home_create() ;
        if( NULL == m_home ) {
            DR_LOG(log_error) << "Error calling su_home_create" << endl ;
        }
        su_log_redirect(NULL, __sofiasip_logger_func, NULL);
        
        /* for now set logging to full debug */
        su_log_set_level(NULL, m_Config->getSofiaLogLevel() ) ;
        setenv("TPORT_LOG", "1", 1) ;
        
        /* this causes su_clone_start to start a new thread */
        su_root_threading( m_root, 0 ) ;
        rv = su_clone_start( m_root, m_clone, this, clone_init, clone_destroy ) ;
        if( rv < 0 ) {
           DR_LOG(log_error) << "Error calling su_clone_start" << endl ;
           return  ;
        }
        
        /* enable extended headers */
        if (sip_update_default_mclass(sip_extend_mclass(NULL)) < 0) {
            DR_LOG(log_error) << "Error calling sip_update_default_mclass" << endl ;
            return  ;
        }
 
         /* create our agent */
        char str[URL_MAXLEN] ;
        memset(str, 0, URL_MAXLEN) ;
        strncpy( str, url.c_str(), url.length() ) ;
        
		m_nta = nta_agent_create( m_root,
                                 URL_STRING_MAKE(str),               /* our contact address */
                                 NULL,         /* no callback function */
                                 NULL,                  /* therefore no context */
                                 TAG_NULL(),
                                 TAG_END() ) ;
        
        if( NULL == m_nta ) {
            DR_LOG(log_error) << "Error calling nta_agent_create" << endl ;
            return ;
        }
        
        m_defaultLeg = nta_leg_tcreate(m_nta, defaultLegCallback, this,
                                      NTATAG_NO_DIALOG(1),
                                      TAG_END());
        if( NULL == m_defaultLeg ) {
            DR_LOG(log_error) << "Error creating default leg" << endl ;
            return ;
        }
        
        
        /* save my contact url, via, etc */
        m_my_contact = nta_agent_contact( m_nta ) ;
        ostringstream s ;
        s << "SIP/2.0/UDP " <<  m_my_contact->m_url[0].url_host ;
        if( m_my_contact->m_url[0].url_port ) s << ":" <<  m_my_contact->m_url[0].url_port  ;
        m_my_via.assign( s.str().c_str(), s.str().length() ) ;
        DR_LOG(log_debug) << "My via header: " << m_my_via << endl ;

        m_pDialogController = boost::make_shared<SipDialogController>( this, &m_clone ) ;
              
        /* sofia event loop */
        DR_LOG(log_notice) << "Starting sofia event loop in main thread: " <<  boost::this_thread::get_id() << endl ;

        /* start a timer */
        m_timer = su_timer_create( su_root_task(m_root), 30000) ;
        su_timer_set_for_ever(m_timer, watchdogTimerHandler, this) ;
 
        su_root_run( m_root ) ;
        DR_LOG(log_notice) << "Sofia event loop ended" << endl ;
        
        su_root_destroy( m_root ) ;
        m_root = NULL ;
        su_home_unref( m_home ) ;
        su_deinit() ;

        m_Config.reset();
        this->deinitializeLogging() ;

        
    }