dringctrl: improve the mini client

Make import robust.
Support Python2.7 in addition to Python3.
Fix call interface.
Add an auto answer option.
Handle keyboard interrupt.

Change-Id: I56160928ef8fa2e3de893c6b64ad716836bbc13f
Tuleap: #541
This commit is contained in:
Patrick Keroulas
2016-04-06 16:00:57 -04:00
committed by gerrit2
parent df929473d4
commit 7bcb447b17
3 changed files with 60 additions and 30 deletions

View File

@ -20,6 +20,7 @@
"""DRing controling class through DBUS""" """DRing controling class through DBUS"""
import sys
import os import os
import random import random
import time import time
@ -28,7 +29,13 @@ import hashlib
from threading import Thread from threading import Thread
from functools import partial from functools import partial
from gi.repository import GObject try:
from gi.repository import GObject
except ImportError as e:
import gobject as GObject
except Exception as e:
print(str(e))
exit(1)
from errors import * from errors import *
@ -36,7 +43,7 @@ try:
import dbus import dbus
from dbus.mainloop.glib import DBusGMainLoop from dbus.mainloop.glib import DBusGMainLoop
except ImportError as e: except ImportError as e:
raise DRingCtrlError("No python3-dbus module found") raise DRingCtrlError("No python-dbus module found")
DBUS_DEAMON_OBJECT = 'cx.ring.Ring' DBUS_DEAMON_OBJECT = 'cx.ring.Ring'
@ -44,13 +51,17 @@ DBUS_DEAMON_PATH = '/cx/ring/Ring'
class DRingCtrl(Thread): class DRingCtrl(Thread):
def __init__(self, name): def __init__(self, name, autoAnswer):
if sys.version_info[0] < 3:
super(DRingCtrl, self).__init__()
else:
super().__init__() super().__init__()
self.activeCalls = {} # list of active calls (known by the client) self.activeCalls = {} # list of active calls (known by the client)
self.activeConferences = {} # list of active conferences self.activeConferences = {} # list of active conferences
self.account = None # current active account self.account = None # current active account
self.name = name # client name self.name = name # client name
self.autoAnswer = autoAnswer
self.currentCallId = "" self.currentCallId = ""
self.currentConfId = "" self.currentConfId = ""
@ -141,7 +152,6 @@ class DRingCtrl(Thread):
except: except:
raise DRingCtrlDeamonError("Client unregistration failed") raise DRingCtrlDeamonError("Client unregistration failed")
def isRegistered(self): def isRegistered(self):
return self.registered return self.registered
@ -149,7 +159,9 @@ class DRingCtrl(Thread):
# Signal handling # Signal handling
# #
def onIncomingCall_cb(self): def onIncomingCall_cb(self, callId):
if self.autoAnswer:
self.Accept(callId)
pass pass
def onCallHangup_cb(self, callId): def onCallHangup_cb(self, callId):
@ -177,7 +189,7 @@ class DRingCtrl(Thread):
'To': to, 'To': to,
'State': ''} 'State': ''}
self.currentCallId = callid self.currentCallId = callid
self.onIncomingCall_cb() self.onIncomingCall_cb(callid)
def onCallHangUp(self, callid): def onCallHangUp(self, callid):
@ -192,7 +204,7 @@ class DRingCtrl(Thread):
""" Update state for this call to Ringing """ """ Update state for this call to Ringing """
self.activeCalls[callid]['State'] = state self.activeCalls[callid]['State'] = state
self.onCallRinging_cb() self.onCallRinging_cb(callid)
def onCallHold(self, callid, state): def onCallHold(self, callid, state):
@ -223,7 +235,7 @@ class DRingCtrl(Thread):
del self.activeCalls[callid] del self.activeCalls[callid]
def onCallStateChanged(self, callid, state): def onCallStateChanged(self, callid, state, code):
""" On call state changed event, set the values for new calls, """ On call state changed event, set the values for new calls,
or delete the call from the list of active calls or delete the call from the list of active calls
""" """
@ -234,7 +246,8 @@ class DRingCtrl(Thread):
callDetails = self.getCallDetails(callid) callDetails = self.getCallDetails(callid)
self.activeCalls[callid] = {'Account': callDetails['ACCOUNTID'], self.activeCalls[callid] = {'Account': callDetails['ACCOUNTID'],
'To': callDetails['PEER_NUMBER'], 'To': callDetails['PEER_NUMBER'],
'State': state } 'State': state,
'Code': code }
self.currentCallId = callid self.currentCallId = callid
@ -609,6 +622,16 @@ class DRingCtrl(Thread):
return self.callmanager.switchInput(callid, inputName) return self.callmanager.switchInput(callid, inputName)
def interruptHandler(self, signum, frame):
print('Signal handler called with signal ' + str(signum))
self.stopThread()
def printAccountDetails(self, account):
details = self.getAccountDetails(account)
print(account)
for k in sorted(details.keys()):
print(" %s: %s" % (k, details[k]))
print()
def run(self): def run(self):
"""Processing method for this thread""" """Processing method for this thread"""
@ -619,8 +642,5 @@ class DRingCtrl(Thread):
context.iteration(True) context.iteration(True)
if self.isStop: if self.isStop:
print("++++++++++++++++++++++++++++++++++++++++") print("++++++++++++++++++ EXIT ++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++++++++++++")
return return

