OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

python lambda as callback

  • Thread starter Thread starter Amir
  • Start date Start date
A

Amir

Guest
I have been reading example of astersik ARI in python and could not understand the following code clearly full sample code :

Code:
#!/usr/bin/env python

"""Example demonstrating ARI channel origination.
"""

#
# Copyright (c) 2013, Digium, Inc.
#
import requests

import ari

from requests import HTTPError

OUTGOING_ENDPOINT = "SIP/blink"

client = ari.connect('http://localhost:8088/', 'hey', 'peekaboo')

#
# Find (or create) a holding bridge.
#
bridges = [b for b in client.bridges.list()
           if b.json['bridge_type'] == 'holding']
if bridges:
    holding_bridge = bridges[0]
    print "Using bridge %s" % holding_bridge.id
else:
    holding_bridge = client.bridges.create(type='holding')
    print "Created bridge %s" % holding_bridge.id


def safe_hangup(channel):
    """Hangup a channel, ignoring 404 errors.
    :param channel: Channel to hangup.
    """
    try:
        channel.hangup()
    except HTTPError as e:
        # Ignore 404's, since channels can go away before we get to them
        if e.response.status_code != requests.codes.not_found:
            raise


def on_start(incoming, event):
    """Callback for StasisStart events.
    When an incoming channel starts, put it in the holding bridge and
    originate a channel to connect to it. When that channel answers, create a
    bridge and put both of them into it.
    :param incoming:
    :param event:
    """
    # Only process channels with the 'incoming' argument
    if event['args'] != ['incoming']:
        return

    # Answer and put in the holding bridge
    incoming.answer()
    incoming.play(media="sound:pls-wait-connect-call")
    holding_bridge.addChannel(channel=incoming.id)

    # Originate the outgoing channel
    outgoing = client.channels.originate(
        endpoint=OUTGOING_ENDPOINT, app="hello", appArgs="dialed")

    # If the incoming channel ends, hangup the outgoing channel
    incoming.on_event('StasisEnd', lambda *args: safe_hangup(outgoing))
    # and vice versa. If the endpoint rejects the call, it is destroyed
    # without entering Stasis()
    outgoing.on_event('ChannelDestroyed',
                      lambda *args: safe_hangup(incoming))

    def outgoing_on_start(channel, event):
        """Callback for StasisStart events on the outgoing channel
        :param channel: Outgoing channel.
        :param event: Event.
        """
        # Create a bridge, putting both channels into it.
        bridge = client.bridges.create(type='mixing')
        outgoing.answer()
        bridge.addChannel(channel=[incoming.id, outgoing.id])
        # Clean up the bridge when done
        outgoing.on_event('StasisEnd', lambda *args: bridge.destroy())

    outgoing.on_event('StasisStart', outgoing_on_start)


client.on_channel_event('StasisStart', on_start)

# Run the WebSocket
client.run(apps="hello")

Why in some case it has used lambda as callback like in :

Code:
incoming.on_event('StasisEnd', lambda *args: safe_hangup(outgoing))
# and vice versa. If the endpoint rejects the call, it is destroyed
# without entering Stasis()
outgoing.on_event('ChannelDestroyed',
                  lambda *args: safe_hangup(incoming))

and in some case it doesn not use lambda like :

Code:
outgoing.on_event('StasisStart', outgoing_on_start)

Why is it using lambda in those cases ?Why we can not call the safe_hangup directly like the outgoing_on_start?
<p>I have been reading example of astersik ARI in python and could not understand the following code clearly
full sample code :</p>
<pre><code>#!/usr/bin/env python

"""Example demonstrating ARI channel origination.
"""

#
# Copyright (c) 2013, Digium, Inc.
#
import requests

import ari

from requests import HTTPError

OUTGOING_ENDPOINT = "SIP/blink"

client = ari.connect('http://localhost:8088/', 'hey', 'peekaboo')

#
# Find (or create) a holding bridge.
#
bridges = [b for b in client.bridges.list()
if b.json['bridge_type'] == 'holding']
if bridges:
holding_bridge = bridges[0]
print "Using bridge %s" % holding_bridge.id
else:
holding_bridge = client.bridges.create(type='holding')
print "Created bridge %s" % holding_bridge.id


def safe_hangup(channel):
"""Hangup a channel, ignoring 404 errors.
:param channel: Channel to hangup.
"""
try:
channel.hangup()
except HTTPError as e:
# Ignore 404's, since channels can go away before we get to them
if e.response.status_code != requests.codes.not_found:
raise


def on_start(incoming, event):
"""Callback for StasisStart events.
When an incoming channel starts, put it in the holding bridge and
originate a channel to connect to it. When that channel answers, create a
bridge and put both of them into it.
:param incoming:
:param event:
"""
# Only process channels with the 'incoming' argument
if event['args'] != ['incoming']:
return

# Answer and put in the holding bridge
incoming.answer()
incoming.play(media="sound:pls-wait-connect-call")
holding_bridge.addChannel(channel=incoming.id)

# Originate the outgoing channel
outgoing = client.channels.originate(
endpoint=OUTGOING_ENDPOINT, app="hello", appArgs="dialed")

# If the incoming channel ends, hangup the outgoing channel
incoming.on_event('StasisEnd', lambda *args: safe_hangup(outgoing))
# and vice versa. If the endpoint rejects the call, it is destroyed
# without entering Stasis()
outgoing.on_event('ChannelDestroyed',
lambda *args: safe_hangup(incoming))

def outgoing_on_start(channel, event):
"""Callback for StasisStart events on the outgoing channel
:param channel: Outgoing channel.
:param event: Event.
"""
# Create a bridge, putting both channels into it.
bridge = client.bridges.create(type='mixing')
outgoing.answer()
bridge.addChannel(channel=[incoming.id, outgoing.id])
# Clean up the bridge when done
outgoing.on_event('StasisEnd', lambda *args: bridge.destroy())

outgoing.on_event('StasisStart', outgoing_on_start)


client.on_channel_event('StasisStart', on_start)

# Run the WebSocket
client.run(apps="hello")
</code></pre>
<p>Why in some case it has used lambda as callback like in :</p>
<pre><code>incoming.on_event('StasisEnd', lambda *args: safe_hangup(outgoing))
# and vice versa. If the endpoint rejects the call, it is destroyed
# without entering Stasis()
outgoing.on_event('ChannelDestroyed',
lambda *args: safe_hangup(incoming))
</code></pre>
<p>and in some case it doesn not use lambda like :</p>
<pre><code>outgoing.on_event('StasisStart', outgoing_on_start)
</code></pre>
<p>Why is it using lambda in those cases ?Why we can not call the safe_hangup directly like the outgoing_on_start?</p>
 

Latest posts

Top