1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
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 */
15 //! host server & client
16
17 mod auth;
18 mod client;
19 mod host_app;
20 mod logger;
21 mod parser;
22 mod server;
23 mod task;
24 mod translate;
25 mod unittest;
26 mod tty_utility;
27
28 use std::{
29 backtrace::{Backtrace, BacktraceStatus},
30 io::ErrorKind,
31 panic,
32 };
33
34 use hdc::common::base;
35 use hdc::common::base::Base;
36 use hdc::config;
37
38 #[cfg(feature = "host")]
39 extern crate ylong_runtime_static as ylong_runtime;
40
41 #[macro_use]
42 extern crate lazy_static;
43
44 // static LOGGER: SimpleHostLogger = SimpleHostLogger;
45
46 // fn logger_init(log_level: log::LevelFilter) {
47 // let log_file: std::path::PathBuf = Path::new(&std::env::temp_dir()).join(config::LOG_FILE_NAME);
48 // let _ = std::fs::File::create(log_file);
49 // let logger: &'static SimpleHostLogger = &SimpleHostLogger { background_mode: false, flushed_size: 100 };
50 // log::set_logger(logger).unwrap();
51 // log::set_max_level(log_level);
52 // }
53
setup_panic_hooknull54 fn setup_panic_hook() {
55 panic::set_hook(Box::new(move |info| {
56 hdc::error!("panic info: {}", info);
57 let backtrace = Backtrace::capture();
58 if backtrace.status() == BacktraceStatus::Disabled {
59 hdc::error!(
60 "backtrace: disabled. run with `RUST_BACKTRACE=1` environment variable to display a backtrace."
61 );
62 } else {
63 hdc::error!("backtrace: \n{}", backtrace);
64 }
65 }));
66 }
67
mainnull68 fn main() {
69 setup_panic_hook();
70 let _ = ylong_runtime::builder::RuntimeBuilder::new_multi_thread()
71 .worker_stack_size(16 * 1024 * 1024)
72 .worker_num(64)
73 .keep_alive_time(std::time::Duration::from_secs(10))
74 .build_global();
75
76 let parsed_cmd = match parser::parse_command(std::env::args()) {
77 Ok(parsed_cmd) => parsed_cmd,
78 Err(e) => {
79 println!("{}", e);
80 return;
81 }
82 };
83
84 logger::logger_init(
85 config::LOG_LEVEL_ORDER[parsed_cmd.log_level],
86 parsed_cmd.run_in_server,
87 parsed_cmd.spawned_server,
88 );
89
90 hdc::debug!("parsed cmd: {:#?}", parsed_cmd);
91
92 if parsed_cmd.run_in_server {
93 if !Base::program_mutex(base::GLOBAL_SERVER_NAME, false) {
94 hdc::debug!("There is already a running service, please exit directly");
95 return;
96 }
97 ylong_runtime::block_on(async {
98 let _ = server::run_server_mode(parsed_cmd.server_addr).await;
99 });
100 } else {
101 hdc::debug!("in client mode, cmd: {:#?}, parameter:{:#?}", parsed_cmd.command, parsed_cmd.parameters);
102 ylong_runtime::block_on(async {
103 if parsed_cmd.command.is_none() {
104 println!("Unknown operation command...");
105 println!("{}", translate::usage());
106 return;
107 }
108
109 if let Err(e) = client::run_client_mode(parsed_cmd).await {
110 match e.kind() {
111 ErrorKind::Other => println!("[Fail]{}", e),
112 _ => {
113 hdc::trace!("client exit with err: {e:?}");
114 }
115 }
116 }
117 })
118 }
119 }
120