Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Babakhina.Sofia/lab1/Config.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/home/barulina/barulina/ProjectsVSCode/OpSys/lab1/folder1
/home/barulina/barulina/ProjectsVSCode/OpSys/lab1/folder2
60
/home/barulina/ProjectsVSCode/OpSys/lab1/folder1
/home/barulina/ProjectsVSCode/OpSys/lab1/folder2
60
2 changes: 2 additions & 0 deletions Babakhina.Sofia/lab2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
.vscode
104 changes: 104 additions & 0 deletions Babakhina.Sofia/lab2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

cmake_minimum_required(VERSION 3.10)

project(library)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")

# Set the path for Qt6
set(CMAKE_PREFIX_PATH "/home/barulina/Qt/6.8.0/gcc_64/lib/cmake")

# Include directories
include_directories(include)

# Find Qt6 package
find_package(Qt6 REQUIRED COMPONENTS Widgets)

# Host and Client source files for FIFO
set(HOST_FIFO_SRC_FILES
host/host.cpp
host/host_fifo.cpp
connection/conn_fifo.cpp
)

set(CLIENT_FIFO_SRC_FILES
client/client.cpp
client/client_fifo.cpp
connection/conn_fifo.cpp
)

# Host and Client source files for Queue
set(HOST_MQ_SRC_FILES
host/host.cpp
host/host_mq.cpp
connection/conn_mq.cpp
)

set(CLIENT_MQ_SRC_FILES
client/client.cpp
client/client_mq.cpp
connection/conn_mq.cpp
)

# Host and Client source files for Seg
set(HOST_SEG_SRC_FILES
host/host.cpp
host/host_seg.cpp
connection/conn_seg.cpp
)

set(CLIENT_SEG_SRC_FILES
client/client.cpp
client/client_seg.cpp
connection/conn_seg.cpp
)

set(SEMAPHORE
semaphore/semaphore.cpp
)

set(GUI
gui/client_gui.cpp
gui/host_gui.cpp
)

# Ensure Qt's moc is run on the ChatWindow header
qt6_wrap_cpp(MOC_SOURCES gui/client_gui.h)
qt6_wrap_cpp(MOC_SOURCES gui/host_gui.h)
# qt6_wrap_cpp(MOC_SOURCES_CLIENT client/client.h)

# Add executables for FIFO communication
add_executable(host_fifo ${HOST_FIFO_SRC_FILES} ${SEMAPHORE} ${GUI} ${MOC_SOURCES})
add_executable(client_fifo ${CLIENT_FIFO_SRC_FILES} ${SEMAPHORE} ${GUI} ${MOC_SOURCES})


# Add executables for Queue communication
add_executable(host_mq ${HOST_MQ_SRC_FILES} ${SEMAPHORE} ${GUI} ${MOC_SOURCES})
add_executable(client_mq ${CLIENT_MQ_SRC_FILES} ${SEMAPHORE} ${GUI} ${MOC_SOURCES})

# Add executables for Queue communication
add_executable(host_seg ${HOST_SEG_SRC_FILES} ${SEMAPHORE} ${GUI} ${MOC_SOURCES})
add_executable(client_seg ${CLIENT_SEG_SRC_FILES} ${SEMAPHORE} ${GUI} ${MOC_SOURCES})

# Link Qt libraries to the GUI executable
target_link_libraries(host_fifo Qt6::Widgets)
target_link_libraries(client_fifo Qt6::Widgets)
target_link_libraries(host_mq Qt6::Widgets)
target_link_libraries(client_mq Qt6::Widgets)
target_link_libraries(host_seg Qt6::Widgets)
target_link_libraries(client_seg Qt6::Widgets)


