Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ members = [
"rustecal-samples/pubsub/person_receive",
"rustecal-samples/pubsub/serde_send",
"rustecal-samples/pubsub/serde_receive",
"rustecal-samples/service/math_client",
"rustecal-samples/service/math_server",
"rustecal-samples/service/mirror_client",
"rustecal-samples/service/mirror_client_instances",
"rustecal-samples/service/mirror_server",
Expand Down
13 changes: 13 additions & 0 deletions rustecal-samples/service/math_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "math_client"
version = "0.1.0"
edition = "2024"

[dependencies]
rustecal = { path = "../../../rustecal", features = ["service"] }
prost = "0.14"


[build-dependencies]
prost-build = "0.14"
prost-reflect-build = "0.16.0"
7 changes: 7 additions & 0 deletions rustecal-samples/service/math_client/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let protos = ["proto/math.proto"];

let protos_inc = ["proto"];

prost_build::compile_protos(&protos, &protos_inc).unwrap();
}
43 changes: 43 additions & 0 deletions rustecal-samples/service/math_client/proto/math.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

syntax = "proto3";

option cc_generic_services = true;

///////////////////////////////////////////////////////
// Math Service
///////////////////////////////////////////////////////
message SFloatTuple
{
double inp1 = 1;
double inp2 = 2;
}

message SFloat
{
double out = 1;
}

service MathService
{
rpc Add (SFloatTuple) returns (SFloat);
rpc Multiply (SFloatTuple) returns (SFloat);
rpc Divide (SFloatTuple) returns (SFloat);
}
81 changes: 81 additions & 0 deletions rustecal-samples/service/math_client/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use prost::Message;
use rustecal::{CallState, ServiceClient, ServiceRequest};
use rustecal::{Ecal, EcalComponents};
use std::thread;
use std::time::Duration;

// Add the protobuf compiled by prost
mod math_pb {
include!(concat!(env!("OUT_DIR"), "/_.rs"));
}
use math_pb::{SFloat, SFloatTuple};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// initialize eCAL
Ecal::initialize(Some("math client rust"), EcalComponents::DEFAULT, None)
.expect("eCAL initialization failed");

let client = ServiceClient::new("MathService")?;

// wait until connected
while client.get_client_instances().is_empty() {
println!("Waiting for a service ..");
thread::sleep(Duration::from_secs(1));
}

let methods = ["Add", "Multiply", "Divide"];
let mut i = 0;

while Ecal::ok() {
let method_name = methods[i % methods.len()];
i += 1;

let payload_pb = SFloatTuple {
inp1: i as f64,
inp2: (i + 1) as f64,
};

let request = ServiceRequest {
payload: payload_pb.encode_to_vec(),
};

for instance in client.get_client_instances() {
let response = instance.call(method_name, request.clone(), Some(1000));

println!();
println!(
"Method '{}' called with message: {:?}",
method_name, payload_pb
);

match response {
Some(res) => match CallState::from(res.success as i32) {
CallState::Executed => {
let response_data = SFloat::decode(&res.payload[..])?;
println!(
"Received response: {:?} from service id {:?}",
response_data, res.server_id.service_id.entity_id
);
}
CallState::Failed => {
println!(
"Received error: {} from service id {:?}",
res.error_msg.unwrap_or_else(|| "Unknown".into()),
res.server_id.service_id.entity_id
);
}
_ => {}
},
None => {
println!("Method blocking call failed ..");
}
}
}

thread::sleep(Duration::from_secs(1));
}

// clean up and finalize eCAL
Ecal::finalize();
Ok(())
}
13 changes: 13 additions & 0 deletions rustecal-samples/service/math_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "math_server"
version = "0.1.0"
edition = "2024"

[dependencies]
rustecal = { path = "../../../rustecal", features = ["service"] }
prost = "0.14"


[build-dependencies]
prost-build = "0.14"
prost-reflect-build = "0.16.0"
7 changes: 7 additions & 0 deletions rustecal-samples/service/math_server/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let protos = ["proto/math.proto"];

let protos_inc = ["proto"];

prost_build::compile_protos(&protos, &protos_inc).unwrap();
}
43 changes: 43 additions & 0 deletions rustecal-samples/service/math_server/proto/math.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

syntax = "proto3";

option cc_generic_services = true;

///////////////////////////////////////////////////////
// Math Service
///////////////////////////////////////////////////////
message SFloatTuple
{
double inp1 = 1;
double inp2 = 2;
}

message SFloat
{
double out = 1;
}

service MathService
{
rpc Add (SFloatTuple) returns (SFloat);
rpc Multiply (SFloatTuple) returns (SFloat);
rpc Divide (SFloatTuple) returns (SFloat);
}
121 changes: 121 additions & 0 deletions rustecal-samples/service/math_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use prost::Message;
use rustecal::{Ecal, EcalComponents};
use rustecal::{MethodInfo, ServiceServer};

// Add the protobuf compiled by prost
mod math_pb {
include!(concat!(env!("OUT_DIR"), "/_.rs"));
}
use math_pb::{SFloat, SFloatTuple};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// initialize eCAL
Ecal::initialize(Some("math server rust"), EcalComponents::DEFAULT, None)
.expect("eCAL initialization failed");

// create the service server named "MathService"
let mut server = ServiceServer::new("MathService")?;

// register "Add" method from protobuf rpc
server.add_method(
"Add",
Box::new(|info: MethodInfo, req: &[u8]| {
// Decode serialized protobuf request
let req_pb = SFloatTuple::decode(req);

match req_pb {
Ok(pb) => {
println!(
"Received request for MathService in Rust: {}",
info.method_name
);
println!("Input1 : {}", pb.inp1);
println!("Input2 : {}", pb.inp2);
println!();

let result = SFloat {
out: pb.inp1 + pb.inp2,
};
// Serialize protobuf response
result.encode_to_vec()
}
_ => {
println!("Unable to decode protobuf");
vec![]
}
}
}),
)?;

// register "Multiply" method from protobuf rpc
server.add_method(
"Multiply",
Box::new(|info: MethodInfo, req: &[u8]| {
// Decode serialized protobuf request
let req_pb = SFloatTuple::decode(req);

match req_pb {
Ok(pb) => {
println!(
"Received request for MathService in Rust: {}",
info.method_name
);
println!("Input1 : {}", pb.inp1);
println!("Input2 : {}", pb.inp2);
println!();

let result = SFloat {
out: pb.inp1 * pb.inp2,
};
// Serialize protobuf response
result.encode_to_vec()
}
_ => {
println!("Unable to decode protobuf");
vec![]
}
}
}),
)?;

// register "Divide" method from protobuf rpc
server.add_method(
"Divide",
Box::new(|info: MethodInfo, req: &[u8]| {
// Decode serialized protobuf request
let req_pb = SFloatTuple::decode(req);

match req_pb {
Ok(pb) => {
println!(
"Received request for MathService in Rust: {}",
info.method_name
);
println!("Input1 : {}", pb.inp1);
println!("Input2 : {}", pb.inp2);
println!();

let result = SFloat {
out: pb.inp1 / pb.inp2,
};
// Serialize protobuf response
result.encode_to_vec()
}
_ => {
println!("Unable to decode protobuf");
vec![]
}
}
}),
)?;

println!("Rust math service running. Press Ctrl+C to exit.");

while Ecal::ok() {
std::thread::sleep(std::time::Duration::from_millis(100));
}

// clean up and finalize eCAL
Ecal::finalize();
Ok(())
}