generated from codecrafters-io/course-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from codecrafters-io/add-cpp-support
CC-1485: Add C++ support for Kafka
- Loading branch information
Showing
34 changed files
with
748 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to compile your program on CodeCrafters | ||
# | ||
# This runs before .codecrafters/run.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake | ||
cmake --build ./build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to run your program on CodeCrafters | ||
# | ||
# This runs after .codecrafters/compile.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
exec $(dirname $0)/build/kafka "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Prerequisites | ||
*.d | ||
|
||
# Object files | ||
*.o | ||
*.ko | ||
*.obj | ||
*.elf | ||
|
||
# Linker output | ||
*.ilk | ||
*.map | ||
*.exp | ||
|
||
# Precompiled Headers | ||
*.gch | ||
*.pch | ||
|
||
# Libraries | ||
*.lib | ||
*.a | ||
*.la | ||
*.lo | ||
|
||
# Shared objects (inc. Windows DLLs) | ||
*.dll | ||
*.so | ||
*.so.* | ||
*.dylib | ||
|
||
# Executables | ||
*.exe | ||
*.out | ||
*.app | ||
*.i*86 | ||
*.x86_64 | ||
*.hex | ||
|
||
# Debug files | ||
*.dSYM/ | ||
*.su | ||
*.idb | ||
*.pdb | ||
|
||
# Kernel Module Compile Results | ||
*.mod* | ||
*.cmd | ||
.tmp_versions/ | ||
modules.order | ||
Module.symvers | ||
Mkfile.old | ||
dkms.conf | ||
|
||
build | ||
vcpkg_installed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cmake_minimum_required(VERSION 3.13) | ||
|
||
project(codecrafters-kafka) | ||
|
||
set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard | ||
|
||
file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp) | ||
|
||
add_executable(kafka ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png) | ||
|
||
This is a starting point for C++ solutions to the | ||
["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka). | ||
|
||
In this challenge, you'll build a toy Kafka clone that's capable of accepting | ||
and responding to APIVersions & Fetch API requests. You'll also learn about | ||
encoding and decoding messages using the Kafka wire protocol. You'll also learn | ||
about handling the network protocol, event loops, TCP sockets and more. | ||
|
||
**Note**: If you're viewing this repo on GitHub, head over to | ||
[codecrafters.io](https://codecrafters.io) to try the challenge. | ||
|
||
# Passing the first stage | ||
|
||
The entry point for your Kafka implementation is in `src/main.cpp`. Study and | ||
uncomment the relevant code, and push your changes to pass the first stage: | ||
|
||
```sh | ||
git commit -am "pass 1st stage" # any msg | ||
git push origin master | ||
``` | ||
|
||
That's all! | ||
|
||
# Stage 2 & beyond | ||
|
||
Note: This section is for stages 2 and beyond. | ||
|
||
1. Ensure you have `cmake` installed locally | ||
1. Run `./your_program.sh` to run your Kafka broker, which is implemented in | ||
`src/main.cpp`. | ||
1. Commit your changes and run `git push origin master` to submit your solution | ||
to CodeCrafters. Test output will be streamed to your terminal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Set this to true if you want debug logs. | ||
# | ||
# These can be VERY verbose, so we suggest turning them off | ||
# unless you really need them. | ||
debug: false | ||
|
||
# Use this to change the C++ version used to run your code | ||
# on Codecrafters. | ||
# | ||
# Available versions: cpp-23 | ||
language_pack: cpp-23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <netdb.h> | ||
#include <string> | ||
#include <unistd.h> | ||
#include <arpa/inet.h> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
|
||
int main(int argc, char* argv[]) { | ||
// Disable output buffering | ||
std::cout << std::unitbuf; | ||
std::cerr << std::unitbuf; | ||
|
||
int server_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (server_fd < 0) { | ||
std::cerr << "Failed to create server socket: " << std::endl; | ||
return 1; | ||
} | ||
|
||
// Since the tester restarts your program quite often, setting SO_REUSEADDR | ||
// ensures that we don't run into 'Address already in use' errors | ||
int reuse = 1; | ||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) { | ||
close(server_fd); | ||
std::cerr << "setsockopt failed: " << std::endl; | ||
return 1; | ||
} | ||
|
||
struct sockaddr_in server_addr{}; | ||
server_addr.sin_family = AF_INET; | ||
server_addr.sin_addr.s_addr = INADDR_ANY; | ||
server_addr.sin_port = htons(9092); | ||
|
||
if (bind(server_fd, reinterpret_cast<struct sockaddr*>(&server_addr), sizeof(server_addr)) != 0) { | ||
close(server_fd); | ||
std::cerr << "Failed to bind to port 9092" << std::endl; | ||
return 1; | ||
} | ||
|
||
int connection_backlog = 5; | ||
if (listen(server_fd, connection_backlog) != 0) { | ||
close(server_fd); | ||
std::cerr << "listen failed" << std::endl; | ||
return 1; | ||
} | ||
|
||
std::cout << "Waiting for a client to connect...\n"; | ||
|
||
struct sockaddr_in client_addr{}; | ||
socklen_t client_addr_len = sizeof(client_addr); | ||
|
||
// You can use print statements as follows for debugging, they'll be visible when running tests. | ||
std::cerr << "Logs from your program will appear here!\n"; | ||
|
||
// Uncomment this block to pass the first stage | ||
// | ||
// int client_fd = accept(server_fd, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len); | ||
// std::cout << "Client connected\n"; | ||
// close(client_fd); | ||
|
||
close(server_fd); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"default-registry": { | ||
"kind": "git", | ||
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d", | ||
"repository": "https://github.com/microsoft/vcpkg" | ||
}, | ||
"registries": [ | ||
{ | ||
"kind": "artifact", | ||
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip", | ||
"name": "microsoft" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"dependencies": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/sh | ||
# | ||
# Use this script to run your program LOCALLY. | ||
# | ||
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit early if any commands fail | ||
|
||
# Copied from .codecrafters/compile.sh | ||
# | ||
# - Edit this to change how your program compiles locally | ||
# - Edit .codecrafters/compile.sh to change how your program compiles remotely | ||
( | ||
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory | ||
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake | ||
cmake --build ./build | ||
) | ||
|
||
# Copied from .codecrafters/run.sh | ||
# | ||
# - Edit this to change how your program runs locally | ||
# - Edit .codecrafters/run.sh to change how your program runs remotely | ||
exec $(dirname $0)/build/kafka "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# syntax=docker/dockerfile:1.7-labs | ||
FROM gcc:14.2.0-bookworm | ||
|
||
# Ensures the container is re-built if dependency files change | ||
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="vcpkg.json,vcpkg-configuration.json" | ||
|
||
RUN apt-get update && \ | ||
apt-get install --no-install-recommends -y zip=3.* && \ | ||
apt-get install --no-install-recommends -y g++=4:* && \ | ||
apt-get install --no-install-recommends -y build-essential=12.* && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
# cmake is required by vcpkg | ||
RUN wget --progress=dot:giga https://github.com/Kitware/CMake/releases/download/v3.30.5/cmake-3.30.5-Linux-x86_64.tar.gz && \ | ||
tar -xzvf cmake-3.30.5-Linux-x86_64.tar.gz && \ | ||
mv cmake-3.30.5-linux-x86_64/ /cmake | ||
|
||
ENV CMAKE_BIN="/cmake/bin" | ||
ENV PATH="${CMAKE_BIN}:$PATH" | ||
|
||
RUN git clone https://github.com/microsoft/vcpkg.git && \ | ||
./vcpkg/bootstrap-vcpkg.sh -disableMetrics | ||
|
||
ENV VCPKG_ROOT="/vcpkg" | ||
ENV PATH="${VCPKG_ROOT}:$PATH" | ||
|
||
WORKDIR /app | ||
|
||
# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses | ||
COPY --exclude=.git --exclude=README.md . /app | ||
|
||
RUN vcpkg install --no-print-usage | ||
RUN sed -i '1s/^/set(VCPKG_INSTALL_OPTIONS --no-print-usage)\n/' ${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake | ||
|
||
RUN mkdir -p /app-cached | ||
RUN if [ -d "/app/build" ]; then mv /app/build /app-cached; fi | ||
RUN if [ -d "/app/vcpkg_installed" ]; then mv /app/vcpkg_installed /app-cached/build; fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to compile your program on CodeCrafters | ||
# | ||
# This runs before .codecrafters/run.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake | ||
cmake --build ./build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to run your program on CodeCrafters | ||
# | ||
# This runs after .codecrafters/compile.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
exec $(dirname $0)/build/kafka "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Prerequisites | ||
*.d | ||
|
||
# Object files | ||
*.o | ||
*.ko | ||
*.obj | ||
*.elf | ||
|
||
# Linker output | ||
*.ilk | ||
*.map | ||
*.exp | ||
|
||
# Precompiled Headers | ||
*.gch | ||
*.pch | ||
|
||
# Libraries | ||
*.lib | ||
*.a | ||
*.la | ||
*.lo | ||
|
||
# Shared objects (inc. Windows DLLs) | ||
*.dll | ||
*.so | ||
*.so.* | ||
*.dylib | ||
|
||
# Executables | ||
*.exe | ||
*.out | ||
*.app | ||
*.i*86 | ||
*.x86_64 | ||
*.hex | ||
|
||
# Debug files | ||
*.dSYM/ | ||
*.su | ||
*.idb | ||
*.pdb | ||
|
||
# Kernel Module Compile Results | ||
*.mod* | ||
*.cmd | ||
.tmp_versions/ | ||
modules.order | ||
Module.symvers | ||
Mkfile.old | ||
dkms.conf | ||
|
||
build | ||
vcpkg_installed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cmake_minimum_required(VERSION 3.13) | ||
|
||
project(codecrafters-kafka) | ||
|
||
set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard | ||
|
||
file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp) | ||
|
||
add_executable(kafka ${SOURCE_FILES}) |
Oops, something went wrong.