blob: a3fb18e1137361824f0c7a640685f565721d49b4 [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
2Start CLI for CHOTestMonkey
3Author: you@onlab.us
4"""
5from multiprocessing.connection import Client
6
7commandMap = {}
8paramNum = {}
9
10def triggerEvent( debugMode, name, scheduleMethod, args ):
11 """
12 This function inserts an event from CLI to CHOTestMonkey
13 """
14 host = "localhost"
15 port = 6000
16 address = ( host, port )
17 conn = Client( address )
18 request = []
19 if debugMode:
20 request.append( 2 )
21 else:
22 request.append( 1 )
23 request.append( name )
24 request.append( scheduleMethod )
25 for arg in args:
26 request.append( arg )
27 conn.send( request )
28 response = conn.recv()
29 return response
30
31def startCLI():
32 debugMode = False
33 while 1:
34 try:
35 if debugMode:
36 cmd = raw_input("CHOTestMonkey-debug>")
37 else:
38 cmd = raw_input("CHOTestMonkey>")
39 except EOFError:
40 print "exit"
41 return
42 except Exception:
43 print "Uncaught exception!"
44 return
45
46 if cmd == 'help':
47 print 'Supported commands:'
48 print 'help'
49 print 'debug'
50 print 'exit'
51 for command in commandMap.keys():
52 print command
53 elif cmd == '':
54 pass
55 elif cmd == 'debug':
56 debugMode = True
57 elif cmd == 'exit':
58 if debugMode:
59 debugMode = False
60 else:
61 return
62 else:
63 cmdList = cmd.split( ' ' )
64 if cmdList[ 0 ] in commandMap.keys():
65 num = paramNum[ cmdList[ 0 ] ]
66 name = commandMap[ cmdList[ 0 ] ]
You Wang7a27f3a2016-07-05 10:12:27 -070067 '''
You Wangdb927a52016-02-26 11:03:28 -080068 if len( cmdList ) < num + 1:
69 print 'not enough arguments'
70 elif len( cmdList ) > num + 1:
71 print 'Too many arguments'
72 else:
You Wang7a27f3a2016-07-05 10:12:27 -070073 '''
74 result = triggerEvent( debugMode, name, 'RUN_BLOCK', cmdList[ 1: ] )
75 if result == 10:
76 pass
77 elif result == 11:
78 print "Scheduler busy...Try later or use debugging mode by entering \'debug\'"
79 elif result == 20:
80 print "Unknown message to server"
81 elif result == 21:
82 print "Unknown event type to server"
83 elif result == 22:
84 print "Unknown schedule method to server"
85 elif result == 23:
86 print "Not enough argument"
87 else:
88 print "Unknown response from server"
You Wangdb927a52016-02-26 11:03:28 -080089 else:
90 print 'Unknown command'
91
92if __name__ == '__main__':
93 import xml.etree.ElementTree
94 try:
95 root = xml.etree.ElementTree.parse( '../CHOTestMonkey.params' ).getroot()
96 except Exception:
97 print "Uncaught exception!"
98 for child in root:
99 if child.tag == 'EVENT':
100 for event in child:
101 for item in event:
102 if item.tag == 'CLI':
103 CLI = str( item.text )
104 if item.tag == 'typeString':
105 name = str( item.text )
106 if item.tag == 'CLIParamNum':
107 num = int( item.text )
108 commandMap[ CLI ] = name
109 paramNum[ CLI ] = num
110 startCLI()
111