async_coap/inbound_context.rs
1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use super::*;
17
18/// Represents the context for processing an inbound message.
19pub trait InboundContext: Send {
20 /// The `SocketAddr` type from the associated `LocalEndpoint`.
21 type SocketAddr: SocketAddrExt;
22
23 /// Returns a copy of the remote address of the inbound message.
24 fn remote_socket_addr(&self) -> Self::SocketAddr;
25
26 /// Indicates if the endpoint thinks this message is a duplicate. This is used
27 /// for non-idempotent methods (like POST) to determine if the operation should
28 /// have real effects or if it should just go through the motions without changing
29 /// state. Duplicates are generally only passed through when the underlying transport
30 /// doesn't support support storing sent replies for this purpose.
31 fn is_dupe(&self) -> bool;
32
33 /// Returns a reference to a MessageRead trait to inspect the content
34 /// of the inbound message.
35 fn message(&self) -> &dyn MessageRead;
36}
37
38/// Represents the context for processing an inbound request that can be responded to.
39pub trait RespondableInboundContext: InboundContext {
40 /// Indicates if the inbound request was a multicast request or not. Multicast
41 /// requests have additional response timing requirements in order to avoid
42 /// congestion.
43 fn is_multicast(&self) -> bool;
44
45 /// Indicates if this inbound request is from a real inbound request or if it
46 /// is a fake request that is being generated internally to solicit a response.
47 /// Fake requests are only generated for the `GET` method.
48 fn is_fake(&self) -> bool;
49
50 /// Responds to this inbound request using a message generated from `msg_gen`.
51 /// The `msg_id` and `msg_token` fields will be automatically populated.
52 /// This method will return the value returned by `msg_gen`.
53 fn respond<F>(&self, msg_gen: F) -> Result<(), Error>
54 where
55 F: Fn(&mut dyn MessageWrite) -> Result<(), Error>;
56}