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.agent_connection; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 import 'dart:typed_data'; | |
10 | |
11 import 'messages.dart'; | |
12 | |
13 /// This class is used to connect to the Fletch Agent from Dart code. Ie. it | |
14 /// cannot be used from Fletch code as it is depending on the dart:io Socket | |
15 /// class. | |
16 /// The class is only for making a one-shot request/reply. The peer socket is | |
17 /// closed after handling the request. This is similar to HTTP without | |
18 /// keep-alive. | |
19 /// The caller/user of this class must handle any errors occurring on the | |
20 /// socket.done future as this class is not handling that. | |
21 class AgentConnection { | |
22 final Socket socket; | |
23 | |
24 AgentConnection(this.socket); | |
25 | |
26 Future<VmData> startVm() async { | |
27 var request = new StartVmRequest(); | |
28 var replyBuffer = await sendRequest(request); | |
29 var reply = new StartVmReply.fromBuffer(replyBuffer); | |
30 if (reply.result == ReplyHeader.START_VM_FAILED) { | |
31 throw new AgentException('Failed to start new Fletch VM.'); | |
32 } else if (reply.result != ReplyHeader.SUCCESS) { | |
33 throw new AgentException( | |
34 'Failed to spawn new VM with unexpected error: ${reply.result}'); | |
35 } | |
36 return new VmData(reply.vmId, reply.vmPort); | |
37 } | |
38 | |
39 Future stopVm(int vmId) async { | |
40 var request = new StopVmRequest(vmId); | |
41 var replyBuffer = await sendRequest(request); | |
42 var reply = new StopVmReply.fromBuffer(replyBuffer); | |
43 if (reply.result == ReplyHeader.UNKNOWN_VM_ID) { | |
44 throw new AgentException('Could not stop VM. Unknown vm id: $vmId'); | |
45 } else if (reply.result != ReplyHeader.SUCCESS) { | |
46 throw new AgentException( | |
47 'Failed to stop VM with unexpected error: ${reply.result}'); | |
48 } | |
49 } | |
50 | |
51 Future signalVm(int vmId, int signal) async { | |
52 var request = new SignalVmRequest(vmId, signal); | |
53 var replyBuffer = await sendRequest(request); | |
54 var reply = new SignalVmReply.fromBuffer(replyBuffer); | |
55 if (reply.result == ReplyHeader.UNKNOWN_VM_ID) { | |
56 throw new AgentException('Could not signal VM. Unknown vm id: $vmId'); | |
57 } else if (reply.result != ReplyHeader.SUCCESS) { | |
58 throw new AgentException( | |
59 'Failed to signal VM with unexpected error: ${reply.result}'); | |
60 } | |
61 } | |
62 | |
63 Future<List<int>> listVms() async { | |
64 throw new AgentException('Not implemented'); | |
65 } | |
66 | |
67 Future upgradeAgent(String version, List<int> binary) async { | |
68 // TODO(karlklose): also send the version string. | |
69 var request = new UpgradeAgentRequest(binary); | |
70 var replyBuffer = await sendRequest(request); | |
71 var reply = new UpgradeAgentReply.fromBuffer(replyBuffer); | |
72 if (reply.result != ReplyHeader.SUCCESS) { | |
73 throw new AgentException('Failed to upgrade fletch-agent package ' | |
74 'with unexpected error: ${reply.result}'); | |
75 } | |
76 } | |
77 | |
78 Future<String> fletchVersion() async { | |
79 var request = new FletchVersionRequest(); | |
80 var replyBuffer = await sendRequest(request); | |
81 var reply = new FletchVersionReply.fromBuffer(replyBuffer); | |
82 if (reply.result != ReplyHeader.SUCCESS) { | |
83 throw new AgentException('Failed to retrive Fletch version ' | |
84 'with unexpected error: ${reply.result}'); | |
85 } | |
86 return reply.fletchVersion; | |
87 } | |
88 | |
89 Future<ByteBuffer> sendRequest(RequestHeader request) async { | |
90 socket.add(request.toBuffer().asUint8List()); | |
91 var replyBytes = await socket.fold([], (p, e) => p..addAll(e)); | |
92 return new Uint8List.fromList(replyBytes).buffer; | |
93 } | |
94 } | |
95 | |
96 class VmData { | |
97 final int id; | |
98 final int port; | |
99 | |
100 VmData(this.id, this.port); | |
101 } | |
102 | |
103 class AgentException implements Exception { | |
104 String message; | |
105 String toString() => 'AgentException($message)'; | |
106 | |
107 AgentException(this.message); | |
108 } | |
OLD | NEW |