network.hpp 3.81 KB
Newer Older
1 2 3 4 5
/**
 * @file network.hpp
 * @brief Contains network-related classes and functions
 */

6 7 8 9 10
#pragma once

#include <string>
struct BatsimContext;

11 12 13
/**
 * @brief Contains the different network stamps used in the network protocol
 */
14 15
enum NetworkStamp : char
{
16 17 18 19 20 21 22 23 24 25
    STATIC_JOB_ALLOCATION = 'J' //!< Decision -> Batsim. Allows the Decision real process to take a scheduling decision: To decide where a job should be executed now
    ,JOB_REJECTION = 'R' //!< Decision -> Batsim. Allows the Decision real process to reject a job (the job will not be computed)
    ,NOP = 'N' //!< Decision <-> Batsim. Does nothing. Since the network protocol is synchronous, the simulation would be stopped if a network component (Batsim or the Decision real process) stopped sending messages. That's what this stamp is used for.
    ,STATIC_JOB_SUBMISSION = 'S' //!< Batsim -> Decision. Batsim tells the Decision real process that a static job (one in the initial workload) has been submitted
    ,STATIC_JOB_COMPLETION = 'C' //!< Batsim -> Decision. Batsim tells the Decision real process that a static job (one in the initial workload) has been completed (finished its execution)
    ,PSTATE_SET = 'P' //!< Decision -> Batsim. The Decision real process wants to change the power state of one or several machines
    ,NOP_ME_LATER = 'n' //!< Decision -> Batsim. The Decision real process wants to be awaken at a future simulation time
    ,TELL_ME_CONSUMED_ENERGY = 'E' //!< Decision -> Batsim. The Decision real process wants to know how much energy has been consumed on computing machines since the beginning of the simulation
    ,PSTATE_HAS_BEEN_SET = 'p' //!< Batsim -> Decision. Batsim acknowledges that the power state of one of several machines has been changed
    ,CONSUMED_ENERGY = 'e' //!< Batsim -> Decision. Batsim tells the Decision process how much energy has been used since the beginning of the simulation
26 27
};

28 29 30
/**
 * @brief Handles the socket used in the network protocol
 */
31 32 33
class UnixDomainSocket
{
public:
34 35 36
    /**
     * @brief Creates a UnixDomainSocket, whose internal sockets do not exist
     */
37
    UnixDomainSocket();
38 39 40 41 42

    /**
     * @brief Creates a UnixDomainSocket, whose internal server socket is created via create_socket
     * @param[in] filename The name of the file that is used as the Unix Domain Socket
     */
43
    UnixDomainSocket(const std::string & filename);
44 45 46 47

    /**
     * @brief Destroys a UnixDomainSocket, closing open sockets
     */
48 49
    ~UnixDomainSocket();

50 51 52 53 54
    /**
     * @brief Creates the server socket
     * @brief This method unlinks the file, creates a socket, binds it and starts listening on it
     * @param[in] filename The name of the file that is used as the Unix Domain Socket
     */
55
    void create_socket(const std::string & filename);
56 57 58 59

    /**
     * @brief Accept one pending connection (wait for it if none has been done yet)
     */
60
    void accept_pending_connection();
61 62 63 64

    /**
     * @brief Closes the internal sockets
     */
65 66
    void close();

67 68 69 70
    /**
     * @brief Waits for a message on the client socket (from the Decision real process) then returns it
     * @return The message received on the client socket
     */
71
    std::string receive();
72 73 74 75 76

    /**
     * @brief Sends a message on the client socket (to the Decision real process)
     * @param[in] message The message to send
     */
77 78 79
    void send(const std::string & message);

private:
Millian Poquet's avatar
Millian Poquet committed
80 81
    int _server_socket = -1; //!< The server-side socket
    int _client_socket = -1; //!< The client-side socket
82 83
};

84 85 86 87 88 89 90
/**
 * @brief The process in charge of doing a Request-Reply iteration with the Decision real process
 * @details This process sends a message to the Decision real process (Request) then waits for the answered message (Reply)
 * @param[in] argc The number of arguments
 * @param[in] argv The arguments' values
 * @return 0
 */
91
int request_reply_scheduler_process(int argc, char *argv[]);