1#!/usr/bin/env python
2#
3#   (C) 2001 by Argonne National Laboratory.
4#       See COPYRIGHT in top-level directory.
5#
6
7"""
8usage: mpdringtest [number of loops]
9Times a single message going around the ring of mpds [num] times (default once)
10"""
11from time import ctime
12__author__ = "Ralph Butler and Rusty Lusk"
13__date__ = ctime()
14__version__ = "$Revision: 1.14 $"
15__credits__ = ""
16
17import sys, os, signal
18
19from  time    import  time
20from  mpdlib  import  mpd_set_my_id, mpd_uncaught_except_tb, mpd_print, \
21                      mpd_handle_signal, mpd_get_my_username, MPDConClientSock, MPDParmDB
22
23def mpdringtest():
24    import sys    # to get access to excepthook in next line
25    sys.excepthook = mpd_uncaught_except_tb
26    if len(sys.argv) > 1  and  ( sys.argv[1] == '-h'  or  sys.argv[1] == '--help' ) :
27        usage()
28    if len(sys.argv) < 2:
29	numLoops = 1
30    else:
31	numLoops = int(sys.argv[1])
32    signal.signal(signal.SIGINT, sig_handler)
33    mpd_set_my_id(myid='mpdringtest')
34
35    parmdb = MPDParmDB(orderedSources=['cmdline','xml','env','rcfile','thispgm'])
36    parmsToOverride = {
37                        'MPD_USE_ROOT_MPD'            :  0,
38                        'MPD_SECRETWORD'              :  '',
39                      }
40    for (k,v) in parmsToOverride.items():
41        parmdb[('thispgm',k)] = v
42    parmdb.get_parms_from_env(parmsToOverride)
43    parmdb.get_parms_from_rcfile(parmsToOverride)
44    if (hasattr(os,'getuid')  and  os.getuid() == 0)  or  parmdb['MPD_USE_ROOT_MPD']:
45        fullDirName = os.path.abspath(os.path.split(sys.argv[0])[0])  # normalize
46        mpdroot = os.path.join(fullDirName,'mpdroot')
47        conSock = MPDConClientSock(mpdroot=mpdroot,secretword=parmdb['MPD_SECRETWORD'])
48    else:
49        conSock = MPDConClientSock(secretword=parmdb['MPD_SECRETWORD'])
50
51    msgToSend = { 'cmd' : 'mpdringtest', 'numloops' : numLoops }
52    conSock.send_dict_msg(msgToSend)
53    starttime = time()
54    msg = conSock.recv_dict_msg()
55    etime = time() - starttime
56    if not msg:
57        print 'mpdringtest terminated early'
58    elif msg['cmd'] != 'mpdringtest_done':
59        if msg['cmd'] == 'already_have_a_console':
60            print 'mpd already has a console (e.g. for long ringtest); try later'
61        else:
62            print 'unexpected message from mpd: %s' % (msg)
63    else:
64	print 'time for %d loops =' % numLoops, etime, 'seconds'
65
66def sig_handler(signum,frame):
67    mpd_handle_signal(signum,frame)  # not nec since I exit next
68    sys.exit(-1)
69
70def usage():
71    print __doc__
72    sys.exit(-1)
73
74if __name__ == '__main__':
75    mpdringtest()
76