From 05ddb487c228c89acde2f8c92e155e156be7a073 Mon Sep 17 00:00:00 2001 From: guilhermebergman Date: Mon, 27 Sep 2021 14:27:57 -0300 Subject: [PATCH] =?UTF-8?q?integra=C3=A7ao=20com=20cliente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TP3/client.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++--- TP3/coordinator.cpp | 44 +++++++----------------- lib/MyLib.cpp | 16 +++++++++ lib/MyLib.h | 12 +++++++ 4 files changed, 118 insertions(+), 36 deletions(-) diff --git a/TP3/client.cpp b/TP3/client.cpp index aa26c39..4345243 100644 --- a/TP3/client.cpp +++ b/TP3/client.cpp @@ -1,13 +1,87 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include "../lib/MyLib.h" +#include + using namespace std; class remoteMutex { private: + int sock; /* data */ public: - remoteMutex(string coord_ip); - int acquire(); - int release(); - ~remoteMutex(); + remoteMutex(char *coord_ip) + { + int valread; + struct sockaddr_in serv_addr; + + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + cout << "Erro criando o socket." << endl; + return; + } + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(PORT); + + if (inet_pton(AF_INET, coord_ip, &serv_addr.sin_addr) <= 0) + { + cout << "Erro no endereço IPv4." << endl; + return; + } + + if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + { + printf("Erro ao conectar no socket do servidor."); + return; + } + } + int acquire() + { + char *buffer = encode(REQUEST, getpid()); + send(sock, buffer, sizeof(char) * 30, 0); + int valread = read(sock, buffer, sizeof(char) * 30); + delete buffer; + if (valread != 0) + { + return 1; + } + else + { + return 0; + } + } + int release() + { + char *buffer = encode(RELEASE, getpid()); + send(sock, buffer, sizeof(char) * 30, 0); + delete buffer; + return 1; + } + ~remoteMutex(){}; }; + +int main(int argc, char *argv[]) +{ + int k = atoi(argv[1]); + int r = atoi(argv[2]); + remoteMutex rm(argv[3]); + ofstream myfile; + for (int i = 0; i < r; i++) + { + if (!rm.acquire()) + return -1; + myfile.open("resultado.txt", ios::app); + myfile << time(0) << " " << getpid() << "\n"; + myfile.close(); + rm.release(); + sleep(k); + } +} \ No newline at end of file diff --git a/TP3/coordinator.cpp b/TP3/coordinator.cpp index f27fbf7..6343eb7 100644 --- a/TP3/coordinator.cpp +++ b/TP3/coordinator.cpp @@ -3,29 +3,15 @@ #include #include #include -#include #include #include #include - -#define PORT 8080 -#define MAXLINE 1024 -#define F 30 +#include "../lib/MyLib.h" using namespace std; void terminal(); -char *encode(int, int); -void decode(char *, int *, int *); - -enum -{ - REQUEST = 1, - GRANT, - RELEASE -}; - -remoteMutex rmutex; +int listener(); class remoteMutex { @@ -34,7 +20,7 @@ class remoteMutex mutex mtx; bool acquired = false; map granted_map; - int grantNext() + void grantNext() { acquired = true; tuple next = mutexQ.front(); @@ -52,8 +38,8 @@ class remoteMutex } public: - remoteMutex(); - int request(int p_id, int p_fd) + remoteMutex(){}; + void request(int p_id, int p_fd) { mtx.lock(); mutexQ.push(make_tuple(p_id, p_fd)); @@ -67,7 +53,7 @@ class remoteMutex } mtx.unlock(); } - int release() + void release() { mtx.lock(); if (mutexQ.empty()) @@ -95,10 +81,16 @@ class remoteMutex void printGrantCount() { mtx.lock(); + for (auto it = granted_map.cbegin(); it != granted_map.cend(); ++it) + { + std::cout << it->first << " " << it->second << "\n"; + } mtx.unlock(); } }; +remoteMutex rmutex; + int main() { pid_t terminal_pid = fork(); @@ -214,15 +206,3 @@ int listener() } return 0; } -char *encode(int msg, int id) -{ - char *encoded = new char[F]{0}; - string temp = to_string(msg) + '|' + to_string(id) + '|'; - temp.copy(encoded, temp.length(), 0); - return encoded; -} -void decode(char *buffer, int *message, int *id) -{ - *message = stoi(strtok(buffer, "|")); - *id = stoi(strtok(NULL, "|")); -} \ No newline at end of file diff --git a/lib/MyLib.cpp b/lib/MyLib.cpp index bd6c426..ea58d09 100644 --- a/lib/MyLib.cpp +++ b/lib/MyLib.cpp @@ -2,6 +2,10 @@ // Function that receives an integer and returns if it is prime or not #include #include "MyLib.h" +#include +#include + +using namespace std; bool isPrime(int number) { @@ -12,3 +16,15 @@ bool isPrime(int number) } return true; } +char *encode(int msg, int id) +{ + char *encoded = new char[F]{0}; + string temp = to_string(msg) + '|' + to_string(id) + '|'; + temp.copy(encoded, temp.length(), 0); + return encoded; +} +void decode(char *buffer, int *message, int *id) +{ + *message = stoi(strtok(buffer, "|")); + *id = stoi(strtok(NULL, "|")); +} \ No newline at end of file diff --git a/lib/MyLib.h b/lib/MyLib.h index a0912d5..eb561ba 100644 --- a/lib/MyLib.h +++ b/lib/MyLib.h @@ -1,3 +1,15 @@ #pragma once +#define PORT 8080 +#define F 30 + +enum +{ + REQUEST = 1, + GRANT, + RELEASE +}; + bool isPrime(int number); +char *encode(int, int); +void decode(char *, int *, int *);