Skip to content
Snippets Groups Projects
Commit 1fcd95de authored by Lukas Güldenstein's avatar Lukas Güldenstein
Browse files

Finishing touches

parent 8976d490
No related branches found
No related tags found
No related merge requests found
......@@ -41,7 +41,7 @@ DOXYFILE_ENCODING = UTF-8
# title of most generated pages and in a few other places.
# The default value is: My Project.
PROJECT_NAME = "My Project"
PROJECT_NAME = "Wordle DE"
# The PROJECT_NUMBER tag can be used to enter a project or revision number. This
# could be handy for archiving the generated documentation or if some version
......@@ -171,7 +171,7 @@ INLINE_INHERITED_MEMB = NO
# shortest path that makes the file name unique will be used
# The default value is: YES.
FULL_PATH_NAMES = YES
FULL_PATH_NAMES = NO
# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path.
# Stripping is only done if one of the specified strings matches the left-hand
......
......@@ -6,6 +6,10 @@
#include <cstring>
#include <unistd.h>
/**
* @brief Construct a new Client object with defailt port and address
*
*/
Client::Client() : sock(), serverAddr() {
sock = socket(AF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
......@@ -13,6 +17,10 @@ Client::Client() : sock(), serverAddr() {
inet_aton(DEFAULT_IP, &serverAddr.sin_addr);
}
/**
* @brief Construct a new Client object with custom port and address
*
*/
Client::Client(const std::string &addr, const int &port) {
sock = socket(AF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
......@@ -20,6 +28,11 @@ Client::Client(const std::string &addr, const int &port) {
inet_aton(addr.c_str(), &serverAddr.sin_addr);
}
/**
* @brief Connects to server
*
* @return int
*/
int Client::connectToServer() {
int rc = connect(sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
if (rc < 0) {
......@@ -28,6 +41,11 @@ int Client::connectToServer() {
return rc;
}
/**
* @brief Disconnects from server
*
* @return int
*/
int Client::disconnectFromServer() {
int rc = close(sock);
if (rc < 0) {
......@@ -36,6 +54,12 @@ int Client::disconnectFromServer() {
return rc;
}
/**
* @brief Sends protobuf message to server
*
* @param msg
* @return uint16_t bytes send
*/
uint16_t Client::sendMessage(const proto::Message &msg) {
uint8_t buf[msg.ByteSizeLong()];
msg.SerializeToArray(buf, msg.ByteSizeLong());
......@@ -43,6 +67,11 @@ uint16_t Client::sendMessage(const proto::Message &msg) {
return bytes;
}
/**
* @brief Receives message from server (blocking)
*
* @return int < 0 signals error
*/
int Client::receiveMessage() {
int nbytesrecv = recv(sock, inputBuffer, INPUT_BUFFER_SIZE, 0);
if (nbytesrecv <= 0) {
......@@ -56,4 +85,9 @@ int Client::receiveMessage() {
return 0;
}
/**
* @brief Sets callback for receiving data
*
* @param fn
*/
void Client::onReceive(receiveCallbackType fn) { onReceiveCB = fn; }
......@@ -25,6 +25,10 @@ enum State {
CHOOSE_MODE
};
/**
* @brief Handles the state of the App
*
*/
struct AppState {
public:
void set(const State &s) {
......@@ -221,7 +225,7 @@ int main(int argc, char *argv[]) {
}
}).detach();
// ui thread
// ui (main) thread
std::cout << "Willkommen zu Wordle auf Deutsch!" << std::endl;
while (true) {
switch (state.get()) {
......
......@@ -14,7 +14,6 @@ const uint16_t USERNAME_MAX_LENGTH = 100;
const uint16_t USER_INPUT_TIMEOUT = 300;
const int DEFAULT_PORT = 40000;
const char DEFAULT_IP[] = "127.0.0.1";
const char DEFAULT_FILENAME[] =
"/Users/lukas/bht/03_NEP/wordle-de/deutsch-filtered.txt";
const char DEFAULT_FILENAME[] = "./word-list.txt";
#endif // _DEFINITIONS_H
......@@ -11,6 +11,11 @@
/* ---------------------------------- Game ---------------------------------- */
/**
* @brief Construct a new Game object from a protobuf object
*
* @param protoGame
*/
Game::Game(const proto::Game &protoGame) {
unsigned i = 0;
for (const auto &g : protoGame.guesses()) {
......@@ -70,6 +75,11 @@ proto::Result Game::guess(const Engine *eng, const std::string &guessedWord) {
return matchStatus;
}
/**
* @brief Converts Game to protobuf Game
*
* @return proto::Game
*/
proto::Game Game::toProto() const {
auto game = proto::Game();
for (const auto &g : guesses) {
......@@ -87,7 +97,7 @@ proto::Game Game::toProto() const {
/* --------------------------------- Engine --------------------------------- */
/**
* @brief Create the instance of the Engine
* @brief Create the instance of the Engine (singleton pattern)
*
*/
void Engine::createInstance() {
......@@ -99,7 +109,7 @@ void Engine::createInstance() {
/**
* @brief Create the instance of the Engine
*
* @param fileName Path to newline seperayed word list
* @param fileName Path to newline seperated word list
*/
void Engine::createInstance(const std::string &fileName) {
if (instance == nullptr) {
......@@ -157,6 +167,13 @@ void Engine::chooseNewWord() {
}
}
/**
* @brief Checks if word is in wordlist
*
* @param word
* @return true
* @return false
*/
bool Engine::inList(const std::string &word) const {
std::ifstream file(wordFile);
if (file.is_open()) {
......
......@@ -5,23 +5,51 @@
#include <iostream>
#include <sys/socket.h>
/**
* @brief Creates a Server instace (singleton)
*
*/
void Server::createInstance() {
if (instance == nullptr) {
instance = new Server(DEFAULT_PORT);
}
}
/**
* @brief Creates a Server instance with custom port
*
* @param port
*/
void Server::createInstance(const int &port) {
if (instance == nullptr) {
instance = new Server(port);
}
}
/**
* @brief Construct a new Server object with default port
*
*/
Server::Server() { setup(DEFAULT_PORT); }
/**
* @brief Construct a new Server object with custom port
*
* @param port
*/
Server::Server(const int &port) { setup(port); }
/**
* @brief Destroy the Server object and close the master socket
*
*/
Server::~Server() { close(mastersocket_fd); }
/**
* @brief Setup server with socket and port
*
* @param port
*/
void Server::setup(int port) {
mastersocket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (mastersocket_fd < 0) {
......@@ -38,6 +66,10 @@ void Server::setup(int port) {
bzero(input_buffer, INPUT_BUFFER_SIZE); // no random data in input buffer
}
/**
* @brief Initializes the master socket
*
*/
void Server::initializeSocket() {
int opt_value = 1;
int rc = setsockopt(mastersocket_fd, SOL_SOCKET, SO_REUSEADDR,
......@@ -48,6 +80,10 @@ void Server::initializeSocket() {
}
}
/**
* @brief Binds to socket
*
*/
void Server::bindSocket() {
int rc = bind(mastersocket_fd, (struct sockaddr *)&server_addr,
sizeof(server_addr));
......@@ -58,6 +94,10 @@ void Server::bindSocket() {
maxfd = mastersocket_fd;
}
/**
* @brief Starts listening on mastersocket
*
*/
void Server::startListen() {
int rc = listen(mastersocket_fd, 3);
if (rc < 0) {
......@@ -65,8 +105,18 @@ void Server::startListen() {
}
}
/**
* @brief Stops the server
*
*/
void Server::stop() { close(mastersocket_fd); }
/**
* @brief Handles new connection
*
* @details Calls the given callback function if client connects
*
*/
void Server::handleNewConnection() {
socklen_t addrLen = sizeof(client_addr);
tempsocket_fd =
......@@ -82,6 +132,13 @@ void Server::handleNewConnection() {
newConnectionCallback(tempsocket_fd);
}
/**
* @brief Receives from existing socket
*
* @details Calls the given callback function if message has been received
*
* @param fd
*/
void Server::recvInputFromExisting(int fd) {
int nbytesrecv = recv(fd, input_buffer, INPUT_BUFFER_SIZE, 0);
......@@ -106,6 +163,13 @@ void Server::recvInputFromExisting(int fd) {
bzero(&input_buffer, INPUT_BUFFER_SIZE);
}
/**
* @brief Starts the server
*
* @details Loops over set of sockets and handles new connections or available
* data on existing connections
*
*/
void Server::run() {
tmpfds = masterfds;
int sel = select(maxfd + 1, &tmpfds, NULL, NULL, NULL);
......@@ -113,7 +177,6 @@ void Server::run() {
perror("select() failed");
stop();
}
// loop over the set of file descriptors and see if we can act
for (int i = 0; i <= maxfd; i++) {
if (FD_ISSET(i, &tmpfds)) {
......@@ -129,22 +192,50 @@ void Server::run() {
}
}
/**
* @brief Wrapper function for initialization
*
*/
void Server::init() {
initializeSocket();
bindSocket();
startListen();
}
/**
* @brief Sets the callback for reveiving data
*
* @param cb
*/
void Server::onInput(receiveCallbackType cb) { receiveCallback = cb; }
/**
* @brief Sets the callback for handling new connections
*
* @param cb
*/
void Server::onConnect(newConnectionCallbackType cb) {
newConnectionCallback = cb;
}
/**
* @brief Sets the callback for handling disconnects
*
* @param cb
*/
void Server::onDisconnect(disconnectCallbackType cb) {
disconnectCallback = cb;
}
/**
* @brief Sends protobuf message on socket
*
* @details Serializes protobuf message as byte data
*
* @param source_fd
* @param msg
* @return uint16_t
*/
uint16_t Server::sendMessage(int source_fd, const proto::Message &msg) {
uint8_t buf[msg.ByteSizeLong()];
msg.SerializeToArray(buf, msg.ByteSizeLong());
......
......@@ -75,7 +75,7 @@ std::string getGuessFromInput() {
enum InterruptReason { NONE, INTERRUPT_NEW_WORD, INTERRUPT_DISCONNECTED };
/**
* @brief Exception to signal interrupt has happened
* @brief Exception to signal that an interrupt has happened
*
*/
class InterruptException : public std::exception {
......@@ -99,6 +99,8 @@ public:
/**
* @brief UI Class for all user inputs and outputs
*
* @details Only hold interrupt status as data
*
*/
class UI {
public:
......
......@@ -15,8 +15,7 @@ std::string string_format(const std::string &format, Args... args) {
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[size]);
std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(),
buf.get() + size - 1); // We don't want the '\0' inside
return std::string(buf.get(), buf.get() + size - 1);
}
// from
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment