2
# -*- coding: utf-8 -*-
6
# copyright: Savoir-Faire Linux (2013) #\n\
7
# author: Patrick keroulas <patrick.keroulas@savoirfairelinux.com> #\n\
8
# description: This script sends a sequence of methods to the daemon #\n\
9
# through the dbus to test the presence feature of SFLPhone. #\n\
10
# SET THE PARAMS IS THE 'data' SECTION OF THIS SCRIPT BEFORE #\n\
11
# YOU EXECUTE IT. 'The normal mode process the tasks #\n\
12
# in order while the random mode generates a random sequence #\n\
13
# A Freeswitch server must be setup since it supports PUBLISH #\n\
14
# requests and Asterisk doesn't. #\n\
15
# The user must have 2 valid accounts. This script will #\n\
16
# use the first account in the list and the IP2IP account. #\n\
17
# This is a self subscribe test, set another buddy IP if needed#\n\
22
import time, sys, gobject
23
from random import randint
24
import dbus, dbus.mainloop.glib
25
import logging, commands
28
#----------------- logger to file and stdout ------------------------------
29
f = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
30
logfile = 'sflphone_doombot.log'
31
logging.basicConfig(filename=logfile,level=logging.INFO,format=f) # log to file
32
logger = logging.getLogger()
33
ch = logging.StreamHandler(sys.stdout) #log to console
34
ch.setLevel(logging.INFO)
35
formatter = logging.Formatter(f)
36
ch.setFormatter(formatter)
39
#------------------ Initialise DBUS ------------------------------
40
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
41
bus = dbus.SessionBus()
42
presenceManagerBus = bus.get_object('org.sflphone.SFLphone', '/org/sflphone/SFLphone/PresenceManager')
43
presenceManager = dbus.Interface(presenceManagerBus, dbus_interface='org.sflphone.SFLphone.PresenceManager')
44
configurationManagerBus = bus.get_object('org.sflphone.SFLphone', '/org/sflphone/SFLphone/ConfigurationManager')
45
configurationManager = dbus.Interface(configurationManagerBus, dbus_interface='org.sflphone.SFLphone.ConfigurationManager')
48
#------------------------- General purpose functions ----------------------------
50
#Get the first non-IP2IP account
51
def get_first_account():
52
accounts = configurationManager.getAccountList()
53
for i, v in enumerate(accounts):
55
details = configurationManager.getAccountDetails(v)
56
if details["Account.type"] == True or details["Account.type"] == "SIP":
60
def get_account_list():
61
accounts = configurationManager.getAccountList()
63
for i, v in enumerate(accounts):
65
details = configurationManager.getAccountDetails(v)
66
if details["Account.type"] == True or details["Account.type"] == "SIP":
70
def registerSend(arg):
71
configurationManager.sendRegister(arg['acc'],arg['enable'])
72
logging.info('> REGISTER : '+ str(arg))
74
#------------------------- Presence functions ----------------------------
76
def presSubscribe(arg):
77
presenceManager.subscribeBuddy(arg['acc'],arg['buddy'],arg['flag'])
78
logging.info('> SUBSCRIBE to ' + str(arg))
81
presenceManager.publish(arg['acc'],arg['status'],arg['note'])
82
logging.info('> PUBLISH ' + str(arg))
84
def presSubApprove(arg):
85
presenceManager.answerServerRequest(arg['uri'],arg['flag'])
86
logging.info('> APPROVE subscription from' + str(arg))
88
def newPresSubCientNotificationHandler(acc, uri, status, activity):
89
logging.info("< SIGNAL : Notification for acc:"+str(acc)+", from:"+str(uri)+" (status:" + str(status)+ ", "+ str(activity)+ ").")
91
def newPresSubServerRequestHandler(uri):
92
logging.info("< SIGNAL : PresenceSubscription request from " +str(uri))
95
def subcriptionStateChangedHandler(acc,uri,flag):
96
logging.info("< SIGNAL : new subscriptionState request for acc:"+str(acc)+" uri " +str(uri) + " flag:" + str(flag))
98
def serverErrorHandler(acc, error, msg):
99
logging.info("< SIGNAL : error from server:"+str(error)+" . "+str(msg))
102
return bool(randint(0,1))
104
#--------------------------- Data -------------------------------
106
TEST_DURATION = 30 # in sec
107
acc_1 = get_account_list()[0]
108
print 'acc_1 : ' + str(acc_1)
109
acc_2 = get_account_list()[1]
110
print 'acc_2 : ' + str(acc_2)
112
server_ip = '192.95.9.63' # asterisk test server
115
host_ip = '192.168.50.196'
116
host_uri = '<sip:'+host_user+'@'+server_ip+'>'
118
buddy_uri_1 = '<sip:6001@'+server_ip+'>'
119
buddy_uri_2 = '<sip:6002@'+server_ip+'>'
120
buddy_ip = host_ip # self subscribing
121
buddy_ip_uri = '<sip:'+buddy_ip+'>' # IP2IP
124
#---------------------------- Sequence ----------------------------
132
sequence_mode = SEQ_MODE_NORMAL
138
(registerSend,{'acc':acc_1, 'enable':True}),
139
(presSubscribe, {'acc':acc_1,'buddy':buddy_uri_1,'flag':True}),
141
(registerSend,{'acc':acc_2, 'enable':True}),
142
(presSubscribe, {'acc':acc_2,'buddy':buddy_uri_2,'flag':True}),
144
(presSend, {'acc':acc_2,'status':randbool(),'note':buddy_uri_1+'is here!'}),
145
(presSend, {'acc':acc_1,'status':randbool(),'note':buddy_uri_2+'is here'}),
147
(presSubscribe, {'acc':acc_1,'buddy':'<sip:6003@192.95.9.63>','flag':True}),
149
(registerSend,{'acc':acc_1, 'enable':False}),
150
(presSubscribe, {'acc':acc_2,'buddy':buddy_uri_2,'flag':False}),
152
(registerSend,{'acc':acc_2, 'enable':False}),
153
(presSubscribe, {'acc':acc_1,'buddy':buddy_uri_1,'flag':False}),
161
(presSubscribe, {'acc':acc_2,'buddy':buddy_uri_2,'flag':True}),
162
(presSubscribe, {'acc':acc_2,'buddy':'<sip:6003@192.95.9.63>','flag':True}),
169
(presSubscribe, {'acc': IP2IP,'buddy':buddy_ip_uri,'flag':True}),
170
(presSend, {'acc': IP2IP,'status':randbool(),'note':'This notify should not be recieved'}),
171
(presSubApprove, {'uri':subscriber_uri,'flag':randbool()}),
172
(presSend, {'acc': IP2IP,'status':randbool(),'note':'Oh yeah!'}),
173
(presSubscribe, {'acc': IP2IP,'buddy':buddy_ip_uri,'flag':False}),
180
if sequence_mode == SEQ_MODE_NORMAL:
182
task_index = task_count%task_N
184
if task_count == task_N+1: # one loop
187
elif sequence_mode == SEQ_MODE_RANDOM:
188
task_index = randint(0,task_N-1)
190
task_list[task_index][0](task_list[task_index][1])
192
if(int(time.time()-start_time) < TEST_DURATION):
193
gobject.timeout_add(2000, run) # const time step in ms
194
#gobject.timeout_add(randint(50,2000), run) # random time step in ms
196
logging.info("Test sequence finished")
197
# TODO clear dbus sessio. Unfortunately stackoverflow.com is down today
200
if __name__ == '__main__':
203
# dbus signal monitor
204
presenceManagerBus.connect_to_signal("newBuddyNotification", newPresSubCientNotificationHandler, dbus_interface='org.sflphone.SFLphone.PresenceManager')
205
presenceManagerBus.connect_to_signal("newServerSubscriptionRequest", newPresSubServerRequestHandler, dbus_interface='org.sflphone.SFLphone.PresenceManager')
206
presenceManagerBus.connect_to_signal("subcriptionStateChanged", subcriptionStateChangedHandler, dbus_interface='org.sflphone.SFLphone.PresenceManager')
207
presenceManagerBus.connect_to_signal("serverError", serverErrorHandler, dbus_interface='org.sflphone.SFLphone.PresenceManager')
209
start_time = time.time()
210
task_N = len(task_list)
211
#sequence_mode = SEQ_MODE_RANDOM
215
except Exception as e:
218
loop = gobject.MainLoop()