2011-07-22 18:22:20 -05:00
|
|
|
use std;
|
2011-08-15 18:38:23 -05:00
|
|
|
import std::vec;
|
2011-08-13 17:20:11 -05:00
|
|
|
import std::task;
|
|
|
|
import std::comm;
|
2011-08-25 13:20:43 -05:00
|
|
|
import std::comm::chan;
|
|
|
|
import std::comm::port;
|
|
|
|
import std::comm::recv;
|
2011-08-13 17:20:11 -05:00
|
|
|
import std::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-13 17:37:07 -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-13 17:37:07 -05:00
|
|
|
fn# packager(&&args: (chan<chan<[u8]>>, chan<msg>)) {
|
|
|
|
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-07-22 18:22:20 -05:00
|
|
|
log "waiting for bytes";
|
2011-08-25 13:20:43 -05:00
|
|
|
let data = recv(p);
|
2011-07-22 18:22:20 -05:00
|
|
|
log "got bytes";
|
2011-08-19 17:16:48 -05:00
|
|
|
if vec::len(data) == 0u { log "got empty bytes, quitting"; break; }
|
2011-07-22 18:22:20 -05:00
|
|
|
log "sending non-empty buffer of length";
|
2011-08-15 18:38:23 -05:00
|
|
|
log vec::len(data);
|
2011-08-13 17:20:11 -05:00
|
|
|
send(msg, received(data));
|
2011-07-22 18:22:20 -05:00
|
|
|
log "sent non-empty buffer";
|
|
|
|
}
|
|
|
|
log "sending closed message";
|
2011-08-13 17:20:11 -05:00
|
|
|
send(msg, closed);
|
2011-07-22 18:22:20 -05:00
|
|
|
log "sent closed message";
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:37:07 -05:00
|
|
|
let pack = task::spawn2((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 17:37:07 -05:00
|
|
|
let prod = task::spawn2(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 {
|
|
|
|
closed. { log "Got close message"; break; }
|
|
|
|
received(data) {
|
|
|
|
log "Got data. Length is:";
|
2011-08-13 02:10:18 -05:00
|
|
|
log 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
|
|
|
}
|