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;
|
|
|
|
import std::comm::_chan;
|
|
|
|
import std::comm::_port;
|
|
|
|
import std::comm::mk_port;
|
|
|
|
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-08-10 11:27:22 -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-08-10 11:27:22 -05:00
|
|
|
fn packager(cb: _chan<_chan<[u8]>>, msg: _chan<msg>) {
|
|
|
|
let p: _port<[u8]> = mk_port();
|
2011-08-13 17:20:11 -05:00
|
|
|
send(cb, p.mk_chan());
|
2011-07-27 07:19:39 -05:00
|
|
|
while true {
|
2011-07-22 18:22:20 -05:00
|
|
|
log "waiting for bytes";
|
2011-08-13 17:20:11 -05:00
|
|
|
let data = p.recv();
|
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-10 11:27:22 -05:00
|
|
|
let p: _port<msg> = mk_port();
|
|
|
|
let recv_reader: _port<_chan<[u8]>> = mk_port();
|
2011-08-19 17:16:48 -05:00
|
|
|
let pack =
|
|
|
|
task::_spawn(bind packager(recv_reader.mk_chan(), p.mk_chan()));
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-10 11:27:22 -05:00
|
|
|
let source_chan: _chan<[u8]> = recv_reader.recv();
|
2011-08-13 17:20:11 -05:00
|
|
|
let prod = task::_spawn(bind producer(source_chan));
|
2011-07-22 18:22:20 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
while true {
|
2011-08-13 17:20:11 -05:00
|
|
|
let msg = p.recv();
|
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
|
|
|
}
|