Skip to content

Commit 830447d

Browse files
committed
fix: mdns bug causing multiple device discovery failure
- multiple devices failed to be passed to the front end
1 parent 4df0145 commit 830447d

File tree

5 files changed

+151
-111
lines changed

5 files changed

+151
-111
lines changed

ETVR.code-workspace

+77-75
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,79 @@
11
{
2-
"folders": [
3-
{
4-
"name": "ETVR GUI",
5-
"path": "./GUI/ETVR"
6-
},
7-
{
8-
"name": "ETVR Assets",
9-
"path": "./GUI/assets"
10-
}
11-
],
12-
"settings": {
13-
"files.autoSave": "afterDelay",
14-
"rust-analyzer.checkOnSave.enable": true,
15-
"rust-analyzer.checkOnSave.command": "clippy",
16-
"rust-analyzer.checkOnSave.allTargets": true,
17-
"rust-analyzer.files.watcher": "client",
18-
"rust-analyzer.files.excludeDirs": [
19-
"**/target",
20-
"**/node_modules",
21-
"**/dist",
22-
"**/build",
23-
"**/public",
24-
"**/assets",
25-
"**/src-tauri/target",
26-
"**/scripts",
27-
"**/src/components",
28-
"**/src/interfaces",
29-
"**/src/pages",
30-
"**/src/static",
31-
"**/src/styles",
32-
"**/src/utils"
33-
],
34-
"editor.defaultFormatter": "esbenp.prettier-vscode",
35-
"[toml]": {
36-
"editor.defaultFormatter": "tamasfe.even-better-toml"
37-
},
38-
"[jsonc]": {
39-
"editor.defaultFormatter": "esbenp.prettier-vscode"
40-
},
41-
"[javascript]": {
42-
"editor.defaultFormatter": "esbenp.prettier-vscode"
43-
},
44-
"[typescript]": {
45-
"editor.defaultFormatter": "esbenp.prettier-vscode"
46-
},
47-
"liveServer.settings.multiRootWorkspaceName": "ETVR",
48-
"[typescriptreact]": {
49-
"editor.defaultFormatter": "esbenp.prettier-vscode"
50-
},
51-
"[css]": {
52-
"editor.defaultFormatter": "vscode.css-language-features"
53-
},
54-
"powershell.cwd": "c:\\Users\\zacar\\Documents\\GitHub\\EyeTrackVR\\GUI\\ETVR",
55-
"[shellscript]": {
56-
"editor.defaultFormatter": "shakram02.bash-beautify"
57-
},
58-
"cSpell.words": [
59-
"APPCONFIG",
60-
"APPDATA",
61-
"clippy",
62-
"clsx",
63-
"dnsquery",
64-
"esbenp",
65-
"kobalte",
66-
"nanos",
67-
"notif",
68-
"partytown",
69-
"shakram",
70-
"solidjs",
71-
"soundfile",
72-
"tamasfe",
73-
"undici",
74-
"vorbis"
75-
]
2+
"folders": [
3+
{
4+
"name": "ETVR GUI",
5+
"path": "./GUI/ETVR"
6+
},
7+
{
8+
"name": "ETVR Assets",
9+
"path": "./GUI/assets"
7610
}
77-
}
11+
],
12+
"settings": {
13+
"files.autoSave": "afterDelay",
14+
"rust-analyzer.checkOnSave.enable": true,
15+
"rust-analyzer.checkOnSave.command": "clippy",
16+
"rust-analyzer.checkOnSave.allTargets": true,
17+
"rust-analyzer.files.watcher": "client",
18+
"rust-analyzer.files.excludeDirs": [
19+
"**/target",
20+
"**/node_modules",
21+
"**/dist",
22+
"**/build",
23+
"**/public",
24+
"**/assets",
25+
"**/src-tauri/target",
26+
"**/scripts",
27+
"**/src/components",
28+
"**/src/interfaces",
29+
"**/src/pages",
30+
"**/src/static",
31+
"**/src/styles",
32+
"**/src/utils"
33+
],
34+
"editor.defaultFormatter": "esbenp.prettier-vscode",
35+
"[toml]": {
36+
"editor.defaultFormatter": "tamasfe.even-better-toml"
37+
},
38+
"[jsonc]": {
39+
"editor.defaultFormatter": "esbenp.prettier-vscode"
40+
},
41+
"[javascript]": {
42+
"editor.defaultFormatter": "esbenp.prettier-vscode"
43+
},
44+
"[typescript]": {
45+
"editor.defaultFormatter": "esbenp.prettier-vscode"
46+
},
47+
"liveServer.settings.multiRootWorkspaceName": "ETVR",
48+
"[typescriptreact]": {
49+
"editor.defaultFormatter": "esbenp.prettier-vscode"
50+
},
51+
"[css]": {
52+
"editor.defaultFormatter": "vscode.css-language-features"
53+
},
54+
"powershell.cwd": "c:\\Users\\zacar\\Documents\\GitHub\\EyeTrackVR\\GUI\\ETVR",
55+
"[shellscript]": {
56+
"editor.defaultFormatter": "shakram02.bash-beautify"
57+
},
58+
"cSpell.words": [
59+
"APPCONFIG",
60+
"APPDATA",
61+
"clippy",
62+
"clsx",
63+
"dnsquery",
64+
"esbenp",
65+
"fullname",
66+
"hashset",
67+
"kobalte",
68+
"nanos",
69+
"notif",
70+
"partytown",
71+
"shakram",
72+
"solidjs",
73+
"soundfile",
74+
"tamasfe",
75+
"undici",
76+
"vorbis"
77+
]
78+
}
79+
}

