| 147 | === Installing Your Callback to Account Object === |
| 148 | |
| 149 | Once you've created the custom callback class, you can install the callback by specifying your callback instance when creating the account, like the previous examples. Here they are again: |
| 150 | |
| 151 | {{{ |
| 152 | #!python |
| 153 | my_cb = MyAccountCallback() |
| 154 | acc = lib.create_account_for_transport(udp, cb=my_cb) |
| 155 | }}} |
| 156 | |
| 157 | or |
| 158 | |
| 159 | {{{ |
| 160 | #!python |
| 161 | acc_cfg = pjsua.AccountConfig("pjsip.org", "someuser", "secretpass") |
| 162 | my_cb = MyAccountCallback() |
| 163 | acc = lib.create_account(acc_cfg, cb=my_cb) |
| 164 | }}} |
| 165 | |
| 166 | ''(Note: we omit error handling for brevity)'' |
| 167 | |
| 168 | That is the preferred way to install the callback, i.e. to specify the callback instance when creating the account. However, if for some reason you cannot do this, you may install the callback later using Account's [[http://www.pjsip.org/python/pjsua.htm#Account-set_callback set_callback()] method. You SHOULD, however, protect the code fragment with library lock to avoid loosing events. A sample code to do this: |
| 169 | |
| 170 | {{{ |
| 171 | #!python |
| 172 | # Create account without callback. Protect the fragment with the |
| 173 | # library lock below. |
| 174 | lck = lib.auto_lock() |
| 175 | acc_cfg = pjsua.AccountConfig("pjsip.org", "someuser", "secretpass") |
| 176 | acc = lib.create_account(acc_cfg) |
| 177 | |
| 178 | # Do something else |
| 179 | ... |
| 180 | |
| 181 | # Create and install the callback |
| 182 | my_cb = MyAccountCallback() |
| 183 | acc.set_callback(my_cb) |
| 184 | |
| 185 | # Release library lock |
| 186 | del lck |
| 187 | }}} |
| 188 | |
| 189 | Without the library lock like above, it is possible that some events may be delivered to the account while the callback has not been installed yet, thus we're loosing the events. The lock will prevent that from happening (in this case), '''however''', please bear in mind that while you're holding the library lock, the worker thread can't run, so incoming SIP messages will be queued in socket buffer. So please use the library lock wisely. |
| 190 | |
| 191 | |