forked from Comcast/littlesheens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.c
284 lines (227 loc) · 7.92 KB
/
demo.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* Copyright 2018 Comcast Cable Communications Management, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "machines.h"
#include "util.h"
void checkrc(int rc) {
if (rc != MACH_OKAY) {
printf("non-zero rc %d\n", rc);
exit(rc);
}
}
void rcprintf(int rc, char *fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
checkrc(rc);
}
int printer(JSON js) {
printf("emitting %s", js);
return 0;
}
int main(int argc, char **argv) {
mach_set_ctx(mach_make_ctx());
int rc = mach_open();
if (rc != MACH_OKAY) {
printf("mach_open error %d\n", rc);
exit(rc);
}
mach_set_spec_provider(NULL, specProvider, MACH_FREE_FOR_PROVIDER);
rc = mach_set_spec_cache_limit(32);
if (rc != MACH_OKAY) {
printf("warning: failed to set spec cache size\n");
}
rc = evalFiles(argc, argv);
if (MACH_OKAY != rc) {
exit(rc);
}
/* Most of functions in the Machines library provide output by
writing JSON to a char*. We allocate that destination here. */
size_t dst_limit = 128*1024;
char *dst = (char*) malloc(dst_limit);
/* A quick utility test. The API function mach_eval is only a
utility function to execute arbitrary Javascript. You probably
won't use this function in production code.
Note that we return a JSON representation of what we
compute. */
rc = mach_eval("JSON.stringify(1+2)", dst, dst_limit);
rcprintf(rc, "eval %s\n", dst);
/* The library function mach_process gives a message to a single
machine. See mach_crew_process for a function that gives a
message to a set ("crew") of machines.
The given spec should be resolved by the spec provider set via
mach_set_spec_provider, and the result should be JSON that
represents a Machines specification.
The return value is a JSON string that represents any state
changes and messages to be emitted. */
rc = mach_process("{\"spec\":\"double\",\"bs\":{\"count\":0},\"node\":\"start\"}",
"{\"double\":10}",
dst,
dst_limit);
rcprintf(rc, "process %s\n", dst);
/* The library function mach_match is another utility function
that probably would not be used in production. This function
invokes pattern matching. The first argument is a JSON
representation of a pattern, and the second argument is a JSON
representation of a message. The result is JSON representing
zero or more bindings. */
rc = mach_match("{\"wants\":\"?wants\"}", "{\"wants\":\"tacos\"}", "{}", dst, dst_limit);
rcprintf(rc, "match %s\n", dst);
/* Make a crew object. A crew is a set of machines. */
char * crew = (char*) malloc(dst_limit);
rc = mach_make_crew("simpsons", crew, dst_limit);
if (rc == MACH_OKAY) {
printf("crew %s\n", crew);
} else {
rcprintf(rc, "make_crew\n");
}
/* Add a machine to the crew.
Args:
crew: JSON representing the target crew
spec name: The name (just a string) for the machine's
specification. The spec provider should be able to resolve
that name into a machine specification (in JSON).
machine id: The id (just a string) for the new machine.
initial machine bindings: In JSON.
The result is JSON representing the updated crew.
*/
rc = mach_set_machine(crew, "hal", "hal9000", "{}", "start", dst, dst_limit);
if (rc == MACH_OKAY) {
printf("added %s\n", dst);
} else {
rcprintf(rc, "set_machine\n");
}
/* Update our crew definition. */
strcpy(crew, dst);
/* Remove that machine. */
rc = mach_rem_machine(crew, "hal", dst, dst_limit);
if (rc == MACH_OKAY) {
printf("removed %s\n", dst);
} else {
rcprintf(rc, "rem_machine\n");
}
strcpy(crew, dst);
/* Add another machine to the crew. */
rc = mach_set_machine(crew, "doubler", "double", "{\"count\":0}", "start", dst, dst_limit);
if (rc == MACH_OKAY) {
printf("added %s\n", dst);
} else {
printf("rc %d\n", rc);
}
strcpy(crew, dst);
/* Process a message. */
char * steppeds = (char*) malloc(dst_limit);
rc = mach_crew_process(crew, "{\"double\":10}", steppeds, dst_limit);
if (rc == MACH_OKAY) {
printf("processed %s\n", steppeds);
} else {
printf("rc %d\n", rc);
}
/* That stepped business includes state changes for machines
that moved as well as any messages emitted by the machines.
"Emitted" really means "generated". At this point, no
messages have actually gone anywhere.
So we'll first deal with any "emitted" messages.
*/
/* Allocate some space to hold the messages we want to examine. */
char * emitted[16];
int i;
for (i = 0; i < 16; i++) {
emitted[i] = (char*) malloc(dst_limit);
}
/* Now parse the 'steppeds' data to extract the generated messages. */
rc = mach_get_emitted(steppeds, emitted, 16, dst_limit);
if (rc == MACH_OKAY) {
for (i = 0; i < 16; i++) {
JSON msg = emitted[i];
if (msg[0]) {
/* Here's a message generated by a machine. We should
really do something with this message, but we
don't. */
printf("emitted %s\n", emitted[i]);
}
}
} else {
printf("emitted error rc %d\n", rc);
}
/* Update the crew state.
Some machines might have changed their states. If so, we'll
update our crew to reflect these state changes.
At this point, we'd likely write out the state changes to
someplace that's durable.
Note that we could be a lot more clever. For example, we
could only write out net state changes. (Also note that a
net state change might not actually require a write if that
last state change is identical to the state of the machine
when message processing started.)
*/
rc = mach_crew_update(crew, steppeds, dst, dst_limit);
if (rc == MACH_OKAY) {
printf("updated %s\n", dst);
} else {
printf("rc %d\n", rc);
}
strcpy(crew, dst);
{
/* Let's run a bunch of messages through our sophisticated
'double' machine. */
int iterations = 10;
int i;
char * msg = (char *) malloc(dst_limit);
for (i = 0; i < iterations; i++) {
snprintf(msg, dst_limit, "{\"double\": %d}", 100*i);
rc = mach_crew_process(crew, msg, steppeds, dst_limit);
if (rc == MACH_OKAY) {
printf("%d processed %s\n", i, steppeds);
} else {
printf("%d processed error rc %d\n", i, rc);
}
/* Show the messages we generated. */
if ((rc = mach_do_emitted(steppeds, printer)) == MACH_OKAY) {
} else {
printf("%d emitted error rc %d\n", i, rc);
}
/* Update our crew state. */
rc = mach_crew_update(crew, steppeds, dst, dst_limit);
if (rc == MACH_OKAY) {
printf("%d updated %s\n", i, dst);
} else {
printf("rc %d\n", rc);
}
strcpy(crew, dst);
}
free(msg);
}
for (i = 0; i < 16; i++) {
free(emitted[i]);
}
free(steppeds);
/* Let's peek into the crew to find the number of times that our
'double' machine doubled something. */
char * src = (char *) malloc(dst_limit);
snprintf(src, dst_limit, "JSON.stringify((%s).machines.doubler.bs.count)", crew);
rc = mach_eval(src, dst, dst_limit);
free(src);
if (rc == MACH_OKAY) {
printf("processed %s\n", dst);
} else {
rcprintf(rc, "mach_eval\n");
}
free(dst);
free(crew);
mach_close();
free(mach_get_ctx());
}