Skip to content

Commit d1a23a1

Browse files
committed
Start moving to a trait for Participant API
process function still needs some work
1 parent 7072648 commit d1a23a1

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

examples/repeat.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
21
extern crate msgflo;
32

4-
use msgflo::participant::{Participant, InfoBuilder};
3+
use msgflo::participant::{ParticipantTrait, InfoBuilder, Info};
54

6-
fn main() {
7-
let info = InfoBuilder::new("rust/Repeat")
8-
.label("Repeats input as-is")
9-
.inport("in")
10-
.outport("out")
11-
.build();
5+
struct Repeat {
6+
state: Option<String>, // we don't really have any state
7+
}
8+
9+
impl ParticipantTrait for Repeat {
1210

13-
fn process_repeat(input: Vec<u8>) -> Result<Vec<u8>, Vec<u8>> {
14-
println!("process_repeat:");
15-
return Ok(input);
11+
fn info(&self) -> Info {
12+
InfoBuilder::new("rust/Repeat")
13+
.label("Repeats input as-is")
14+
.inport("in")
15+
.outport("out")
16+
.build()
1617
}
1718

18-
let p = Participant { info: info, process: process_repeat };
19+
}
20+
21+
fn process(input: Vec<u8>) -> Result<Vec<u8>, Vec<u8>> {
22+
println!("repeat process():");
23+
return Ok(input);
24+
}
1925

20-
msgflo::participant::main(p);
26+
fn main() {
27+
let r = Repeat { state: None };
28+
msgflo::participant::main(&r, process);
2129
}

src/participant.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ impl InfoBuilder {
6767

6868
type SendFunction = fn(String, Vec<u8>);
6969
pub type ProcessFunction = fn(Vec<u8>) -> Result<Vec<u8>, Vec<u8>>;
70-
pub struct Participant {
71-
pub info: Info,
72-
pub process: ProcessFunction,
70+
71+
pub trait ParticipantTrait {
72+
fn info(&self) -> Info;
73+
// fn process(&self, Vec<u8>) -> Result<Vec<u8>, Vec<u8>>;
74+
}
75+
76+
struct Participant {
77+
info: Info,
78+
process: ProcessFunction
7379
}
7480

7581
struct Connection {
@@ -265,13 +271,15 @@ fn parse(options: &mut Options) {
265271
// XXX: seems rust-amqp makes program hangs forever if error occurs / channel is borked?
266272
// TODO: pass port info in/out of process()
267273
// TODO: nicer way to declare ports? ideally they are enums not stringly typed?
268-
pub fn main(orig: Participant) {
274+
pub fn main(orig: &ParticipantTrait, func: ProcessFunction) {
269275

270276
let mut options = Options { .. Default::default() };
271277
parse(&mut options);
272278

273-
let info = normalize_info(&orig.info, &options);
274-
let p = Participant { info: info, process: orig.process }; // XXX: hack
279+
let i : Info = orig.info();
280+
let info = normalize_info(&i, &options);
281+
282+
let p = Participant { info: info, process: func }; // XXX: hack
275283

276284
let mut c = start_participant(&p, &options);
277285
println!("{}({}) started", &p.info.role, &p.info.component);

0 commit comments

Comments
 (0)