GUI/ETVR/src-tauri/src/main.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,31 @@ async fn get_user() -> Result<String, String> {
8989
/// - `service_type` The service type to query for
9090
/// - `scan_time` The number of seconds to query for
9191
#[tauri::command]
92-
async fn run_mdns_query(service_type: String, scan_time: u64) -> Result<String, String> {
92+
async fn run_mdns_query(
93+
service_type: String,
94+
scan_time: u64,
95+
) -> Result<m_dnsquery::MdnsData, String> {
9396
println!("Starting MDNS query to find devices");
94-
let base_url = Arc::new(Mutex::new(HashMap::new()));
95-
let thread_arc = base_url.clone();
96-
let mut mdns: m_dnsquery::Mdns = m_dnsquery::Mdns {
97-
base_url: thread_arc,
98-
names: Vec::new(),
99-
ip: Vec::new(),
100-
};
97+
let mut mdns: m_dnsquery::Mdns = m_dnsquery::Mdns::new();
98+
let mut mdns_data = m_dnsquery::MdnsData::new();
10199
let ref_mdns = &mut mdns;
102100
println!("MDNS Service Thread acquired lock");
103-
m_dnsquery::run_query(ref_mdns, service_type, scan_time)
101+
m_dnsquery::run_query(ref_mdns, service_type, &mut mdns_data, scan_time)
104102
.await
105103
.expect("Error in mDNS query");
106104
println!("MDNS query complete");
107105
println!(
108106
"MDNS query results: {:#?}",
109107
m_dnsquery::get_urls(&*ref_mdns)
110108
); // get's an array of the base urls found
111-
let json = m_dnsquery::generate_json(&*ref_mdns)
112-
.await
113-
.expect("Failed to generate JSON object"); // generates a json file with the base urls found
114-
//tokio::fs::write("config/config.json", json)
115-
// .await
116-
// .expect("Failed to write JSON file");
117-
Ok(json)
109+
Ok(mdns_data)
110+
//let json = m_dnsquery::generate_json(&*ref_mdns)
111+
// .await
112+
// .expect("Failed to generate JSON object"); // generates a json file with the base urls found
113+
// //tokio::fs::write("config/config.json", json)
114+
// // .await
115+
// // .expect("Failed to write JSON file");
116+
//println!("MDNS query complete {:#?}", json);
118117
}
119118

120119
#[tauri::command]

GUI/ETVR/src-tauri/src/modules/m_dnsquery/mod.rs

+44-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,43 @@ pub struct Mdns {
1818
pub ip: Vec<String>,
1919
}
2020

21+
/// A struct to hold the mDNS query results and pass them to the front end
22+
/// - `names`: a vector of the names of the devices found
23+
/// - `ip`: a vector of the ip addresses of the devices found
24+
#[derive(Debug, Serialize)]
25+
pub struct MdnsData {
26+
pub names: Vec<String>,
27+
pub ips: Vec<String>,
28+
}
29+
30+
impl Mdns {
31+
/// A function to create a new Mdns struct
32+
/// ## Returns
33+
/// A new Mdns struct
34+
pub fn new() -> Mdns {
35+
Mdns {
36+
base_url: Arc::new(Mutex::new(HashMap::new())),
37+
names: Vec::new(),
38+
ip: Vec::new(),
39+
}
40+
}
41+
}
42+
43+
impl MdnsData {
44+
/// A function to create a new MdnsData struct
45+
/// ## Arguments
46+
/// - `names`: a vector of the names of the devices found
47+
/// - `ip`: a vector of the ip addresses of the devices found
48+
/// ## Returns
49+
/// A new MdnsData struct
50+
pub fn new() -> MdnsData {
51+
MdnsData {
52+
names: Vec::new(),
53+
ips: Vec::new(),
54+
}
55+
}
56+
}
57+
2158
/// Runs a mDNS query for X seconds
2259
/// ## Arguments
2360
/// - `mdns` A mutable reference to the Mdns struct
@@ -40,6 +77,7 @@ pub struct Mdns {
4077
pub async fn run_query(
4178
instance: &mut Mdns,
4279
mut service_type: String,
80+
mdns_data: &mut MdnsData,
4381
scan_time: u64,
4482
) -> Result<(), Box<dyn std::error::Error>> {
4583
let mdns =
@@ -82,10 +120,13 @@ pub async fn run_query(
82120
name = name.trim_end_matches(".local");
83121
instance.names.push(name.to_string());
84122
let ip = info.get_addresses();
85-
// grab the ip adress' from the hashset and add them to the vector
123+
// grab the ip address' from the hashset and add them to the vector
86124
for i in ip {
87125
instance.ip.push(i.to_string());
88126
}
127+
//* add the name and ip to the MdnsData struct
128+
mdns_data.names = instance.names.clone();
129+
mdns_data.ips = instance.ip.clone();
89130
}
90131
other_event => {
91132
println!(
@@ -162,7 +203,7 @@ pub fn get_urls(instance: &Mdns) -> Vec<String> {
162203
urls
163204
}
164205

165-
pub async fn generate_json(instance: &Mdns) -> Result<String, Box<dyn std::error::Error>> {
206+
/* pub async fn generate_json(instance: &Mdns) -> Result<String, Box<dyn std::error::Error>> {
166207
let data = get_urls(instance);
167208
//let mut json: serde_json::Value = serde_json::from_str("{}").unwrap();
168209
let mut json: Option<serde_json::Value> = None;
@@ -195,4 +236,4 @@ pub async fn generate_json(instance: &Mdns) -> Result<String, Box<dyn std::error
195236
// return the json object as a string
196237
197238
Ok(to_string_json)
198-
}
239+
} */

GUI/ETVR/src/store/mdns/mdns.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export enum MdnsStatus {
88
FAILED = 'FAILED',
99
}
1010

11-
interface IMdnsResponse {
11+
export interface IMdnsResponse {
1212
ips: string[]
13-
urls: string[]
13+
names: string[]
1414
}
1515

1616
interface IMdnsStore {
@@ -22,7 +22,7 @@ export const defaultState: IMdnsStore = {
2222
mdnsStatus: MdnsStatus.DISABLED,
2323
mdnsData: {
2424
ips: [],
25-
urls: [],
25+
names: [],
2626
},
2727
}
2828

GUI/ETVR/src/utils/hooks/api/useMDNSScanner.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { invoke } from '@tauri-apps/api/tauri'
22
import { setAddCameraMDNS } from '@src/store/camera/camera'
33
import { enableMDNS } from '@store/app/settings/selectors'
4-
import { setMdnsStatus, setMdnsData, MdnsStatus } from '@store/mdns/mdns'
4+
import { setMdnsStatus, setMdnsData, MdnsStatus, type IMdnsResponse } from '@store/mdns/mdns'
55

66
export const useMDNSScanner = (serviceType: string, scanTime: number) => {
77
if (!enableMDNS()) return
@@ -16,19 +16,17 @@ export const useMDNSScanner = (serviceType: string, scanTime: number) => {
1616
scanTime,
1717
})
1818
.then((res) => {
19-
if (typeof res === 'string') {
20-
const parsedResponse = JSON.parse(res)
21-
console.log('[MDNS Scanner]: parsedResponse', parsedResponse)
22-
setMdnsStatus(MdnsStatus.ACTIVE)
23-
setMdnsData(parsedResponse)
24-
// loop through the parsedResponse and add the cameras to the store
25-
const size = Object.keys(parsedResponse.urls).length
26-
for (let i = 0; i < size; i++) {
27-
// grab the unknown key and use it to access the parsedResponse.urls object
28-
const key = Object.keys(parsedResponse.urls)[i]
29-
console.log('[MDNS Scanner]: adding camera', key)
30-
setAddCameraMDNS(parsedResponse.urls[key])
31-
}
19+
console.log('[MDNS Scanner]: res', res)
20+
const response = res as IMdnsResponse
21+
setMdnsStatus(MdnsStatus.ACTIVE)
22+
setMdnsData(response)
23+
// loop through the res and add the cameras to the store
24+
const size = Object.keys(response.names).length
25+
for (let i = 0; i < size; i++) {
26+
// grab the unknown key and use it to access the res.urls object
27+
const key = Object.keys(response.names)[i]
28+
console.log('[MDNS Scanner]: adding camera', key)
29+
setAddCameraMDNS(response.names[key])
3230
}
3331
})
3432
.catch((e) => {

0 commit comments

Comments
 (0)