Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Ignore build directories
BUILD/
build/

runFile/
# Ignore log files
*.log

# Ignore object files and executables
*.o
*.a
*.so
*.exe
*.sh
# Ignore temporary files
*.tmp
*.swp
*.swo

# Ignore IDE-specific files
.vscode/
*.sublime-workspace
*.sublime-project

# Ignore system-specific files
.DS_Store
Thumbs.db

user_1.cpp
user_2.cpp
user_3.cpp
main_bus.cpp
80 changes: 80 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
cmake_minimum_required(VERSION 3.10)
project(basicApi)

# FetchContent for GoogleTest and GoogleMock
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
)
FetchContent_MakeAvailable(googletest)

FetchContent_Declare(
googlemock
GIT_REPOSITORY https://github.com/google/googlemock.git
GIT_TAG release-1.11.0
)
FetchContent_MakeAvailable(googlemock)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)

# Add source files
set(SOURCES
src/Packet.cpp
src/communication.cpp
test/testCommunication.cpp
)

# Include directories
include_directories(${gtest_SOURCE_DIR}/include ${gmock_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/logger)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/sockets)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/test)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../googletest/googletest/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../googletest/googlemock/include)

# Add the logger source files
set(LOGGER_SOURCES
${CMAKE_SOURCE_DIR}/logger/logger.cpp
)

# Add the test source files
set(TEST_SOURCES
${CMAKE_SOURCE_DIR}/test/loggerTest.cpp
)

# Find and link pthread
find_package(Threads REQUIRED)

# Add GTest and GMock
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../googletest googletest-build)

# Define LOG_LEVEL for compilation (default: INFO)
set(DEFAULT_LOG_LEVEL "logger::LogLevel::INFO")
set(LOG_LEVEL ${DEFAULT_LOG_LEVEL} CACHE STRING "Set the logging level (e.g., logger::LogLevel::ERROR, logger::LogLevel::INFO, logger::LogLevel::DEBUG)")
add_definitions(-DLOG_LEVEL=${LOG_LEVEL})

# Add the test executable
add_executable(testCommunication ${SOURCES})

# Create the test executable
add_executable(LoggerTests ${LOGGER_SOURCES} ${TEST_SOURCES})

# Link the GTest and GMock libraries
target_link_libraries(LoggerTests
gtest
gmock
pthread
)
target_link_libraries(testCommunication gtest gmock gtest_main gmock_main Threads::Threads)

# Enable testing
enable_testing()

# Ensure the test executable is built
add_test(NAME LoggerTests COMMAND LoggerTests)

# Add tests
add_test(NAME testCommunication COMMAND testCommunication)
55 changes: 55 additions & 0 deletions communication/include/bus_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once
#include <mutex>
#include <thread>
#include <atomic>
#include "../include/server_connection.h"
#include <iostream>
#include "packet.h"
#include "global_clock.h"

// Manager class responsible for handling CAN BUS-like communication and collision management
class BusManager {
public:
// Returns the singleton instance of Manager
static BusManager *getInstance(std::vector<uint32_t> idShouldConnect,
uint32_t limit);

// Starts server connection
ErrorCode startConnection();

// Stops server connection
static void stopConnection();

// Receives a packet and checks for collisions before sending
void receiveData(Packet &p);

// Destructor for cleaning up
~BusManager();
private:
ServerConnection server; // Handles communication with the server
static BusManager *instance; // Singleton instance
static std::mutex managerMutex; // Mutex for singleton
Packet *lastPacket; // Stores the last packet received
std::mutex lastPacketMutex; // Protects access to lastPacket
std::atomic<bool> stopFlag; // Indicates if the collision timer should stop
std::thread collisionTimerThread; // Thread for collision management

// Private constructor to ensure singleton pattern
BusManager(std::vector<uint32_t> idShouldConnect, uint32_t limit);

// Sends packet to clients based on whether it's broadcast or unicast
ErrorCode sendToClients(const Packet &packet);

// Starts the timer to check for packet collisions
void startCollisionTimer();

// Checks if the current packet collides with the last one
void checkCollision(Packet &currentPacket);

// Determines packet priority in case of collision, based on CAN BUS protocol
Packet *packetPriority(Packet &a, Packet &b);

// Sends the last packet if necessary and clears it
void checkLastPacket();

};
62 changes: 62 additions & 0 deletions communication/include/client_connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once
#include <thread>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <functional>
#include <iostream>
#include <atomic>
#include "message.h"
#include "../sockets/Isocket.h"
#include "../sockets/mock_socket.h"
#include "../sockets/real_socket.h"
#include <string>
#include "error_code.h"
#include "../include/global_clock.h"

#define PORT 8080
#define IP "127.0.0.1"

class ClientConnection
{
private:
int clientSocket;
sockaddr_in servAddress;
std::atomic<bool> connected;
std::function<void(Packet &)> passPacketCom;
ISocket* socketInterface;
std::thread receiveThread;

public:
// Constructor
ClientConnection(std::function<void(Packet &)> callback, ISocket* socketInterface = new RealSocket());

// Requesting a connection to the server
ErrorCode connectToServer(int id);

// Sends the packet to the manager-sync
ErrorCode sendPacket(Packet &packet);

// Waits for a message and forwards it to Communication
void receivePacket();

// Closes the connection
ErrorCode closeConnection();

// Setter for passPacketCom
void setCallback(std::function<void(Packet&)> callback);

// Setter for socketInterface
void setSocketInterface(ISocket* socketInterface);

// For testing
int getClientSocket();

int isConnected();

bool isReceiveThreadRunning();

//Destructor
~ClientConnection();
};

Loading