Skip to content

Commit 2d3fbc2

Browse files
authored
Merge pull request #97 from codecrafters-io/andy/add-scala
CC-2126: Add Scala starter code
2 parents b5354e0 + abeef5b commit 2d3fbc2

File tree

33 files changed

+504
-0
lines changed

33 files changed

+504
-0
lines changed
Lines changed: 12 additions & 0 deletions
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+
# The option is needed for Java 17+ to allow native memory access operations.
12+
SBT_OPTS="--enable-native-access=ALL-UNNAMED --sun-misc-unsafe-memory-access=allow" sbt -error assembly
Lines changed: 11 additions & 0 deletions
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 java -jar "$(dirname "$0")/target/scala-3.8.2/kafka.jar" "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/scala/.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# macOS
2+
.DS_Store
3+
4+
# sbt specific
5+
dist/*
6+
debug/
7+
target/
8+
lib_managed/
9+
src_managed/
10+
project/boot/
11+
project/plugins/project/
12+
project/local-plugins.sbt
13+
.history
14+
.ensime
15+
.ensime_cache/
16+
.sbt-scripted/
17+
local.sbt
18+
19+
# Bloop
20+
.bsp
21+
22+
# VS Code
23+
.vscode/
24+
25+
# Metals
26+
.bloop/
27+
.metals/
28+
metals.sbt

compiled_starters/scala/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png)
2+
3+
This is a starting point for Scala 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
17+
`src/main/scala/codecrafters_kafka/App.scala`. Study and uncomment the relevant
18+
code, and push your changes to pass the first stage:
19+
20+
```sh
21+
git commit -am "pass 1st stage" # any msg
22+
git push origin master
23+
```
24+
25+
That's all!
26+
27+
# Stage 2 & beyond
28+
29+
Note: This section is for stages 2 and beyond.
30+
31+
1. Ensure you have `sbt (1.12.5)` installed locally
32+
1. Run `./your_program.sh` to run your Kafka broker, which is implemented in
33+
`src/main/scala/codecrafters_kafka/App.scala`.
34+
1. Commit your changes and run `git push origin master` to submit your solution
35+
to CodeCrafters. Test output will be streamed to your terminal.

compiled_starters/scala/build.sbt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ThisBuild / scalaVersion := "3.8.2"
2+
ThisBuild / version := "0.1.0-SNAPSHOT"
3+
ThisBuild / organization := "com.codecrafters"
4+
ThisBuild / organizationName := "CodeCrafters"
5+
6+
assembly / assemblyJarName := "kafka.jar"
7+
8+
lazy val root = (project in file("."))
9+
.settings(
10+
name := "codecrafter-kafka",
11+
// List your dependencies here
12+
libraryDependencies ++= Seq(
13+
)
14+
)
Lines changed: 11 additions & 0 deletions
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 Scala version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: scala-3.8
11+
buildpack: scala-3.8
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.12.5
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package codecrafters_kafka
2+
3+
import java.io.IOException
4+
import java.net.{ServerSocket, Socket}
5+
6+
object Main extends App {
7+
var clientSocket: Socket = null
8+
val port = 9092
9+
10+
try {
11+
// You can use print statements as follows for debugging, they'll be visible when running tests.
12+
System.err.println("Logs from your program will appear here!")
13+
14+
// TODO: Uncomment the code below to pass the first stage
15+
// val serverSocket = new ServerSocket(port)
16+
// // Since the tester restarts your program quite often, setting SO_REUSEADDR
17+
// // ensures that we don't run into 'Address already in use' errors
18+
// serverSocket.setReuseAddress(true)
19+
// // Wait for connection from client.
20+
// clientSocket = serverSocket.accept()
21+
} catch {
22+
case e: IOException => System.out.println("IOException: " + e.getMessage)
23+
} finally {
24+
try {
25+
if (clientSocket != null) {
26+
clientSocket.close()
27+
}
28+
} catch {
29+
case e: IOException => System.out.println("IOException: " + e.getMessage)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)