blob: 2c6412c061902219a8089eb5216e14d000cfb3eb [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
Jon Hall2bb3e212017-05-24 17:07:25 -070010
You Wangdb927a52016-02-26 11:03:28 -080011def triggerEvent( debugMode, name, scheduleMethod, args ):
12 """
13 This function inserts an event from CLI to CHOTestMonkey
14 """
15 host = "localhost"
16 port = 6000
17 address = ( host, port )
18 conn = Client( address )
19 request = []
20 if debugMode:
21 request.append( 2 )
22 else:
23 request.append( 1 )
24 request.append( name )
25 request.append( scheduleMethod )
26 for arg in args:
27 request.append( arg )
28 conn.send( request )
29 response = conn.recv()
30 return response
31
Jon Hall2bb3e212017-05-24 17:07:25 -070032
You Wangdb927a52016-02-26 11:03:28 -080033def startCLI():
34 debugMode = False
Jon Hall2bb3e212017-05-24 17:07:25 -070035 while True:
You Wangdb927a52016-02-26 11:03:28 -080036 try:
37 if debugMode:
Jon Hall2bb3e212017-05-24 17:07:25 -070038 cmd = raw_input( "CHOTestMonkey-debug>" )
You Wangdb927a52016-02-26 11:03:28 -080039 else:
Jon Hall2bb3e212017-05-24 17:07:25 -070040 cmd = raw_input( "CHOTestMonkey>" )
You Wangdb927a52016-02-26 11:03:28 -080041 except EOFError:
42 print "exit"
43 return
44 except Exception:
45 print "Uncaught exception!"
46 return
47
48 if cmd == 'help':
49 print 'Supported commands:'
50 print 'help'
51 print 'debug'
52 print 'exit'
53 for command in commandMap.keys():
54 print command
55 elif cmd == '':
56 pass
57 elif cmd == 'debug':
58 debugMode = True
59 elif cmd == 'exit':
60 if debugMode:
61 debugMode = False
62 else:
63 return
64 else:
65 cmdList = cmd.split( ' ' )
66 if cmdList[ 0 ] in commandMap.keys():
67 num = paramNum[ cmdList[ 0 ] ]
68 name = commandMap[ cmdList[ 0 ] ]
Jon Hall2bb3e212017-05-24 17:07:25 -070069 """
You Wangdb927a52016-02-26 11:03:28 -080070 if len( cmdList ) < num + 1:
71 print 'not enough arguments'
72 elif len( cmdList ) > num + 1:
73 print 'Too many arguments'
74 else:
Jon Hall2bb3e212017-05-24 17:07:25 -070075 """
You Wang7a27f3a2016-07-05 10:12:27 -070076 result = triggerEvent( debugMode, name, 'RUN_BLOCK', cmdList[ 1: ] )
77 if result == 10:
78 pass
79 elif result == 11:
80 print "Scheduler busy...Try later or use debugging mode by entering \'debug\'"
81 elif result == 20:
82 print "Unknown message to server"
83 elif result == 21:
84 print "Unknown event type to server"
85 elif result == 22:
86 print "Unknown schedule method to server"
87 elif result == 23:
88 print "Not enough argument"
89 else:
90 print "Unknown response from server"
You Wangdb927a52016-02-26 11:03:28 -080091 else:
92 print 'Unknown command'
93
94if __name__ == '__main__':
95 import xml.etree.ElementTree
96 try:
97 root = xml.etree.ElementTree.parse( '../CHOTestMonkey.params' ).getroot()
98 except Exception:
99 print "Uncaught exception!"
100 for child in root:
101 if child.tag == 'EVENT':
102 for event in child:
103 for item in event:
104 if item.tag == 'CLI':
105 CLI = str( item.text )
106 if item.tag == 'typeString':
107 name = str( item.text )
108 if item.tag == 'CLIParamNum':
109 num = int( item.text )
110 commandMap[ CLI ] = name
111 paramNum[ CLI ] = num
112 startCLI()