summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/Backup/_OriglandlineCall.py
blob: 9cec7e36a84e74d7aeb858fac2c55ebe5490b7ae (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sys
import pjsua as pj
from time import sleep
import string

server = "sipgate.de"
username = "1289459"
password = "MMW9AX"
number = ""

LOG_LEVEL = 2
current_call = None
is486 = False
isConfirmed = False
isRinging = False
isConnected = False
isStopped = False
firstTime502 = True
firstTime100 = True
TIMEOUT_LIMIT = 30

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

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

    def __init__(self, account=None):
        pj.AccountCallback.__init__(self, account)
        
# 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 isConfirmed
	global isConnected
	global isRinging
	global is486
	
	if self.call.info().state == pj.CallState.CALLING:
		print "CALLING..."
	
	if self.call.info().last_code == 486:
		is486 = True

	if self.call.info().state == pj.CallState.EARLY:
		isRinging = True
			
	if self.call.info().state == pj.CallState.CONFIRMED:
		isConfirmed = True
		isConnected = True
		isRinging = False

       	if self.call.info().state == pj.CallState.DISCONNECTED:      		
		current_call = None		
		isConnected = False
		

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

print "Whenever something comes from the server (at the moment from you), it will continue. Imitating the server behaviour!"

while not isStopped:

	lib = pj.Lib()
	lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
	transport = lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(0))
	lib.start()
	lib.set_null_snd_dev()
	
#	server = classServer.ServerHandler(50099) 
#	tried = server.openSocket()
#
#	if server.error != 'No error':
#		print server.error
#		if server.error == '[Errno 98] Address already in use':
#			print "Trying to kill the port"
#			print server.killPort()
#			server.closeConection()

	acc_cfg = pj.AccountConfig(server,username,password)	
	acc = lib.create_account(acc_cfg, cb=MyAccountCallback())
	

	if acc.info().reg_status == 502:
		if firstTime502: 		
			print "CONNECTION ERROR!!"
			print "Please check your internet connection!\n"
			firstTime502 = False
		
		acc.delete()
		lib.destroy()
		lib = None
		acc = None
#		server.sendData('NOT ready')
		sleep(2)
		continue
	
	
	while acc.info().reg_status == 100:
		if firstTime100:
			print "Trying to register."
			firstTime100 = False


	while acc.info().reg_status == 408:
		print "408: REGISTRATION FAILED DUE TO TIMEOUT!!"
		print "Check your internet connection and SIP settings!"
#		server.sendData('NOT ready')


	if acc.info().reg_status == 200:			
		print ("REGISTRATION IS SUCCESSFUL") #server.sendData('ready')		
			
		while 1:
#			try:	
				print "Waiting for server(at the moment YOU!) to send command here:" #This line should be deleted further!	
				startStop = sys.stdin.readline().rstrip("\r\n")                      #startStop = server.receiveData()
				
				if startStop == "start":
					timeCounterRinging = 0
					firstTimeRinging = True							
						
					while 1:
						#number = server.receiveData()
						print "Please enter the number to call:"
						number = sys.stdin.readline().rstrip("\r\n")
						if number <> "":
							numberToCall = "sip:"+number+"@"+server					
							current_call = make_call(numberToCall) 
		
							while 1:
								if isRinging:
									sleep(1)
									timeCounterRinging = timeCounterRinging + 1
									if firstTimeRinging:					
										print "RINGING..."
										firstTimeRinging = False				
								if is486:				
									print "THE NUMBER THAT YOU'VE DIALLED IS BUSY!!"			
									isConfirmed = False
									isConnected = False
									isRinging = False
									is486 = False
									break			
							
								if isConnected and isConfirmed:
									print "CALL SUCCESSFULLY ESTABLISHED!!"				
									sleep(5)				
									current_call.hangup()			
									print "CALL SUCCESSFULLY TERMINATED!!"
									isConfirmed = False
									isConnected = False
									isRinging = False
									is486 = False
									break
								
								if timeCounterRinging == TIMEOUT_LIMIT and isRinging:
									current_call.hangup()
									print "THE NUMBER THAT YOU'VE DIALLED IS NOT ANSWERING!!"
									isConfirmed = False
									isConnected = False
									isRinging = False
									is486 = False
									break
							break
			
				if startStop == "stop":
					isStopped = True
					break
			
#			except ValueError:
#					print "813 General socket layer error: Failed send message"
#					server.closeConnection()
#					del server

#		server.closeConnection()
#		del server 

#	server.closeConnection()
#	del server 

sleep(2)
print "LOGGING OUT..."
acc.delete()
lib.destroy()
lib = None
acc = None
print "LOGOUT WAS SUCCESSFULL!!"