2011-07-22 18:22:20 -05:00
|
|
|
use std;
|
2011-12-13 18:25:51 -06:00
|
|
|
import vec;
|
|
|
|
import task;
|
|
|
|
import comm;
|
|
|
|
import comm::chan;
|
|
|
|
import comm::port;
|
|
|
|
import comm::recv;
|
|
|
|
import comm::send;
|
2011-07-22 18:22:20 -05:00
|
|
|
|
2011-07-18 10:41:35 -05:00
|
|
|
tag msg { closed; received([u8]); }
|
2011-07-22 18:22:20 -05:00
|
|
|
|
2011-10-20 22:34:04 -05:00
|
|
|
fn producer(c: chan<[u8]>) {
|
2011-08-19 17:16:48 -05:00
|
|
|
send(c, [1u8, 2u8, 3u8, 4u8]);
|
|
|
|
let empty: [u8] = [];
|
2011-08-13 17:20:11 -05:00
|
|
|
send(c, empty);
|
2011-07-22 18:22:20 -05:00
|
|
|
}
|
|
|
|
|
2011-10-20 22:34:04 -05:00
|
|
|
fn packager(&&args: (chan<chan<[u8]>>, chan<msg>)) {
|
2011-10-13 17:37:07 -05:00
|
|
|
let (cb, msg) = args;
|
2011-08-25 13:20:43 -05:00
|
|
|
let p: port<[u8]> = port();
|
|
|
|
send(cb, chan(p));
|
2011-07-27 07:19:39 -05:00
|
|
|
while true {
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("waiting for bytes");
|
2011-08-25 13:20:43 -05:00
|
|
|
let data = recv(p);
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("got bytes");
|
|
|
|
if vec::len(data) == 0u {
|
|
|
|
#debug("got empty bytes, quitting");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#debug("sending non-empty buffer of length");
|
|
|
|
log_full(core::debug, vec::len(data));
|
2011-08-13 17:20:11 -05:00
|
|
|
send(msg, received(data));
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("sent non-empty buffer");
|
2011-07-22 18:22:20 -05:00
|
|
|
}
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("sending closed message");
|
2011-08-13 17:20:11 -05:00
|
|
|
send(msg, closed);
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("sent closed message");
|
2011-07-22 18:22:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2011-08-25 13:20:43 -05:00
|
|
|
let p: port<msg> = port();
|
|
|
|
let recv_reader: port<chan<[u8]>> = port();
|
2011-10-13 23:23:07 -05:00
|
|
|
let pack = task::spawn((chan(recv_reader), chan(p)), packager);
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-25 13:20:43 -05:00
|
|
|
let source_chan: chan<[u8]> = recv(recv_reader);
|
2011-10-13 23:23:07 -05:00
|
|
|
let prod = task::spawn(source_chan, producer);
|
2011-07-22 18:22:20 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
while true {
|
2011-08-25 13:20:43 -05:00
|
|
|
let msg = recv(p);
|
2011-07-27 07:19:39 -05:00
|
|
|
alt msg {
|
2011-12-22 16:42:52 -06:00
|
|
|
closed. { #debug("Got close message"); break; }
|
2011-07-27 07:19:39 -05:00
|
|
|
received(data) {
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("Got data. Length is:");
|
|
|
|
log_full(core::debug, vec::len::<u8>(data));
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-07-22 18:22:20 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-18 10:41:35 -05:00
|
|
|
}
|