~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/test/scripts/presence_test.py

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
print "\
 
4
#                               --SFLPhone--                                #\n\
 
5
#                                                                           #\n\
 
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\
 
18
#\n"
 
19
 
 
20
 
 
21
 
 
22
import time, sys, gobject
 
23
from random import randint
 
24
import dbus, dbus.mainloop.glib
 
25
import logging, commands
 
26
 
 
27
 
 
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)
 
37
logger.addHandler(ch)
 
38
 
 
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')
 
46
 
 
47
 
 
48
#-------------------------    General purpose functions     ----------------------------
 
49
 
 
50
#Get the first non-IP2IP account
 
51
def get_first_account():
 
52
    accounts = configurationManager.getAccountList()
 
53
    for i, v in enumerate(accounts):
 
54
        if v != "IP2IP":
 
55
            details = configurationManager.getAccountDetails(v)
 
56
            if details["Account.type"] == True or details["Account.type"] == "SIP":
 
57
                return v
 
58
    return "IP2IP"
 
59
 
 
60
def get_account_list():
 
61
    accounts = configurationManager.getAccountList()
 
62
    result = []
 
63
    for i, v in enumerate(accounts):
 
64
        if v != "IP2IP":
 
65
            details = configurationManager.getAccountDetails(v)
 
66
            if details["Account.type"] == True or details["Account.type"] == "SIP":
 
67
                result.append(v)
 
68
    return result
 
69
 
 
70
def registerSend(arg):
 
71
    configurationManager.sendRegister(arg['acc'],arg['enable'])
 
72
    logging.info('> REGISTER : '+ str(arg))
 
73
 
 
74
#-------------------------   Presence functions      ----------------------------
 
75
 
 
76
def presSubscribe(arg):
 
77
    presenceManager.subscribeBuddy(arg['acc'],arg['buddy'],arg['flag'])
 
78
    logging.info('> SUBSCRIBE to ' + str(arg))
 
79
 
 
80
def presSend(arg):
 
81
    presenceManager.publish(arg['acc'],arg['status'],arg['note'])
 
82
    logging.info('> PUBLISH ' + str(arg))
 
83
 
 
84
def presSubApprove(arg):
 
85
    presenceManager.answerServerRequest(arg['uri'],arg['flag'])
 
86
    logging.info('> APPROVE subscription from' + str(arg))
 
87
 
 
88
def newPresSubCientNotificationHandler(acc, uri, status, activity):
 
89
    logging.info("< SIGNAL : Notification for  acc:"+str(acc)+", from:"+str(uri)+" (status:" + str(status)+ ", "+ str(activity)+ ").")
 
90
 
 
91
def newPresSubServerRequestHandler(uri):
 
92
    logging.info("< SIGNAL : PresenceSubscription request from " +str(uri))
 
93
    subscriber_uri = uri
 
94
 
 
95
def subcriptionStateChangedHandler(acc,uri,flag):
 
96
    logging.info("< SIGNAL : new subscriptionState request for acc:"+str(acc)+" uri " +str(uri) + " flag:" + str(flag))
 
97
 
 
98
def serverErrorHandler(acc, error, msg):
 
99
    logging.info("< SIGNAL : error from server:"+str(error)+" . "+str(msg))
 
100
 
 
101
def randbool():
 
102
    return bool(randint(0,1))
 
103
 
 
104
#---------------------------     Data           -------------------------------
 
105
 
 
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)
 
111
IP2IP = 'IP2IP'
 
112
server_ip = '192.95.9.63' # asterisk test server
 
113
 
 
114
host_user = '6001'
 
115
host_ip = '192.168.50.196'
 
116
host_uri = '<sip:'+host_user+'@'+server_ip+'>'
 
117
 
 
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
 
122
subscriber_uri = ''
 
123
 
 
124
#----------------------------    Sequence        ----------------------------
 
125
 
 
126
start_time = 0
 
127
task_count = 0
 
128
task_N = 0
 
129
 
 
130
SEQ_MODE_NORMAL = 0
 
131
SEQ_MODE_RANDOM = 1
 
132
sequence_mode = SEQ_MODE_NORMAL
 
133
 
 
134
 
 
135
# regular test
 
136
task_list = [
 
137
 
 
138
    (registerSend,{'acc':acc_1, 'enable':True}),
 
139
    (presSubscribe, {'acc':acc_1,'buddy':buddy_uri_1,'flag':True}),
 
140
 
 
141
    (registerSend,{'acc':acc_2, 'enable':True}),
 
142
    (presSubscribe, {'acc':acc_2,'buddy':buddy_uri_2,'flag':True}),
 
143
 
 
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'}),
 
146
 
 
147
    (presSubscribe, {'acc':acc_1,'buddy':'<sip:6003@192.95.9.63>','flag':True}),
 
148
 
 
149
    (registerSend,{'acc':acc_1, 'enable':False}),
 
150
    (presSubscribe, {'acc':acc_2,'buddy':buddy_uri_2,'flag':False}),
 
151
 
 
152
    (registerSend,{'acc':acc_2, 'enable':False}),
 
153
    (presSubscribe, {'acc':acc_1,'buddy':buddy_uri_1,'flag':False}),
 
154
]
 
155
 
 
156
 
 
157
"""
 
158
# simple sub
 
159
task_list = [
 
160
 
 
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}),
 
163
]
 
164
"""
 
165
 
 
166
"""
 
167
# IP2IP
 
168
task_list = [
 
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}),
 
174
]
 
175
"""
 
176
 
 
177
 
 
178
def run():
 
179
 
 
180
    if sequence_mode == SEQ_MODE_NORMAL:
 
181
        global task_count
 
182
        task_index = task_count%task_N
 
183
        task_count += 1
 
184
        if task_count == task_N+1: # one loop
 
185
            return
 
186
 
 
187
    elif sequence_mode == SEQ_MODE_RANDOM:
 
188
        task_index = randint(0,task_N-1)
 
189
 
 
190
    task_list[task_index][0](task_list[task_index][1])
 
191
 
 
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
 
195
    else:
 
196
        logging.info("Test sequence finished")
 
197
        # TODO clear dbus sessio. Unfortunately stackoverflow.com is down today
 
198
 
 
199
 
 
200
if __name__ == '__main__':
 
201
 
 
202
    try:
 
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')
 
208
 
 
209
        start_time = time.time()
 
210
        task_N = len(task_list)
 
211
        #sequence_mode = SEQ_MODE_RANDOM
 
212
 
 
213
        run()
 
214
 
 
215
    except Exception as e:
 
216
        print e
 
217
 
 
218
    loop = gobject.MainLoop()
 
219
    loop.run()