-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.em
127 lines (98 loc) · 3.47 KB
/
state.em
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
system.require('std/core/bind.em');
if(typeof(state) === 'undefined')
state = {};
state.defaultTimeout = 5 /* * u.s */;
state.Connection = system.Class.extend({
init: function(service) {
this.timeout = state.defaultTimeout;
this.service = service;
this.transaction = 1;
},
login: function(username, password, create) {
if(typeof(create) === 'undefined')
create = false;
this.username = username;
this.password = password;
var errorResponse = function(message, sender) {
throw('Error response "' + message.error +
'" received from state service ' + sender.toString());
};
var printResponse = function(message, sender) {
system.print(message.print);
};
var loginConfirm = function(message, sender) {};
var loginFailed = function() {
throw('Login for user ' + username +
' failed: message timed out');
};
var action = (create ? 'create' : 'login');
{action: action, username: this.username, password: this.password} >>
this.service >> [loginConfirm, this.timeout, loginFailed];
system.print(action);
system.print(username);
system.print(password);
errorResponse << [{'error'::}] << this.service;
printResponse << [{'print'::}] << this.service;
},
create: function(username, password) {
this.login(username, password, true);
},
logout: function() {
{action: 'logout', username: this.username} >> this.service >> [];
},
get: function(key, callback) {
this.callback = callback;
var onResponse = function(message, sender) {
if('error' in message)
return;
if(!('value' in message))
throw('Bad mesage: response for value "' + key +
'" did not have a value field');
callback(message.value);
};
var onNoResponse = function() {
throw('Could not get value "' + key + '": message timed out');
};
{action: 'get', key: key} >> this.service >>
[onResponse, this.timeout, onNoResponse];
},
give: function(mine, recipient, callback) {
var onResponse = function(message, sender) {
if(!('error' in message) && typeof(callback) !== 'undefined')
callback();
};
var onNoResponse = function() {
throw('Could not give "' + std.core.pretty(mine) + '" to ' +
std.core.pretty(recipient) + ': message timed out');
};
{action: 'give', what: mine, to: recipient} >> this.service >>
[onResponse, this.timeout, onNoResponse];
},
trade: function(mine, yours, you, onSuccess, onFailure) {
var onSuccessMessage = function(message, sender) {
if(typeof(onSuccess) !== 'undefined')
onSuccess();
};
var onFailureMessage = function(message, sender) {
if(typeof(onFailure) !== 'undefined')
onFailure();
};
var onResponse = function(message, sender) {
if(!('seqNo' in message) || !('id' in message))
throw('Response "' + std.core.pretty(message) + 'to trade "' +
std.core.pretty(mine) + '" to ' + you + ' for "' +
std.core.pretty(yours) + '" is incorrectly formatted -- '+
'must contain seqNo and id.');
onSuccessMessage << [{seqNo:message.seqNo:}, {id:message.id:},
{status:'success':}] << this.service;
onFailureMessage << [{seqNo:message.seqNo:}, {id:message.id:},
{status:'failure':}] << this.service;
};
var onNoResponse = function() {
throw('Could not give "' + std.core.pretty(mine) + '" to ' +
std.core.pretty(recipient) + ': message timed out');
};
{action: 'trade', what: mine, to: you, 'for': yours} >>
this.service >> [onResponse, this.timeout, onNoResponse];
}
});