1 // Copyright 2023 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 use std::sync::{Arc, OnceLock, RwLock};
16
17 use crate::captures::capture::Captures;
18 use crate::version::get_version;
19
20 static RESOURCES: OnceLock<Resource> = OnceLock::new();
21
22 /// Resource struct includes all the global and possibly shared
23 /// resources for netsim. Each field within Resource should be an Arc
24 /// protected by a RwLock or Mutex.
25 pub struct Resource {
26 version: String,
27 captures: Arc<RwLock<Captures>>,
28 }
29
30 impl Resource {
new() -> Self31 pub fn new() -> Self {
32 Self { version: get_version(), captures: Arc::new(RwLock::new(Captures::new())) }
33 }
34
35 #[allow(dead_code)]
get_version_resource(self) -> String36 pub fn get_version_resource(self) -> String {
37 self.version
38 }
39 }
40
clone_captures() -> Arc<RwLock<Captures>>41 pub fn clone_captures() -> Arc<RwLock<Captures>> {
42 Arc::clone(&RESOURCES.get_or_init(Resource::new).captures)
43 }
44
45 #[cfg(test)]
46 mod tests {
47 use super::*;
48
49 #[test]
test_version()50 fn test_version() {
51 let resource = Resource::new();
52 assert_eq!(get_version(), resource.get_version_resource());
53 }
54 }
55