Package xmpp :: Module session :: Class Session

[hide private]
[frames] | no frames]

# Class Session

source code
The Session class instance is used for storing all session-related info like credentials, socket/xml stream/session state flags, roster items (in case of client type connection) etc. Session object have no means of discovering is any info is ready to be read. Instead you should use poll() (recomended) or select() methods for this purpose. Session can be one of two types: 'server' and 'client'. 'server' session handles inbound connection and 'client' one used to create an outbound one. Session instance have multitude of internal attributes. The most imporant is the 'peer' one. It is set once the peer is authenticated (client).

Instance Methods | [hide private]

__init__(self, socket, owner, xmlns=None, peer=None) When the session is created it's type (client/server) is determined from the beginning. | source code

StartStream(self) This method is used to initialise the internal xml expat parser and to send initial stream header (in case of client connection). | source code

receive(self) Reads all pending incoming data. | source code

sendnow(self, chunk) Put chunk into "immidiatedly send" queue. | source code

enqueue(self, stanza) Takes Protocol instance as argument. | source code

push_queue(self, failreason=`'``urn:ietf:params:xml:ns:xmpp-stanzas recipient-unavailable``'`) If stream is authenticated than move items from "send" queue to "immidiatedly send" queue. | source code

flush_queue(self) Put the "immidiatedly send" queue content on the wire. | source code

_dispatch(self, stanza, trusted=0) This is callback that is used to pass the received stanza forth to owner's dispatcher _if_ the stream is authorised. | source code

_catch_stream_id(self, ns=None, tag=`'``stream``'`, attrs=`{``}`) This callback is used to detect the stream namespace of incoming stream. | source code

_stream_open(self, ns=None, tag=`'``stream``'`, attrs=`{``}`) This callback is used to handle opening stream tag of the incoming stream. | source code

feature(self, feature) Declare some stream feature as activated one. | source code

unfeature(self, feature) Declare some feature as illegal. | source code

_stream_close(self, unregister=1) Write the closing stream tag and destroy the underlaying socket. | source code

terminate_stream(self, error=None, unregister=1) Notify the peer about stream closure. | source code

_destroy_socket(self) Break cyclic dependancies to let python's GC free memory right now. | source code

start_feature(self, f) Declare some feature as "negotiating now" to prevent other features from start negotiating. | source code

stop_feature(self, f) Declare some feature as "negotiated" to allow other features start negotiating. | source code

set_socket_state(self, newstate) Change the underlaying socket state. | source code

set_session_state(self, newstate) Change the session state. | source code

set_stream_state(self, newstate) Change the underlaying XML stream state Stream starts with STREAM__NOT_OPENED and then proceeds with STREAM__OPENED, STREAM__CLOSING and STREAM__CLOSED states. | source code

Method Details | [hide private]

### __init__(self, socket, owner, xmlns=None, peer=None)
(Constructor)

source code

When the session is created it's type (client/server) is determined from the beginning. socket argument is the pre-created socket-like object. It must have the following methods: send, recv, fileno, close. owner is the 'master' instance that have Dispatcher plugged into it and generally will take care about all session events. xmlns is the stream namespace that will be used. Client must set this argument If server sets this argument than stream will be dropped if opened with some another namespace. peer is the name of peer instance. This is the flag that differentiates client session from server session. Client must set it to the name of the server that will be connected, server must leave this argument alone.

### StartStream(self)

source code

This method is used to initialise the internal xml expat parser and to send initial stream header (in case of client connection). Should be used after initial connection and after every stream restart.

### receive(self)

source code

Reads all pending incoming data. Raises IOError on disconnection. Blocks until at least one byte is read.

### sendnow(self, chunk)

source code

Put chunk into "immidiatedly send" queue. Should only be used for auth/TLS stuff and like. If you just want to shedule regular stanza for delivery use enqueue method.

### enqueue(self, stanza)

source code

Takes Protocol instance as argument. Puts stanza into "send" fifo queue. Items into the send queue are hold until stream authenticated. After that this method is effectively the same as "sendnow" method.

### push_queue(self, failreason=`'``urn:ietf:params:xml:ns:xmpp-stanzas recipient-unavailable``'`)

source code

If stream is authenticated than move items from "send" queue to "immidiatedly send" queue. Else if the stream is failed then return all queued stanzas with error passed as argument. Otherwise do nothing.

### flush_queue(self)

source code

Put the "immidiatedly send" queue content on the wire. Blocks until at least one byte sent.

### _dispatch(self, stanza, trusted=0)

source code

This is callback that is used to pass the received stanza forth to owner's dispatcher _if_ the stream is authorised. Otherwise the stanza is just dropped. The 'trusted' argument is used to emulate stanza receive. This method is used internally.

### _catch_stream_id(self, ns=None, tag=`'``stream``'`, attrs=`{``}`)

source code

This callback is used to detect the stream namespace of incoming stream. Used internally.

### _stream_open(self, ns=None, tag=`'``stream``'`, attrs=`{``}`)

source code

This callback is used to handle opening stream tag of the incoming stream. In the case of client session it just make some validation. Server session also sends server headers and if the stream valid the features node. Used internally.

### unfeature(self, feature)

source code

Declare some feature as illegal. Illegal features can not be used. Example: BIND feature becomes illegal after Non-SASL auth.

### _stream_close(self, unregister=1)

source code

Write the closing stream tag and destroy the underlaying socket. Used internally.

### terminate_stream(self, error=None, unregister=1)

source code

Notify the peer about stream closure. Ensure that xmlstream is not brokes - i.e. if the stream isn't opened yet - open it before closure. If the error condition is specified than create a stream error and send it along with closing stream tag. Emulate receiving 'unavailable' type presence just before stream closure.

### set_socket_state(self, newstate)

source code

Change the underlaying socket state. Socket starts with SOCKET_UNCONNECTED state and then proceeds (possibly) to SOCKET_ALIVE and then to SOCKET_DEAD

### set_session_state(self, newstate)

source code

Change the session state. Session starts with SESSION_NOT_AUTHED state and then comes through SESSION_AUTHED, SESSION_BOUND, SESSION_OPENED and SESSION_CLOSED states.

### set_stream_state(self, newstate)

source code

Change the underlaying XML stream state Stream starts with STREAM__NOT_OPENED and then proceeds with STREAM__OPENED, STREAM__CLOSING and STREAM__CLOSED states. Note that some features (like TLS and SASL) requires stream re-start so this state can have non-linear changes.