Skip to content

Commit

Permalink
integraçao com cliente
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmanG committed Sep 27, 2021
1 parent 5aca3f5 commit 05ddb48
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 36 deletions.
82 changes: 78 additions & 4 deletions TP3/client.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,87 @@
#include <string>
#include <iostream>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fstream>
#include <netinet/in.h>
#include <ctime>
#include "../lib/MyLib.h"
#include <stdlib.h>

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);
}
}
44 changes: 12 additions & 32 deletions TP3/coordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@
#include <iostream>
#include <unistd.h>
#include <queue>
#include <string.h>
#include <tuple>
#include <mutex>
#include <map>

#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
{
Expand All @@ -34,7 +20,7 @@ class remoteMutex
mutex mtx;
bool acquired = false;
map<int, int> granted_map;
int grantNext()
void grantNext()
{
acquired = true;
tuple<int, int> next = mutexQ.front();
Expand All @@ -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));
Expand All @@ -67,7 +53,7 @@ class remoteMutex
}
mtx.unlock();
}
int release()
void release()
{
mtx.lock();
if (mutexQ.empty())
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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, "|"));
}
16 changes: 16 additions & 0 deletions lib/MyLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Function that receives an integer and returns if it is prime or not
#include <math.h>
#include "MyLib.h"
#include <string>
#include <string.h>

using namespace std;

bool isPrime(int number)
{
Expand All @@ -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, "|"));
}
12 changes: 12 additions & 0 deletions lib/MyLib.h
Original file line number Diff line number Diff line change
@@ -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 *);

0 comments on commit 05ddb48

Please sign in to comment.