1+ /********************************************************************************
2+ * Copyright (c) 2024 Contributors to the Eclipse Foundation
3+ *
4+ * See the NOTICE file(s) distributed with this work for additional
5+ * information regarding copyright ownership.
6+ *
7+ * This program and the accompanying materials are made available under the
8+ * terms of the Apache License Version 2.0 which is available at
9+ * https://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * SPDX-License-Identifier: Apache-2.0
12+ ********************************************************************************/
13+
114use async_trait:: async_trait;
215use hello_world_protos:: hello_world_service:: { HelloRequest , HelloResponse } ;
316use log:: { error, trace} ;
417use std:: fs:: canonicalize;
518use std:: path:: PathBuf ;
619use std:: sync:: Arc ;
720use std:: thread;
21+ use up_rust:: communication:: {
22+ InMemoryRpcServer , RequestHandler , RpcServer , ServiceInvocationError , UPayload ,
23+ } ;
824use up_rust:: UPayloadFormat :: UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY ;
9- use up_rust:: { UListener , UMessage , UMessageBuilder , UStatus , UTransport , UUri } ;
25+ use up_rust:: { UCode , UStatus , UUri } ;
1026use up_transport_vsomeip:: UPTransportVsomeip ;
11- use up_transport_vsomeip:: UeId ;
1227
1328const HELLO_SERVICE_ID : u16 = 0x6000 ;
1429const HELLO_INSTANCE_ID : u32 = 0x0001 ;
@@ -31,27 +46,28 @@ const _CLIENT_RESOURCE_ID: u16 = 0;
3146
3247const _REQUEST_TTL: u32 = 1000 ;
3348
34- struct ServiceRequestResponder {
35- client : Arc < dyn UTransport > ,
36- }
37- impl ServiceRequestResponder {
38- pub fn new ( client : Arc < dyn UTransport > ) -> Self {
39- Self { client }
49+ struct ServiceRequestHandler ;
50+ impl ServiceRequestHandler {
51+ pub fn new ( ) -> Self {
52+ Self
4053 }
4154}
4255#[ async_trait]
43- impl UListener for ServiceRequestResponder {
44- async fn on_receive ( & self , msg : UMessage ) {
45- println ! ( "ServiceRequestResponder: Received a message: {msg:?}" ) ;
46-
47- let mut msg = msg. clone ( ) ;
48-
49- if let Some ( ref mut attributes) = msg. attributes . as_mut ( ) {
50- attributes. payload_format =
51- :: protobuf:: EnumOrUnknown :: new ( UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY ) ;
52- }
53-
54- let hello_request = msg. extract_protobuf_payload :: < HelloRequest > ( ) ;
56+ impl RequestHandler for ServiceRequestHandler {
57+ async fn handle_request (
58+ & self ,
59+ resource_id : u16 ,
60+ request_payload : Option < UPayload > ,
61+ ) -> Result < Option < UPayload > , ServiceInvocationError > {
62+ println ! ( "ServiceRequestHandler: Received a resource_id: {resource_id} request_payload: {request_payload:?}" ) ;
63+
64+ let hello_request_vsomeip_unspecified_payload_format = request_payload. unwrap ( ) ;
65+ let hello_request_protobuf_payload_format = UPayload :: new (
66+ hello_request_vsomeip_unspecified_payload_format. payload ( ) ,
67+ UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY ,
68+ ) ;
69+ let hello_request =
70+ hello_request_protobuf_payload_format. extract_protobuf :: < HelloRequest > ( ) ;
5571
5672 let hello_request = match hello_request {
5773 Ok ( hello_request) => {
@@ -60,7 +76,10 @@ impl UListener for ServiceRequestResponder {
6076 }
6177 Err ( err) => {
6278 error ! ( "Unable to parse HelloRequest: {err:?}" ) ;
63- return ;
79+ return Err ( ServiceInvocationError :: RpcError ( UStatus :: fail_with_code (
80+ UCode :: INTERNAL ,
81+ "Unable to parse hello_request" ,
82+ ) ) ) ;
6483 }
6584 } ;
6685
@@ -69,14 +88,9 @@ impl UListener for ServiceRequestResponder {
6988 ..Default :: default ( )
7089 } ;
7190
72- let response_msg = UMessageBuilder :: response_for_request ( msg. attributes . as_ref ( ) . unwrap ( ) )
73- . build_with_wrapped_protobuf_payload ( & hello_response)
74- . unwrap ( ) ;
75- self . client . send ( response_msg) . await . unwrap ( ) ;
76- }
91+ println ! ( "Making response to send back: {hello_response:?}" ) ;
7792
78- async fn on_error ( & self , err : UStatus ) {
79- println ! ( "ServiceRequestResponder: Encountered an error: {err:?}" ) ;
93+ Ok ( Some ( UPayload :: try_from_protobuf ( hello_response) . unwrap ( ) ) )
8094 }
8195}
8296
@@ -94,41 +108,30 @@ async fn main() -> Result<(), UStatus> {
94108
95109 // There will be a single vsomeip_transport, as there is a connection into device and a streamer
96110 // TODO: Add error handling if we fail to create a UPTransportVsomeip
97- let service: Arc < dyn UTransport > = Arc :: new (
111+ let service_uuri = UUri :: try_from_parts (
112+ HELLO_SERVICE_AUTHORITY ,
113+ HELLO_SERVICE_UE_ID ,
114+ HELLO_SERVICE_MAJOR ,
115+ // HELLO_SERVICE_RESOURCE_ID,
116+ 0 ,
117+ )
118+ . unwrap ( ) ;
119+ let service = Arc :: new (
98120 UPTransportVsomeip :: new_with_config (
99- & HELLO_SERVICE_AUTHORITY . to_string ( ) ,
121+ service_uuri ,
100122 & CLIENT_AUTHORITY . to_string ( ) ,
101- HELLO_SERVICE_UE_ID as UeId ,
102123 & vsomeip_config. unwrap ( ) ,
103124 None ,
104125 )
105126 . unwrap ( ) ,
106127 ) ;
128+ let l2_service = InMemoryRpcServer :: new ( service. clone ( ) , service. clone ( ) ) ;
107129
108- let source_filter = UUri {
109- authority_name : "*" . to_string ( ) ,
110- ue_id : 0x0000_FFFF ,
111- ue_version_major : 0xFF ,
112- resource_id : 0xFFFF ,
113- ..Default :: default ( )
114- } ;
115- let sink_filter = UUri {
116- authority_name : HELLO_SERVICE_AUTHORITY . to_string ( ) ,
117- ue_id : HELLO_SERVICE_UE_ID ,
118- ue_version_major : HELLO_SERVICE_MAJOR as u32 ,
119- resource_id : HELLO_SERVICE_RESOURCE_ID as u32 ,
120- ..Default :: default ( )
121- } ;
122- let service_request_responder: Arc < dyn UListener > =
123- Arc :: new ( ServiceRequestResponder :: new ( service. clone ( ) ) ) ;
124- // TODO: Need to revisit how the vsomeip config file is used in non point-to-point cases
125- service
126- . register_listener (
127- & source_filter,
128- Some ( & sink_filter) ,
129- service_request_responder. clone ( ) ,
130- )
131- . await ?;
130+ let service_request_handler = Arc :: new ( ServiceRequestHandler :: new ( ) ) ;
131+ l2_service
132+ . register_endpoint ( None , HELLO_SERVICE_RESOURCE_ID , service_request_handler)
133+ . await
134+ . expect ( "Unable to register endpoint" ) ;
132135
133136 thread:: park ( ) ;
134137 Ok ( ( ) )
0 commit comments