Skip to content

Commit

Permalink
Merge pull request #7 from urielsalis/java-support
Browse files Browse the repository at this point in the history
Add java support
  • Loading branch information
rohitpaulk authored Nov 27, 2023
2 parents 0b287b0 + 575329e commit 1562236
Show file tree
Hide file tree
Showing 24 changed files with 485 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiled_starters/java/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions compiled_starters/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java_dns.jar
target/*
target/
.idea/
37 changes: 37 additions & 0 deletions compiled_starters/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/dns-server.png)

This is a starting point for Java solutions to the
["Build Your Own DNS server" Challenge](https://app.codecrafters.io/courses/dns-server/overview).

In this challenge, you'll build a DNS server that's capable of parsing and
creating DNS packets, responding to DNS queries, handling various record types
and doing recursive resolve. Along the way we'll learn about the DNS protocol,
DNS packet format, root servers, authoritative servers, forwarding servers,
various record types (A, AAAA, CNAME, etc) 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 `your_server.sh` implementation is in
`src/main/java/Main.java`. Study and uncomment the relevant code, and push your
changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `java (21)` installed locally
1. Run `./your_server.sh` to run your program, which is implemented in
`src/main/java/Main.java`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions compiled_starters/java/codecrafters.yml
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 Java version used to run your code
# on Codecrafters.
#
# Available versions: java-21
language_pack: java-21
52 changes: 52 additions & 0 deletions compiled_starters/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.codecrafters</groupId>
<artifactId>build-your-own-dns</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>java_dns</finalName> <!-- Please do not change this final artifact name-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the main class of your program which will be executed-->
<mainClass>Main</mainClass>
</manifest>
</archive>
<outputDirectory>${dir}</outputDirectory>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
27 changes: 27 additions & 0 deletions compiled_starters/java/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Main {
public static void main(String[] args){
// You can use print statements as follows for debugging, they'll be visible when running tests.
System.out.println("Logs from your program will appear here!");

// Uncomment this block to pass the first stage
//
// while(true) {
// try(DatagramSocket serverSocket = new DatagramSocket(2053)) {
// final byte[] buf = new byte[512];
// final DatagramPacket packet = new DatagramPacket(buf, buf.length);
// serverSocket.receive(packet);
// System.out.println("Received data");
//
// final byte[] bufResponse = new byte[512];
// final DatagramPacket packetResponse = new DatagramPacket(bufResponse, bufResponse.length, packet.getSocketAddress());
// serverSocket.send(packetResponse);
// } catch (IOException e) {
// System.out.println("IOException: " + e.getMessage());
// }
// }
}
}
10 changes: 10 additions & 0 deletions compiled_starters/java/your_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
set -e
mvn -B --quiet package -Ddir=/tmp/codecrafters-dns-target
exec java -jar /tmp/codecrafters-dns-target/java_dns.jar "$@"
2 changes: 2 additions & 0 deletions course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ languages:
- slug: "go"
- slug: "python"
- slug: "rust"
- slug: "java"
release_status: "beta"

marketing:
# Shown in the catalog.
Expand Down
16 changes: 16 additions & 0 deletions dockerfiles/java-21.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM maven:3.9.5-eclipse-temurin-21-alpine

COPY pom.xml /app/pom.xml

WORKDIR /app

# Download the dependencies
RUN mvn -B package -Ddir=/tmp/codecrafters-dns-target

# Cache Dependencies
RUN mkdir -p /app-cached
RUN mv /app/target /app-cached # Is this needed?

# Pre-compile steps
RUN echo "cd \${CODECRAFTERS_SUBMISSION_DIR} && mvn -B package -Ddir=/tmp/codecrafters-dns-target && sed -i 's/^\(mvn .*\)/#\1/' ./your_server.sh" > /codecrafters-precompile.sh
RUN chmod +x /codecrafters-precompile.sh
1 change: 1 addition & 0 deletions solutions/java/01-init/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions solutions/java/01-init/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java_dns.jar
target/*
target/
.idea/
37 changes: 37 additions & 0 deletions solutions/java/01-init/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/dns-server.png)

This is a starting point for Java solutions to the
["Build Your Own DNS server" Challenge](https://app.codecrafters.io/courses/dns-server/overview).

In this challenge, you'll build a DNS server that's capable of parsing and
creating DNS packets, responding to DNS queries, handling various record types
and doing recursive resolve. Along the way we'll learn about the DNS protocol,
DNS packet format, root servers, authoritative servers, forwarding servers,
various record types (A, AAAA, CNAME, etc) 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 `your_server.sh` implementation is in
`src/main/java/Main.java`. Study and uncomment the relevant code, and push your
changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `java (21)` installed locally
1. Run `./your_server.sh` to run your program, which is implemented in
`src/main/java/Main.java`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions solutions/java/01-init/code/codecrafters.yml
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 Java version used to run your code
# on Codecrafters.
#
# Available versions: java-21
language_pack: java-21
52 changes: 52 additions & 0 deletions solutions/java/01-init/code/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.codecrafters</groupId>
<artifactId>build-your-own-dns</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>java_dns</finalName> <!-- Please do not change this final artifact name-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the main class of your program which will be executed-->
<mainClass>Main</mainClass>
</manifest>
</archive>
<outputDirectory>${dir}</outputDirectory>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
22 changes: 22 additions & 0 deletions solutions/java/01-init/code/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Main {
public static void main(String[] args){
while(true) {
try(DatagramSocket serverSocket = new DatagramSocket(2053)) {
final byte[] buf = new byte[512];
final DatagramPacket packet = new DatagramPacket(buf, buf.length);
serverSocket.receive(packet);
System.out.println("Received data");

final byte[] bufResponse = new byte[512];
final DatagramPacket packetResponse = new DatagramPacket(bufResponse, bufResponse.length, packet.getSocketAddress());
serverSocket.send(packetResponse);
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
}
10 changes: 10 additions & 0 deletions solutions/java/01-init/code/your_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
set -e
mvn -B --quiet package -Ddir=/tmp/codecrafters-dns-target
exec java -jar /tmp/codecrafters-dns-target/java_dns.jar "$@"
41 changes: 41 additions & 0 deletions solutions/java/01-init/diff/src/main/java/Main.java.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@@ -1,27 +1,22 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Main {
public static void main(String[] args){
- // You can use print statements as follows for debugging, they'll be visible when running tests.
- System.out.println("Logs from your program will appear here!");
+ while(true) {
+ try(DatagramSocket serverSocket = new DatagramSocket(2053)) {
+ final byte[] buf = new byte[512];
+ final DatagramPacket packet = new DatagramPacket(buf, buf.length);
+ serverSocket.receive(packet);
+ System.out.println("Received data");

- // Uncomment this block to pass the first stage
- //
- // while(true) {
- // try(DatagramSocket serverSocket = new DatagramSocket(2053)) {
- // final byte[] buf = new byte[512];
- // final DatagramPacket packet = new DatagramPacket(buf, buf.length);
- // serverSocket.receive(packet);
- // System.out.println("Received data");
- //
- // final byte[] bufResponse = new byte[512];
- // final DatagramPacket packetResponse = new DatagramPacket(bufResponse, bufResponse.length, packet.getSocketAddress());
- // serverSocket.send(packetResponse);
- // } catch (IOException e) {
- // System.out.println("IOException: " + e.getMessage());
- // }
- // }
+ final byte[] bufResponse = new byte[512];
+ final DatagramPacket packetResponse = new DatagramPacket(bufResponse, bufResponse.length, packet.getSocketAddress());
+ serverSocket.send(packetResponse);
+ } catch (IOException e) {
+ System.out.println("IOException: " + e.getMessage());
+ }
+ }
}
}
30 changes: 30 additions & 0 deletions solutions/java/01-init/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
The entry point for your dns implementation is in `src/main/java/Main.java`.

Study and uncomment the relevant code:

```java
// Uncomment this block to pass the first stage

while(true) {
try(DatagramSocket serverSocket = new DatagramSocket(2053)) {
final byte[] buf = new byte[512];
final DatagramPacket packet = new DatagramPacket(buf, buf.length);
serverSocket.receive(packet);
System.out.println("Received data");

final byte[] bufResponse = new byte[512];
final DatagramPacket packetResponse = new DatagramPacket(bufResponse, bufResponse.length, packet.getSocketAddress());
serverSocket.send(packetResponse);
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```
Loading

0 comments on commit 1562236

Please sign in to comment.