SIP Part (Landline, SIP, University SIP)¶
The Idea Behind¶
The idea behind the handler for the SIP is as follows:
The controller only fetch the information from database about what kind of test is going to be performed and send it to the SIP handler. After this point SIP handler takes control of everything. It looks more or less like this situation:
When CEO in a company wants to order new computer, he just contacts his secretary to get new computer. Then his secretary takes care of the rest e.g. calling financial manager, supplier etc. After his secretary finishes his job, she writes a report to CEO and CEO gets the report and his new computer.
SIP handler gets information about the task such as status state (Caller or Receiver), SIP ID and destination/caller number. After getting all this information, SIP handler tries to contact the SIP server to register itself on the system for making call or being incoming state. When SIP handler finishes doing its job, SIP handler sends back the result into the controller and after sending successfully, SIP handler is terminated.
Now no more direct connection to the database however we use controller to talk with database. We just receive the information massage from controller and send the result back to the controller when we finish with the task. How to SIP handler work will explain on the schema. We manage this handler can sign be SIP or Landline or University SIP event can sign be caller or receiver in every event, so no more other handler need to build. Only use one handler which can be everything controller need.
SIP Handler Schema¶
How SIP Handler works¶
The way of SIP handler work is really simple.
Controller wake-up SIP handler and given port number and which handler should be ( SIP/Landline/ University SIP) then SIP open his socket and wait 30 seconds message from controller. After got hello message from controller, SIP handler will send back hello respond to controller then controller will send state message which sign SIP handler to be caller/receiver. After initiated this message, SIP will trying register to the sip server. after he got authorisation from the SIP server, and next step SIP handler will send ready message to controller. Then controller will reply with Start Call/Receiver call and finally will report the result to controller.
SIP Use Case¶
Build and Installation
Getting the Modules
You can get the installation module from the official website from PJSUA [http://www.pjsip.org/download.htm]
The Python modules source codes along with some samples are included in PJSIP distribution, in pjproject/trunk/pjsip-apps/src/python p directory. No separate download is needed.
1. The Utilities Packets, e.g. make, gcc, and binutils.
2. Python: this normally should have been installed on every Linux.
3. Python development package, to build the C extension. This might be called python-devel (e.g. on Fedora) or python2.4-dev or python2.5-dev (e.g. on Ubuntu).
Building the Module
Using Python build script:
1. Build the PJSIP libraries first with the usual "./configure && make dep && make" commands.
2. Go to pjsip-apps/src/python directory.
3. Run "sudo python ./setup.py install"
$ cd your-pjsip-root-dir $ ./configure && make dep && make $ cd pjsip-apps/src/python $ sudo python ./setup.py install
if you found some error such as:
_pjsua.h:25:20: fatal error: Python.h: No such file or directory compilation terminated. error: command 'gcc' failed with exit status 1
as you need just to install python-dev which match with version of your python ex: python2.6-dev.
To check your python version:
python -- version
Testing the installation
To test the installation, simply run python and import ''pjsua'' module.
$ python > import pjsua > ^Z $
Assign process name for the thread
The library allows a process to change its title (as displayed by system tools such as ps and top). More information: http://pypi.python.org/pypi/setproctitle
Changing the title is mostly useful in multi-process systems, for example when a master process is forked: changing the children's title allows to identify the task each process is busy with. The technique is used by PostgreSQL and the OpenSSH Server for example. In our software we using kind of multi process systems since we use controller as parent and call handlers as child
You need to install "easy_install" first, using this command:
$ sudo apt-get install python-setuptools
You can use easy_install to install the module: to perform a system-wide installation use:
sudo easy_install setproctitle
The module exports the following functions:
Set title as the title for the current process.
Return the current process title.
How to use it?
To assign process name its really simple. This is the example how to use the library
import setproctile #set process name setproctile.setproctitle('SIP Handler') setproctile.getproctitle()
More information for this library can find here http://pypi.python.org/pypi/setproctitle
Creating Account or Register Account¶
Accounts provide identity (or identities) of the user who is currently using the application. An account has one SIP Uniform Resource Identifier (URI) associated with it. In SIP terms, the identity is used as the From header in outgoing requests.
Account may or may not have client registration associated with it. An account is also associated with route set and some authentication credentials, which are used when sending SIP request messages using the account. An account also has presence online status, which will be reported to remote peer when they subscribe to the account's presence, or which is published to a presence server if presence publication is enabled for the account.
At least one account MUST be created in the application, since any outgoing requests require an account context. If no user association is required, application can create a user-less account by calling lib.create_account_for_transport(). A userless account identifies local endpoint instead of a particular user, and it corresponds to a particular transport instance.
Also one account must be set as the default account, which will be used as the account identity when pjsua fails to match the request with any accounts using the stricter matching rules.
To register account to the SIP server, we just need little script:
acc_cfg = pjsua.AccountConfig("SIPServer", "someuser", "secretpass") my_cb = MyAccountCallback() acc = lib.create_account(acc_cfg, cb=my_cb)
Class on SIP¶
This class use for socket communication between handler and controller. More information from this class, click Here
class MyCallCallback(pj.CallCallback): def on_state(self): global current_call if self.call.info().state <> pj.CallState.DISCONNECTED: if self.call.info().state_text == "CONNECTING": print "CONNECTING CALL" print "CALL CONFIRMED and ESTABLISH", self.call.info().state_text sleep(1) current_call.hangup() server.sendData('200') else: sleep(0.5) if self.call.info().last_reason <> "": if self.call.info().last_reason == "Busy Here": server.sendData('486') if self.call.info().state == pj.CallState.DISCONNECTED: current_call = None
Class MyCallCallback class which handler event from call which trigger by class library pjsua Callcallback. When library of PJSUA started and the registration account successfully, every event from call will trigger MyCallCallback class and return information about the event such as ringing message, disconnected message or connecting message. After identify kind of event, this class will automatically process every event from call and send result back to the controller.