testing out a stream

trunk
vilmibm 2022-07-09 02:15:24 -05:00
parent a746b12329
commit 10a5794cd9
3 changed files with 87 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io"
"log" "log"
"time" "time"
@ -21,6 +22,53 @@ var (
serverHostOverride = flag.String("server_host_override", "x.test.example.com", "The server name used to verify the hostname returned by the TLS handshake") serverHostOverride = flag.String("server_host_override", "x.test.example.com", "The server name used to verify the hostname returned by the TLS handshake")
) )
func messages(cs *ClientState) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stream, err := cs.Client.Messages(ctx, cs.SessionInfo)
if err != nil {
return err
}
for {
msg, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return err
}
cs.AddMessage(msg)
}
return nil
}
type ClientState struct {
App *tview.Application
Client proto.GameWorldClient
SessionInfo *proto.SessionInfo
MaxMessages int
messagesView *tview.TextView
messages []*proto.ClientMessage
}
func (cs *ClientState) AddMessage(msg *proto.ClientMessage) {
// TODO i don't like this function
cs.messages = append(cs.messages, msg)
if len(cs.messages) > cs.MaxMessages {
cs.messages = cs.messages[1 : len(cs.messages)-1]
}
cs.App.QueueUpdateDraw(func() {
cs.messagesView.SetText("")
for _, msg := range cs.messages {
fmt.Fprintf(cs.messagesView, "%#v\n", msg)
}
})
}
func _main() error { func _main() error {
var opts []grpc.DialOption var opts []grpc.DialOption
if *tls { if *tls {
@ -47,17 +95,31 @@ func _main() error {
client := proto.NewGameWorldClient(conn) client := proto.NewGameWorldClient(conn)
// TODO registration and login stuff
app := tview.NewApplication()
// TODO make a NewClientState
cs := &ClientState{
App: app,
SessionInfo: &proto.SessionInfo{},
Client: client,
MaxMessages: 15, // TODO for testing
messages: []*proto.ClientMessage{},
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
pong, err := client.Ping(ctx, &proto.SessionInfo{}) pong, err := cs.Client.Ping(ctx, cs.SessionInfo)
if err != nil { if err != nil {
log.Fatalf("%v.Ping -> %v", client, err) log.Fatalf("%v.Ping -> %v", cs.Client, err)
} }
//stream, err := messageStream(client, sessionInfo)
log.Printf("%#v", pong) log.Printf("%#v", pong)
app := tview.NewApplication()
pages := tview.NewPages() pages := tview.NewPages()
pages.AddPage("splash", pages.AddPage("splash",
@ -82,6 +144,9 @@ func _main() error {
pages.AddPage("main", mainPage, true, false) pages.AddPage("main", mainPage, true, false)
msgView := tview.NewTextView()
cs.messagesView = msgView
gamePage := tview.NewGrid(). gamePage := tview.NewGrid().
SetRows(1, 40, 3). SetRows(1, 40, 3).
SetColumns(-1, -1). SetColumns(-1, -1).
@ -93,7 +158,7 @@ func _main() error {
tview.NewTextView().SetTextAlign(tview.AlignRight).SetText("TODO server status"), tview.NewTextView().SetTextAlign(tview.AlignRight).SetText("TODO server status"),
0, 1, 1, 1, 1, 1, false). 0, 1, 1, 1, 1, 1, false).
AddItem( AddItem(
tview.NewTextView().SetText("TODO game messages"), msgView,
1, 0, 1, 1, 10, 20, false). 1, 0, 1, 1, 10, 20, false).
AddItem( AddItem(
tview.NewTextView().SetText("TODO detail window"), tview.NewTextView().SetText("TODO detail window"),
@ -104,6 +169,8 @@ func _main() error {
pages.AddPage("game", gamePage, true, false) pages.AddPage("game", gamePage, true, false)
go messages(cs)
return app.SetRoot(pages, true).SetFocus(pages).Run() return app.SetRoot(pages, true).SetFocus(pages).Run()
} }

View File

@ -62,8 +62,9 @@ message ClientMessage {
GLOBAL = 4; // the system sent out a PSA GLOBAL = 4; // the system sent out a PSA
} }
ClientMessageType cmt = 1; ClientMessageType type = 1;
optional string speaker = 2; optional string speaker = 2;
string text = 3;
} }
message MapData { message MapData {

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"time"
"github.com/vilmibm/hermeticum/proto" "github.com/vilmibm/hermeticum/proto"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -68,6 +69,19 @@ func (s *gameWorldServer) Ping(ctx context.Context, _ *proto.SessionInfo) (*prot
return pong, nil return pong, nil
} }
func (s *gameWorldServer) Messages(si *proto.SessionInfo, stream proto.GameWorld_MessagesServer) error {
for x := 0; x < 20; x++ {
msg := &proto.ClientMessage{}
speaker := "snoozy"
msg.Speaker = &speaker
msg.Type = proto.ClientMessage_WHISPER
msg.Text = fmt.Sprintf("have message %d", x)
stream.Send(msg)
time.Sleep(2 * time.Second)
}
return nil
}
func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (*proto.SessionInfo, error) { func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (*proto.SessionInfo, error) {
// TODO // TODO
return nil, nil return nil, nil