#include #include #include #include #include #include "lib/enchufe.h" #include "lib/lib.h" #include "lib/log.h" #define BUF_LEN 0x100 #define SLEEP_TIME 3 size_t total_time = 0; int main(int argc, char** argv) { assert(argc == 3 && "You must provide three arguments."); // Create the socket IPv4 ip = parse_address(argv[1]); Port port = (Port)atoi(argv[2]); log(INFO, "Connecting to: %d.%d.%d.%d on port: %d\n", ip.bytes[0], ip.bytes[1], ip.bytes[2], ip.bytes[3], port); Enchufe enchufe = enchufa(ip, port); conecta(enchufe); /* run the command * here I used `ps auxww -Aeo args` in order to use the entire string used * by `ps`. */ FILE* fp = popen("ps -o args | sed '1d'", "r"); exists(fp); // Initialize random seed generator. srand((uint32_t)time(NULL)); char program[BUF_LEN]; while (fgets(program, BUF_LEN, fp)) { // path has a line feed character at the end that must be removed size_t len = safe_strlen(program, BUF_LEN) - 1; program[len] = '\0'; // Create the process that will be sent Proc proc = { .time = (Time)rand() % SLEEP_TIME, .program = { .buf = (Byte*)program, .len = len, }, }; total_time += proc.time; // Send the process. log(INFO, "Sent program: %s with time %d.\n", proc.program.buf, proc.time); Buffer in_buf = serialize(proc); zumba(enchufe, in_buf); /* Clear the buffer for the program and free the memory associated to * what was just sent. */ memset(program, 0, BUF_LEN); free(in_buf.buf); // Create new buffer to receive output from cluster. Byte buf[BUF_LEN] = {0}; Buffer out_buf = { .buf = buf, .len = BUF_LEN, }; // Receive message from cluster. size_t msg_len = recibe(enchufe, out_buf); assert(safe_strlen((char*)out_buf.buf, BUF_LEN) == msg_len); log(INFO, "Received: %s.\n", (char*)out_buf.buf); } // Let the user know that the program is done. log(INFO, "Done.\n"); log(INFO, "Total time: %zu.\n", total_time); // Close socket. desenchufa(enchufe); return 0; }