libosip
5.3.0
|
The interesting and somewhat complex feature implemented by osip is the 4 states machines that applied to the different transactions defined by the SIP rfc.
SIP defines the following 4 state machines, abreviations used in osip are provided below:
As you can notice if you have read the rfc (do it!), those 4 state machines are provided as drawings within the SIP rfc3261.txt (section 17.1 and 17.2)
As an exemple of what you'll find in the rfc3261, here is the drawing that apply to the "Invite Client Transaction" (page 127)
|INVITE from TU Timer A fires |INVITE sent Reset A, V Timer B fires INVITE sent +-----------+ or Transport Err. +---------| |---------------+inform TU | | Calling | | +-------->| |-------------->| +-----------+ 2xx | | | 2xx to TU | | |1xx | 300-699 +---------------+ |1xx to TU | ACK sent | | | resp. to TU | 1xx V | | 1xx to TU -----------+ | | +---------| | | | | |Proceeding |-------------->| | +-------->| | 2xx | | +-----------+ 2xx to TU | | 300-699 | | | ACK sent, | | | resp. to TU| | | | | NOTE: | 300-699 V | | ACK sent +-----------+Transport Err. | transitions | +---------| |Inform TU | labeled with | | | Completed |-------------->| the event | +-------->| | | over the action | +-----------+ | to take | ^ | | | | | Timer D fires | +--------------+ | - | | | V | +-----------+ | | | | | Terminated|<--------------+ | | +-----------+ Figure 5: INVITE client transaction
As you can expect, with osip an Invite Client Transaction may be in the CALLING, PROCEEDING, COMPLETED or TERMINATED state. To "execute" the state machine, you will build events, provide them to the correct transaction context and the the state of the transaction will be updated if the event is allowed in the current state.
Events are divided in three categories:
Let's assume you want to implement a User Agent and you want to start a REGISTER transaction. Using the parser library, you will first have to build a SIP compliant message. (oSIP, as a low layer library provides an interface to build SIP messages, but it's up to you to correctly fill all the required fields.)
As soon as you have build the SIP message, you are ready to start a new transaction. Here is the code:
Adding new events in the fsm is made with similar code.
The previous step show how to create a transaction and one possible way to add a new event. (Note, that some events -the TIMEOUT_* ones- will be added by oSIP not by the application). In this step, we describe how the oSIP stack will consume events. In fact, this is very simple, but you should be aware that it's not always allowed to consume an event at any time! The fsm MUST consume events sequentially within a transaction. This means that when your are calling osip_transaction_execute(), it is forbidden to call this method again with the same transaction context until the first call has returned. In a multi threaded application, if one thread handles one transaction, the code will be the following:
Looking at the case of a usual outgoing REGISTER transaction, this behaviour is expected.
When an event is seen as useful for the fsm, it means that a transition from one state to another has to be done on the transaction context. If the event is SND_REQUEST (this is the case for an outgoing REGISTER), the callback previously registered to announce this action will be called. This callback is useless for the application as no action has to be taken at this step. A more interesting announcement will be made when consuming the first final response received. If the callbacks associated to 2xx message is called, then the transaction has succeeded. Inside this callback, you will probably inform the user of the success of the registration if you want to do so...
If the final response is not a 2xx, or the network callback is called, you'll probably want to take some actions. For example, if you receive a 302, you'll probably want to retry a registration at the new location. All that decision is up to you.
When the transaction reach the TERMINATED state (when the kill callback is called, you must remove it from the list of known transactions with