Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tokio codec #18

Merged
merged 8 commits into from
Apr 11, 2024
Merged

Conversation

RaulTrombin
Copy link
Member

@RaulTrombin RaulTrombin commented Apr 9, 2024

This fully implement codec structure using tokio_util Decoder/Encoder.

Add codec and error modules to src.

Codec have it's own tests.

There is an example/test using it with tokio runtime:
cargo test --features local_runner

Also fails with any panic as the one related in this issue for msg_id=1300.

@RaulTrombin RaulTrombin force-pushed the Add_tokio_codec branch 3 times, most recently from f2d2d79 to 69a31c8 Compare April 9, 2024 19:07
@RaulTrombin RaulTrombin marked this pull request as ready for review April 9, 2024 19:13

// Assert that the encoded bytes match the original buffer
assert_eq!(encoded.to_vec(), buffer);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a text to check a wrong buffer fails.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

eprintln!("Error on serial write: {:?}", e);
}
}
sleep(Duration::from_millis(200)).await;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just remove this sleep ?

Copy link
Member Author

@RaulTrombin RaulTrombin Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests still fine. Ok

}

#[tokio::test]
#[cfg_attr(not(feature = "local_runner"), ignore)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to run_ping_test ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

different test runtime, also would require pass all dev-dependencies to source, to early, would like to do this after devices.

);

match Messages::try_from(&item) {
Ok(Messages::Ping1D(ping1d::Messages::DeviceId(answer))) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should print all messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, fill the match arms for each get method request

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs #21

}
Ok(_) => {}
Err(e) => {
eprintln!("Error on decoder: {:?}", e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should panic here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

panic for both cases: "Unexpected package" and "Error on decoder"

Cargo.toml Outdated Show resolved Hide resolved
tests/async_codec.rs Outdated Show resolved Hide resolved
tests/async_codec.rs Outdated Show resolved Hide resolved
Comment on lines 31 to 36
match write_result {
Ok(_) => (),
Err(e) => {
eprintln!("Error on serial write: {:?}", e);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we panic here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should, thks

Comment on lines 47 to 51
println!(
"Received: {} packages, actual package: {:?}",
{ n + 1 },
item
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we use an assert to do this check instead of a println?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert!(item.has_valid_crc());

src/codec.rs Outdated Show resolved Hide resolved
Comment on lines +32 to +44
crate::decoder::DecoderResult::InProgress => {
consumed += 1;
if consumed == src.len() {
src.advance(consumed)
}
}
crate::decoder::DecoderResult::Success(msg) => {
src.advance(consumed + 1);
return Ok(Some(msg));
}
crate::decoder::DecoderResult::Error(e) => {
src.advance(consumed + 1);
return Err(PingError::ParseError(e));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checking... The first match variant modifies consumed, but the next two, don't. Is this the intent?

Copy link
Member Author

@RaulTrombin RaulTrombin Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, will just advance with (consumed+1(actual_byte)).
and in a specif case, when somehow receive a half package, it will advance with consumed and in next loop will check if a byte is available.


fn encode(&mut self, item: ProtocolMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.extend_from_slice(&item.serialized());
Ok(())
}
}

#[cfg(test)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be squashed with e841a96

);

match Messages::try_from(&item) {
Ok(Messages::Ping1D(ping1d::Messages::DeviceId(answer))) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs #21

tests/async_codec.rs Outdated Show resolved Hide resolved
@patrickelectric patrickelectric merged commit 155dcd8 into bluerobotics:master Apr 11, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants