ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
vmhost_spec.h
Go to the documentation of this file.
1 
7 #include "../src/ceptr.h"
8 #include "../src/vmhost.h"
9 #include "http_example.h"
10 #include "../src/shell.h"
11 #include "../src/protocol.h"
12 
15  VMHost *v = _v_new();
16  SemTable *sem = v->r->sem;
17  // test that the base contexts and defs were created
18  spec_is_equal(sem->contexts,_NUM_DEFAULT_CONTEXTS);
19  spec_is_equal(_t_children(_t_child(sem->stores[0].definitions,SEM_TYPE_SYMBOL)),NUM_SYS_SYMBOLS-1);
20  spec_is_equal(_t_children(_t_child(sem->stores[0].definitions,SEM_TYPE_STRUCTURE)),NUM_SYS_STRUCTURES-1);
21 
22  spec_is_str_equal(t2s(v->r->root),"(RECEPTOR_INSTANCE (INSTANCE_OF:SYS_RECEPTOR) (CONTEXT_NUM:0) (PARENT_CONTEXT_NUM:-1) (RECEPTOR_STATE (FLUX (DEFAULT_ASPECT (EXPECTATIONS) (SIGNALS))) (PENDING_SIGNALS) (PENDING_RESPONSES) (CONVERSATIONS) (RECEPTOR_ELAPSED_TIME:0)))");
23 
24  // test the installed receptors scape
25  spec_is_sem_equal(v->installed_receptors->key_source,RECEPTOR_IDENTIFIER);
26  spec_is_sem_equal(v->installed_receptors->data_source,RECEPTOR_SURFACE);
27  _v_free(v);
29 }
30 
31 /* #define HTTP_SERVER_RECEPTOR_UUID 4321 */
32 /* /\** */
33 /* * generate an http server receptor package */
34 /* * */
35 /* * @snippet spec/vmhost_spec.h makeTestHTTPServerReceptorPackage */
36 /* *\/ */
37 /* //! [makeTestReceptorPackage] */
38 /* T *_makeTestHTTPServerReceptorPackage() { */
39 /* T *p = _t_new_root(RECEPTOR_PACKAGE); */
40 /* T *m = _t_newr(p,MANIFEST); // specification for which manifest variables can be configured */
41 
42 /* _t_newi(p,RECEPTOR_IDENTIFIER,HTTP_SERVER_RECEPTOR_UUID); */
43 /* T *defs = _t_newr(p,DEFINITIONS); */
44 /* _t_add(defs,_t_clone(test_HTTP_structures)); */
45 /* _t_add(defs,_t_clone(test_HTTP_symbols)); */
46 
47 /* T *procs = _t_newr(defs,PROCESSES); */
48 /* // initialization process installs the listener for host registration */
49 /* //@todo for now we fake this, but adding it manually as c code in the install example */
50 
51 /* _t_newr(defs,SCAPES); // for now we don't have any scapes */
52 
53 /* // server must be instantiated in the context of an octet stream input and output carriers */
54 /* T *aspects = _t_newr(p,ASPECTS); */
55 /* T *a = _t_newr(aspects,ASPECT_DEF); */
56 /* _t_newi(a,EXTERNAL_ASPECT,DEFAULT_ASPECT); */
57 /* _t_newi(a,CARRIER,OCTET_STREAM); */
58 /* _t_newi(a,CARRIER,OCTET_STREAM); */
59 
60 /* //@todo internally server speaks to document providers */
61 /* a = _t_newr(aspects,ASPECT_DEF); */
62 /* _t_newi(a,INTERNAL_ASPECT,DOCUMENT_PROVIDER); */
63 /* _t_newi(a,CARRIER,DOCUMENT); */
64 /* _t_newi(a,CARRIER,DOCUMENT_PATH); */
65 
66 /* //@todo internally server speaks to host registerers */
67 /* a = _t_newr(aspects,ASPECT_DEF); */
68 /* _t_newi(a,INTERNAL_ASPECT,HOST_REGISTRY); */
69 /* _t_newi(a,CARRIER,HOST_REGISTRATION_REQUEST); */
70 /* _t_newi(a,CARRIER,HOST_REGISTRATION_RESULT); */
71 
72 /* return p; */
73 /* } */
74 /* //! [makeTestReceptorPackage] */
75 
76 /* #define HELLO_WORLD_UUID 12345 */
77 /* /\** */
78 /* * generate a a document server receptor package (which can attach to a an http server) */
79 /* * */
80 /* * @snippet spec/vmhost_spec.h makeTestHTTPAppReceptorPackage */
81 /* *\/ */
82 /* //! [makeTestHTTPAppReceptorPackage] */
83 /* T *_makeTestHTTPAppReceptorPackage() { */
84 /* T *p = _t_new_root(RECEPTOR_PACKAGE); */
85 /* T *m = _t_newr(p,MANIFEST); */
86 /* T *mp = _t_newr(m,MANIFEST_PAIR); */
87 /* _t_new(mp,MANIFEST_LABEL,"host",5); // binds to a host value */
88 /* _t_newi(mp,MANIFEST_SPEC,HTTP_REQUEST_HOST); */
89 /* // T *me = _t_newr(mp,PATTERN); */
90 
91 /* _t_newi(p,RECEPTOR_IDENTIFIER,HELLO_WORLD_UUID); */
92 
93 /* T *defs = _t_newr(p,DEFINITIONS); */
94 /* _t_add(defs,_t_clone(test_HTTP_structures)); */
95 /* _t_add(defs,_t_clone(test_HTTP_symbols)); */
96 /* T *procs = _t_newr(defs,PROCESSES); */
97 /* // a process that simply reduces to an HTTP_RESPONSE indicating an off-line status */
98 /* T *resp = _t_new_root(RESPOND); */
99 /* _t_news(resp,CARRIER,HTTP_RESPONSE);*/
100 /* T *http_resp = _t_newr(resp,HTTP_RESPONSE); */
101 /* _t_new(http_resp,HTTP_RESPONSE_CONTENT_TYPE,"Text/Plain",11); */
102 /* _t_new(http_resp,TEST_STR_SYMBOL,"Hello World!",13); */
103 /* T *input = _t_new_root(INPUT_SIGNATURES); */
104 /* T *output = _t_new_root(OUTPUT_SIGNATURE); */
105 /* _d_define_process(procs,resp,"hellow","respond with hello",signature); */
106 
107 /* _t_newr(defs,SCAPES); // for now we don't have any scapes */
108 
109 /* // an hello world app speaks the document provider protocol which operates on */
110 /* // a DOCUMENT_PATH input carrier and a DOCUMENT output carrier */
111 /* T *aspects = _t_newr(p,ASPECTS); */
112 /* T *a = _t_newr(aspects,ASPECT_DEF); */
113 /* _t_newi(a,EXTERNAL_ASPECT,DEFAULT_ASPECT); */
114 /* _t_newi(a,CARRIER,DOCUMENT_PATH); */
115 /* _t_newi(a,CARRIER,DOCUMENT); */
116 
117 /* return p; */
118 /* } */
119 /* //! [makeTestHTTPAppReceptorPackage] */
120 
121 /* void testVMHostLoadReceptorPackage() { */
122 /* //! [testVMHostLoadReceptorPackage] */
123 /* VMHost *v = _v_new(); */
124 /* T *p = _makeTestHTTPAppReceptorPackage(); */
125 
126 /* Xaddr x = _v_load_receptor_package(v,p); */
127 /* T *p1 = _r_get_instance(v->c,x); */
128 
129 /* spec_is_ptr_equal(p,p1); */
130 /* _v_free(v); */
131 /* //! [testVMHostLoadReceptorPackage] */
132 /* } */
133 
134 /* void testVMHostInstallReceptor() { */
135 /* //! [testVMHostInstallReceptor] */
136 /* VMHost *v = _v_new(); */
137 
138 /* T *p = _makeTestHTTPServerReceptorPackage(); */
139 /* Xaddr xp = _v_load_receptor_package(v,p); */
140 
141 /* Xaddr x = _v_install_r(v,xp,0,"hello world"); */
142 
143 /* // installing the receptor should instantiate a receptor from the package with the given bindings and symbol label */
144 /* T *r = _r_get_instance(v->r,x); */
145 /* spec_is_symbol_equal(v->r,x.symbol,INSTALLED_RECEPTOR); */
146 /* spec_is_symbol_equal(v->r,_t_symbol(_t_child(r,1)),_r_get_symbol_by_label(v->r,"hello world")); */
147 
148 /* // and the definition labels of the instantiated receptor should all be set up properly */
149 /* Receptor *httpr = (Receptor *)_t_surface(_t_child(r,1)); */
150 /* spec_is_equal(_r_get_symbol_by_label(httpr,"HTTP_REQUEST"),HTTP_REQUEST); */
151 /* spec_is_equal(_r_get_symbol_by_label(httpr,"hellow"),-1); // hard-coded process symbol */
152 
153 /* // trying to re-install the receptor should fail */
154 /* x = _v_install_r(v,xp,0,"hellow world"); */
155 /* spec_is_true(is_null_xaddr(x)); */
156 
157 /* // because the receptor's id is in the installed_receptors scape */
158 /* T *pack = _r_get_instance(v->c,xp); */
159 /* T *id = _t_child(pack,2); */
160 /* TreeHash h = _t_hash(v->r->sem,id); */
161 /* spec_is_xaddr_equal(v->r->sem,_s_get(v->installed_receptors,h),xp); */
162 
163 /* _v_free(v); */
164 /* //! [testVMHostInstallReceptor] */
165 /* } */
166 
167 /* void testVMHostActivateReceptor() { */
168 /* //! [testVMHostActivateReceptor] */
169 /* VMHost *v = _v_new(); */
170 
171 /* // create and install a stub HTTP server receptor */
172 /* T *httpd_rp = _makeTestHTTPServerReceptorPackage(); */
173 /* Xaddr httpd_px = _v_load_receptor_package(v,httpd_rp); */
174 /* Xaddr httpd_x = _v_install_r(v,httpd_px,0,"http server"); */
175 /* T *installed_httpd = _r_get_instance(v->r,httpd_x); */
176 /* Receptor *httpd_r = (Receptor *)_t_surface(_t_child(installed_httpd,1)); */
177 
178 /* // create and install an app bound to a HOST */
179 /* T *p = _makeTestHTTPAppReceptorPackage(); */
180 /* Xaddr xp = _v_load_receptor_package(v,p); */
181 
182 /* T *b = _t_new_root(BINDINGS); */
183 /* T *pair = _t_newr(b,BINDING_PAIR); */
184 /* char *host = "helloworld.com"; */
185 
186 /* _t_new(pair,MANIFEST_LABEL,"host",5); */
187 /* _t_new(pair,HTTP_REQUEST_HOST,host,strlen(host)); */
188 
189 /* Xaddr x = _v_install_r(v,xp,b,"hello world app"); */
190 /* T *installed_hellow = _r_get_instance(v->r,x); */
191 /* Receptor *hello_r = (Receptor *)_t_surface(_t_child(installed_hellow,1)); */
192 
193 /* // add a listener that matches on any request with "Host: helloworld.org" */
194 /* // /HTTP_REQUEST/.*,HTTP_REQUEST_HOST=helloworld.org */
195 /* T *act = _t_newp(0,ACTION,-1); */
196 /* T *expect = _t_new_root(PATTERN); */
197 /* T *req = _t_newi(expect,SEMTREX_SYMBOL_LITERAL,HTTP_REQUEST); */
198 /* T *ss = _t_newi(req,SEMTREX_SEQUENCE,0); */
199 /* T *sss = _t_newi(ss,SEMTREX_ZERO_OR_MORE,0); */
200 /* _t_newi(sss,SEMTREX_SYMBOL_ANY,0); */
201 
202 /* int size = sizeof(Svalue)+strlen(host)+1; */
203 /* Svalue *sv = malloc(size); */
204 /* sv->symbol = HTTP_REQUEST_HOST; */
205 /* sv->flags = 0; */
206 /* sv->count = 1; */
207 /* sv->length = strlen(host); */
208 /* strcpy((char *)&sv->value,host); */
209 /* _t_new(ss,SEMTREX_VALUE_LITERAL,sv,size); */
210 
211 /* char buf[2000]; */
212 /* // just check that we got our semtrex right */
213 /* spec_is_str_equal(_dump_semtrex(G_sem,req,buf),"/(HTTP_REQUEST/.*,HTTP_REQUEST_HOST=helloworld.com)"); */
214 /* free(sv); */
215 
216 /* _r_add_expectation(hello_r,DEFAULT_ASPECT,HTTP_REQUEST,expect,act,0,0,0); */
217 
218 /* // build up an HTTP_REQUEST tree that corresponds with a simple get for host helloworld.com */
219 /* T *s = _t_new_root(HTTP_REQUEST); */
220 /* T *s_version = _t_newr(s,HTTP_REQUEST_VERSION); */
221 /* _t_newi(s_version,VERSION_MAJOR,1); */
222 /* _t_newi(s_version,VERSION_MINOR,1); */
223 /* T *s_method = _t_new(s,HTTP_REQUEST_METHOD,"GET",4); */
224 /* T *s_path = _t_newr(s,HTTP_REQUEST_PATH); */
225 /* T *s_host = _t_new(s,HTTP_REQUEST_HOST,"helloworld.com",15); */
226 
227 /* // put it on the flux to simulate that it has just been parsed out of an octet stream from a TCP/IP receptor */
228 /* /// @todo */
229 /* //int path[] = {1,2,TREE_PATH_TERMINATOR}; */
230 /* //T *fs = _t_get(httpd_r->flux,path); */
231 
232 
233 
234 /* //simulate that an HTTP_Request signal from the http_server receptor has been sent */
235 
236 
237 /* Xaddr xs = _v_send(v,x,httpd_x,DEFAULT_ASPECT,s); */
238 
239 /* // confirm that the signal has not been delivered and is still in pending list (because receptors aren't active) */
240 /* spec_is_equal(_t_children(v->pending_signals),1); */
241 
242 /* // activate the two receptors */
243 /* _v_activate(v,httpd_x); */
244 /* _v_activate(v,x); */
245 
246 Receptor *_makeAliveProtocolReceptor(VMHost *v);
247 
250  VMHost *v = _v_new();
251  SemTable *sem = v->r->sem;
252 
253  Receptor *server = _r_new(sem,TEST_RECEPTOR);
254  _o_express_role(server,ALIVE,SERVER,DEFAULT_ASPECT,NULL);
255 
256  Receptor *client = _r_new(sem,TEST_RECEPTOR);
257 
258  T *noop = _t_new_root(NOOP);
259  _t_newi(noop,TEST_INT_SYMBOL,314);
260  Process proc = _r_define_process(client,noop,"do nothing","long desc...",NULL,NULL);
261 
262  T *binding = _t_new_root(PROTOCOL_BINDINGS);
263  T *res = _t_newr(binding,RESOLUTION);
264  _t_news(res,GOAL,HANDLER);
265  _t_news(res,ACTUAL_PROCESS,proc);
266  _o_express_role(client,ALIVE,CLIENT,DEFAULT_ASPECT,binding);
267  _t_free(binding);
268 
269  Xaddr sx = _v_new_receptor(v,v->r,TEST_RECEPTOR,server);
270  Xaddr cx = _v_new_receptor(v,v->r,TEST_RECEPTOR,client);
271  _v_activate(v,sx);
272  _v_activate(v,cx);
273 
274  // confirm that the activate list has new children
275  spec_is_equal(v->active_receptor_count,2);
276 
277  // and that they are the same as the installed receptors
278  Receptor *ar;
279  ar = v->active_receptors[0].r;
280  spec_is_ptr_equal(ar,server);
281  ar = v->active_receptors[1].r;
282  spec_is_ptr_equal(ar,client);
283 
284  _v_send(v,client->addr,server->addr,DEFAULT_ASPECT,ALIVE,_t_newi(0,PING,0));
285 
286  spec_is_str_equal(_td(client,v->r->pending_signals),"(PENDING_SIGNALS (SIGNAL (ENVELOPE (FROM_ADDRESS (CONTEXT_NUM:3)) (TO_ADDRESS (CONTEXT_NUM:2)) (ASPECT_IDENT:DEFAULT_ASPECT) (CARRIER:ALIVE) (SIGNAL_UUID)) (BODY:{(PING)})))");
287 
288  // simulate round-robin processing of signals
289  //debug_enable(D_SIGNALS);
290  _v_deliver_signals(v,v->r);
291  _p_reduceq(server->q);
292  _v_deliver_signals(v,server);
293  // _p_reduceq(client->q);
294 
295  // now confirm that the signal was sent,
296  // first that the pending signals list is empty
297  spec_is_equal(_t_children(v->r->pending_signals),0);
298 
299  // @todo delivery of response doesn't work here because _v_send is broken and there's no process
300  // q or anything for the delivery to unblock. This is a broken spec that we need to fix!!
301  // debug_disable(D_SIGNALS);
302 
303  _v_free(v);
305 }
306 
307 
308 void testVMHostShell() {
309 
310  // set up the vmhost
311  G_vm = _v_new();
312  SemTable *gsem = G_sem;
313  G_sem = G_vm->sem;
314 
316 
317  // allocate c input out streams to mimic stdin and stdout
318  FILE *input,*output;
319  char commands[] = "receptors\ntime\n";
320  input = fmemopen(commands, strlen(commands), "r");
321  char *output_data = NULL;
322  size_t size;
323  output = open_memstream(&output_data,&size);
324 
325  Stream *output_stream, *input_stream;
326  Receptor *i_r,*o_r;
327 
328  makeShell(G_vm,input,output,&i_r,&o_r,&input_stream,&output_stream);
329 
330  // debug_enable(D_STREAM+D_SIGNALS+D_TREE+D_PROTOCOL+D_STEP);
331  _v_start_vmhost(G_vm);
332  sleep(1);
333  debug_disable(D_STREAM+D_SIGNALS+D_TREE+D_PROTOCOL);
334 
335  // confirm that the completed processes get cleaned up in the course of the vmhosts processing
336  spec_is_ptr_equal(i_r->q->completed,NULL);
337  spec_is_ptr_equal(o_r->q->completed,NULL);
338 
339  // kill the string after elapsed time because it's system dependent
340  T *e = _t_child(_t_child(i_r->root,ReceptorInstanceStateIdx),ReceptorElapsedTimeIdx);
341  spec_is_true(*(int *)_t_surface(e)>0);
342 
343  spec_is_true(output_data != 0); // protect against seg-faults when nothing was written to the stream...
344  if (output_data != 0) {
345  output_data[121] =0; // clip the tick so it work regardless of the time
346  spec_is_str_equal(output_data,"COMPOSITORY:0 DEV_COMPOSITORY:1 TEST_RECEPTOR:2 CLOCK_RECEPTOR:3 shell:4 STREAM_EDGE:5 STREAM_EDGE:6 \n(TICK (TODAY (YEAR:");}
347  __r_kill(G_vm->r);
348 
349  _v_join_thread(&G_vm->clock_thread);
350  _v_join_thread(&G_vm->vm_thread);
351 
352  // free the memory used by the SYS_RECEPTOR
353  _v_free(G_vm);
354  G_vm = NULL;
355  G_sem = gsem;
356 
357  _st_free(input_stream);
358  _st_free(output_stream);
359  free(output_data);
360 }
361 
362 void testVMHostSerialize() {
363  G_vm = _v_new();
365 
366  spec_is_str_equal(t2s(G_vm->r->root),"(SYS_RECEPTOR (DEFINITIONS (STRUCTURES) (SYMBOLS) (PROCESSES) (PROTOCOLS) (SCAPES)) (FLUX (DEFAULT_ASPECT (EXPECTATIONS) (SIGNALS))) (RECEPTOR_STATE) (PENDING_SIGNALS) (PENDING_RESPONSES))");
367 
368  Receptor *clock = G_vm->active_receptors[0].r;
369  // _testReceptorClockAddExpectation(clock);
370 
371  void *surface;
372  size_t length;
373  _r_serialize(G_vm->r,&surface,&length);
374  Receptor *r = _r_unserialize(G_vm->r->sem,surface);
375  spec_is_str_equal(t2s(r->root),"(SYS_RECEPTOR (DEFINITIONS (STRUCTURES) (SYMBOLS) (PROCESSES) (PROTOCOLS) (SCAPES)) (FLUX (DEFAULT_ASPECT (EXPECTATIONS) (SIGNALS))) (RECEPTOR_STATE) (PENDING_SIGNALS) (PENDING_RESPONSES))");
376 
377  __r_kill(clock);
378  _v_join_thread(&G_vm->clock_thread);
379 
380  // __r_dump_instances(r);
381  _r_free(r);
382  free(surface);
383  _v_free(G_vm);
384  G_vm=NULL;
385 }
386 void testVMHost() {
388  //testVMHostLoadReceptorPackage();
389  //testVMHostInstallReceptor();
390  //testVMHostActivateReceptor();
391  testVMHostShell();
392  // testVMHostSerialize();
393 }
T * _t_new_root(Symbol symbol)
Definition: tree.c:160
Definition: ceptr_types.h:114
void _r_serialize(Receptor *r, void **surfaceP, size_t *lengthP)
Definition: receptor.c:432
Definition: stream.h:30
void _v_send(VMHost *v, ReceptorAddress from, ReceptorAddress to, Aspect aspect, Symbol carrier, T *contents)
Definition: vmhost.c:194
void _v_instantiate_builtins(VMHost *v)
Definition: vmhost.c:324
void _r_free(Receptor *r)
Definition: receptor.c:186
Error _p_reduceq(Q *q)
Definition: process.c:2126
T * _t_child(T *t, int i)
Definition: tree.c:1251
void _v_free(VMHost *v)
Definition: vmhost.c:71
ReceptorAddress addr
the address by which to get messages to this receptor instance
Definition: ceptr_types.h:241
VMHost * _v_new()
Creates a new virtual machine host.
Definition: vmhost.c:41
Q * q
process queue
Definition: ceptr_types.h:250
void * _t_surface(T *t)
Definition: tree.c:1215
void _v_activate(VMHost *v, Xaddr x)
Definition: vmhost.c:172
Receptor * _r_new(SemTable *sem, SemanticID r)
Creates a new receptor.
Definition: receptor.c:88
SemTable * sem
Semantic Table for definitions on this host.
Definition: vmhost.h:44
Receptor * _r_unserialize(SemTable *sem, void *surface)
Definition: receptor.c:459
void testVMHostActivateReceptor()
Definition: vmhost_spec.h:248
void testVMHostCreate()
Definition: vmhost_spec.h:13
void _st_free(Stream *st)
Definition: stream.c:507
Definition: vmhost.h:40
T * root
RECEPTOR_INSTANCE semantic tree.
Definition: ceptr_types.h:238
SemTable * sem
pointer back to the genotype table for this receptor's vmhost instance
Definition: ceptr_types.h:242
ActiveReceptor active_receptors[MAX_ACTIVE_RECEPTORS]
pointer to array that holds all currently active receptors
Definition: vmhost.h:45
void _o_express_role(Receptor *r, Protocol protocol, Symbol role, Aspect aspect, T *bindings)
Definition: protocol.c:409
Receptor * r
Receptor data for this vm host.
Definition: vmhost.h:43
Qe * completed
completed processes (pending cleanup)
Definition: ceptr_types.h:210
void _v_deliver_signals(VMHost *v, Receptor *sender)
Definition: vmhost.c:235
int _t_children(T *t)
Definition: tree.c:1205
Process _r_define_process(Receptor *r, T *code, char *name, char *intention, T *signature, T *link)
Definition: receptor.c:275
void _t_free(T *t)
Definition: tree.c:526