Skip to main content

rocketmq_protocol/protocol/remoting_command/
constructors.rs

1// Copyright 2023 The RocketMQ Rust Authors
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//     http://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
15use bytes::Bytes;
16use cheetah_string::CheetahString;
17
18use super::next_request_id;
19use super::ExtensionFields;
20use super::RemotingCommand;
21use super::SerializeType;
22use crate::protocol::command_custom_header::CommandCustomHeader;
23use crate::protocol::header_codec::BinaryHeaderFields;
24use crate::protocol::remoting_command_defaults::application_remoting_command_factory;
25use crate::protocol::remoting_command_defaults::RemotingCommandDefaults;
26use crate::protocol::LanguageCode;
27
28impl RemotingCommand {
29    /// Constructs a command from defaults resolved by the owning facade.
30    ///
31    /// The protocol crate deliberately does not read process environment or configuration files.
32    /// Legacy facades resolve those sources and pass the resulting wire values here.
33    pub fn with_resolved_defaults(version: i32, serialize_type: SerializeType) -> Self {
34        let opaque = next_request_id();
35        RemotingCommand {
36            code: 0,
37            language: LanguageCode::RUST, // Replace with your actual enum variant
38            version,
39            opaque,
40            flag: 0,
41            remark: None,
42            ext_fields: ExtensionFields::default(),
43            body: None,
44            suspended: false,
45            command_custom_header: None,
46            custom_header_to_net: false,
47            serialize_type,
48        }
49    }
50
51    pub(crate) fn from_binary_wire_parts(
52        code: i32,
53        language: LanguageCode,
54        version: i32,
55        opaque: i32,
56        flag: i32,
57        remark: Option<CheetahString>,
58        ext_fields: BinaryHeaderFields,
59    ) -> Self {
60        Self {
61            code,
62            language,
63            version,
64            opaque,
65            flag,
66            remark,
67            ext_fields: ExtensionFields::from_rocketmq_raw(ext_fields),
68            body: None,
69            suspended: false,
70            command_custom_header: None,
71            custom_header_to_net: false,
72            serialize_type: SerializeType::ROCKETMQ,
73        }
74    }
75
76    pub fn new_request(code: impl Into<i32>, body: impl Into<Bytes>) -> Self {
77        application_remoting_command_factory().create_request(code, body)
78    }
79
80    pub fn create_request_command<T>(code: impl Into<i32>, header: T) -> Self
81    where
82        T: CommandCustomHeader + Sync + Send + 'static,
83    {
84        application_remoting_command_factory().create_request_command(code, header)
85    }
86
87    /// Creates a request using defaults resolved by the transport/facade owner.
88    pub fn create_request_command_with_defaults<T>(
89        code: impl Into<i32>,
90        header: T,
91        version: i32,
92        serialize_type: SerializeType,
93    ) -> Self
94    where
95        T: CommandCustomHeader + Sync + Send + 'static,
96    {
97        crate::protocol::remoting_command_defaults::RemotingCommandFactory::new(RemotingCommandDefaults::new(
98            version,
99            serialize_type,
100        ))
101        .create_request_command(code, header)
102    }
103
104    pub fn create_remoting_command(code: impl Into<i32>) -> Self {
105        application_remoting_command_factory().create_remoting_command(code)
106    }
107
108    pub fn get_and_add() -> i32 {
109        next_request_id()
110    }
111
112    pub fn create_response_command_with_code(code: impl Into<i32>) -> Self {
113        application_remoting_command_factory().create_response_command_with_code(code)
114    }
115
116    /// Creates a response with an explicit code and typed custom header.
117    pub fn create_response_command_with_code_and_header(
118        code: impl Into<i32>,
119        header: impl CommandCustomHeader + Sync + Send + 'static,
120    ) -> Self {
121        application_remoting_command_factory().create_response_command_with_code_and_header(code, header)
122    }
123
124    pub fn create_response_command_with_code_remark(code: impl Into<i32>, remark: impl Into<CheetahString>) -> Self {
125        application_remoting_command_factory().create_response_command_with_code_remark(code, remark)
126    }
127
128    /// Creates an explicitly successful response.
129    pub fn create_success_response_command() -> Self {
130        application_remoting_command_factory().create_success_response_command()
131    }
132
133    /// Creates an explicitly successful response with a typed custom header.
134    pub fn create_success_response_command_with_header(
135        header: impl CommandCustomHeader + Sync + Send + 'static,
136    ) -> Self {
137        application_remoting_command_factory().create_success_response_command_with_header(header)
138    }
139
140    /// Creates the unset error response used by Java's typed-header factory.
141    pub fn create_java_default_error_response_command() -> Self {
142        application_remoting_command_factory().create_java_default_error_response_command()
143    }
144
145    /// Creates the unset Java-compatible error response with a typed header.
146    pub fn create_java_default_error_response_command_with_header(
147        header: impl CommandCustomHeader + Sync + Send + 'static,
148    ) -> Self {
149        application_remoting_command_factory().create_java_default_error_response_command_with_header(header)
150    }
151
152    pub fn create_new_request_id() -> i32 {
153        next_request_id()
154    }
155}