Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jmo lab3 #76

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
39 changes: 39 additions & 0 deletions Jason/lab3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.10)
project(Lab3)


find_package(Protobuf REQUIRED)

# Define the libraries
add_library(msg STATIC proto/message.pb.cc)
add_library(server STATIC src/server.cpp)
add_library(client STATIC src/client.cpp)

target_include_directories(client PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>"
)
target_include_directories(server PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>"
)
target_include_directories(msg PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/proto>"
"$<INSTALL_INTERFACE:include>"
)

#Link the necessary targets
target_link_libraries(msg PUBLIC protobuf::libprotobuf)
target_link_libraries(client PUBLIC msg)
target_link_libraries(server PUBLIC msg)

# Add the server executable
add_executable(server_exe app/server_driver.cpp)
target_link_libraries(server_exe PUBLIC server)


# Add the client executable
add_executable(client_exe app/client_driver.cpp)
target_link_libraries(client_exe PUBLIC client)

install (TARGETS server_exe client_exe DESTINATION ${CMAKE_INSTALL_BINDIR})
53 changes: 53 additions & 0 deletions Jason/lab3/app/client_driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "client.hpp"
#include <iostream>

/**
* @brief Executable that initiates Client
*
* This files boots up the Client
*/
int main() {
// Initialize the client with server IP and port
Client client("127.0.0.1", 8080); // Replace with the server's IP address AKA the car

// Create and send a message with the input '1'
message::data message;
message.set_sender_name("Msg From Client");

std::string input;
std::cout << "Press Enter to send a message: ";
std::getline(std::cin, input);

while (true) {
std::cout << "Press Enter to send a message (or type -1 to exit): ";
std::getline(std::cin, input);

// If the user enters -1, break the loop and exit
if (input == "-1") {
std::cout << "Exiting program..." << std::endl;
break;
}

// If Enter is pressed (input is empty), send the message
if (input.empty()) {
// Send the message
if (client.SendMessage(message)) {
std::cout << "Message sent successfully" << std::endl;
} else {
std::cerr << "Failed to send message" << std::endl;
}

// Receive and print the response
message::data responseMessage;
if (client.ReceiveMessage(responseMessage)) {
std::cout << "Received response from server" << std::endl;
} else {
std::cerr << "Failed to receive response" << std::endl;
}
} else {
std::cout << "No message sent." << std::endl;
}
}

return 0;
}
35 changes: 35 additions & 0 deletions Jason/lab3/app/server_driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "server.hpp"
#include <iostream>

/**
* @brief Executable that initiates Server
*
* This files boots up the Server
*/
int main() {
// Initiate Server
Server server("0.0.0.0", 8080);

std::cout << "Server is running and listening on port 8080..." << std::endl;
// Be on the constant lookout for messages
while (true) {
message::data receivedMessage;

// Receive message from client
if (server.ReceiveMessage(receivedMessage)) {
// Prepare and send a response to client
message::data responseMessage;
responseMessage.set_sender_name("Message from Server");
// Send message
if (server.SendMessage(responseMessage)) {
std::cout << "Response sent successfully" << std::endl;
} else {
std::cerr << "Failed to send response" << std::endl;
}
} else {
std::cerr << "Failed to receive message" << std::endl;
}
}

return 0;
}
9 changes: 9 additions & 0 deletions Jason/lab3/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ stdenv, cmake, protobuf }:

stdenv.mkDerivation rec {
pname = "lab3";
version = "0.1.0";
src = ./.;
nativeBuildInputs = [ cmake ];
buildInputs = [ protobuf ];
}
60 changes: 60 additions & 0 deletions Jason/lab3/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions Jason/lab3/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
description = "Lab 3 flake";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
nixutils.url = "github:numtide/flake-utils";
};

outputs = {
self, nixpkgs, nixutils
}:

let
system = "aarch64-darwin";
lab3_overlay = final: prev: {
lab3 = final.callPackage ./default.nix { };
};

my_overlays = [ lab3_overlay ];
pkgs = import nixpkgs {
system = "aarch64-darwin";
overlays = [ self.overlays.default ];
};

in {
overlays.default = nixpkgs.lib.composeManyExtensions my_overlays;

packages.aarch64-darwin =
rec {
lab3 = pkgs.lab3;
default = lab3;
};

devShells.aarch64-darwin.default =
pkgs.mkShell rec {
name = "nix-devshell";
packages = with pkgs; [
cmake
protobuf
lab3
];
shellHook = ''
export PS1="$(echo -e '\uF121') {\[$(tput sgr0)\]\[\033[38;5;228m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]} (${name}) \\$ \[$(tput sgr0)\]"
'';
};

legacyPackages.aarch64-darwin =
import nixpkgs {
inherit system;
overlays = [
(final: _: { lab3 = final.callPackage ./default.nix { }; })
];
};
};
}
24 changes: 24 additions & 0 deletions Jason/lab3/include/UDPSocket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef UDP_SOCKET_H
#define UDP_SOCKET_H

#include <string>
#include <arpa/inet.h>

class UDPSocket {
public:
UDPSocket();
~UDPSocket();

bool bindSocket(const std::string& ip, int port);
ssize_t sendTo(const std::string& message, const std::string& ip, int port);
ssize_t receiveFrom(std::string& message, std::string& senderIp, int& senderPort);

private:
int sockfd;
struct sockaddr_in localAddr;
struct sockaddr_in remoteAddr;

void closeSocket();
};

#endif // UDP_SOCKET_H
25 changes: 25 additions & 0 deletions Jason/lab3/include/client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef CLIENT_H
#define CLIENT_H

#include <string>
#include <arpa/inet.h> // For sockaddr_in
#include <sys/socket.h> // For socket functions
#include <unistd.h> // For close

#include "message.pb.h"

class Client {
public:
Client(const std::string& server_ip, uint16_t server_port);
~Client();

bool SendMessage(const message::data& message);
bool ReceiveMessage(message::data& message);

private:
int _sockfd;
sockaddr_in _server_addr;
static const int _BUFFER_SIZE = 1024;
};

#endif // CLIENT_H
26 changes: 26 additions & 0 deletions Jason/lab3/include/server.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef SERVER_H
#define SERVER_H

#include <string>
#include <arpa/inet.h> // For sockaddr_in
#include <sys/socket.h> // For socket functions
#include <unistd.h> // For close

#include "message.pb.h"

class Server {
public:
Server(const std::string& server_ip, uint16_t server_port);
~Server();

bool SendMessage(const message::data& message);
bool ReceiveMessage(message::data& message);

private:
int _sock;
sockaddr_in _server_addr;
sockaddr_in _client_addr;
static const int _BUFFER_SIZE = 1024;
};

#endif // SERVER_H
Loading