-
Notifications
You must be signed in to change notification settings - Fork 16
Add L2 API examples #162
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
Merged
sophokles73
merged 3 commits into
eclipse-uprotocol:main
from
SoftwareDefinedVehicle:add_local_transport
Jul 23, 2024
Merged
Add L2 API examples #162
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use protobuf::well_known_types::wrappers::StringValue; | ||
| use up_rust::{ | ||
| communication::{CallOptions, Notifier, SimpleNotifier, UPayload}, | ||
| local_transport::LocalTransport, | ||
| LocalUriProvider, UListener, UMessage, | ||
| }; | ||
|
|
||
| struct ConsolePrinter {} | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl UListener for ConsolePrinter { | ||
| async fn on_receive(&self, msg: UMessage) { | ||
| if let Ok(payload) = msg.extract_protobuf::<StringValue>() { | ||
| println!("received notification: {}", payload.value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| pub async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| const ORIGIN_RESOURCE_ID: u16 = 0xd100; | ||
|
|
||
| let transport = Arc::new(LocalTransport::new("my-vehicle", 0xa34b, 0x01)); | ||
| let uri_provider: Arc<dyn LocalUriProvider> = transport.clone(); | ||
| let notifier = SimpleNotifier::new(transport.clone(), uri_provider.clone()); | ||
| let topic = uri_provider.get_resource_uri(ORIGIN_RESOURCE_ID); | ||
| let listener = Arc::new(ConsolePrinter {}); | ||
|
|
||
| notifier.start_listening(&topic, listener.clone()).await?; | ||
|
|
||
| let value = StringValue { | ||
| value: "Hello".to_string(), | ||
| ..Default::default() | ||
| }; | ||
| let payload = UPayload::try_from_protobuf(value)?; | ||
| notifier | ||
| .notify( | ||
| ORIGIN_RESOURCE_ID, | ||
| &uri_provider.get_source_uri(), | ||
| CallOptions::for_notification(None, None, None), | ||
| Some(payload), | ||
| ) | ||
| .await?; | ||
|
|
||
| notifier.stop_listening(&topic, listener).await?; | ||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use protobuf::well_known_types::wrappers::StringValue; | ||
| use up_rust::{ | ||
| communication::{CallOptions, Publisher, SimplePublisher, UPayload}, | ||
| local_transport::LocalTransport, | ||
| LocalUriProvider, UListener, UMessage, UTransport, | ||
| }; | ||
|
|
||
| struct ConsolePrinter {} | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl UListener for ConsolePrinter { | ||
| async fn on_receive(&self, msg: UMessage) { | ||
| if let Ok(payload) = msg.extract_protobuf::<StringValue>() { | ||
| println!("received event: {}", payload.value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| pub async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| const ORIGIN_RESOURCE_ID: u16 = 0xb4c1; | ||
| let transport = Arc::new(LocalTransport::new("my-vehicle", 0xa34b, 0x01)); | ||
| let uri_provider: Arc<dyn LocalUriProvider> = transport.clone(); | ||
| let publisher = SimplePublisher::new(transport.clone(), uri_provider.clone()); | ||
| let listener = Arc::new(ConsolePrinter {}); | ||
|
|
||
| transport | ||
| .register_listener( | ||
| &uri_provider.get_resource_uri(ORIGIN_RESOURCE_ID), | ||
| None, | ||
| listener.clone(), | ||
| ) | ||
| .await?; | ||
|
|
||
| let value = StringValue { | ||
| value: "Hello".to_string(), | ||
| ..Default::default() | ||
| }; | ||
| let payload = UPayload::try_from_protobuf(value)?; | ||
| publisher | ||
| .publish( | ||
| ORIGIN_RESOURCE_ID, | ||
| CallOptions::for_publish(None, None, None), | ||
| Some(payload), | ||
| ) | ||
| .await?; | ||
|
|
||
| transport | ||
| .unregister_listener( | ||
| &uri_provider.get_resource_uri(ORIGIN_RESOURCE_ID), | ||
| None, | ||
| listener, | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| /*! | ||
| This example illustrates how client code can use the _Communication Level API_ to invoke a | ||
| service operation. It also shows how the corresponding service provider can be implemented. | ||
| */ | ||
| use std::sync::Arc; | ||
|
|
||
| use protobuf::well_known_types::wrappers::StringValue; | ||
| use up_rust::{ | ||
| communication::{ | ||
| CallOptions, InMemoryRpcClient, InMemoryRpcServer, RequestHandler, RpcClient, RpcServer, | ||
| ServiceInvocationError, UPayload, | ||
| }, | ||
| local_transport::LocalTransport, | ||
| LocalUriProvider, | ||
| }; | ||
|
|
||
| struct EchoOperation {} | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl RequestHandler for EchoOperation { | ||
| async fn invoke_method( | ||
| &self, | ||
| _resource_id: u16, | ||
| request_payload: Option<UPayload>, | ||
| ) -> Result<Option<UPayload>, ServiceInvocationError> { | ||
| if let Some(req_payload) = request_payload { | ||
| Ok(Some(req_payload)) | ||
| } else { | ||
| Err(ServiceInvocationError::InvalidArgument( | ||
| "request has no payload".to_string(), | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| pub async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| const METHOD_RESOURCE_ID: u16 = 0x00a0; | ||
| let transport = Arc::new(LocalTransport::new("my-vehicle", 0xa34b, 0x01)); | ||
| let uri_provider: Arc<dyn LocalUriProvider> = transport.clone(); | ||
|
|
||
| // create the RpcServer using the local transport | ||
| let rpc_server = InMemoryRpcServer::new(transport.clone(), uri_provider.clone()); | ||
| // and register an endpoint for the service operation | ||
| let echo_op = Arc::new(EchoOperation {}); | ||
|
|
||
| rpc_server | ||
| .register_endpoint(None, METHOD_RESOURCE_ID, echo_op.clone()) | ||
| .await?; | ||
|
|
||
| // now create an RpcClient attached to the same local transport | ||
| let rpc_client = InMemoryRpcClient::new(transport.clone(), uri_provider.clone()).await?; | ||
| // and invoke the service operation without any payload | ||
| match rpc_client | ||
| .invoke_method( | ||
| uri_provider.get_resource_uri(METHOD_RESOURCE_ID), | ||
| CallOptions::for_rpc_request(1_000, None, None, None), | ||
| None, // no payload | ||
| ) | ||
| .await | ||
| { | ||
| Err(ServiceInvocationError::InvalidArgument(msg)) => { | ||
| println!("service returned expected error: {}", msg) | ||
| } | ||
| _ => panic!("expected service to return an Invalid Argument error"), | ||
| } | ||
|
|
||
| // now invoke the operaiton with a message in the request payload | ||
| let value = StringValue { | ||
| value: "Hello".to_string(), | ||
| ..Default::default() | ||
| }; | ||
| let payload = UPayload::try_from_protobuf(value)?; | ||
| // and make sure that the response contains a message in the payload | ||
| match rpc_client | ||
| .invoke_method( | ||
| uri_provider.get_resource_uri(METHOD_RESOURCE_ID), | ||
| CallOptions::for_rpc_request(1_000, None, None, None), | ||
| Some(payload), | ||
| ) | ||
| .await | ||
| { | ||
| Ok(Some(payload)) => { | ||
| let value = payload.extract_protobuf::<StringValue>()?; | ||
| println!("service returned message: {}", value.value); | ||
| } | ||
| _ => panic!("expected service to return response message"), | ||
| } | ||
|
|
||
| // and finally unregister the endpoint | ||
| rpc_server | ||
| .unregister_endpoint(None, METHOD_RESOURCE_ID, echo_op) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.