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
2 changes: 2 additions & 0 deletions .idea/cpp-chat-client.iml

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

4 changes: 4 additions & 0 deletions .idea/encodings.xml

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

7 changes: 7 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

542 changes: 542 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(cpp-chat-client)
set(CMAKE_CXX_STANDARD 17)

set(TARGET_NAME cpp-chat-client)
add_executable(${TARGET_NAME} main.cpp vusocket.h Client.cpp Client.h Application.cpp Application.h vusocket.cpp CircularLineBuffer.cpp CircularLineBuffer.h)
add_executable(${TARGET_NAME} main.cpp vusocket.h Client.cpp Client.h Application.cpp Application.h vusocket.cpp CircularLineBuffer.cpp CircularLineBuffer.h Server.cpp Server.h)
if(WIN32)
target_link_libraries(${TARGET_NAME} Ws2_32)
else()
Expand Down
56 changes: 56 additions & 0 deletions CircularLineBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Created by Gebruiker on 22/02/2019.
//

#include "CircularLineBuffer.h"
bool CircularLineBuffer::_writeChars(const char *chars, size_t nchars) {
if (nchars > freeSpace() || nchars == 0) {
return false;
}

int begin = nextFreeIndex();
for (int i = 0; i < nchars; i++) {
buffer[(begin+i)%bufferSize] = chars[i];
}

count += nchars;
return true;
}

std::string CircularLineBuffer::_readLine() {
if (hasLine()) {
std::string result;
int end = findNewline();

result.resize(abs(end-start)); // abs ?

for (int i = 0; i < abs(end-start); i++) {
result[i] = buffer[(start+i)%bufferSize];
// std::cout << buffer[(start+i)%bufferSize] << std::endl;
}


count -= end+1-start;
start = end+1;
return result;
} else {
return "";
}
}

int CircularLineBuffer::findNewline() {
int i = 0;
while (buffer[(i+start)%bufferSize] != '\n') {
i++;
if (i >= count) {
return -1;
}
}

return start+i;
}

bool CircularLineBuffer::hasLine() {
return findNewline() >= 0;
}

135 changes: 122 additions & 13 deletions CircularLineBuffer.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,151 @@
//
// Created by Jesse on 2019-01-10.
//

#ifndef CPP_CHAT_CLIENT_CIRCULARBUFFER_H
#define CPP_CHAT_CLIENT_CIRCULARBUFFER_H


#include <cstdlib>
#include <string>
#include <mutex>

/**
* Assignment 3
*
* See the lab manual for the assignment description.
* See the lab manual for more details.
*/
class CircularLineBuffer {
private:
static const int bufferSize = 100; // 4096
/**
* You may increase the size of the buffer, if you want. :)
* Reducing the size of the buffer allows for easier debugging.
*/
static const int bufferSize = 4096;
char buffer[bufferSize] = {0};

/**
* You may ignore this variable.
*/
std::mutex mtx;
/**
* Use 'start' to keep track of the start of the buffer.
* Use 'count' to keep track of the current number of characters in the buffer.
*/
int start = 0, count = 0;

/**
* This method writes the given number of characters into the buffer,
* starting at the next free location.
* If there is not enough space left in the buffer, it writes nothing into the buffer and returns false.
*
* @param chars Pointer to the characters to write into the buffer.
* @param nchars The number of characters to write.
* @return False if there was not enough space in the buffer. True otherwise.
*/
bool _writeChars(const char *chars, size_t nchars);

/**
* This method reads a line from the buffer,
* starting from location 'start'.
* If there is no complete line (no '\n') in the buffer, this method returns an empty string.
*
* @return The next string from the buffer. Returns an empty string if the buffer is empty.
*/
std::string _readLine();

public:
int freeSpace();
/**
* @return The amount of free space in the buffer in number of characters.
*/
int freeSpace() {
return bufferSize - count;
}

bool isFull();
/**
*
* @return true if and only if (iff) the buffer is full.
*/
bool isFull() {
return freeSpace() <= 0;
}

bool isEmpty();
/**
*
* @return true if and only if (iff) the buffer is empty.
*/
bool isEmpty() {
return freeSpace() == bufferSize;
}

int nextFreeIndex();
/**
* This method should return the next free spot in the buffer as seen from the current value of 'start'.
*
* For example, consider the following buffer:
* S
* [H,E,L,L,O,\n,-,-]
* Here 'S' points to the start of the buffer contents, and '-' indicates an empty space in the buffer.
* For this buffer, nextFreeIndex should return 6,
* because it is the first free position in the buffer.
*
* If the buffer is full, the behavior is undefined.
*
* @return The index of the first free position in the buffer.
*/
int nextFreeIndex() {
return start+count;
}

/**
* The position of the next newline character (\n), as seen from the current value of 'start'.
*
* For example, consider the following buffer:
* S
* [\n,H,I,\n,-,-]
* Here 'S' points to the start of the buffer contents, and '-' indicates an empty space in the buffer.
* For this buffer, findNewline should return 3, because,
* when starting from S, index 3 contains the first '\n' character.
*
* @return The position of the next newline character (\n), as seen from the current value of 'start'.
*/
int findNewline();

/**
* Checks if there is a complete line in the buffer.
* You can make your life easier by implementing this method using the method above.
*
* @return true if and only if (iff) there is at least one complete line in the buffer.
*/
bool hasLine();

bool writeChars(const char *chars, size_t nchars);
/**
* This method writes the given number of characters into the buffer.
* It forwards its input to _writeChars, which you will implement.
*
* You don't have to read, understand, or edit the calls to 'mtx'. It prevents concurrent modification errors,
* which you will discuss in another course.
*
* @param chars Pointer to the characters to write into the buffer.
* @param nchars The number of characters to write.
* @return False if there was not enough space in the buffer. True otherwise.
*/
inline bool writeChars(const char *chars, size_t nchars) {
mtx.lock();
auto res = _writeChars(chars, nchars);
mtx.unlock();
return res;
}

std::string readLine();
/**
* This method reads a line from the buffer.
* It forwards its input to _readLine, which you will implement.
*
* You don't have to read, understand, or edit the calls to 'mtx'. It prevents concurrent modification errors,
* which you will discuss in another course.
*
* @return The next string from the buffer. Returns an empty string if the buffer is empty.
*/
inline std::string readLine() {
mtx.lock();
auto res = _readLine();
mtx.unlock();
return res;
}
};


Expand Down
Loading