2
2
3
3
#![ allow( dead_code, unused_imports, unused_variables) ]
4
4
#![ cfg_attr(
5
- all( not( debug_assertions) , target_os = "windows" ) ,
6
- windows_subsystem = "windows"
5
+ all( not( debug_assertions) , target_os = "windows" ) ,
6
+ windows_subsystem = "windows"
7
7
) ]
8
8
9
9
use log:: { error, info} ;
@@ -19,9 +19,9 @@ pub type MdnsMap = Arc<Mutex<HashMap<String, String>>>; // Arc<Mutex<HashMap<Str
19
19
/// - `name`: a vector of the names of the devices found
20
20
#[ derive( Debug ) ]
21
21
pub struct Mdns {
22
- pub base_url : MdnsMap ,
23
- pub names : Vec < String > ,
24
- pub ip : Vec < String > ,
22
+ pub base_url : MdnsMap ,
23
+ pub names : Vec < String > ,
24
+ pub ip : Vec < String > ,
25
25
}
26
26
27
27
/// Runs a mDNS query for X seconds
@@ -44,66 +44,66 @@ pub struct Mdns {
44
44
/// ***The service type should not have a '.' or a 'local' at the end.*** <br>
45
45
/// ***The program adds ".local." automatically.***
46
46
pub async fn run_query (
47
- instance : & mut Mdns ,
48
- mut service_type : String ,
49
- scan_time : u64 ,
47
+ instance : & mut Mdns ,
48
+ mut service_type : String ,
49
+ scan_time : u64 ,
50
50
) -> Result < ( ) , Box < dyn std:: error:: Error > > {
51
- let mdns = ServiceDaemon :: new ( )
52
- . expect ( "Failed to create daemon. Please install Bonjour on your system" ) ;
53
- //* Browse for a service type.
54
- service_type. push_str ( ".local." ) ;
55
- let receiver = mdns. browse ( & service_type) . map_err ( |e| e. to_string ( ) ) ?;
56
- let now = std:: time:: Instant :: now ( ) ;
57
- //* listen for event then stop the event loop after 5 seconds.
58
- // while let Ok(event) = receiver.recv() {}
59
- while now. elapsed ( ) . as_secs ( ) < scan_time {
60
- //* let event = receiver.recv().expect("Failed to receive event");
61
- if let Ok ( event) = receiver. recv_async ( ) . await {
62
- match event {
63
- ServiceEvent :: ServiceResolved ( info) => {
64
- info ! (
65
- "At {:?}: Resolved a new service: {} IP: {:#?}:{:#?} Hostname: {:#?}" ,
66
- now. elapsed( ) ,
67
- info. get_fullname( ) ,
68
- info. get_addresses( ) ,
69
- info. get_port( ) ,
70
- info. get_hostname( ) ,
71
- ) ;
72
- //* split the fullname by '.' and take the first element
73
- let name = info. get_hostname ( ) ;
74
- info ! ( "Service name: {}" , name) ;
75
- //* remove the period at the end of the name
76
- let mut name = name. trim_end_matches ( '.' ) ;
77
- //* append name to 'http://' to get the base url
78
- let mut base_url = String :: from ( "http://" ) ;
79
- base_url. push_str ( name) ;
80
- info ! ( "Base URL: {}" , base_url) ;
81
- //* add the base url to the hashmap
82
- instance
83
- . base_url
84
- . lock ( )
85
- . map_err ( |e| e. to_string ( ) ) ?
86
- . insert ( name. to_string ( ) , base_url) ;
87
- //* remove the `.local` from the name
88
- name = name. trim_end_matches ( ".local" ) ;
89
- instance. names . push ( name. to_string ( ) ) ;
90
- let ip = info. get_addresses ( ) ;
91
- // grab the ip adress' from the hashset and add them to the vector
92
- for i in ip {
93
- instance. ip . push ( i. to_string ( ) ) ;
94
- }
95
- }
96
- other_event => {
97
- info ! (
98
- "At {:?} : Received other event: {:?}" ,
99
- now. elapsed( ) ,
100
- & other_event
101
- ) ;
102
- }
103
- }
51
+ let mdns =
52
+ ServiceDaemon :: new ( ) . expect ( "Failed to create daemon. Please install Bonjour on your system" ) ;
53
+ //* Browse for a service type.
54
+ service_type. push_str ( ".local." ) ;
55
+ let receiver = mdns. browse ( & service_type) . map_err ( |e| e. to_string ( ) ) ?;
56
+ let now = std:: time:: Instant :: now ( ) ;
57
+ //* listen for event then stop the event loop after 5 seconds.
58
+ // while let Ok(event) = receiver.recv() {}
59
+ while now. elapsed ( ) . as_secs ( ) < scan_time {
60
+ //* let event = receiver.recv().expect("Failed to receive event");
61
+ if let Ok ( event) = receiver. recv_async ( ) . await {
62
+ match event {
63
+ ServiceEvent :: ServiceResolved ( info) => {
64
+ println ! (
65
+ "At {:?}: Resolved a new service: {} IP: {:#?}:{:#?} Hostname: {:#?}" ,
66
+ now. elapsed( ) ,
67
+ info. get_fullname( ) ,
68
+ info. get_addresses( ) ,
69
+ info. get_port( ) ,
70
+ info. get_hostname( ) ,
71
+ ) ;
72
+ //* split the fullname by '.' and take the first element
73
+ let name = info. get_hostname ( ) ;
74
+ println ! ( "Service name: {}" , name) ;
75
+ //* remove the period at the end of the name
76
+ let mut name = name. trim_end_matches ( '.' ) ;
77
+ //* append name to 'http://' to get the base url
78
+ let mut base_url = String :: from ( "http://" ) ;
79
+ base_url. push_str ( name) ;
80
+ println ! ( "Base URL: {}" , base_url) ;
81
+ //* add the base url to the hashmap
82
+ instance
83
+ . base_url
84
+ . lock ( )
85
+ . map_err ( |e| e. to_string ( ) ) ?
86
+ . insert ( name. to_string ( ) , base_url) ;
87
+ //* remove the `.local` from the name
88
+ name = name. trim_end_matches ( ".local" ) ;
89
+ instance. names . push ( name. to_string ( ) ) ;
90
+ let ip = info. get_addresses ( ) ;
91
+ // grab the ip adress' from the hashset and add them to the vector
92
+ for i in ip {
93
+ instance. ip . push ( i. to_string ( ) ) ;
94
+ }
104
95
}
96
+ other_event => {
97
+ println ! (
98
+ "At {:?} : Received other event: {:?}" ,
99
+ now. elapsed( ) ,
100
+ & other_event
101
+ ) ;
102
+ }
103
+ }
105
104
}
106
- Ok ( ( ) )
105
+ }
106
+ Ok ( ( ) )
107
107
}
108
108
109
109
/// Returns a map of the base urls found
@@ -125,7 +125,8 @@ pub async fn run_query(
125
125
///let urls_map = m_dnsquery::get_url_map(ref_mdns);
126
126
/// ```
127
127
pub fn get_url_map ( instance : & mut Mdns ) -> & mut MdnsMap {
128
- & mut instance. base_url
128
+ println ! ( "Base URL: {:?}" , & instance. base_url) ;
129
+ & mut instance. base_url
129
130
}
130
131
131
132
/// Returns a vector of the base urls found
@@ -147,56 +148,56 @@ pub fn get_url_map(instance: &mut Mdns) -> &mut MdnsMap {
147
148
/// let vec = m_dnsquery::get_urls(ref_mdns);
148
149
/// ```
149
150
pub fn get_urls ( instance : & Mdns ) -> Vec < String > {
150
- let mut urls: Vec < String > = Vec :: new ( ) ;
151
-
152
- let instance_lock = instance. base_url . lock ( ) ;
153
- let instance_check_result = match instance_lock {
154
- Ok ( instance_lock) => instance_lock,
155
- Err ( err) => {
156
- error ! (
157
- "Failed to lock the instance: {:?} with error: {} in get_urls" ,
158
- instance, err
159
- ) ;
160
- return urls;
161
- }
162
- } ;
151
+ let mut urls: Vec < String > = Vec :: new ( ) ;
163
152
164
- for ( _, url) in instance_check_result. iter ( ) {
165
- urls. push ( url. to_string ( ) ) ;
153
+ let instance_lock = instance. base_url . lock ( ) ;
154
+ let instance_check_result = match instance_lock {
155
+ Ok ( instance_lock) => instance_lock,
156
+ Err ( err) => {
157
+ error ! (
158
+ "Failed to lock the instance: {:?} with error: {} in get_urls" ,
159
+ instance, err
160
+ ) ;
161
+ return urls;
166
162
}
167
- urls
163
+ } ;
164
+
165
+ for ( _, url) in instance_check_result. iter ( ) {
166
+ urls. push ( url. to_string ( ) ) ;
167
+ }
168
+ urls
168
169
}
169
170
170
171
pub async fn generate_json ( instance : & Mdns ) -> Result < String , Box < dyn std:: error:: Error > > {
171
- let data = get_urls ( instance) ;
172
- //let mut json: serde_json::Value = serde_json::from_str("{}").unwrap();
173
- let mut json: Option < serde_json:: Value > = None ;
174
- // create a data iterator
175
- for ( i, url) in data. iter ( ) . enumerate ( ) {
176
- json = Some ( serde_json:: json!( {
177
- "ips" : [ instance. ip[ i] . to_string( ) ] ,
178
- "urls" : {
179
- instance. names[ i] . to_string( ) : url. to_string( )
180
- } ,
181
- } ) ) ;
182
- }
183
- let config: serde_json:: Value ;
184
- if let Some ( json) = json {
185
- let _serde_json = serde_json:: from_value ( json) ;
186
- let serde_json_result = match _serde_json {
187
- Ok ( serde_json) => serde_json,
188
- Err ( err) => {
189
- error ! ( "Error configuring JSON config file: {}" , err) ;
190
- return Err ( "Error configuring JSON config file" . into ( ) ) ;
191
- }
192
- } ;
193
- config = serde_json_result;
194
- } else {
195
- config = serde_json:: json!( { } ) ;
196
- }
197
- info ! ( "{:?}" , config) ;
198
- // write the json object to a file
199
- let to_string_json = serde_json:: to_string_pretty ( & config) ?;
200
- // return the json object as a string
201
- Ok ( to_string_json)
172
+ let data = get_urls ( instance) ;
173
+ //let mut json: serde_json::Value = serde_json::from_str("{}").unwrap();
174
+ let mut json: Option < serde_json:: Value > = None ;
175
+ // create a data iterator
176
+ for ( i, url) in data. iter ( ) . enumerate ( ) {
177
+ json = Some ( serde_json:: json!( {
178
+ "ips" : [ instance. ip[ i] . to_string( ) ] ,
179
+ "urls" : {
180
+ instance. names[ i] . to_string( ) : url. to_string( )
181
+ } ,
182
+ } ) ) ;
183
+ }
184
+ let config: serde_json:: Value ;
185
+ if let Some ( json) = json {
186
+ let _serde_json = serde_json:: from_value ( json) ;
187
+ let serde_json_result = match _serde_json {
188
+ Ok ( serde_json) => serde_json,
189
+ Err ( err) => {
190
+ error ! ( "Error configuring JSON config file: {}" , err) ;
191
+ return Err ( "Error configuring JSON config file" . into ( ) ) ;
192
+ }
193
+ } ;
194
+ config = serde_json_result;
195
+ } else {
196
+ config = serde_json:: json!( { } ) ;
197
+ }
198
+ info ! ( "{:?}" , config) ;
199
+ // write the json object to a file
200
+ let to_string_json = serde_json:: to_string_pretty ( & config) ?;
201
+ // return the json object as a string
202
+ Ok ( to_string_json)
202
203
}
0 commit comments