Cloudbus simulation in Rust programming language

4 views
Skip to first unread message

Ian Ajzenszmidt

unread,
Feb 24, 2025, 12:51:20 PMFeb 24
to mlug-au
The source code of the Cloudbus  was generated by chatgpt.com, and follows the discussion on Monday February 24/02/2025.

--------------------------------------------------------------------------------------------------------------------------------------------------
ian@ian-PORTEGE-Z930:~$ nano cloudbussim.rs
ian@ian-PORTEGE-Z930:~$ rustc cloudbussim.rs
ian@ian-PORTEGE-Z930:~$ ls clou*
cloudburst1.rs  cloudbursttest  cloudbursttest.rs  cloudbussim  cloudbussim.rs
ian@ian-PORTEGE-Z930:~$ ./cloudbussim
CloudBus Simulation Started!

--- Simulation Step 1 ---
Task 1 assigned to Server-A with initial duration 5.
Task 2 assigned to Server-B with initial duration 8.
Task 3 assigned to Server-C with initial duration 3.
Task 4 assigned to Server-A with initial duration 7.
Task 5 assigned to Server-B with initial duration 4.
Processing Task 1 on Server-A. Remaining time: 4
Processing Task 2 on Server-B. Remaining time: 7
Processing Task 3 on Server-C. Remaining time: 2
Processing Task 4 on Server-A. Remaining time: 6
Processing Task 5 on Server-B. Remaining time: 3

--- Simulation Step 2 ---
Processing Task 1 on Server-A. Remaining time: 3
Processing Task 2 on Server-B. Remaining time: 6
Processing Task 3 on Server-C. Remaining time: 1
Processing Task 4 on Server-A. Remaining time: 5
Processing Task 5 on Server-B. Remaining time: 2

--- Simulation Step 3 ---
Processing Task 1 on Server-A. Remaining time: 2
Processing Task 2 on Server-B. Remaining time: 5
Processing Task 3 on Server-C. Remaining time: 0
Processing Task 4 on Server-A. Remaining time: 4
Processing Task 5 on Server-B. Remaining time: 1
Task 3 on Server-C completed.

--- Simulation Step 4 ---
Processing Task 1 on Server-A. Remaining time: 1
Processing Task 2 on Server-B. Remaining time: 4
Processing Task 4 on Server-A. Remaining time: 3
Processing Task 5 on Server-B. Remaining time: 0
Task 5 on Server-B completed.

--- Simulation Step 5 ---
Processing Task 1 on Server-A. Remaining time: 0
Processing Task 2 on Server-B. Remaining time: 3
Processing Task 4 on Server-A. Remaining time: 2
Task 1 on Server-A completed.

--- Simulation Step 6 ---
Processing Task 2 on Server-B. Remaining time: 2
Processing Task 4 on Server-A. Remaining time: 1

--- Simulation Step 7 ---
Processing Task 2 on Server-B. Remaining time: 1
Processing Task 4 on Server-A. Remaining time: 0
Task 4 on Server-A completed.

--- Simulation Step 8 ---
Processing Task 2 on Server-B. Remaining time: 0
Task 2 on Server-B completed.

CloudBus Simulation Completed in 8 steps.
ian@ian-PORTEGE-Z930:~$ cat cloudbussim.rs
use std::thread;
use std::time::Duration;

/// A task represents a unit of work in the simulation.
struct Task {
    id: usize,
    remaining: u32, // simulation steps remaining for this task
    assigned_server: Option<&'static str>,
}

impl Task {
    fn new(id: usize, duration: u32) -> Task {
        Task {
            id,
            remaining: duration,
            assigned_server: None,
        }
    }
}

fn main() {
    println!("CloudBus Simulation Started!");

    // Define available servers (simulating cloud nodes on the bus)
    let servers = ["Server-A", "Server-B", "Server-C"];

    // Create a list of tasks with predetermined durations (in simulation steps)
    let mut tasks = vec![
        Task::new(1, 5),
        Task::new(2, 8),
        Task::new(3, 3),
        Task::new(4, 7),
        Task::new(5, 4),
    ];

    let mut step = 0;
    // Run the simulation loop until all tasks are completed.
    while !tasks.is_empty() {
        step += 1;
        println!("\n--- Simulation Step {} ---", step);

        // Assign tasks to servers if not already assigned.
        for task in tasks.iter_mut() {
            if task.assigned_server.is_none() {
                // Use a simple round-robin assignment based on task id.
                let server = servers[(task.id - 1) % servers.len()];
                task.assigned_server = Some(server);
                println!(
                    "Task {} assigned to {} with initial duration {}.",
                    task.id, server, task.remaining
                );
            }
        }

        // Process each task for one simulation step.
        for task in tasks.iter_mut() {
            if task.remaining > 0 {
                task.remaining -= 1;
                println!(
                    "Processing Task {} on {}. Remaining time: {}",
                    task.id,
                    task.assigned_server.unwrap(),
                    task.remaining
                );
            }
        }

        // Remove completed tasks and print a completion message.
        tasks.retain(|task| {
            if task.remaining == 0 {
                println!(
                    "Task {} on {} completed.",
                    task.id,
                    task.assigned_server.unwrap()
                );
                false // remove from list
            } else {
                true // keep in list
            }
        });

        // Sleep to simulate real-time progress (half a second per step)
        thread::sleep(Duration::from_millis(500));
    }

    println!("\nCloudBus Simulation Completed in {} steps.", step);
}
ian@ian-PORTEGE-Z930:~$
Reply all
Reply to author
Forward
0 new messages