Skip to content

Commit

Permalink
implemented simple tcp server
Browse files Browse the repository at this point in the history
  • Loading branch information
stanipetrosyan committed Apr 12, 2024
1 parent f7345ad commit a53e376
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/networkbus/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ var eventbus = goeventbus.NewEventBus()
func main() {
network := goeventbus.NewNetworkBus(eventbus, "localhost", "/bus")

network.Client().Connect()
network.Client("localhost", "/bus").Connect()
}
2 changes: 1 addition & 1 deletion examples/networkbus/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ var eventbus = goeventbus.NewEventBus()

func main() {
network := goeventbus.NewNetworkBus(eventbus, "localhost", "/bus")
network.Server().Listen()
network.Server("localhost", "/bus").Listen()
}
10 changes: 5 additions & 5 deletions network_bus.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package goeventbus

type NetworkBus interface {
Server() Server
Client() Client
Server(address, path string) Server
Client(address, path string) Client
}

type defaultNetworkBus struct {
Expand All @@ -15,10 +15,10 @@ func NewNetworkBus(bus EventBus, address, path string) NetworkBus {
return defaultNetworkBus{localBus: bus, address: address, path: path}
}

func (b defaultNetworkBus) Server() Server {
return NewServer()
func (b defaultNetworkBus) Server(address, path string) Server {
return NewServer(address, path)
}

func (b defaultNetworkBus) Client() Client {
func (b defaultNetworkBus) Client(address, path string) Client {
return NewClient()
}
46 changes: 44 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
package goeventbus

import (
"fmt"
"net"
)

type Server interface {
Listen()
}

type tcpServer struct {
address, path string
}

func (s tcpServer) Listen() {

listener, err := net.Listen("tcp", s.address)
if err != nil {
fmt.Println("Error:", err)
return
}

defer listener.Close()

for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error:", err)
continue
}

handleClient(conn)
}
}

func NewServer() Server {
return tcpServer{}
func NewServer(address, path string) Server {
return tcpServer{address: address, path: path}
}

func handleClient(conn net.Conn) {
defer conn.Close()

buffer := make([]byte, 1024)

for {
// Read data from the client
n, err := conn.Read(buffer)
if err != nil {
fmt.Println("Error:", err)
return
}

// Process and use the data (here, we'll just print it)
fmt.Printf("Received: %s\n", buffer[:n])
}
}
21 changes: 21 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package goeventbus

import (
"net"
"testing"

"github.com/stretchr/testify/assert"
)

func TestServer(t *testing.T) {
go NewServer("localhost:8080", "/").Listen()

conn, err := net.Dial("tcp", "localhost:8080")
assert.Nil(t, err)

defer conn.Close()

data := []byte("Hello, Server!")
_, err = conn.Write(data)
assert.Nil(t, err)
}

0 comments on commit a53e376

Please sign in to comment.