Skip to content

Commit 4e35ce0

Browse files
committed
Update
1 parent ef1ec03 commit 4e35ce0

1 file changed

Lines changed: 33 additions & 38 deletions

File tree

src/main.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, Mutex};
1+
use std::sync::Arc;
22
use std::thread;
33
use std::time::{SystemTime, Duration};
44
use std::env;
@@ -51,7 +51,7 @@ fn main() -> std::io::Result<()> {
5151
}
5252

5353
// Initialize network
54-
let network_port = 7878 + elev_port - 15657; // Network ports start at 7878
54+
let network_port = 7878 + elev_port - 15657;
5555
let network_manager = p2p_connect::start_peer_manager(network_port);
5656

5757

@@ -62,9 +62,6 @@ fn main() -> std::io::Result<()> {
6262
Arc::clone(&network_manager)
6363
));
6464

65-
66-
67-
// *** Add this line to broadcast the current state ***
6865
elevator_system.broadcast_state();
6966

7067

@@ -89,16 +86,20 @@ fn main() -> std::io::Result<()> {
8986
// Try to connect to other potential elevators
9087
for i in 0..3 {
9188
if i != (elev_port - 15657) as usize {
92-
// IP adresses for physical machines:
89+
90+
// Uncomment if using physical machines with IP address:
91+
// // ----- Physical machine begin here: -----
9392
/*
9493
let peer_message_port = 8878 ;
9594
// ip adresses are hardcoded for now
9695
let peer_addr = format!("10.24.139.104:{}", peer_message_port);
9796
let peer_addr_2 = format!("10.100.23.35:{}", peer_message_port);
9897
println!("Testing connection to potential peer at {}", peer_addr);
9998
*/
99+
// // ----- Physical machine end here -----
100+
100101

101-
// Localhost for simulators:
102+
// ----- Localhost for simulators begin here -----:
102103
let peer_message_port = 8878 + i;
103104
let peer_addr = format!("localhost:{}", peer_message_port);
104105

@@ -115,15 +116,25 @@ fn main() -> std::io::Result<()> {
115116
Arc::clone(&elevator_system_clone.network_manager),
116117
&peer_addr
117118
);
119+
118120
// Uncomment if using physical machines with IP address
119-
/*p2p_connect::connect(
121+
/*
122+
// ----- Physical machine begin here: -----
123+
p2p_connect::connect(
120124
Arc::clone(&elevator_system_clone.network_manager),
121125
&peer_addr_2
122-
);*/
123-
// 2) Add the peer to our local ElevatorSystem list (so we know about it)
126+
);
127+
// ----- Physical machine end here -----
128+
*/
129+
130+
// 2) Add the peer to our local ElevatorSystem list
124131
elevator_system_clone.add_peer(peer_addr.clone());
125-
// Uncomment if using physical machines with IP address
132+
133+
// Uncomment if using physical machines with IP address:
134+
// ----- Physical machine begin here: -----
135+
// elevator_system_clone.add_peer(peer_addr.clone());
126136
// elevator_system_clone.add_peer(peer_addr_2.clone());
137+
// ----- Physical machine end here -----
127138

128139
// 3) Send our initial state to the peer
129140
match std::net::TcpStream::connect(&peer_addr) {
@@ -156,7 +167,7 @@ fn main() -> std::io::Result<()> {
156167
// Set up polling
157168
let poll_period = Duration::from_millis(25);
158169

159-
// Crossbeam for call buttons
170+
// Crossbeams for various polling functions
160171
let (call_button_tx, call_button_rx) = cbc::unbounded::<elevio::poll::CallButton>();
161172
{
162173
// Clone the elevator handle so that there can be a new thread dedicated to it.
@@ -166,7 +177,6 @@ fn main() -> std::io::Result<()> {
166177
spawn(move || elevio::poll::call_buttons(elevator, call_button_tx, poll_period));
167178
}
168179

169-
// Crossbeam for floor sensor
170180
let (floor_sensor_tx, floor_sensor_rx) = cbc::unbounded::<u8>();
171181
{
172182
let elevator_new = elevator.clone();
@@ -179,7 +189,6 @@ fn main() -> std::io::Result<()> {
179189
spawn(move || elevio::poll::stop_button(elevator, stop_button_tx, poll_period));
180190
}
181191

182-
// Crossbeam for obstruction
183192
let (obstruction_tx, obstruction_rx) = cbc::unbounded::<bool>();
184193
{
185194
let elevator = elevator.clone();
@@ -227,20 +236,17 @@ fn main() -> std::io::Result<()> {
227236
if !elevator.call_buttons.iter().any(|x| x == &callbutton) {
228237
elevator.call_buttons.push(callbutton.clone()); // Clone here
229238

230-
// --- Added Persistence Call ---
231239
println!("Persisting state after adding cab call: floor {}, call {}", call_button.floor, call_button.call);
232240
fault_handler::persist_elevator_state(
233241
&elevator_system.local_id,
234242
elevator.current_floor,
235243
elevator.current_direction,
236244
&elevator.call_buttons // Pass reference
237245
).unwrap_or_else(|e| eprintln!("Failed to persist state: {}", e));
238-
// --- End of Added Persistence Call ---
239246

240247
}
241248

242-
// Start elevator if needed
243-
// Pass callbutton floor and a placeholder direction (0) as start_elevator recalculates if needed
249+
// Start elevator
244250
start_elevator(&mut elevator, callbutton[0], 0); // Use callbutton[0]
245251

246252
} else {
@@ -263,10 +269,10 @@ fn main() -> std::io::Result<()> {
263269
let stop = stop_btn.unwrap();
264270
println!("Stop button: {:?}", stop);
265271
if stop {
266-
let local_id = elevator_system.local_id.clone(); // Clone id for use after lock drop
272+
let local_id = elevator_system.local_id.clone();
267273
let mut calls_to_reassign = Vec::new();
268274

269-
{ // Scope for elevator lock
275+
{
270276
let mut elevator = elevator_system.local_elevator.lock().unwrap();
271277

272278
// Immediately stop the elevator
@@ -280,12 +286,11 @@ fn main() -> std::io::Result<()> {
280286
}
281287
}
282288

283-
// Clear local pending call requests (cab calls and assigned hall calls)
289+
// Clear local pending call requests
284290
elevator.call_buttons.clear();
285291

286-
// --- Start of Added Reassignment Logic ---
287292
// Find hall calls assigned to this elevator
288-
{ // Scope for hall_calls lock
293+
{
289294
let hall_calls = elevator_system.hall_calls.lock().unwrap();
290295
for ((floor, direction), (assigned_to, timestamp)) in hall_calls.iter() {
291296
if *assigned_to == local_id {
@@ -294,27 +299,24 @@ fn main() -> std::io::Result<()> {
294299
println!("Stop button: Marking call ({}, {}) for reassignment from {}", floor, direction, local_id);
295300
}
296301
}
297-
} // hall_calls lock released here
298-
// --- End of Added Reassignment Logic ---
299-
302+
}
300303

301304
// Open the door if at a floor
302305
if elevator.floor_sensor().is_some() {
303306
elevator.door_light(true);
304-
// Note: Blocking sleep, consider async/timer if this becomes an issue
305307
std::thread::sleep(Duration::from_secs(3));
306308
elevator.door_light(false);
307309
}
308310

309-
// Persist the cleared state (optional but good practice)
311+
// Persist the cleared state
310312
fault_handler::persist_elevator_state(
311313
&local_id,
312314
elevator.current_floor,
313315
elevator.current_direction,
314316
&elevator.call_buttons
315317
).unwrap_or_else(|e| eprintln!("Failed to persist state after stop: {}", e));
316318

317-
} // elevator lock released here
319+
}
318320

319321
// Reassign the collected hall calls outside the elevator lock
320322
for (floor, direction, timestamp) in calls_to_reassign {
@@ -323,8 +325,6 @@ fn main() -> std::io::Result<()> {
323325
elevator_system.assign_hall_call(floor, direction, timestamp);
324326
}
325327

326-
327-
// Broadcast updated (stopped) state
328328
elevator_system.broadcast_state();
329329
}
330330
},
@@ -356,8 +356,8 @@ fn main() -> std::io::Result<()> {
356356
// Force the door to stay open
357357
elevator.door_light(true);
358358

359-
// IMMEDIATELY reassign calls without waiting 10 seconds
360-
drop(elevator); // Release lock before acquiring call locks
359+
// Reassign calls without waiting 10 seconds
360+
drop(elevator);
361361

362362
// Find and reassign this elevator's calls
363363
let calls_to_reassign = {
@@ -376,13 +376,11 @@ fn main() -> std::io::Result<()> {
376376

377377
// Reassign each call
378378
for (floor, direction, timestamp) in calls_to_reassign {
379-
// Mark the call as unassigned
380379
{
381380
let mut hall_calls = elevator_system.hall_calls.lock().unwrap();
382381
hall_calls.insert((floor, direction), (String::new(), timestamp));
383382
}
384383

385-
// Reassign the call
386384
elevator_system.assign_hall_call(floor, direction, timestamp);
387385
}
388386

@@ -394,9 +392,6 @@ fn main() -> std::io::Result<()> {
394392

395393
// Reset obstruction timer
396394
elevator.obstruction_start_time = None;
397-
398-
// Allow door to close if no pending obstruction
399-
// Don't automatically close the door - let the main algorithm handle it
400395
}
401396
} else {
402397
obstr = false;

0 commit comments

Comments
 (0)