OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 library fletch_agent.client; | |
6 | |
7 import 'dart:io'; | |
8 import 'dart:typed_data'; | |
9 | |
10 import '../lib/messages.dart'; | |
11 import '../lib/agent_connection.dart'; | |
12 | |
13 void printUsage() { | |
14 print(''' | |
15 Usage: | |
16 The Fletch agent command line client supports the following flags: | |
17 | |
18 --port: the port on which to connect, default: 12121 | |
19 --host: the ip address on which to connect, default: 127.0.0.1 | |
20 --cmd: the command to send to the Fletch agent, default: 0 (START_VM) | |
21 --pid: the pid of the vm to stop, only used when --cmd=1 (STOP_VM) | |
22 --signal: which signal to send to the vm. Requires the --pid option to | |
23 be specified | |
24 --pkg: path to the package file to be used with --cmd=3 (UPGRADE_PKG) | |
25 | |
26 Example: Get the version of the agent running on 192.168.1.1: | |
27 dart client.dart --cmd=4 --host=192.168.1.1.'''); | |
28 exit(1); | |
29 } | |
30 | |
31 /// Small dart program to issue commands to the fletch agent. | |
32 void main(List<String> arguments) async { | |
33 // Startup the agent listening on specified port. | |
34 int port = 12121; | |
35 String host = '127.0.0.1'; | |
36 int cmd = RequestHeader.START_VM; | |
37 int id = 1; // The default id used. | |
38 int pid; | |
39 int signal; | |
40 Socket socket; | |
41 String packageFile; | |
42 | |
43 void checkSuccess(ReplyHeader header) { | |
44 if (header == null) { | |
45 print('Received invalid reply. Could not parse header.'); | |
46 socket.close(); | |
47 exit(1); | |
48 } | |
49 if (header.id != id) { | |
50 print('Received out of sync message. Expected id $id and got ' | |
51 'id ${header.id}'); | |
52 socket.close(); | |
53 exit(1); | |
54 } | |
55 if (header.result != ReplyHeader.SUCCESS) { | |
56 print('Received reply with id ${header.id} and result ' | |
57 '${header.result}'); | |
58 socket.close(); | |
59 exit(1); | |
60 } | |
61 } | |
62 | |
63 for (var argument in arguments) { | |
64 var parts = argument.split('='); | |
65 if (parts[0] == '--cmd') { | |
66 if (parts.length != 2) { | |
67 printUsage(); | |
68 } | |
69 cmd = int.parse(parts[1]); | |
70 } else if (parts[0] == '--id') { | |
71 if (parts.length != 2) { | |
72 printUsage(); | |
73 } | |
74 id = int.parse(parts[1]); | |
75 } else if (parts[0] == '--pid') { | |
76 if (parts.length != 2) { | |
77 printUsage(); | |
78 } | |
79 pid = int.parse(parts[1]); | |
80 } else if (parts[0] == '--host') { | |
81 if (parts.length != 2) { | |
82 printUsage(); | |
83 } | |
84 host = parts[1]; | |
85 } else if (parts[0] == '--port') { | |
86 if (parts.length != 2) { | |
87 printUsage(); | |
88 } | |
89 port = int.parse(parts[1]); | |
90 } else if (parts[0] == '--signal') { | |
91 if (parts.length != 2) { | |
92 printUsage(); | |
93 } | |
94 signal = int.parse(parts[1]); | |
95 } else if (parts[0] == '--pkg') { | |
96 if (parts.length != 2) { | |
97 printUsage(); | |
98 } | |
99 packageFile = parts[1]; | |
100 } | |
101 } | |
102 | |
103 try { | |
104 socket = await Socket.connect(host, port); | |
105 } on SocketException catch (error) { | |
106 print('Could not connect to Fletch Agent on \'$host:$port\'. ' | |
107 'Received error: $error'); | |
108 printUsage(); | |
109 } | |
110 | |
111 var connection = new AgentConnection(socket); | |
112 switch (cmd) { | |
113 case RequestHeader.START_VM: | |
114 VmData vmData = await connection.startVm(); | |
115 print('Started VM: id=${vmData.id}, port=${vmData.port}'); | |
116 break; | |
117 case RequestHeader.STOP_VM: | |
118 if (pid == null) { | |
119 print('Please specify which pid to stop with --pid=<pid>'); | |
120 exit(1); | |
121 } | |
122 await connection.stopVm(pid); | |
123 print('Stopped VM: id=$pid'); | |
124 break; | |
125 case RequestHeader.LIST_VMS: | |
126 await connection.listVms(); | |
127 break; | |
128 case RequestHeader.UPGRADE_AGENT: | |
129 if (packageFile == null) { | |
130 print('Please specify the path to the package with --pkg=<path>'); | |
131 exit(1); | |
132 } | |
133 Uri packageUri = new Uri.file(packageFile); | |
134 List<String> nameParts = packageUri.pathSegments.last.split('_'); | |
135 if (nameParts.length != 3 || nameParts[0] != 'fletch-agent') { | |
136 print('A fletch-agent package must have a name of the form\n' | |
137 ' fletch-agent_<version>_<platform>'); | |
138 exit(1); | |
139 } | |
140 String version = nameParts[1]; | |
141 List<int> data = await new File(packageFile).readAsBytes(); | |
142 print('Sending package for version $version' | |
143 ' (length: ${data.length} bytes).'); | |
144 await connection.upgradeAgent(version, data); | |
145 print('Update finished'); | |
146 break; | |
147 case RequestHeader.FLETCH_VERSION: | |
148 String version = await connection.fletchVersion(); | |
149 print('Fletch Agent Version $version'); | |
150 break; | |
151 case RequestHeader.SIGNAL_VM: | |
152 if (pid == null) { | |
153 print('Please specify which pid to stop with --pid=<pid>'); | |
154 printUsage(); | |
155 exit(1); | |
156 } | |
157 if (signal == null) { | |
158 print('Please specify the signal to send to pid.'); | |
159 printUsage(); | |
160 exit(1); | |
161 } | |
162 await connection.signalVm(pid, signal); | |
163 print('Send signal $signal to VM: id=$pid'); | |
164 break; | |
165 default: | |
166 print('Invalid command: $cmd'); | |
167 exit(1); | |
168 } | |
169 socket.close(); | |
170 } | |
OLD | NEW |