# Set output directories for all executables
set_target_properties(host_fifo PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(client_fifo PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(host_mq PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(client_mq PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(host_seg PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(client_seg PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})


# Custom target to build all
add_custom_target(all_targets DEPENDS host_fifo client_fifo host_mq client_mq host_seg client_seg)
17 changes: 17 additions & 0 deletions Babakhina.Sofia/lab2/book.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#include <vector>

struct Book {
std::string name;
int count;
};

const std::vector<Book> books = {
{"crime & punisment", 15},
{"the idiot", 15},
{"the karamazov brothers", 15},
{"white nights ", 7},
{"the gambler", }
};
13 changes: 13 additions & 0 deletions Babakhina.Sofia/lab2/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
mkdir -p build
cd build
cmake ..
make

find . -type f -name '*.o' -delete
find . -type f -name '*.cmake' -delete
find . -type f -name 'CMakeCache.txt' -delete
find . -type f -name 'Makefile' -delete
rm -rf CMakeFiles
rm -rf .qt
rm -rf gui
108 changes: 108 additions & 0 deletions Babakhina.Sofia/lab2/client/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "client.h"

#include <QMessageBox>
#include <QApplication>
#include <QTimer>
#include <fstream>
#include <unistd.h>
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <csignal>


Client::Client(std::vector<Book> books) : client_books(std::move(books)), window(client_books) {
read_host_pid();
window.client_pid = client_pid;
semaphore = new Semaphore(host_pid, 0);
}

void Client::read_host_pid() {
std::ifstream pid_file("host_pid.txt");
if (!pid_file) {
std::cout << "Failed to open PID file\n";
return;
}
pid_file >> host_pid;
pid_file.close();
if (std::remove("host_pid.txt") != 0) {
std::cout << "Error deleting PID file\n";
return;
}
}

void Client::read_from_host() {
while (is_running) {
if (client_conn && client_conn->is_valid()) {
std::string message;
const size_t max_size = 1024;
semaphore->wait();
if (client_conn->read(message, max_size)) {
bool flag = true;
std::string str;
if (message.rfind("YES", 0) == 0) {
window.success_take_book();
str = message.substr(3);
}
else if (message.rfind("NO", 0) == 0) {
window.fail_take_book();
window.wrap_reset_timer();
flag = false;
str = message.substr(2);
}
int del_pos = str.find("#");
std::string book_name = str.substr(0, del_pos);
std::string time = str.substr(del_pos + 1);
window.update_books(books, "TAKE", book_name, time, flag);
}
semaphore->post();
}
sleep(0.5);
}
}

void Client::write_to_host() {
QObject::connect(&window, &ClientWindow::book_selected, [this](const QString& book_name) {
auto time = std::chrono::system_clock::now();
std::time_t time_c = std::chrono::system_clock::to_time_t(time);
std::tm* ttime = std::localtime(&time_c);
std::ostringstream oss;
oss << std::put_time(ttime, "%H:%M");
std::string request = "TAKE " + book_name.toStdString() + "#" + client_name + "#" + oss.str();
semaphore->wait();
if (!host_conn->write(request)) {
std::cout << "Failed to send request\n";
return;
}
window.wrap_stop_timer();
semaphore->post();
});

QObject::connect(&window, &ClientWindow::book_returned, [this] (const QString& book_name) {
auto time = std::chrono::system_clock::now();
std::time_t time_c = std::chrono::system_clock::to_time_t(time);
std::tm* ttime = std::localtime(&time_c);

std::ostringstream oss;
oss << std::put_time(ttime, "%H:%M");
std::string request = "RETURN " + book_name.toStdString() + "#" + client_name + "#" + oss.str();
semaphore->wait();

if (!host_conn->write(request)) {
std::cout << "Failed to send request\n";
return;
}

window.wrap_reset_timer();

semaphore->post();
window.update_books(books, "RETURN", book_name.toStdString(), oss.str(), true);
});
}

void read_wrap(Client& client) {
client.read_from_host();
}
52 changes: 52 additions & 0 deletions Babakhina.Sofia/lab2/client/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "../gui/client_gui.h"
#include "../semaphore/semaphore.h"
#include "../book.h"

#include <QMessageBox>
#include <QTimer>
#include <QApplication>

#include <fstream>
#include <iostream>
#include <unistd.h>
#include <vector>
#include <sys/ipc.h>
#include <thread>
#include <stdlib.h>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <csignal>


class Client{
protected:
std::vector<Book> client_books;

pid_t client_pid = getpid();
pid_t host_pid;

public:
std::string client_name = "Fryasha";
bool is_running = true;

Semaphore* semaphore;

Conn* client_conn;
Conn* host_conn;

ClientWindow window;

Client(std::vector<Book> books);
virtual ~Client() {}

virtual bool setup_conn() = 0;
void read_from_host();
void write_to_host();
void read_host_pid();
};

void read_wrap(Client& client);
47 changes: 47 additions & 0 deletions Babakhina.Sofia/lab2/client/client_fifo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "client_fifo.h"


bool ClientFifo::setup_conn() {
std::string client_path = "/tmp/chat_client_fifo" + std::to_string(host_pid);
std::string host_path = "/tmp/chat_host_fifo" + std::to_string(host_pid);

client_conn = new ConnFifo(client_path, false);
if (!client_conn->is_valid()) {
std::cout << "Failed to open FIFO for writing\n";
return false;
}

host_conn = new ConnFifo(host_path, false);
if (!host_conn->is_valid()) {
std::cout << "Failed to open FIFO for reading\n";
return false;
}
return true;
};


ClientFifo::ClientFifo(std::vector<Book> books) : Client(std::move(books)) {
setup_conn();
}


ClientFifo::~ClientFifo() {
delete client_conn;
delete host_conn;
delete semaphore;
}

int main(int argc, char* argv[]) {
QApplication app(argc, argv);
ClientFifo client(books);
std::thread read_thread(read_wrap, std::ref(client));
client.write_to_host();
client.window.show();
int res = app.exec();
client.is_running = false;
if (read_thread.joinable()) {
read_thread.join();
}

return res;
};
14 changes: 14 additions & 0 deletions Babakhina.Sofia/lab2/client/client_fifo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "client.h"
#include "../connection/conn_fifo.h"


class ClientFifo : public Client {
public:
ClientFifo(std::vector<Book> books);
~ClientFifo();

bool setup_conn() override;
};

Loading