SipMiddlewareApi
Version 34 (Adrian Georgescu, 04/13/2009 11:07 am) → Version 35/238 (Luci Stanescu, 04/16/2009 10:46 am)
= Middleware API =
[[TOC(WikiStart, Sip*, depth=3)]]
This chapter describes the event driven middleware API that can be used by a developer to build a user interface for SIP SIMPLE client library.
* To communicate with other sub-systems, the middleware uses the notification system provided by the [http://pypi.python.org/pypi/python-application python-application] package
* The middleware use the [wiki:SipConfigurationAPI configuration API] to access general and SIP account settings
[[Image(sipsimple-middleware.png)]]
== Classes ==
=== Session ===
A {{{sipsimple.session.Session}}} object represents a complete SIP session between the local and a remote endpoints, including media streams.
The currently supported media streams are audio and MSRP chat.
Both incoming and outgoing sessions are represented by this class.
A {{{Session}}} instance is a stateful object, meaning that it has a {{{state}}} attribute and that the lifetime of the session traverses different states, from session creation to termination.
State changes are triggered by methods called on the object by the application or by received network events.
Every time this attribute changes, a {{{SIPSessionChangedState}}} notification is sent by the object.
These states and their transitions are represented in the following diagram:
[[Image(sipsimple-session-state-machine.png)]]
Although these states are crucial to the correct operation of the {{{Session}}} object, an application using this object does not need to keep track of these states, as a different set of notifications is also emitted, which provide all the necessary information to the application.
==== attributes ====
'''state'''::
The state the object is currently in, being one of the states from the diagram above.
'''account'''::
The {{{sipsimple.account.Account}}} or {{{sipsimple.account.BonjourAccount}}} object that the {{{Session}}} is associated with.
On an outbound session, this is the account the application specified on object instantiation.
'''direction'''::
A string indicating the direction of the initial negotiation of the session.
This can be either {{{None}}}, "incoming" or "outgoing".
'''start_time'''::
The time the session started as a {{{datetime.datetime}}} object, or {{{None}}} if the session was not yet started.
'''stop_time'''::
The time the session stopped as a {{{datetime.datetime}}} object, or {{{None}}} if the session has not yet terminated.
'''on_hold_by_local'''::
Boolean indicating whether the session was put on hold by the local party.
'''on_hold_by_remote'''::
Boolean indicating whether the session was put on hold by the remote party.
'''on_hold'''::
Boolean indicating whether the session was put on hold, either by the local or the remote party.
'''remote_user_agent'''::
A string indicating the remote user agent, if it provided one.
Initially this will be {{{None}}}, it will be set as soon as this information is received from the remote party (which may be never).
'''local_uri'''::
The {{{sipsimple.core.SIPURI}}} of the local party, if the session is active.
'''remote_uri'''::
The {{{sipsimple.core.SIPURI}}} of the remote party, if the session is active.
'''caller_uri'''::
The {{{sipsimple.core.SIPURI}}} of the calling party, if the session is active.
Depending on the direction, this is either {{{local_uri}}} or {{{remote_uri}}}.
'''callee_uri'''::
The {{{sipsimple.core.SIPURI}}} of the called party, if the session is active.
Depending on the direction, this is either {{{local_uri}}} or {{{remote_uri}}}.
'''route'''::
A copy of the {{{sipsimple.core.Route}}} object passed when the {{{connect()}}} method was called.
On incoming or inactive sessions this is {{{None}}}.
'''audio_transport'''::
The {{{sipsimple.core.AudioTransport}}} object used by the session, if it currently contains an audio stream.
Normally the application will not need to access this directly.
'''has_audio'''::
A boolean indicating if this {{{Session}}} currently has an active audio stream.
'''audio_sample_rate'''::
If the audio stream was started, this attribute contains the sample rate of the audio negotiated.
'''audio_codec'''::
If the audio stream was started, this attribute contains the name of the audio codec that was negotiated.
'''audio_srtp_active'''::
If the audio stream was started, this boolean attribute indicates if SRTP is currently being used on the stream.
'''audio_local_rtp_address'''::
If an audio stream is present within the session, this attribute contains the local IP address used for the audio stream.
'''audio_local_rtp_port'''::
If an audio stream is present within the session, this attribute contains the local UDP port used for the audio stream.
'''audio_remote_rtp_address_sdp'''::
If the audio stream was started, this attribute contains the IP address that the remote party gave to send audio to.
'''audio_remote_rtp_port_sdp'''::
If the audio stream was started, this attribute contains the UDP port that the remote party gave to send audio to.
'''audio_remote_rtp_address_received'''::
If the audio stream was started, this attribute contains the remote IP address from which the audio stream is being received.
'''audio_remote_rtp_port_received'''::
If the audio stream was started, this attribute contains the remote UDP port from which the audio stream is being received.
'''audio_was_received'''::
This boolean property indicates if audio was actually received on the audio stream contained within this session.
'''audio_recording_file_name'''::
If the audio stream is currently being recorded to disk, this property contains the name of the {{{.wav}}} file being recorded to.
'''chat_transport'''::
The {{{sipsimple.msrp.MSRPChat}}} object used by the session as chat transport, if the session currently contains a chat stream.
Normally the application will not need to access this directly.
'''has_chat'''::
A boolean property indicating if this {{{Session}}} currently has an active chat stream.
==== methods ====
'''!__init!__'''(''self'', '''account''')::
Creates a new {{{Session}}} object in the {{{NULL}}} state.
[[BR]]''account'':[[BR]]
The local account to be associated with this {{{Session}}}.
'''connect'''(''self'', '''callee_uri''', '''routes''', '''audio'''={{{False}}}, '''chat'''={{{False}}})::
Will set up the {{{Session}}} as outbound and propose the new session to the specified remote party and move the state machine to the {{{CALLING}}} state.
Before contacting the remote party, a {{{SIPSessionNewOutgoing}}} notification will be emitted.
If there is a failure or the remote party rejected the offer, a {{{SIPSessionDidFail}}} notification will be sent, followed by a {{{SIPSessionDidEnd}}}.
Any time a ringing indication is received from the remote party, a {{{SIPSessionGotRingIndication}}} notification is sent.
If the remote party accepted the session, a {{{SIPSessionWillStart}}} notification will be sent, followed by a {{{SIPSessionDidStart}}} notification when the session is actually established.
This method may only be called while in the {{{NULL}}} state.
[[BR]]''callee_uri'':[[BR]]
A {{{sipsimple.core.SIPURI}}} object representing the remote host to initiate the session to.
[[BR]]''routes'':[[BR]]
An iterable of {{{sipsimple.core.Route}}} objects, specifying the IP, port and transport to the outbound proxy.
These routes will be tried in order, until one of them succeeds.
[[BR]]''audio'':[[BR]]
A boolean indicating whether an audio stream should be initially included in this session.
[[BR]]''chat'':[[BR]]
A boolean indicating whether a chat stream should be initially included in this session.
'''accept'''(''self'', '''audio'''={{{False}}}, '''chat'''={{{False}}})::
Calling this methods will accept an incoming session and move the state machine to the {{{ACCEPTING}}} state.
When there is a new incoming session, a {{{SIPSessionNewIncoming}}} notification is sent, after which the application can call this method on the sender of the notification.
After this method is called, {{{SIPSessionWillStart}}} followed by {{{SIPSessionDidStart}}} will be emitted, or {{{SIPSessionDidFail}}} followed by {{{SIPSessionDidEnd}}} on an error.
This method may only be called while in the {{{INCOMING}}} state.
[[BR]]''audio'':[[BR]]
A boolean indicating whether an audio stream should be accepted for this session.
Note that this may only be set to {{{True}}} if an audio stream was actually proposed by the remote party.
[[BR]]''chat'':[[BR]]
A boolean indicating whether a chat stream should be accepted for this session.
Note that this may only be set to {{{True}}} if a chat stream was actually proposed by the remote party.
'''reject'''(''self'')::
Reject an incoming session and move it to the {{{TERMINATING}}} state, which eventually leads to the {{{TERMINATED}}} state.
Calling this method will cause the {{{Session}}} object to emit a {{{SIPSessionWillEnd}}} notification, followed by a {{{SIPSessionDidEnd}}} notification.
This method may only be called while in the {{{INCOMING}}} state.
'''hold'''(''self'')::
Put the session on hold.
This will cause a {{{SIPGotHoldRequest}}} notification to be sent.
This method may only be called while in the {{{ESTABLISHED}}} state.
'''unhold'''(''self'')::
Take the session out of hold.
This will cause a {{{SIPGotUnholdRequest}}} notification to be sent.
This method may only be called while in the {{{ESTABLISHED}}} state.
'''start_recording_audio'''(''self'', '''file_name'''={{{None}}})::
If an audio stream is present within this session, calling this method will record the audio to a {{{.wav}}} file.
Note that when the session is on hold, nothing will be recorded to the file.
Right before starting the recording a {{{SIPSessionWillStartRecordingAudio}}} notification will be emitted, followed by a {{{SIPSessionDidStartRecordingAudio}}}.
This method may only be called while in the {{{ESTABLISHED}}} state.
[[BR]]''file_name'':[[BR]]
The name of the {{{.wav}}} file to record to.
If this is set to {{{None}}}, a default file name including the session participants and the timestamp will be generated.
'''stop_recording_audio'''(''self'')::
This will stop a previously started recording.
Before stopping, a {{{SIPSessionWillStopRecordingAudio}}} notification will be sent, followed by a {{{SIPSessionDidStopRecordingAudio}}}.
This method may only be called while in the {{{ESTABLISHED}}} state.
'''send_dtmf'''(''self'', '''digit''')::
If this session currently has an active audio stream, send a DTMF digit to the remote party over it.
This method may only be called while in the {{{ESTABLISHED}}} state and if the session has an active audio stream.
[[BR]]''digit'':[[BR]]
This should a string of length 1, containing a valid DTMF digit value.
'''send_message'''(''self'', '''content''', '''content_type'''="text/plain", '''to_uri'''={{{None}}}, '''dt'''={{{None}}})::
If this session currently has an active MSRP chat with the remote party, send a message over with the with the specified parameters.
This will result in either a {{{SIPSessionDidDeliverMessage}}} or a {{{SIPSessionDidNotDeliverMessage}}} notification being sent.
These notifications include a unique ID as data attribute which is also returned by this method.
This method may only be called while in the {{{ESTABLISHED}}} state.
[[BR]]''content'':[[BR]]
The body of the MSRP message as a string.
[[BR]]''content_type'':[[BR]]
The Content-Type of the body as a string
[[BR]]''to_uri'':[[BR]]
The {{{sipsimple.core.SIPURI}}} that should be put in the {{{To:}}} header of the CPIM wrapper of the message.
This defaults to the SIP URI of the remote party of the session if the argument is set to {{{None}}}
[[BR]]''dt'':[[BR]]
A {{{datetime.datetime}}} object representing the timestamp to put on the CPIM wrapper of the message.
When set to {{{None}}}, this defaults to now.
'''add_audio'''(''self'')::
Propose the remote party to add an audio stream to this session.
Calling this method will cause a {{{SIPSessionGotStreamProposal}}} notification to be emitted.
After this, the state machine will move into the {{{PROPOSING}}} state until either a {{{SIPSessionAcceptedStreamProposal}}} or {{{SIPSessionRejectedStreamProposal}}} notification is sent, informing the application if the remote party accepted the proposal.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that does not currently have an audio stream.
'''remove_audio'''(''self'')::
Stop the audio stream currently active within the session and inform the remote party of this.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that has an audio stream.
'''add_chat'''(''self'')::
Propose the remote party to add a chat stream to this session.
Calling this method will cause a {{{SIPSessionGotStreamProposal}}} notification to be emitted.
After this, the state machine will move into the {{{PROPOSING}}} state until either a {{{SIPSessionAcceptedStreamProposal}}} or {{{SIPSessionRejectedStreamProposal}}} notification is sent, informing the application if the remote party accepted the proposal.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that does not currently have a chat stream.
'''remove_chat'''(''self'')::
Stop the chat stream currently active within the session and inform the remote party of this.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that has a chat stream.
'''accept_proposal'''(''self'', '''audio'''={{{False}}}, '''chat'''={{{False}}})::
When the remote party proposes to add a new stream, signaled by the {{{SIPSessionGotStreamProposal}}} notification, the application can use this method to accept the stream(s) being proposed.
After calling this method a {{{SIPSessionAcceptedStreamProposal}}} notification is sent, unless an error occurs while setting up the new stream, in which case a {{{SIPSessionRejectedStreamProposal}}} notification is sent and a rejection is sent to the remote party.
This method may only be called while in the {{{PROPOSED}}} state.
[[BR]]''audio'':[[BR]]
A boolean indicating whether an audio stream should be accepted for this proposal.
Note that this may only be set to {{{True}}} if an audio stream was actually proposed by the remote party.
[[BR]]''chat'':[[BR]]
A boolean indicating whether a chat stream should be accepted for this proposal.
Note that this may only be set to {{{True}}} if a chat stream was actually proposed by the remote party.
'''reject_proposal'''(''self'')::
When the remote party proposes (a) stream(s) that the application does not want to accept, this method can be used to reject the proposal, after which a {{{SIPSessionRejectedStreamProposal}}} notification is sent.
This method may only be called while in the {{{PROPOSED}}} state.
'''end'''(''self'')::
This method may be called any time when the {{{Session}}} object is active (i.e. not in the {{{NULL}}}, {{{TERMINATING}}} or {{{TERMINATED}}} states) in order to terminate the session.
Right before termination a {{{SIPSessionWillEnd}}} notification is sent, after termination {{{SIPSessionDidEnd}}} is sent.
==== notifications ====
'''SIPSessionChangedState'''::
Will be sent whenever the {{{Session}}} object changes its state.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''prev_state'':[[BR]]
The previous state state the object was in.
[[BR]]''state'':[[BR]]
The new state the object is in.
'''SIPSessionNewIncoming'''::
Will be sent when a new incoming {{{Session}}} is received.
The application should listen for this notification from all objects specifically to get informed of incoming sessions.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''streams'':[[BR]]
A list of strings indicating the streams that were proposed by the remote party.
'''SIPSessionNewOutgoing'''::
Will be sent when the applcation requests a new outgoing {{{Session}}}.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''streams'':[[BR]]
A list of strings indicating the streams that were proposed to the remote party.
'''SIPSessionGotRingIndication'''::
Will be sent when an outgoing {{{Session}}} receives an indication that a remote device is ringing.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionWillStart'''::
Will be sent just before a {{{Session}}} completes negotiation.
In terms of SIP, this is sent after the final response to the {{{INVITE}}}, but before the {{{ACK}}}.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionDidStart'''::
Will be sent when a {{{Session}}} completes negotiation.
In terms of SIP this is sent after the {{{ACK}}} was sent or received.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionDidFail'''::
This notification is sent whenever the session fails.
The failure reason is included in the data attributes.
This notification is always followed by {{{SIPSessionDidEnd}}}.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating the origin of the failure.
This will either be "local" or "remote".
[[BR]]''code'':[[BR]]
The SIP error code of the failure.
If this is 0, the error was an internal exception.
[[BR]]''reason'':[[BR]]
A string explaining the reason of the failure.
'''SIPSessionWillEnd'''::
Will be sent just before terminating a {{{Session}}} at the request of the application.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionDidEnd'''::
Will be sent always when a {{{Session}}} ends, either because of a failure (in which case it is preceded by {{{SIPSessionDidFail}}}), remote or local session termination.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating who originated the termination.
This will either be "local" or "remote".
'''SIPSessionGotHoldRequest'''::
Will be sent when the session got put on hold, either by the local or the remote party.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating who originated the hold request, and consequently in which direction the session got put on hold.
'''SIPSessionGotUnholdRequest'''::
Will be sent when the session got taken out of hold, either by the local or the remote party.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating who sent the original hold request, and consequently in which direction the session got taken out of hold.
'''SIPSessionWillStartRecordingAudio'''::
Will be sent when the application requested that the audio stream active within the session be record to a {{{.wav}}} file, just before recording starts.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionDidStartRecordingAudio'''::
Will be sent when the application requested that the audio stream active within the session be record to a {{{.wav}}} file, just after recording starts.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionWillStopRecordingAudio'''::
Will be sent when the application requested ending the recording to a {{{.wav}}} file, just before recording stops.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionDidStopRecordingAudio'''::
Will be sent when the application requested ending the recording to a {{{.wav}}} file, just before recording stops.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionGotNoAudio'''::
This notification will be sent if 5 seconds after the audio stream starts, no audio was received from the remote party.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionGotDTMF'''::
Will be send if there is a DMTF digit received from the remote party on the audio stream.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''digit'':[[BR]]
The DTMF digit that was received, in the form of a string of length 1.
'''SIPSessionGotMessage'''::
Will be sent whenever a MSRP message is received on the chat stream of the session.
[[BR]]''content'':[[BR]]
The body of the message.
[[BR]]''content_type'':[[BR]]
The Content-Type of the body.
[[BR]]''cpim_headers'':[[BR]]
A dictionary of headers included in the CPIM wrapper.
[[BR]]''message'':[[BR]]
Raw MSRP message, an msrplib.protocol.MSRPData instance
'''SIPSessionDidDeliverMessage'''::
Will be sent when a previously sent MSRP chat message got delivered to the remote party.
[[BR]]''message_id'':[[BR]]
The unique identifier of this message as a string, as previously returned by the {{{send_message()}}} method.
[[BR]]''code'':[[BR]]
The response code of the confirmation report.
[[BR]]''reason'':[[BR]]
The reason string of the confirmation report.
[[BR]]''message'':[[BR]]
Raw MSRP message, an msrplib.protocol.MSRPData instance
'''SIPSessionDidDeliverMessage'''::
Will be sent when a previously sent MSRP chat message did not get delivered to the remote party.
[[BR]]''message_id'':[[BR]]
The unique identifier of this message as a string, as previously returned by the {{{send_message()}}} method.
[[BR]]''code'':[[BR]]
The response code of the confirmation report.
[[BR]]''reason'':[[BR]]
The reason string of the confirmation report.
[[BR]]''message'':[[BR]]
Raw MSRP message, an msrplib.protocol.MSRPData instance
'''SIPSessionGotStreamProposal'''::
Will be sent when either the local or the remote party proposes to add a stream to the session.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''proposer'':[[BR]]
The party that did the stream proposal, can be either "local" or "remote".
[[BR]]''streams'':[[BR]]
A list of strings indicating the streams that were proposed.
'''SIPSessionRejectedStreamProposal'''::
Will be sent when either the local or the remote party rejects a proposal to have (a) stream(s) added to the session.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''proposer'':[[BR]]
The party that did the stream proposal, can be either "local" or "remote".
[[BR]]''reason'':[[BR]]
The reason for rejecting the stream proposal.
'''SIPSessionRejectedStreamProposal'''::
Will be sent when either the local or the remote party accepts a proposal to have (a) stream(s) added to the session.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''proposer'':[[BR]]
The party that did the stream proposal, can be either "local" or "remote".
'''SIPSessionGotStreamUpdate'''::
Will be sent when a media stream is either activated or deactivated.
An application should listen to this notification in order to know when a media stream can be used.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''streams'':[[BR]]
A list indicating which streams are active on the session from this point onwards.
As an example for how to use the {{{Session}}} object, the following provides a basic Python program that initiates an outgoing SIP session request:
{{{
import sys
from threading import Event
from zope.interface import implements
from application.notification import IObserver, NotificationCenter
from sipsimple.configuration import ConfigurationManager
from sipsimple.account import AccountManager
from sipsimple.engine import Engine
from sipsimple.session import Session
from sipsimple.core import SIPURI, Route
class SimpleOutboundCall(object):
# indicate that we implement the application.notification.IObserver interface
implements(IObserver)
def __init__(self, remote_uri, route):
# setup a threading.Event to signal that the Engine has stopped
self.engine_ended_event = Event()
# start the configuration manager
ConfigurationManager().start()
# start the account manager
am = AccountManager()
am.start()
# start the Engine with configuration framework parameters
Engine().start_cfg()
# create a new Session using the default account
self.session = Session(am.default_account)
# listen for the notification that the Engine stopped
NotificationCenter().add_observer(self, "SIPEngineDidEnd", Engine())
# listen for the notification that the Session ended
NotificationCenter().add_observer(self, "SIPSessionDidEnd", self.session)
# start a new outbound session
self.session.connect(remote_uri, [route], audio=True)
def end(self):
# if the Session is still active, terminate it
self.session.end()
# wait for the engine to stop, processed in handle_notification
self.engine_ended_event.wait()
# quit the progam, as this can only be done from the main thread
sys.exit()
def handle_notification(self, notification):
if notification.name == "SIPSessionDidEnd":
# if for whatever reason the session ended, stop the Engine
print "Session ended"
Engine().stop()
elif notification.name == "SIPEngineDidEnd":
# once the Engine has stopped, signal the (possibly) waiting main
# thread through a threading.Event
self.engine_ended_event.set()
# place an audio call from the specified account to the specified URI, through
# the specified SIP proxy
# edit this to reflect real settings
call = SimpleOutboundCall(SIPURI(user="bob", host="example.com"), Route("1.2.3.4"))
# block waiting for user input
print "Placing call, press enter to quit program"
raw_input()
# block in end() until the Engine has stopped
call.end()
}}}
=== SessionManager ===
The {{{sipsimple.session.SessionManager}}} class is a singleton, which acts as the central aggregation point for sessions within the middleware.
Although it is mainly used internally, the application can use it to query information about all active sessions.
The SessionManager is implemented as a singleton, meaning that only one instance of this class exists within the middleware.
Note that, in order to be able to receive calls, the application has to instantiate this object.
==== attributes ====
'''sessions'''::
A property providing a copy of the list of all active {{{Sesssion}}} objects within the application, meaning any {{{Session}}} object that exists globally within the application and is not in the {{{NULL}}} or {{{TERMINATED}}} state.
==== methods ====
'''!__init!__'''(''self'')::
This either returns a new {{{SessionManager}}} object with default configuration objects, or returns a copy of the already existing instance.
=== Account ===
The {{{sipsimple.account.Account} objects represent the SIP accounts which are used by the middleware. It has a dual purpose: it acts as both a container for account-related settings and as a complex object which can be used to interact with various per-account functions, such as presence, registration etc. This page documents the latter case, while the former is explained in the [wiki:SipConfigurationAPI#Account Configuration API].
There is exactly one instance of {{{Account}}} per SIP account used and it is uniquely identifiable by its SIP ID, in the form ''user@domain''. It is a singleton, in the sense that instantiating {{{Account}}} using an already used SIP ID will return the same object. However, this is not the recommended way of accessing accounts, as this can lead to creation of new ones; the recommended way is by using the [wiki:SipMiddlewareApi#AccountManager AccountManager]. The next sections will use a lowercase, monospaced {{{account}}} to represent an instance of {{{Account}}}.
==== Account states ====
The {{{Account}}} objects have a setting flag called {{{enabled}}} which, if set to {{{False}}} will deactivate it: none of the internal functions will work in this case; in addition, the application using the middleware should not do anything with a disabled account. After changing it's value, the {{{save()}}} method needs to be called, as the flag is a setting and will not be used until this method is called:
{{{
account.enabled = True
account.save()
}}}
The other functions of {{{Account}}} which run automatically have other enabled flags as well. They will only be activated when both the global enabled flag is set and the function-specific one. These are:
'''Account.registration.enabled'''::
This flag controls the automatic registration of the account. The notifications '''SIPAccountRegistrationDidSucceed''', '''SIPAccountRegistrationDidFail''' and '''SIPAccountRegistrationDidEnd''' are used to inform the status of this registration.
'''Account.presence.enabled'''::
This flag controls the automatic subscription to buddies for the ''presence'' event and the publication of data in this event. (Not implemented yet)
'''Account.dialog_event.enabled'''::
This flag controls the automatic subscription to buddies for the ''dialog-info'' event and the publication of data in this event. (Not implemented yet)
'''Account.message_summary.enabled'''::
This flag controls the automatic subscription to the ''message-summary'' event in order to find out about voicemail messages. (Not implemented yet)
The {{{save()}}} method needs to be called after changing these flags in order for them to take effect.
[[TOC(WikiStart, Sip*, depth=3)]]
This chapter describes the event driven middleware API that can be used by a developer to build a user interface for SIP SIMPLE client library.
* To communicate with other sub-systems, the middleware uses the notification system provided by the [http://pypi.python.org/pypi/python-application python-application] package
* The middleware use the [wiki:SipConfigurationAPI configuration API] to access general and SIP account settings
[[Image(sipsimple-middleware.png)]]
== Classes ==
=== Session ===
A {{{sipsimple.session.Session}}} object represents a complete SIP session between the local and a remote endpoints, including media streams.
The currently supported media streams are audio and MSRP chat.
Both incoming and outgoing sessions are represented by this class.
A {{{Session}}} instance is a stateful object, meaning that it has a {{{state}}} attribute and that the lifetime of the session traverses different states, from session creation to termination.
State changes are triggered by methods called on the object by the application or by received network events.
Every time this attribute changes, a {{{SIPSessionChangedState}}} notification is sent by the object.
These states and their transitions are represented in the following diagram:
[[Image(sipsimple-session-state-machine.png)]]
Although these states are crucial to the correct operation of the {{{Session}}} object, an application using this object does not need to keep track of these states, as a different set of notifications is also emitted, which provide all the necessary information to the application.
==== attributes ====
'''state'''::
The state the object is currently in, being one of the states from the diagram above.
'''account'''::
The {{{sipsimple.account.Account}}} or {{{sipsimple.account.BonjourAccount}}} object that the {{{Session}}} is associated with.
On an outbound session, this is the account the application specified on object instantiation.
'''direction'''::
A string indicating the direction of the initial negotiation of the session.
This can be either {{{None}}}, "incoming" or "outgoing".
'''start_time'''::
The time the session started as a {{{datetime.datetime}}} object, or {{{None}}} if the session was not yet started.
'''stop_time'''::
The time the session stopped as a {{{datetime.datetime}}} object, or {{{None}}} if the session has not yet terminated.
'''on_hold_by_local'''::
Boolean indicating whether the session was put on hold by the local party.
'''on_hold_by_remote'''::
Boolean indicating whether the session was put on hold by the remote party.
'''on_hold'''::
Boolean indicating whether the session was put on hold, either by the local or the remote party.
'''remote_user_agent'''::
A string indicating the remote user agent, if it provided one.
Initially this will be {{{None}}}, it will be set as soon as this information is received from the remote party (which may be never).
'''local_uri'''::
The {{{sipsimple.core.SIPURI}}} of the local party, if the session is active.
'''remote_uri'''::
The {{{sipsimple.core.SIPURI}}} of the remote party, if the session is active.
'''caller_uri'''::
The {{{sipsimple.core.SIPURI}}} of the calling party, if the session is active.
Depending on the direction, this is either {{{local_uri}}} or {{{remote_uri}}}.
'''callee_uri'''::
The {{{sipsimple.core.SIPURI}}} of the called party, if the session is active.
Depending on the direction, this is either {{{local_uri}}} or {{{remote_uri}}}.
'''route'''::
A copy of the {{{sipsimple.core.Route}}} object passed when the {{{connect()}}} method was called.
On incoming or inactive sessions this is {{{None}}}.
'''audio_transport'''::
The {{{sipsimple.core.AudioTransport}}} object used by the session, if it currently contains an audio stream.
Normally the application will not need to access this directly.
'''has_audio'''::
A boolean indicating if this {{{Session}}} currently has an active audio stream.
'''audio_sample_rate'''::
If the audio stream was started, this attribute contains the sample rate of the audio negotiated.
'''audio_codec'''::
If the audio stream was started, this attribute contains the name of the audio codec that was negotiated.
'''audio_srtp_active'''::
If the audio stream was started, this boolean attribute indicates if SRTP is currently being used on the stream.
'''audio_local_rtp_address'''::
If an audio stream is present within the session, this attribute contains the local IP address used for the audio stream.
'''audio_local_rtp_port'''::
If an audio stream is present within the session, this attribute contains the local UDP port used for the audio stream.
'''audio_remote_rtp_address_sdp'''::
If the audio stream was started, this attribute contains the IP address that the remote party gave to send audio to.
'''audio_remote_rtp_port_sdp'''::
If the audio stream was started, this attribute contains the UDP port that the remote party gave to send audio to.
'''audio_remote_rtp_address_received'''::
If the audio stream was started, this attribute contains the remote IP address from which the audio stream is being received.
'''audio_remote_rtp_port_received'''::
If the audio stream was started, this attribute contains the remote UDP port from which the audio stream is being received.
'''audio_was_received'''::
This boolean property indicates if audio was actually received on the audio stream contained within this session.
'''audio_recording_file_name'''::
If the audio stream is currently being recorded to disk, this property contains the name of the {{{.wav}}} file being recorded to.
'''chat_transport'''::
The {{{sipsimple.msrp.MSRPChat}}} object used by the session as chat transport, if the session currently contains a chat stream.
Normally the application will not need to access this directly.
'''has_chat'''::
A boolean property indicating if this {{{Session}}} currently has an active chat stream.
==== methods ====
'''!__init!__'''(''self'', '''account''')::
Creates a new {{{Session}}} object in the {{{NULL}}} state.
[[BR]]''account'':[[BR]]
The local account to be associated with this {{{Session}}}.
'''connect'''(''self'', '''callee_uri''', '''routes''', '''audio'''={{{False}}}, '''chat'''={{{False}}})::
Will set up the {{{Session}}} as outbound and propose the new session to the specified remote party and move the state machine to the {{{CALLING}}} state.
Before contacting the remote party, a {{{SIPSessionNewOutgoing}}} notification will be emitted.
If there is a failure or the remote party rejected the offer, a {{{SIPSessionDidFail}}} notification will be sent, followed by a {{{SIPSessionDidEnd}}}.
Any time a ringing indication is received from the remote party, a {{{SIPSessionGotRingIndication}}} notification is sent.
If the remote party accepted the session, a {{{SIPSessionWillStart}}} notification will be sent, followed by a {{{SIPSessionDidStart}}} notification when the session is actually established.
This method may only be called while in the {{{NULL}}} state.
[[BR]]''callee_uri'':[[BR]]
A {{{sipsimple.core.SIPURI}}} object representing the remote host to initiate the session to.
[[BR]]''routes'':[[BR]]
An iterable of {{{sipsimple.core.Route}}} objects, specifying the IP, port and transport to the outbound proxy.
These routes will be tried in order, until one of them succeeds.
[[BR]]''audio'':[[BR]]
A boolean indicating whether an audio stream should be initially included in this session.
[[BR]]''chat'':[[BR]]
A boolean indicating whether a chat stream should be initially included in this session.
'''accept'''(''self'', '''audio'''={{{False}}}, '''chat'''={{{False}}})::
Calling this methods will accept an incoming session and move the state machine to the {{{ACCEPTING}}} state.
When there is a new incoming session, a {{{SIPSessionNewIncoming}}} notification is sent, after which the application can call this method on the sender of the notification.
After this method is called, {{{SIPSessionWillStart}}} followed by {{{SIPSessionDidStart}}} will be emitted, or {{{SIPSessionDidFail}}} followed by {{{SIPSessionDidEnd}}} on an error.
This method may only be called while in the {{{INCOMING}}} state.
[[BR]]''audio'':[[BR]]
A boolean indicating whether an audio stream should be accepted for this session.
Note that this may only be set to {{{True}}} if an audio stream was actually proposed by the remote party.
[[BR]]''chat'':[[BR]]
A boolean indicating whether a chat stream should be accepted for this session.
Note that this may only be set to {{{True}}} if a chat stream was actually proposed by the remote party.
'''reject'''(''self'')::
Reject an incoming session and move it to the {{{TERMINATING}}} state, which eventually leads to the {{{TERMINATED}}} state.
Calling this method will cause the {{{Session}}} object to emit a {{{SIPSessionWillEnd}}} notification, followed by a {{{SIPSessionDidEnd}}} notification.
This method may only be called while in the {{{INCOMING}}} state.
'''hold'''(''self'')::
Put the session on hold.
This will cause a {{{SIPGotHoldRequest}}} notification to be sent.
This method may only be called while in the {{{ESTABLISHED}}} state.
'''unhold'''(''self'')::
Take the session out of hold.
This will cause a {{{SIPGotUnholdRequest}}} notification to be sent.
This method may only be called while in the {{{ESTABLISHED}}} state.
'''start_recording_audio'''(''self'', '''file_name'''={{{None}}})::
If an audio stream is present within this session, calling this method will record the audio to a {{{.wav}}} file.
Note that when the session is on hold, nothing will be recorded to the file.
Right before starting the recording a {{{SIPSessionWillStartRecordingAudio}}} notification will be emitted, followed by a {{{SIPSessionDidStartRecordingAudio}}}.
This method may only be called while in the {{{ESTABLISHED}}} state.
[[BR]]''file_name'':[[BR]]
The name of the {{{.wav}}} file to record to.
If this is set to {{{None}}}, a default file name including the session participants and the timestamp will be generated.
'''stop_recording_audio'''(''self'')::
This will stop a previously started recording.
Before stopping, a {{{SIPSessionWillStopRecordingAudio}}} notification will be sent, followed by a {{{SIPSessionDidStopRecordingAudio}}}.
This method may only be called while in the {{{ESTABLISHED}}} state.
'''send_dtmf'''(''self'', '''digit''')::
If this session currently has an active audio stream, send a DTMF digit to the remote party over it.
This method may only be called while in the {{{ESTABLISHED}}} state and if the session has an active audio stream.
[[BR]]''digit'':[[BR]]
This should a string of length 1, containing a valid DTMF digit value.
'''send_message'''(''self'', '''content''', '''content_type'''="text/plain", '''to_uri'''={{{None}}}, '''dt'''={{{None}}})::
If this session currently has an active MSRP chat with the remote party, send a message over with the with the specified parameters.
This will result in either a {{{SIPSessionDidDeliverMessage}}} or a {{{SIPSessionDidNotDeliverMessage}}} notification being sent.
These notifications include a unique ID as data attribute which is also returned by this method.
This method may only be called while in the {{{ESTABLISHED}}} state.
[[BR]]''content'':[[BR]]
The body of the MSRP message as a string.
[[BR]]''content_type'':[[BR]]
The Content-Type of the body as a string
[[BR]]''to_uri'':[[BR]]
The {{{sipsimple.core.SIPURI}}} that should be put in the {{{To:}}} header of the CPIM wrapper of the message.
This defaults to the SIP URI of the remote party of the session if the argument is set to {{{None}}}
[[BR]]''dt'':[[BR]]
A {{{datetime.datetime}}} object representing the timestamp to put on the CPIM wrapper of the message.
When set to {{{None}}}, this defaults to now.
'''add_audio'''(''self'')::
Propose the remote party to add an audio stream to this session.
Calling this method will cause a {{{SIPSessionGotStreamProposal}}} notification to be emitted.
After this, the state machine will move into the {{{PROPOSING}}} state until either a {{{SIPSessionAcceptedStreamProposal}}} or {{{SIPSessionRejectedStreamProposal}}} notification is sent, informing the application if the remote party accepted the proposal.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that does not currently have an audio stream.
'''remove_audio'''(''self'')::
Stop the audio stream currently active within the session and inform the remote party of this.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that has an audio stream.
'''add_chat'''(''self'')::
Propose the remote party to add a chat stream to this session.
Calling this method will cause a {{{SIPSessionGotStreamProposal}}} notification to be emitted.
After this, the state machine will move into the {{{PROPOSING}}} state until either a {{{SIPSessionAcceptedStreamProposal}}} or {{{SIPSessionRejectedStreamProposal}}} notification is sent, informing the application if the remote party accepted the proposal.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that does not currently have a chat stream.
'''remove_chat'''(''self'')::
Stop the chat stream currently active within the session and inform the remote party of this.
This method may only be called while in the {{{ESTABLISHED}}} state on a {{{Session}}} object that has a chat stream.
'''accept_proposal'''(''self'', '''audio'''={{{False}}}, '''chat'''={{{False}}})::
When the remote party proposes to add a new stream, signaled by the {{{SIPSessionGotStreamProposal}}} notification, the application can use this method to accept the stream(s) being proposed.
After calling this method a {{{SIPSessionAcceptedStreamProposal}}} notification is sent, unless an error occurs while setting up the new stream, in which case a {{{SIPSessionRejectedStreamProposal}}} notification is sent and a rejection is sent to the remote party.
This method may only be called while in the {{{PROPOSED}}} state.
[[BR]]''audio'':[[BR]]
A boolean indicating whether an audio stream should be accepted for this proposal.
Note that this may only be set to {{{True}}} if an audio stream was actually proposed by the remote party.
[[BR]]''chat'':[[BR]]
A boolean indicating whether a chat stream should be accepted for this proposal.
Note that this may only be set to {{{True}}} if a chat stream was actually proposed by the remote party.
'''reject_proposal'''(''self'')::
When the remote party proposes (a) stream(s) that the application does not want to accept, this method can be used to reject the proposal, after which a {{{SIPSessionRejectedStreamProposal}}} notification is sent.
This method may only be called while in the {{{PROPOSED}}} state.
'''end'''(''self'')::
This method may be called any time when the {{{Session}}} object is active (i.e. not in the {{{NULL}}}, {{{TERMINATING}}} or {{{TERMINATED}}} states) in order to terminate the session.
Right before termination a {{{SIPSessionWillEnd}}} notification is sent, after termination {{{SIPSessionDidEnd}}} is sent.
==== notifications ====
'''SIPSessionChangedState'''::
Will be sent whenever the {{{Session}}} object changes its state.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''prev_state'':[[BR]]
The previous state state the object was in.
[[BR]]''state'':[[BR]]
The new state the object is in.
'''SIPSessionNewIncoming'''::
Will be sent when a new incoming {{{Session}}} is received.
The application should listen for this notification from all objects specifically to get informed of incoming sessions.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''streams'':[[BR]]
A list of strings indicating the streams that were proposed by the remote party.
'''SIPSessionNewOutgoing'''::
Will be sent when the applcation requests a new outgoing {{{Session}}}.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''streams'':[[BR]]
A list of strings indicating the streams that were proposed to the remote party.
'''SIPSessionGotRingIndication'''::
Will be sent when an outgoing {{{Session}}} receives an indication that a remote device is ringing.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionWillStart'''::
Will be sent just before a {{{Session}}} completes negotiation.
In terms of SIP, this is sent after the final response to the {{{INVITE}}}, but before the {{{ACK}}}.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionDidStart'''::
Will be sent when a {{{Session}}} completes negotiation.
In terms of SIP this is sent after the {{{ACK}}} was sent or received.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionDidFail'''::
This notification is sent whenever the session fails.
The failure reason is included in the data attributes.
This notification is always followed by {{{SIPSessionDidEnd}}}.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating the origin of the failure.
This will either be "local" or "remote".
[[BR]]''code'':[[BR]]
The SIP error code of the failure.
If this is 0, the error was an internal exception.
[[BR]]''reason'':[[BR]]
A string explaining the reason of the failure.
'''SIPSessionWillEnd'''::
Will be sent just before terminating a {{{Session}}} at the request of the application.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionDidEnd'''::
Will be sent always when a {{{Session}}} ends, either because of a failure (in which case it is preceded by {{{SIPSessionDidFail}}}), remote or local session termination.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating who originated the termination.
This will either be "local" or "remote".
'''SIPSessionGotHoldRequest'''::
Will be sent when the session got put on hold, either by the local or the remote party.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating who originated the hold request, and consequently in which direction the session got put on hold.
'''SIPSessionGotUnholdRequest'''::
Will be sent when the session got taken out of hold, either by the local or the remote party.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''originator'':[[BR]]
A string indicating who sent the original hold request, and consequently in which direction the session got taken out of hold.
'''SIPSessionWillStartRecordingAudio'''::
Will be sent when the application requested that the audio stream active within the session be record to a {{{.wav}}} file, just before recording starts.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionDidStartRecordingAudio'''::
Will be sent when the application requested that the audio stream active within the session be record to a {{{.wav}}} file, just after recording starts.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionWillStopRecordingAudio'''::
Will be sent when the application requested ending the recording to a {{{.wav}}} file, just before recording stops.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionDidStopRecordingAudio'''::
Will be sent when the application requested ending the recording to a {{{.wav}}} file, just before recording stops.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''file_name'':[[BR]]
The name of the recording {{{.wav}}} file, including full path.
'''SIPSessionGotNoAudio'''::
This notification will be sent if 5 seconds after the audio stream starts, no audio was received from the remote party.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
'''SIPSessionGotDTMF'''::
Will be send if there is a DMTF digit received from the remote party on the audio stream.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''digit'':[[BR]]
The DTMF digit that was received, in the form of a string of length 1.
'''SIPSessionGotMessage'''::
Will be sent whenever a MSRP message is received on the chat stream of the session.
[[BR]]''content'':[[BR]]
The body of the message.
[[BR]]''content_type'':[[BR]]
The Content-Type of the body.
[[BR]]''cpim_headers'':[[BR]]
A dictionary of headers included in the CPIM wrapper.
[[BR]]''message'':[[BR]]
Raw MSRP message, an msrplib.protocol.MSRPData instance
'''SIPSessionDidDeliverMessage'''::
Will be sent when a previously sent MSRP chat message got delivered to the remote party.
[[BR]]''message_id'':[[BR]]
The unique identifier of this message as a string, as previously returned by the {{{send_message()}}} method.
[[BR]]''code'':[[BR]]
The response code of the confirmation report.
[[BR]]''reason'':[[BR]]
The reason string of the confirmation report.
[[BR]]''message'':[[BR]]
Raw MSRP message, an msrplib.protocol.MSRPData instance
'''SIPSessionDidDeliverMessage'''::
Will be sent when a previously sent MSRP chat message did not get delivered to the remote party.
[[BR]]''message_id'':[[BR]]
The unique identifier of this message as a string, as previously returned by the {{{send_message()}}} method.
[[BR]]''code'':[[BR]]
The response code of the confirmation report.
[[BR]]''reason'':[[BR]]
The reason string of the confirmation report.
[[BR]]''message'':[[BR]]
Raw MSRP message, an msrplib.protocol.MSRPData instance
'''SIPSessionGotStreamProposal'''::
Will be sent when either the local or the remote party proposes to add a stream to the session.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''proposer'':[[BR]]
The party that did the stream proposal, can be either "local" or "remote".
[[BR]]''streams'':[[BR]]
A list of strings indicating the streams that were proposed.
'''SIPSessionRejectedStreamProposal'''::
Will be sent when either the local or the remote party rejects a proposal to have (a) stream(s) added to the session.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''proposer'':[[BR]]
The party that did the stream proposal, can be either "local" or "remote".
[[BR]]''reason'':[[BR]]
The reason for rejecting the stream proposal.
'''SIPSessionRejectedStreamProposal'''::
Will be sent when either the local or the remote party accepts a proposal to have (a) stream(s) added to the session.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''proposer'':[[BR]]
The party that did the stream proposal, can be either "local" or "remote".
'''SIPSessionGotStreamUpdate'''::
Will be sent when a media stream is either activated or deactivated.
An application should listen to this notification in order to know when a media stream can be used.
[[BR]]''timestamp'':[[BR]]
A {{{datetime.datetime}}} object indicating when the notification was sent.
[[BR]]''streams'':[[BR]]
A list indicating which streams are active on the session from this point onwards.
As an example for how to use the {{{Session}}} object, the following provides a basic Python program that initiates an outgoing SIP session request:
{{{
import sys
from threading import Event
from zope.interface import implements
from application.notification import IObserver, NotificationCenter
from sipsimple.configuration import ConfigurationManager
from sipsimple.account import AccountManager
from sipsimple.engine import Engine
from sipsimple.session import Session
from sipsimple.core import SIPURI, Route
class SimpleOutboundCall(object):
# indicate that we implement the application.notification.IObserver interface
implements(IObserver)
def __init__(self, remote_uri, route):
# setup a threading.Event to signal that the Engine has stopped
self.engine_ended_event = Event()
# start the configuration manager
ConfigurationManager().start()
# start the account manager
am = AccountManager()
am.start()
# start the Engine with configuration framework parameters
Engine().start_cfg()
# create a new Session using the default account
self.session = Session(am.default_account)
# listen for the notification that the Engine stopped
NotificationCenter().add_observer(self, "SIPEngineDidEnd", Engine())
# listen for the notification that the Session ended
NotificationCenter().add_observer(self, "SIPSessionDidEnd", self.session)
# start a new outbound session
self.session.connect(remote_uri, [route], audio=True)
def end(self):
# if the Session is still active, terminate it
self.session.end()
# wait for the engine to stop, processed in handle_notification
self.engine_ended_event.wait()
# quit the progam, as this can only be done from the main thread
sys.exit()
def handle_notification(self, notification):
if notification.name == "SIPSessionDidEnd":
# if for whatever reason the session ended, stop the Engine
print "Session ended"
Engine().stop()
elif notification.name == "SIPEngineDidEnd":
# once the Engine has stopped, signal the (possibly) waiting main
# thread through a threading.Event
self.engine_ended_event.set()
# place an audio call from the specified account to the specified URI, through
# the specified SIP proxy
# edit this to reflect real settings
call = SimpleOutboundCall(SIPURI(user="bob", host="example.com"), Route("1.2.3.4"))
# block waiting for user input
print "Placing call, press enter to quit program"
raw_input()
# block in end() until the Engine has stopped
call.end()
}}}
=== SessionManager ===
The {{{sipsimple.session.SessionManager}}} class is a singleton, which acts as the central aggregation point for sessions within the middleware.
Although it is mainly used internally, the application can use it to query information about all active sessions.
The SessionManager is implemented as a singleton, meaning that only one instance of this class exists within the middleware.
Note that, in order to be able to receive calls, the application has to instantiate this object.
==== attributes ====
'''sessions'''::
A property providing a copy of the list of all active {{{Sesssion}}} objects within the application, meaning any {{{Session}}} object that exists globally within the application and is not in the {{{NULL}}} or {{{TERMINATED}}} state.
==== methods ====
'''!__init!__'''(''self'')::
This either returns a new {{{SessionManager}}} object with default configuration objects, or returns a copy of the already existing instance.
=== Account ===
The {{{sipsimple.account.Account} objects represent the SIP accounts which are used by the middleware. It has a dual purpose: it acts as both a container for account-related settings and as a complex object which can be used to interact with various per-account functions, such as presence, registration etc. This page documents the latter case, while the former is explained in the [wiki:SipConfigurationAPI#Account Configuration API].
There is exactly one instance of {{{Account}}} per SIP account used and it is uniquely identifiable by its SIP ID, in the form ''user@domain''. It is a singleton, in the sense that instantiating {{{Account}}} using an already used SIP ID will return the same object. However, this is not the recommended way of accessing accounts, as this can lead to creation of new ones; the recommended way is by using the [wiki:SipMiddlewareApi#AccountManager AccountManager]. The next sections will use a lowercase, monospaced {{{account}}} to represent an instance of {{{Account}}}.
==== Account states ====
The {{{Account}}} objects have a setting flag called {{{enabled}}} which, if set to {{{False}}} will deactivate it: none of the internal functions will work in this case; in addition, the application using the middleware should not do anything with a disabled account. After changing it's value, the {{{save()}}} method needs to be called, as the flag is a setting and will not be used until this method is called:
{{{
account.enabled = True
account.save()
}}}
The other functions of {{{Account}}} which run automatically have other enabled flags as well. They will only be activated when both the global enabled flag is set and the function-specific one. These are:
'''Account.registration.enabled'''::
This flag controls the automatic registration of the account. The notifications '''SIPAccountRegistrationDidSucceed''', '''SIPAccountRegistrationDidFail''' and '''SIPAccountRegistrationDidEnd''' are used to inform the status of this registration.
'''Account.presence.enabled'''::
This flag controls the automatic subscription to buddies for the ''presence'' event and the publication of data in this event. (Not implemented yet)
'''Account.dialog_event.enabled'''::
This flag controls the automatic subscription to buddies for the ''dialog-info'' event and the publication of data in this event. (Not implemented yet)
'''Account.message_summary.enabled'''::
This flag controls the automatic subscription to the ''message-summary'' event in order to find out about voicemail messages. (Not implemented yet)
The {{{save()}}} method needs to be called after changing these flags in order for them to take effect.