Skip to content

Commit cd93569

Browse files
authored
Merge pull request #20 from codecrafters-io/add-cpp-support
CC-1485: Add C++ support for Kafka
2 parents ed45e8b + 31b70bc commit cd93569

34 files changed

+748
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
12+
cmake --build ./build
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec $(dirname $0)/build/kafka "$@"

compiled_starters/cpp/.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/cpp/.gitignore

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf
53+
54+
build
55+
vcpkg_installed

compiled_starters/cpp/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(codecrafters-kafka)
4+
5+
set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard
6+
7+
file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp)
8+
9+
add_executable(kafka ${SOURCE_FILES})

compiled_starters/cpp/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png)
2+
3+
This is a starting point for C++ solutions to the
4+
["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka).
5+
6+
In this challenge, you'll build a toy Kafka clone that's capable of accepting
7+
and responding to APIVersions & Fetch API requests. You'll also learn about
8+
encoding and decoding messages using the Kafka wire protocol. You'll also learn
9+
about handling the network protocol, event loops, TCP sockets and more.
10+
11+
**Note**: If you're viewing this repo on GitHub, head over to
12+
[codecrafters.io](https://codecrafters.io) to try the challenge.
13+
14+
# Passing the first stage
15+
16+
The entry point for your Kafka implementation is in `src/main.cpp`. Study and
17+
uncomment the relevant code, and push your changes to pass the first stage:
18+
19+
```sh
20+
git commit -am "pass 1st stage" # any msg
21+
git push origin master
22+
```
23+
24+
That's all!
25+
26+
# Stage 2 & beyond
27+
28+
Note: This section is for stages 2 and beyond.
29+
30+
1. Ensure you have `cmake` installed locally
31+
1. Run `./your_program.sh` to run your Kafka broker, which is implemented in
32+
`src/main.cpp`.
33+
1. Commit your changes and run `git push origin master` to submit your solution
34+
to CodeCrafters. Test output will be streamed to your terminal.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the C++ version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: cpp-23
11+
language_pack: cpp-23

compiled_starters/cpp/src/main.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <cstdlib>
2+
#include <cstring>
3+
#include <iostream>
4+
#include <netdb.h>
5+
#include <string>
6+
#include <unistd.h>
7+
#include <arpa/inet.h>
8+
#include <sys/socket.h>
9+
#include <sys/types.h>
10+
11+
int main(int argc, char* argv[]) {
12+
// Disable output buffering
13+
std::cout << std::unitbuf;
14+
std::cerr << std::unitbuf;
15+
16+
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
17+
if (server_fd < 0) {
18+
std::cerr << "Failed to create server socket: " << std::endl;
19+
return 1;
20+
}
21+
22+
// Since the tester restarts your program quite often, setting SO_REUSEADDR
23+
// ensures that we don't run into 'Address already in use' errors
24+
int reuse = 1;
25+
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
26+
close(server_fd);
27+
std::cerr << "setsockopt failed: " << std::endl;
28+
return 1;
29+
}
30+
31+
struct sockaddr_in server_addr{};
32+
server_addr.sin_family = AF_INET;
33+
server_addr.sin_addr.s_addr = INADDR_ANY;
34+
server_addr.sin_port = htons(9092);
35+
36+
if (bind(server_fd, reinterpret_cast<struct sockaddr*>(&server_addr), sizeof(server_addr)) != 0) {
37+
close(server_fd);
38+
std::cerr << "Failed to bind to port 9092" << std::endl;
39+
return 1;
40+
}
41+
42+
int connection_backlog = 5;
43+
if (listen(server_fd, connection_backlog) != 0) {
44+
close(server_fd);
45+
std::cerr << "listen failed" << std::endl;
46+
return 1;
47+
}
48+
49+
std::cout << "Waiting for a client to connect...\n";
50+
51+
struct sockaddr_in client_addr{};
52+
socklen_t client_addr_len = sizeof(client_addr);
53+
54+
// You can use print statements as follows for debugging, they'll be visible when running tests.
55+
std::cerr << "Logs from your program will appear here!\n";
56+
57+
// Uncomment this block to pass the first stage
58+
//
59+
// int client_fd = accept(server_fd, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
60+
// std::cout << "Client connected\n";
61+
// close(client_fd);
62+
63+
close(server_fd);
64+
return 0;
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"default-registry": {
3+
"kind": "git",
4+
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
5+
"repository": "https://github.com/microsoft/vcpkg"
6+
},
7+
"registries": [
8+
{
9+
"kind": "artifact",
10+
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
11+
"name": "microsoft"
12+
}
13+
]
14+
}

compiled_starters/cpp/vcpkg.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dependencies": []
3+
}

