Skip to content

Commit

Permalink
Refactor chat (#47)
Browse files Browse the repository at this point in the history
* examples/chat/client: Refactor to use bufio.Scanner

* examples/chat/client: Refactor to inject listen/connect interfaces and ports

* examples/chat/server: Refactor to inject listening interface and port

* examples/chat/server: Refactor use select{} instead of an empty channel.

* examples/chat/build.sh: Replace with types/generate.go

* examples/chat/server: Add missing flag.Parse.

* examples/chat/types/generate.go: Remove extra proto_proto.
  • Loading branch information
yarcat authored Aug 30, 2023
1 parent 57c621d commit b68a4c4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
1 change: 0 additions & 1 deletion examples/chat/build.sh

This file was deleted.

23 changes: 11 additions & 12 deletions examples/chat/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,37 @@ func (c *client) Receive(ctx *actor.Context) {

func main() {
var (
port = flag.String("port", ":3000", "")
username = flag.String("username", "", "")
listenAt = flag.String("listen", "127.0.0.1:3000", "")
connectTo = flag.String("connect", "127.0.0.1:4000", "")
username = flag.String("username", "", "")
)
flag.Parse()

e := actor.NewEngine()
rem := remote.New(e, remote.Config{
ListenAddr: "127.0.0.1" + *port,
ListenAddr: *listenAt,
})
e.WithRemote(rem)

var (
// the process ID of the server
serverPID = actor.NewPID("127.0.0.1:4000", "server")
serverPID = actor.NewPID(*connectTo, "server")
// Spawn our client receiver
clientPID = e.Spawn(newClient(*username, serverPID), "client")
r = bufio.NewReader(os.Stdin)
scanner = bufio.NewScanner(os.Stdin)
)
for {
str, err := r.ReadString('\n')
if err != nil {
log.Errorw("failed to read message from stdin", log.M{"err": err})
break
}
for scanner.Scan() {
msg := &types.Message{
Msg: str,
Msg: scanner.Text(),
Username: *username,
}
// We use SendWithSender here so the server knows who
// is sending the message.
e.SendWithSender(serverPID, msg, clientPID)
}
if err := scanner.Err(); err != nil {
log.Errorw("failed to read message from stdin", log.M{"err": err})
}

// When breaked out of the loop on error let the server know
// we need to disconnect.
Expand Down
10 changes: 8 additions & 2 deletions examples/chat/server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"flag"

"github.com/anthdm/hollywood/actor"
"github.com/anthdm/hollywood/examples/chat/types"
"github.com/anthdm/hollywood/log"
Expand Down Expand Up @@ -52,13 +54,17 @@ func (s *server) handleMessage(ctx *actor.Context, msg *types.Message) {
}

func main() {
var (
listenAt = flag.String("listen", "127.0.0.1:4000", "")
)
flag.Parse()
log.SetLevel(log.LevelInfo)
e := actor.NewEngine()
rem := remote.New(e, remote.Config{
ListenAddr: "127.0.0.1:4000",
ListenAddr: *listenAt,
})
e.WithRemote(rem)
e.Spawn(newServer, "server")

<-(make(chan struct{}))
select {}
}
3 changes: 3 additions & 0 deletions examples/chat/types/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package types

//go:generate protoc --proto_path=. --go_out=. --go_opt=paths=source_relative types.proto

0 comments on commit b68a4c4

Please sign in to comment.