Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic harded get with udp is implemented #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"os"

"github.com/kaustuk/go-udp-memcache/pkg/client"
)

func main() {
c, err := client.InitUdpConnection()
if err != nil {
os.Exit(-1)
}

header := []byte{0, 1, 0, 0, 0, 1, 0, 0}
query := []byte("get key1\r\n")
fullquery := append(header, query...)

_, err = c.SendMessage(fullquery)

if err != nil {
os.Exit(-1)
}

p := make([]byte, 4096)

nn, err := c.ReceiveMessage(p)

if err != nil {
fmt.Printf("Read err %v\n", err)
os.Exit(-1)
}
fmt.Printf("%v\n", string(p[:nn]))

c.CloseConnection()
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/kaustuk/go-udp-memcache

go 1.20
Empty file added go.sum
Empty file.
58 changes: 58 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"log"
"net"
"os"
)

func main() {
// query := "get key1\r\n"

// format := []string{"H", "h", "h", "h"}
// values := []interface{}{1, 0, 1, 0}

// bp := binary_pack.BinaryPack{}

// data, err := bp.Pack(format, values)

// if err != nil {
// panic("error while packing")
// }

header := []byte{0, 1, 0, 0, 0, 1, 0, 0}
query := []byte("get key1\r\n")
fullquey := append(header, query...)
fmt.Println("hello from main")
fmt.Printf("%v\n", fullquey)

if err := os.WriteFile("file.txt", fullquey, 0644); err != nil {
log.Fatal(err)
}

conn, err := net.Dial("udp", "localhost:11212")

if err != nil {
fmt.Printf("Dial err %v", err)
os.Exit(-1)
}

defer conn.Close()

if _, err = conn.Write(fullquey); err != nil {
fmt.Printf("Write err %v", err)
os.Exit(-1)
}

p := make([]byte, 4096)
nn, err := conn.Read(p)
if err != nil {
fmt.Printf("Read err %v\n", err)
os.Exit(-1)
}

fmt.Printf("%v\n", string(p[:nn]))

// fmt.Printf("%v\n", query)
}
7 changes: 7 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package client

import "net"

type Client struct {
conn net.Conn
}
7 changes: 7 additions & 0 deletions pkg/client/client_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package client

type ClientInterface interface {
CloseConnection() error
SendMessage([]byte) (int, error)
ReceiveMessage([]byte) (int, error)
}
30 changes: 30 additions & 0 deletions pkg/client/udp_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package client

import (
"fmt"
"net"
)

func InitUdpConnection() (ClientInterface, error) {
fmt.Println("*********** init udp connection **********")
conn, err := net.Dial("udp", "localhost:11212")
if err != nil {
return nil, err
}
client := Client{conn: conn}
clientInterface := ClientInterface(&client)
return clientInterface, nil
}

func (client *Client) CloseConnection() error {
fmt.Println("************* close connection *************")
return client.conn.Close()
}

func (client *Client) SendMessage(b []byte) (int, error) {
return client.conn.Write(b)
}

func (client *Client) ReceiveMessage(b []byte) (int, error) {
return client.conn.Read(b)
}