compiled_starters/cpp/your_program.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
#
3+
# Use this script to run your program LOCALLY.
4+
#
5+
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit early if any commands fail
10+
11+
# Copied from .codecrafters/compile.sh
12+
#
13+
# - Edit this to change how your program compiles locally
14+
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
15+
(
16+
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
17+
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
18+
cmake --build ./build
19+
)
20+
21+
# Copied from .codecrafters/run.sh
22+
#
23+
# - Edit this to change how your program runs locally
24+
# - Edit .codecrafters/run.sh to change how your program runs remotely
25+
exec $(dirname $0)/build/kafka "$@"

course-definition.yml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ short_description_md: |-
1515
completion_percentage: 15
1616

1717
languages:
18+
- slug: "cpp"
1819
- slug: "gleam"
1920
- slug: "go"
2021
- slug: "java"

dockerfiles/cpp-23.Dockerfile

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# syntax=docker/dockerfile:1.7-labs
2+
FROM gcc:14.2.0-bookworm
3+
4+
# Ensures the container is re-built if dependency files change
5+
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="vcpkg.json,vcpkg-configuration.json"
6+
7+
RUN apt-get update && \
8+
apt-get install --no-install-recommends -y zip=3.* && \
9+
apt-get install --no-install-recommends -y g++=4:* && \
10+
apt-get install --no-install-recommends -y build-essential=12.* && \
11+
apt-get clean && \
12+
rm -rf /var/lib/apt/lists/*
13+
14+
# cmake is required by vcpkg
15+
RUN wget --progress=dot:giga https://github.com/Kitware/CMake/releases/download/v3.30.5/cmake-3.30.5-Linux-x86_64.tar.gz && \
16+
tar -xzvf cmake-3.30.5-Linux-x86_64.tar.gz && \
17+
mv cmake-3.30.5-linux-x86_64/ /cmake
18+
19+
ENV CMAKE_BIN="/cmake/bin"
20+
ENV PATH="${CMAKE_BIN}:$PATH"
21+
22+
RUN git clone https://github.com/microsoft/vcpkg.git && \
23+
./vcpkg/bootstrap-vcpkg.sh -disableMetrics
24+
25+
ENV VCPKG_ROOT="/vcpkg"
26+
ENV PATH="${VCPKG_ROOT}:$PATH"
27+
28+
WORKDIR /app
29+
30+
# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses
31+
COPY --exclude=.git --exclude=README.md . /app
32+
33+
RUN vcpkg install --no-print-usage
34+
RUN sed -i '1s/^/set(VCPKG_INSTALL_OPTIONS --no-print-usage)\n/' ${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
35+
36+
RUN mkdir -p /app-cached
37+
RUN if [ -d "/app/build" ]; then mv /app/build /app-cached; fi
38+
RUN if [ -d "/app/vcpkg_installed" ]; then mv /app/vcpkg_installed /app-cached/build; fi
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
12+
cmake --build ./build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec $(dirname $0)/build/kafka "$@"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

solutions/cpp/01-vi6/code/.gitignore

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf
53+
54+
build
55+
vcpkg_installed
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(codecrafters-kafka)
4+
5+
set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard
6+
7+
file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp)
8+
9+
add_executable(kafka ${SOURCE_FILES})

0 commit comments

Comments
 (0)