summaryrefslogtreecommitdiffstats
path: root/Handler/SIPCall.py
blob: 45fdca23ba100b7ddbc99450eb77c8dcec04c087 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import sys
import pjsua as pj
import socket
import time
import MySQLdb
import subprocess
from time import sleep
from datetime import datetime
import string



HOST = None
PORT = 50099
s = None

stop ="false"
server = "132.230.4.8"
username = "mpselftest1"
password = "1mpselftest"

LOG_LEVEL=2
current_call = None
success = ""




# Logging callback
def log_cb(level, str, len):
    print str, "SIP log"


# Callback to receive events from account
class MyAccountCallback(pj.AccountCallback):

    def __init__(self, account=None):
        pj.AccountCallback.__init__(self, account)

    # Notification on incoming call##


        
# Callback to receive events from Call
class MyCallCallback(pj.CallCallback):
   
    def __init__(self, call=None):
        pj.CallCallback.__init__(self, call)

    # Notification when call state has changed
    def on_state(self):
        global current_call
	global success

	if self.call.info().state_text <> "DISCONNCTD":
		if self.call.info().state_text == "CONNECTING":
			print "CONNECTING CALL"
			#print "isine opo:", self.call.info().state_text
			print "CALL CONFIRMED and ESTABLISH", self.call.info().state_text
			sleep(5)
			current_call.hangup()
			success = "true"
			connection.send(success)
		else:
			sleep(1)
	#print ""

	if self.call.info().last_reason <> "":
		
		if self.call.info().last_reason == "Busy Here":
			
			print "Destination Number is Busy or offline"
			print ""
			success = "false"
			connection.send(success)
			

		#print self.call.info().state_text
        
        if self.call.info().state == pj.CallState.DISCONNECTED:
            current_call = None
            print 'Current call Disconnected'


def make_call(uri):
    try:
        print "Making call to", uri
	cb=MyCallCallback()
        return acc.make_call(uri, cb)
    except pj.Error, e:
        print "Exception: " + str(e)
        return None
        



lib = pj.Lib()
while stop <> "true":


	for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    		af, socktype, proto, canonname, sa = res
    		try:
        		s = socket.socket(af, socktype, proto)
    		except socket.error, msg:
        		s = None
        		continue

		try:
		        s.bind(sa)
		        s.listen(1)
		except socket.error, msg:
			s.close()
		        s = None
		        continue
		break
	
	if s is None:
		print 'could not open socket'
		sys.exit(1)


	lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
	transport = lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(0))

	print "\nListening on", transport.info().host, 
	print "port", transport.info().port, "\n"
	lib.start()
	lib.set_null_snd_dev()	

	try:
		acc_cfg = pj.AccountConfig(server, username, password)	
		acc = lib.create_account(acc_cfg, cb=MyAccountCallback())
			
		if acc.info().reg_status < 700:
			
			my_sip_uri = "sip:" + transport.info().host + ":" + str(transport.info().port)
			
			connection, addr = s.accept()
			print 'Connected by', addr
			connection.send('I am ready')
			
			while 1:
				try:
					data = connection.recv(1024)
					
					if data == "start":
						
						while 1:

							num = connection.recv(1024)
							if num <> "":
								
								number = "sip:"+num+"@132.230.4.8"
								current_call = make_call(number)
								break

					if data == "terminated":
						stop = "true"
						break
	
				except ValueError:
					print "error when sending message"
					connection.close()

			connection.close()
			
					
		else:
			Regis_status= "Bad"
			print "error when register"

			lib.destroy()
			lib = None
    			acc = None


		connection.close()	
	except ValueError:    
		print "Exception: " + str(e)

print "Goodbye"
sleep(3)
acc.delete()
lib.destroy()
connection.close()
lib = None
acc = None