| OLD | NEW | 
|---|
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE.md file. | 
| 4 | 4 | 
| 5 library fletch_agent.agent_connection; | 5 library dartino_agent.agent_connection; | 
| 6 | 6 | 
| 7 import 'dart:async'; | 7 import 'dart:async'; | 
| 8 import 'dart:io'; | 8 import 'dart:io'; | 
| 9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; | 
| 10 | 10 | 
| 11 import 'messages.dart'; | 11 import 'messages.dart'; | 
| 12 | 12 | 
| 13 /// This class is used to connect to the Fletch Agent from Dart code. Ie. it | 13 /// This class is used to connect to the Dartino Agent from Dart code. Ie. it | 
| 14 /// cannot be used from Fletch code as it is depending on the dart:io Socket | 14 /// cannot be used from Dartino code as it is depending on the dart:io Socket | 
| 15 /// class. | 15 /// class. | 
| 16 /// The class is only for making a one-shot request/reply. The peer socket is | 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 | 17 /// closed after handling the request. This is similar to HTTP without | 
| 18 /// keep-alive. | 18 /// keep-alive. | 
| 19 /// The caller/user of this class must handle any errors occurring on the | 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. | 20 /// socket.done future as this class is not handling that. | 
| 21 class AgentConnection { | 21 class AgentConnection { | 
| 22   final Socket socket; | 22   final Socket socket; | 
| 23 | 23 | 
| 24   AgentConnection(this.socket); | 24   AgentConnection(this.socket); | 
| 25 | 25 | 
| 26   Future<VmData> startVm() async { | 26   Future<VmData> startVm() async { | 
| 27     var request = new StartVmRequest(); | 27     var request = new StartVmRequest(); | 
| 28     var replyBuffer = await sendRequest(request); | 28     var replyBuffer = await sendRequest(request); | 
| 29     var reply = new StartVmReply.fromBuffer(replyBuffer); | 29     var reply = new StartVmReply.fromBuffer(replyBuffer); | 
| 30     if (reply.result == ReplyHeader.START_VM_FAILED) { | 30     if (reply.result == ReplyHeader.START_VM_FAILED) { | 
| 31       throw new AgentException('Failed to start new Fletch VM.'); | 31       throw new AgentException('Failed to start new Dartino VM.'); | 
| 32     } else if (reply.result != ReplyHeader.SUCCESS) { | 32     } else if (reply.result != ReplyHeader.SUCCESS) { | 
| 33       throw new AgentException( | 33       throw new AgentException( | 
| 34           'Failed to spawn new VM with unexpected error: ${reply.result}'); | 34           'Failed to spawn new VM with unexpected error: ${reply.result}'); | 
| 35     } | 35     } | 
| 36     return new VmData(reply.vmId, reply.vmPort); | 36     return new VmData(reply.vmId, reply.vmPort); | 
| 37   } | 37   } | 
| 38 | 38 | 
| 39   Future stopVm(int vmId) async { | 39   Future stopVm(int vmId) async { | 
| 40     var request = new StopVmRequest(vmId); | 40     var request = new StopVmRequest(vmId); | 
| 41     var replyBuffer = await sendRequest(request); | 41     var replyBuffer = await sendRequest(request); | 
| (...skipping 21 matching lines...) Expand all  Loading... | 
| 63   Future<List<int>> listVms() async { | 63   Future<List<int>> listVms() async { | 
| 64     throw new AgentException('Not implemented'); | 64     throw new AgentException('Not implemented'); | 
| 65   } | 65   } | 
| 66 | 66 | 
| 67   Future upgradeAgent(String version, List<int> binary) async { | 67   Future upgradeAgent(String version, List<int> binary) async { | 
| 68     // TODO(karlklose): also send the version string. | 68     // TODO(karlklose): also send the version string. | 
| 69     var request = new UpgradeAgentRequest(binary); | 69     var request = new UpgradeAgentRequest(binary); | 
| 70     var replyBuffer = await sendRequest(request); | 70     var replyBuffer = await sendRequest(request); | 
| 71     var reply = new UpgradeAgentReply.fromBuffer(replyBuffer); | 71     var reply = new UpgradeAgentReply.fromBuffer(replyBuffer); | 
| 72     if (reply.result != ReplyHeader.SUCCESS) { | 72     if (reply.result != ReplyHeader.SUCCESS) { | 
| 73       throw new AgentException('Failed to upgrade fletch-agent package ' | 73       throw new AgentException('Failed to upgrade dartino-agent package ' | 
| 74           'with unexpected error: ${reply.result}'); | 74           'with unexpected error: ${reply.result}'); | 
| 75     } | 75     } | 
| 76   } | 76   } | 
| 77 | 77 | 
| 78   Future<String> fletchVersion() async { | 78   Future<String> dartinoVersion() async { | 
| 79     var request = new FletchVersionRequest(); | 79     var request = new DartinoVersionRequest(); | 
| 80     var replyBuffer = await sendRequest(request); | 80     var replyBuffer = await sendRequest(request); | 
| 81     var reply = new FletchVersionReply.fromBuffer(replyBuffer); | 81     var reply = new DartinoVersionReply.fromBuffer(replyBuffer); | 
| 82     if (reply.result != ReplyHeader.SUCCESS) { | 82     if (reply.result != ReplyHeader.SUCCESS) { | 
| 83       throw new AgentException('Failed to retrive Fletch version ' | 83       throw new AgentException('Failed to retrive Dartino version ' | 
| 84           'with unexpected error: ${reply.result}'); | 84           'with unexpected error: ${reply.result}'); | 
| 85     } | 85     } | 
| 86     return reply.fletchVersion; | 86     return reply.dartinoVersion; | 
| 87   } | 87   } | 
| 88 | 88 | 
| 89   Future<ByteBuffer> sendRequest(RequestHeader request) async { | 89   Future<ByteBuffer> sendRequest(RequestHeader request) async { | 
| 90     socket.add(request.toBuffer().asUint8List()); | 90     socket.add(request.toBuffer().asUint8List()); | 
| 91     var replyBytes = await socket.fold([], (p, e) => p..addAll(e)); | 91     var replyBytes = await socket.fold([], (p, e) => p..addAll(e)); | 
| 92     return new Uint8List.fromList(replyBytes).buffer; | 92     return new Uint8List.fromList(replyBytes).buffer; | 
| 93   } | 93   } | 
| 94 } | 94 } | 
| 95 | 95 | 
| 96 class VmData { | 96 class VmData { | 
| 97   final int id; | 97   final int id; | 
| 98   final int port; | 98   final int port; | 
| 99 | 99 | 
| 100   VmData(this.id, this.port); | 100   VmData(this.id, this.port); | 
| 101 } | 101 } | 
| 102 | 102 | 
| 103 class AgentException implements Exception { | 103 class AgentException implements Exception { | 
| 104   String message; | 104   String message; | 
| 105   String toString() => 'AgentException($message)'; | 105   String toString() => 'AgentException($message)'; | 
| 106 | 106 | 
| 107   AgentException(this.message); | 107   AgentException(this.message); | 
| 108 } | 108 } | 
| OLD | NEW | 
|---|