OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart 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 file. | |
4 | |
5 // Copied from MintMakerRpcTest (under tests/isolate/src/). | |
6 | |
7 #library('mintmaker'); | |
8 | |
9 class Mint { | |
10 Mint(); | |
11 Purse createPurse(int initialBalance) { | |
12 return new Purse(initialBalance); | |
13 } | |
14 } | |
15 | |
16 class Purse { | |
17 int _balance; | |
18 | |
19 Purse(this._balance) {} | |
20 | |
21 int queryBalance() { | |
22 return _balance; | |
23 } | |
24 | |
25 int deposit(int amount, Purse source) { | |
26 if (source._balance < amount) { | |
27 throw "OverdraftException"; | |
28 } | |
29 source._balance -= amount; | |
30 _balance += amount; | |
31 return _balance; | |
32 } | |
33 } | |
34 | |
35 | |
36 class MintProxy extends RpcProxy { | |
37 MintProxy(Future<SendPort> sendPort) : super(sendPort) { } | |
38 Future<PurseProxy> createPurse(int initialBalance) { | |
39 return sendCommand("createPurse", [initialBalance], (sendPort) { | |
40 Completer<SendPort> completer = new Completer(); | |
41 completer.complete(sendPort); | |
42 return new PurseProxy(completer.future); | |
43 }); | |
44 } | |
45 Future<String> close() { | |
46 return sendCommand("close", null, null); | |
47 } | |
48 } | |
49 | |
50 class MintReceiver extends RpcReceiver<Mint> { | |
51 MintReceiver(ReceivePort receivePort) : super(new Mint(), receivePort) {} | |
52 Object receiveCommand(String command, List args) { | |
53 switch(command) { | |
54 case "createPurse": | |
55 int balance = args[0]; | |
56 Purse purse = target.createPurse(balance); | |
57 return new PurseReceiver(purse, new ReceivePort()); | |
58 case "close": | |
59 RpcReceiver.closeAll(); | |
60 return "close command processed"; | |
61 default: | |
62 throw "MintReceiver unrecognized command"; | |
63 } | |
64 } | |
65 } | |
66 | |
67 class PurseProxy extends RpcProxy { | |
68 PurseProxy(Future<SendPort> sendPort) : super(sendPort) { } | |
69 Future<int> queryBalance() { | |
70 return sendCommand("queryBalance", null, null); | |
71 } | |
72 Future<int> deposit(int amount, PurseProxy from) { | |
73 return sendCommand("deposit", [amount, from], null); | |
74 } | |
75 } | |
76 | |
77 class PurseReceiver extends RpcReceiver<Purse> { | |
78 | |
79 PurseReceiver(Purse purse, ReceivePort receivePort) : super(purse, receivePort
) {} | |
80 | |
81 Object receiveCommand(String command, List args) { | |
82 switch(command) { | |
83 case "queryBalance": | |
84 return target.queryBalance(); | |
85 case "deposit": | |
86 int amount = args[0]; | |
87 Purse fromPurse = args[1]; | |
88 return target.deposit(amount, fromPurse); | |
89 default: | |
90 throw "PurseReceiver unrecognized command"; | |
91 } | |
92 } | |
93 } | |
94 | |
95 class MintIsolate extends Isolate { | |
96 MintIsolate() : super.light() {} | |
97 | |
98 MintProxy open() { | |
99 return new MintProxy(spawn()); | |
100 } | |
101 | |
102 void main() { | |
103 ReceivePort receivePort = port; | |
104 new MintReceiver(receivePort); | |
105 } | |
106 } | |
OLD | NEW |