@@ -18,17 +18,15 @@ use crate::control::{
1818
1919// Structure to hold shared state for all elevators in the system
2020pub struct ElevatorSystem {
21- // Local elevator state
2221 pub local_id : String ,
2322 pub local_elevator : Arc < Mutex < Elevator > > ,
2423
25- // Global system state
26- pub hall_calls : Arc < Mutex < HashMap < ( u8 , u8 ) , ( String , u64 ) > > > , // (floor, direction) -> (aigned_to,ss timestamp)
24+ pub hall_calls : Arc < Mutex < HashMap < ( u8 , u8 ) , ( String , u64 ) > > > ,
2725 pub elevator_states : Arc < Mutex < HashMap < String , ElevatorState > > > ,
2826
29- // Network communication
27+
3028 pub network_manager : Arc < p2p_connect:: NetworkManager > ,
31- pub peers : Arc < Mutex < Vec < String > > > , // Store peer addresses separately
29+ pub peers : Arc < Mutex < Vec < String > > > ,
3230}
3331
3432// Structure to keep track of other elevator states
@@ -37,7 +35,7 @@ pub struct ElevatorState {
3735 pub floor : u8 ,
3836 pub direction : u8 ,
3937 pub call_buttons : Vec < Vec < u8 > > ,
40- pub last_seen : u64 , // Timestamp of last update
38+ pub last_seen : u64 ,
4139 pub is_obstructed : bool ,
4240 pub obstruction_duration : u64 ,
4341}
@@ -53,13 +51,26 @@ pub fn start_reconnection_service(elevator_system: Arc<ElevatorSystem>) {
5351 let elevator_id = elevator_system. local_id . parse :: < usize > ( ) . unwrap_or ( 0 ) ;
5452 if i as usize != elevator_id - 1 {
5553 let peer_message_port = 8878 + i;
54+
55+
56+ // Comment out if using simulator setup:
57+ // ----- Simulator setup begin here: -----
5658 let peer_addr = format ! ( "localhost:{}" , peer_message_port) ;
57- // let peer_addr = format!("10.24.139.104:{}", peer_message_port);
58- // let peer_addr_2 = format!("10.100.23.35:{}", peer_message_port);
59+ elevator_system. establish_bidirectional_connection ( & peer_addr) ;
60+ // ----- Simulator setup end here: -----
61+
62+
63+ // Uncomment and type correct IP if using physical setup:
64+ /*
65+ // ----- Physical machine begin here: -----
66+ let peer_addr = format!("10.24.139.104:{}", peer_message_port);
67+ let peer_addr_2 = format!("10.100.23.35:{}", peer_message_port);
5968
6069 // Try to establish bidirectional connection
6170 elevator_system.establish_bidirectional_connection(&peer_addr);
62- // elevator_system.establish_bidirectional_connection(&peer_addr_2);
71+ elevator_system.establish_bidirectional_connection(&peer_addr_2);
72+ // ----- Physical machine end here -----
73+ */
6374 }
6475 }
6576 }
@@ -73,7 +84,7 @@ pub fn message_listener(elevator_system: Arc<ElevatorSystem>, port: u16, fault_m
7384 println ! ( "Message listener started on port {}" , port) ;
7485
7586 // Create a channel for passing messages to the processor
76- let ( tx, rx) = cbc:: unbounded :: < ( String , String ) > ( ) ; // (message, from_addr)
87+ let ( tx, rx) = cbc:: unbounded :: < ( String , String ) > ( ) ;
7788
7889 // Accept connections and handle messages
7990 thread:: spawn ( move || {
@@ -182,7 +193,6 @@ impl ElevatorSystem {
182193 } ;
183194
184195 if stream. write_all ( msg. to_string ( ) . as_bytes ( ) ) . is_ok ( ) {
185- // println!("Successfully established connection with {}", peer_addr);
186196 return true ;
187197 }
188198 } ,
@@ -207,7 +217,6 @@ impl ElevatorSystem {
207217 // Find the address for this elevator ID
208218 let peers = self . peers . lock ( ) . unwrap ( ) ;
209219 for peer_addr in & * peers {
210- // Here we're sending to all peers, but ideally would target just the requesting elevator
211220 p2p_connect:: send ( Arc :: clone ( & self . network_manager ) , peer_addr, & sync_msg. to_string ( ) ) ;
212221 }
213222 }
@@ -240,7 +249,7 @@ impl ElevatorSystem {
240249 println ! ( "DEBUG: Received hall call message for floor {}, direction {}" ,
241250 floor, direction_to_string( direction) ) ;
242251
243- // ALWAYS turn on the hall call light first (unconditionally)
252+ // Always turn on the hall call light first
244253 {
245254 let elevator = self . local_elevator . lock ( ) . unwrap ( ) ;
246255 elevator. call_button_light ( floor, direction, true ) ;
@@ -365,7 +374,7 @@ impl ElevatorSystem {
365374 }
366375 } ;
367376
368- // ALWAYS turn off the hall call light, regardless of who it was assigned to
377+ // Always turn off the hall call light, regardless of who it was assigned to
369378 {
370379 let elevator = self . local_elevator . lock ( ) . unwrap ( ) ;
371380 elevator. call_button_light ( floor, direction, false ) ;
@@ -394,21 +403,13 @@ impl ElevatorSystem {
394403 }
395404 }
396405
397-
398-
399- // Assign a hall call to the best elevator
400- // that properly handles ties in cost calculation
401-
402-
403-
404406 pub fn assign_hall_call ( & self , floor : u8 , direction : u8 , timestamp : u64 ) {
405407 // Check if call is already assigned
406408 {
407409 let hall_calls = self . hall_calls . lock ( ) . unwrap ( ) ;
408410 if let Some ( ( assigned_id, existing_ts) ) = hall_calls. get ( & ( floor, direction) ) {
409- // If already assigned to someone, and no " newer" timestamp, do nothing
411+ // If already assigned to someone, and no newer timestamp, do nothing
410412 if !assigned_id. is_empty ( ) && * existing_ts == timestamp {
411- // Don't print anything - reduces console spam
412413 return ;
413414 }
414415 }
@@ -458,7 +459,7 @@ impl ElevatorSystem {
458459 return ;
459460 }
460461
461- // Sort by cost (ascending) and then by id (ascending) for consistent tie-breaking
462+ // Sort by cost and then id for tie-breaks.
462463 all_costs. sort_by ( |a, b| {
463464 match a. 0 . cmp ( & b. 0 ) {
464465 std:: cmp:: Ordering :: Equal => a. 1 . cmp ( & b. 1 ) ,
@@ -558,7 +559,7 @@ impl ElevatorSystem {
558559
559560 /// Process a message received from another elevator
560561 pub fn process_message ( & self , message : ElevatorMessage , from_addr : Option < String > ) {
561- // If we got this message from a specific address, make sure it's in our peer list
562+ // Check if message comes from a peer
562563 if let Some ( addr) = from_addr {
563564 self . add_peer ( addr) ;
564565 }
@@ -573,7 +574,6 @@ impl ElevatorSystem {
573574 ElevatorMessage :: CompletedCall { floor, direction } => {
574575 self . handle_completed_call_message ( floor, direction) ;
575576 } ,
576- /* */
577577 ElevatorMessage :: SyncRequest { id } => {
578578 self . handle_sync_request ( id) ;
579579 }
@@ -587,7 +587,7 @@ impl ElevatorSystem {
587587 Err ( _) => 0 ,
588588 } ;
589589
590- let timeout_duration = 5 ; // Match ELEVATOR_TIMEOUT in fault_handler.rs
590+ let timeout_duration = 5 ;
591591 let mut disconnected_ids = Vec :: new ( ) ;
592592
593593 // Identify disconnected elevators with more detailed logging
@@ -617,7 +617,6 @@ impl ElevatorSystem {
617617 }
618618 }
619619
620- // Find and explicitly mark calls for reassignment
621620 self . process_calls_from_disconnected_elevators ( & disconnected_ids) ;
622621 }
623622 }
0 commit comments