/* Test: * get/put/read * get!/put!/read! * select */ #include #include #include #include struct res { int n; String result; res() {} res(const res& o) { result = o.result; result += "C"; n = o.n; } res & operator=(const res& o) { result = o.result; result += "A"; n = o.n; return *this; } }; useDefaultMarshal(int); useCustomMarshal(res); mStream& operator << (mStream &s, const res& d) { ((res &)d).result += "M"; s << d.n << (int)d.result.length(); s.add(d.result.chars(), d.result.length()); return s; } umStream& operator >> (umStream &s, res& d) { int len; s >> d.n >> len; char *str = (char *)malloc(len+1); s.remove(str, len); str[len] = 0; d.result = str; free(str); d.result += "u"; return s; } declareCtxType(res); template class @share; template class @share @stream; template class @share @reply; typedef @share bag; typedef @share @stream str; typedef @share @reply rep; useCtxMarshal(bag); useCtxMarshal(str); useCtxMarshal(rep); int gen_res(res *a, int seq, int test) { res b; char buf[1000]; sprintf(buf, "%9lx : A string number %d ",(long)a,seq); b.result = buf; b.n = seq; a->result = b.result; a->n = b.n; return 0; } @process receiver(bag in1, bag in2, str ok, rep r) { res *ptr, actual, extra; int errors = 0; int i; for (i=0;i<20;i++) { @select { @get(in1, actual) { @put(ok, actual); } @get!(in2, ptr) { @put!(ok, ptr); } @resource!(r, ptr) { i--; char buf[1000]; sprintf(buf, "%lx : %s X ",(long)ptr,(char *)ptr->result); ptr->result = buf; } @reply! (r, ptr); } } actual.n = -1; char buf[1000]; sprintf(buf, "receiver got %d duds ", errors); actual.result = buf; @put(ok, actual); } void paws(bag empty, int start) { res i; while (start > 0) { @select { @get(empty,i) { ; } @else { ; } } start -= 50; } } @process sender(bag out, str ok, int start, bag empty, rep r) { res a, *b; int i; for (i=0;i<5;i++) { paws(empty, start); gen_res(&a, i+start, 0); a.result += " W "; @call(r, a, a); a.result += " Y "; @put(out, a); } char buf[1000]; sprintf(buf, "%d: Wrote first 5 successfully ", start); a.result = buf; @put(ok, a); for (; i< 10;i++) { paws(empty, start); b = new res; gen_res(b, i+start, 0); b->result += " W "; @call!(r, b, b); b->result += " Z "; @put!(out , b); } b = new res; sprintf(buf, "%d: Wrote second 5 successfully ", start); b->result = buf; @put!(ok, b); @get(empty, a); } int eMain() { res *a; bag bag1, bag2; str ok; bag empty; rep r; @subordinate receiver(bag1, bag2, ok, r); @subordinate sender(bag2, ok, 0, empty, r); @subordinate sender(bag1, ok, 100, empty, r); while (1) { @get!(ok, a); printf("%8lx : %s\n", (long)a, (char *)a->result); if (a->n < 0) break; free(a); } return 0; }