View File

@ -22,20 +22,20 @@ import os
import random import random
import time import time
import argparse import argparse
import signal
from gi.repository import GObject try:
from gi.repository import GObject
except ImportError as e:
import gobject as GObject
except Exception as e:
print(str(e))
exit(1)
from errors import * from errors import *
from controler import DRingCtrl from controler import DRingCtrl
from tester import DRingTester from tester import DRingTester
def printAccountDetails(account):
details = ctrl.getAccountDetails(account)
print(account)
for k in sorted(details.keys()):
print(" %s: %s" % (k, details[k]))
print()
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -93,14 +93,11 @@ if __name__ == "__main__":
parser.add_argument('--toggle-video', help='Launch toggle video tests', action='store_true') parser.add_argument('--toggle-video', help='Launch toggle video tests', action='store_true')
parser.add_argument('--test', help=' '.join(str(test) for test in DRingTester().getTestName() ), metavar='<testName>') parser.add_argument('--test', help=' '.join(str(test) for test in DRingTester().getTestName() ), metavar='<testName>')
parser.add_argument('--auto-answer', help='Keep running and auto-answer the calls', action='store_true')
args = parser.parse_args() args = parser.parse_args()
ctrl = DRingCtrl(sys.argv[0]) ctrl = DRingCtrl(sys.argv[0], args.auto_answer)
if len(sys.argv) == 1:
ctrl.run()
sys.exit(0)
if args.add_ring_account: if args.add_ring_account:
accDetails = {'Account.type':'RING', 'Account.alias':args.add_ring_account if args.add_ring_account!='' else 'RingAccount'} accDetails = {'Account.type':'RING', 'Account.alias':args.add_ring_account if args.add_ring_account!='' else 'RingAccount'}
@ -126,7 +123,7 @@ if __name__ == "__main__":
if args.get_all_accounts_details: if args.get_all_accounts_details:
for account in ctrl.getAllAccounts(): for account in ctrl.getAllAccounts():
printAccountDetails(account) ctrl.printAccountDetails(account)
if args.get_active_codecs_details: if args.get_active_codecs_details:
for codecId in ctrl.getActiveCodecs(args.get_active_codecs_details): for codecId in ctrl.getActiveCodecs(args.get_active_codecs_details):
@ -138,7 +135,7 @@ if __name__ == "__main__":
ctrl.setAccount(args.set_active_account) ctrl.setAccount(args.set_active_account)
if args.get_account_details: if args.get_account_details:
printAccountDetails(args.get_account_details) ctrl.printAccountDetails(args.get_account_details)
if hasattr(args, 'get_active_codecs'): if hasattr(args, 'get_active_codecs'):
print(ctrl.getActiveCodecs(args.get_active_codec)) print(ctrl.getActiveCodecs(args.get_active_codec))
@ -196,3 +193,9 @@ if __name__ == "__main__":
ctrl.videomanager.startCamera() ctrl.videomanager.startCamera()
time.sleep(2) time.sleep(2)
ctrl.videomanager.stopCamera() ctrl.videomanager.stopCamera()
if len(sys.argv) == 1 or ctrl.autoAnswer:
signal.signal(signal.SIGINT, ctrl.interruptHandler)
ctrl.run()
sys.exit(0)

View File

@ -21,7 +21,14 @@
import sys import sys
import os import os
import time import time
import configparser
try:
import configparser
except ImportError as e:
import ConfigParser as configparser
except Exception as e:
print(str(e))
exit(1)
from threading import Thread from threading import Thread
from random import shuffle from random import shuffle