-
Notifications
You must be signed in to change notification settings - Fork 13
/
sipsimple_hello_world2.py
126 lines (93 loc) · 3.45 KB
/
sipsimple_hello_world2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
from cmd import Cmd
from application.notification import NotificationCenter, IObserver
from application.python import Null
from threading import Event
from zope.interface import implements
from sipsimple.account import AccountManager
from sipsimple.application import SIPApplication
from sipsimple.storage import FileStorage
from sipsimple.core import ToHeader, SIPURI
from sipsimple.lookup import DNSLookup, DNSLookupError
from sipsimple.session import Session
from sipsimple.streams import AudioStream
from sipsimple.threading.green import run_in_green_thread
class TestApplication(object):
implements(IObserver)
def __init__(self):
self.application = SIPApplication()
self.quit_event = Event()
notification_center = NotificationCenter()
notification_center.add_observer(self, sender=self.application)
def start(self):
self.application.start(FileStorage(os.path.realpath('test-config')))
def stop(self):
self.application.stop()
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification)
def _NH_SIPApplicationDidStart(self, notification):
print 'SIP application started'
def _NH_SIPApplicationDidEnd(self, notification):
self.quit_event.set()
class OutgoingCallHandler(object):
implements(IObserver)
def __init__(self):
self.session = None
@run_in_green_thread
def call(self, destination):
if self.session is not None:
print 'Another session is in progress'
return
callee = ToHeader(SIPURI.parse(destination))
try:
routes = DNSLookup().lookup_sip_proxy(callee.uri, ['udp']).wait()
except DNSLookupError, e:
print 'DNS lookup failed: %s' % str(e)
else:
account = AccountManager().default_account
self.session = Session(account)
NotificationCenter().add_observer(self, sender=self.session)
self.session.connect(callee, routes, [AudioStream()])
def hangup(self):
if self.session is None:
print 'There is no session to hangup'
return
self.session.end()
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification)
def _NH_SIPSessionGotRingIndication(self, notification):
print 'Ringing!'
def _NH_SIPSessionDidStart(self, notification):
print 'Session started!'
def _NH_SIPSessionDidFail(self, notification):
print 'Failed to connect'
self.session = None
NotificationCenter().remove_observer(self, sender=notification.sender)
def _NH_SIPSessionDidEnd(self, notification):
print 'Session ended'
self.session = None
NotificationCenter().remove_observer(self, sender=notification.sender)
application = TestApplication()
application.start()
call_handler = OutgoingCallHandler()
class Stop(object):
pass
class Interface(Cmd):
prompt = 'cli> '
def emptyline(self):
return
def do_call(self, line):
call_handler.call(line)
def do_hangup(self, line):
call_handler.hangup()
def do_exit(self, line):
application.stop()
application.quit_event.wait()
return Stop
def postcmd(self, stop, line):
if stop is Stop:
return True
interface = Interface()
interface.cmdloop()