1
1
use crate :: channel:: { Receiver , Sender } ;
2
2
use crate :: core:: message:: internal:: * ;
3
+ use crate :: model:: node:: nodes:: dsl;
4
+ use crate :: model:: node:: Node ;
3
5
use diesel;
4
6
use diesel:: prelude:: * ;
5
7
use diesel:: r2d2:: ConnectionManager ;
6
- use crate :: model:: node:: nodes:: dsl;
7
- use crate :: model:: node:: Node ;
8
8
use r2d2:: * ;
9
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
9
10
10
11
const MIN_NODE_ID : u8 = 1 ;
11
12
const MAX_NODE_ID : u8 = 254 ;
@@ -18,42 +19,75 @@ pub fn handle(
18
19
) {
19
20
loop {
20
21
match receiver. recv ( ) {
21
- Ok ( internal_message_request) => match internal_message_request {
22
- InternalMessage { node_id : 255 , child_sensor_id : 255 , ack : 0 , sub_type : InternalType :: IdRequest , ref payload } if payload == "0"
23
- => match get_next_node_id ( & db_connection) {
24
- Some ( new_node_id) => {
25
- let mut node_id_response = internal_message_request. clone ( ) ;
26
- node_id_response. sub_type = InternalType :: IdResponse ;
27
- node_id_response. payload = new_node_id. to_string ( ) ;
28
- match create_node ( & db_connection, new_node_id as i32 ) {
29
- Ok ( _) => match response_sender. send ( node_id_response. to_string ( ) ) {
30
- Ok ( _) => continue ,
31
- Err ( _) => error ! ( "Error while sending to node_handler" ) ,
32
- } ,
33
- Err ( _) => error ! ( "Error while creating node with new id" ) ,
34
- }
35
- } ,
36
- None => error ! ( "There is no free node id! All 254 id's are already reserved!" ) ,
37
- } ,
38
- InternalMessage { node_id, child_sensor_id : 255 , ack : _, sub_type : InternalType :: DiscoverResponse , ref payload } => {
39
- let parent_node_id = payload. parse :: < u8 > ( ) . unwrap ( ) ;
40
- match update_network_topology ( & db_connection, node_id as i32 , parent_node_id as i32 ) {
41
- Ok ( _) => info ! ( "Updated network topology" ) ,
42
- Err ( e) => error ! ( "Update network topology failed {:?}" , e) ,
43
- }
44
- forward_to_controller ( controller_forward_sender, internal_message_request)
45
- } ,
46
- _ => forward_to_controller ( controller_forward_sender, internal_message_request) ,
22
+ Ok ( message) => match message. sub_type {
23
+ InternalType :: IdRequest => send_node_id ( & db_connection, response_sender, message) ,
24
+ InternalType :: Time => send_current_time ( response_sender, message) ,
25
+ InternalType :: DiscoverResponse => {
26
+ send_discover_response ( & db_connection, & message) ;
27
+ forward_to_controller ( controller_forward_sender, message)
28
+ }
29
+ _ => forward_to_controller ( controller_forward_sender, message) ,
47
30
} ,
48
31
_ => ( ) ,
49
32
}
50
33
}
51
34
}
52
35
36
+ fn send_node_id (
37
+ db_connection : & PooledConnection < ConnectionManager < SqliteConnection > > ,
38
+ response_sender : & Sender < String > ,
39
+ mut message : InternalMessage ,
40
+ ) -> ( ) {
41
+ match get_next_node_id ( db_connection) {
42
+ Some ( new_node_id) => match create_node ( db_connection, new_node_id as i32 ) {
43
+ Ok ( _) => match response_sender. send ( message. as_response ( new_node_id. to_string ( ) ) ) {
44
+ Ok ( _) => ( ) ,
45
+ Err ( _) => error ! ( "Error while sending to node_handler" ) ,
46
+ } ,
47
+ Err ( _) => error ! ( "Error while creating node with new id" ) ,
48
+ } ,
49
+ None => error ! ( "There is no free node id! All 254 id's are already reserved!" ) ,
50
+ }
51
+ }
52
+
53
+ fn send_current_time ( response_sender : & Sender < String > , mut message : InternalMessage ) {
54
+ let start = SystemTime :: now ( ) ;
55
+ let since_the_epoch = start
56
+ . duration_since ( UNIX_EPOCH )
57
+ . expect ( "Time went backwards" ) ;
58
+ match response_sender. send ( message. as_response ( since_the_epoch. as_secs ( ) . to_string ( ) ) ) {
59
+ Ok ( _) => ( ) ,
60
+ Err ( _) => error ! ( "Error while sending to node_handler" ) ,
61
+ }
62
+ }
63
+
64
+ fn send_discover_response (
65
+ db_connection : & PooledConnection < ConnectionManager < SqliteConnection > > ,
66
+ message : & InternalMessage ,
67
+ ) {
68
+ match message. payload . parse :: < u8 > ( ) {
69
+ Ok ( parent_node_id) => match update_network_topology (
70
+ & db_connection,
71
+ message. node_id as i32 ,
72
+ parent_node_id as i32 ,
73
+ ) {
74
+ Ok ( _) => info ! ( "Updated network topology" ) ,
75
+ Err ( e) => error ! ( "Update network topology failed {:?}" , e) ,
76
+ } ,
77
+ Err ( e) => error ! (
78
+ "Error {:?} while parsing discover message payload {:?}" ,
79
+ e, & message. payload
80
+ ) ,
81
+ }
82
+ }
83
+
53
84
fn forward_to_controller ( controller_sender : & Sender < String > , message : InternalMessage ) {
54
85
match controller_sender. send ( message. to_string ( ) ) {
55
86
Ok ( _) => ( ) ,
56
- Err ( error) => error ! ( "Error while forwarding internal message to controller {:?}" , error) ,
87
+ Err ( error) => error ! (
88
+ "Error while forwarding internal message to controller {:?}" ,
89
+ error
90
+ ) ,
57
91
}
58
92
}
59
93
@@ -75,12 +109,16 @@ pub fn create_node(conn: &SqliteConnection, id: i32) -> Result<usize, diesel::re
75
109
. execute ( conn)
76
110
}
77
111
78
- pub fn update_network_topology ( conn : & SqliteConnection , _node_id : i32 , _parent_node_id : i32 ) -> Result < usize , diesel:: result:: Error > {
112
+ pub fn update_network_topology (
113
+ conn : & SqliteConnection ,
114
+ _node_id : i32 ,
115
+ _parent_node_id : i32 ,
116
+ ) -> Result < usize , diesel:: result:: Error > {
79
117
use crate :: model:: node:: nodes:: dsl:: * ;
80
118
diesel:: update ( nodes)
81
119
. filter ( node_id. eq ( _node_id) )
82
120
. set ( parent_node_id. eq ( _parent_node_id) )
83
- . execute ( conn)
121
+ . execute ( conn)
84
122
}
85
123
86
124
pub fn get_next_node_id ( conn : & SqliteConnection ) -> Option < u8 > {
0 commit comments