I honestly just don’t understand what I’m supposed to be doing for this one. The documentation provided is confusing and requirements aren’t particularly descriptive. I get an error when I push my code. This is what my code looks like:
package main
import (
"encoding/binary"
"fmt"
"net"
"os"
)
func main() {
// First create a server that can connect on port 9092
fmt.Println("Creating listener . . . ")
l, err := net.Listen("tcp", "0.0.0.0:9092")
if err != nil {
fmt.Println("Failed to bind to port 9092")
os.Exit(1)
}
defer l.Close()
fmt.Println("Listening on port 9092 . . .")
// Accept the incoming connection
for {
fmt.Println("Accepting an oncoming connection . . .")
netConnection, err := l.Accept()
if err != nil {
fmt.Println("Error accepting connection: ", err.Error())
continue
}
go handleConnection(netConnection)
}
}
// This function takes in a net.Conn struct and obtains the header value from it. Following this, it will return a response
func handleConnection(conn net.Conn) {
// 3 parts to our response
var message_size int32
var correlation_id int32 // This is part of the response header
var request_api_version int16
var request_api_key int16
/*
Steps:
1.) The client generates a correlation_id
2.) The client sends a request that includes the correlation_id
3.) The server sends a response that includes the same correlation_id
4.) The client receives the response and matches the received correlation_id to the original request
*/
// Getting the correlation ID:
//Since we know the message size is 4 bytes long (i.e. 32 bits . . .) and the
// correlationID is part of the header, where it is the 32nd through 64th bits, then we know that bits 64-86 are the necessary bits
buffer := make([]byte, 12) // We need to take in 12 bytes
_, err := conn.Read(buffer)
if err != nil {
fmt.Println("Failed to read message: ", err)
os.Exit(1)
}
// message_size is first 4 bytes
message_size = int32(binary.BigEndian.Uint32(buffer[0:4])) // A number value representing the number of bits/bytes including the 32 bits of the message_size portion!
// Now we need to parse so that of the 12 bytes we have, we take the 8th through 11th bytes
correlation_id = int32(binary.BigEndian.Uint32(buffer[8:12])) // 4 bytes
request_api_version = int16(binary.BigEndian.Uint16(buffer[6:8])) // 2 bytes
request_api_key = int16(binary.BigEndian.Uint16(buffer[4:6])) // 2 bytes
//Now we want to send the binary values
binary.Write(conn, binary.BigEndian, message_size) // 4 bytes in length. The value we write may be subject to change
binary.Write(conn, binary.BigEndian, correlation_id) // 4 bytes
api_error_code := valid_version(request_api_version)
binary.Write(conn, binary.BigEndian, api_error_code) // 2 bytes
binary.Write(conn, binary.BigEndian, request_api_key) // 2 bytes
binary.Write(conn, binary.BigEndian, int16(3)) //min version - 2 bytes
binary.Write(conn, binary.BigEndian, int16(4)) //max version - 2 bytes
binary.Write(conn, binary.BigEndian, int32(0)) // throttle time - 4 bytes
}
// Check that we're dealing with API version 4 or above!
func valid_version(api_version int16) int16 {
if api_version > 4 {
return 35
}
return 0
}
This is the error I get:
remote: ⏳ Turbo test runners busy. You are in queue.
remote:
remote: Upgrade to skip the wait: https://codecrafters.io/turbo
remote:
remote: Running tests on your code. Logs should appear shortly...
remote:
remote: [compile] Moved ./.codecrafters/run.sh → ./your_program.sh
remote: [compile] Compilation successful.
remote:
remote: Debug = true
remote:
remote: [tester::#PV1] Running tests for Stage #PV1 (Handle APIVersions requests)
remote: [tester::#PV1] $ ./your_program.sh /tmp/server.properties
remote: [tester::#PV1] Connecting to broker at: localhost:9092
remote: [your_program] Creating listener . . .
remote: [your_program] Listening on port 9092 . . .
remote: [your_program] Accepting an oncoming connection . . .
remote: [your_program] Accepting an oncoming connection . . .
remote: [tester::#PV1] Connection to broker at localhost:9092 successful
remote: [tester::#PV1] Sending "ApiVersions" (version: 4) request (Correlation id: 1578789074)
remote: [tester::#PV1] Hexdump of sent "ApiVersions" request:
remote: [tester::#PV1] Idx | Hex | ASCII
remote: [tester::#PV1] -----+-------------------------------------------------+-----------------
remote: [tester::#PV1] 0000 | 00 00 00 23 00 12 00 04 5e 1a 68 d2 00 09 6b 61 | ...#....^.h...ka
remote: [tester::#PV1] 0010 | 66 6b 61 2d 63 6c 69 00 0a 6b 61 66 6b 61 2d 63 | fka-cli..kafka-c
remote: [tester::#PV1] 0020 | 6c 69 04 30 2e 31 00 | li.0.1.
remote: [tester::#PV1]
remote: [tester::#PV1] Hexdump of received "ApiVersions" response:
remote: [tester::#PV1] Idx | Hex | ASCII
remote: [tester::#PV1] -----+-------------------------------------------------+-----------------
remote: [tester::#PV1] 0000 | 00 00 00 23 5e 1a 68 d2 00 00 00 12 00 03 00 04 | ...#^.h.........
remote: [tester::#PV1] 0010 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
remote: [tester::#PV1] 0020 | 00 00 00 00 00 00 00 | .......
remote: [tester::#PV1]
remote: [tester::#PV1] [Decoder] - .ResponseHeader
remote: [tester::#PV1] [Decoder] - .correlation_id (1578789074)
remote: [tester::#PV1] [Decoder] - .ResponseBody
remote: [tester::#PV1] [Decoder] - .error_code (0)
remote: [tester::#PV1] [Decoder] - .num_api_keys (0)
remote: [tester::#PV1] [Decoder] - .throttle_time_ms (301990656)
remote: [tester::#PV1] [Decoder] - .TAG_BUFFER
remote: [tester::#PV1] Received:
remote: [tester::#PV1] Hex (bytes 15-30) | ASCII
remote: [tester::#PV1] ------------------------------------------------+------------------
remote: [tester::#PV1] 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
remote: [tester::#PV1] ^ ^
remote: [tester::#PV1] Error: unexpected 15 bytes remaining in decoder after decoding ApiVersionsResponse
remote: [tester::#PV1] Context:
remote: [tester::#PV1] - ApiVersions v3
remote: [tester::#PV1] - Response Body
remote: [tester::#PV1]
remote: [tester::#PV1] Test failed
remote: [tester::#PV1] Terminating program
remote: [tester::#PV1] Program terminated successfully