#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>


/*
 * The following is a forwarding table for the following network
 *
 * 0   1
 *  \ /
 *   2
 *    \
 *     3 -- 4
 *
 * It answers the question:
 *   If node X wants to sent to node Y, what neighbor should it send to?
 *
 * For instance, if node 0 wants to send to node 4, it will send to node 2
 * and node 2 will in turn send to 3 which in turn will send to 4.
 *
 * Looking up in the table works as follows:
 *  - First index is from, 
 *  - Second index is to,
 *  - Result is neighbor to send to
 *
 * Example:
 *   forward_table[0][4] gives 2
 * 
 * -1 indicates that you can't send to yourself.
 */
int forward_table[5][5] = {
    {-1, 2, 2, 2, 2},
    {2, -1, 2, 2, 2},
    {0, 1, -1, 3, 3},
    {2, 2, 2, -1, 4},
    {3, 3, 3, 3, -1}
};



/*
 * An array of the five nodes in the network. Node 0 is at index 0 etc.
 * Note: The 'Node' type must be defined somewhere. Feel free to rename it, if desired.
 */
Node* nodes[5];


int main(int argc, char** argv) {

    for (int i = 0; i < 5; i++) {
        // Set up each nodes[i]
    }


    // Things to send:
    //send(0, 4, "Test");


    // Simulate network
    printf("Starting network...\n");
    printf("Press CTRL+C to stop.\n");

    int step = 1;

    while (true) {
        printf("Step %d\n", step);

        for (int i = 0; i < 5; i++) {
            // Move a packet (if any) from nodes[i]'s incoming queue to its outgoing.
            // Except if the packet is for nodes[i] itself, then trigger the receive function.
        }

        for (int i = 0; i < 5; i++) {
            // Move a packet (if any) from nodes[i]'s outgoing queue 
            // to the appropriate neighbor's incoming queue using the forward_table.
        }

        step++;
        sleep(1);
    }

    return 0;
}
