Skip to content

Commit

Permalink
word server
Browse files Browse the repository at this point in the history
  • Loading branch information
pallab committed May 29, 2023
1 parent 97515c0 commit 73a5f9b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 64 deletions.
6 changes: 3 additions & 3 deletions go_rpc_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"google.golang.org/grpc"
"log"
"net"
pb "overengineered.com/rpc/market"
pb "overengineered.com/rpc/words"
)

func main() {
Expand All @@ -18,11 +18,11 @@ func main() {
}

server := grpc.NewServer()
pb.RegisterStockMarketServer(server, &serverImpl{})
pb.RegisterWordsServer(server, &serverImpl{})

server.Serve(listener)
}

type serverImpl struct {
pb.StockMarketServer
pb.WordsServer
}
10 changes: 10 additions & 0 deletions go_rpc_server/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ func (r *Rand) RandString(n int) string {
return string(b)
}

func (r *Rand) RandString2(n int) string {
b := make([]uint8, n, n)

for i := range b {
b[i] = uint8(rand.Intn(123-32) + 32)
}

return string(b)
}

func (r *Rand) RandNum(min int, max int) int {
return rand.Intn(max-min) + min
}
Expand Down
78 changes: 17 additions & 61 deletions go_rpc_server/rpc_methods.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,39 @@
package main

import (
"context"
"errors"
"log"
pb "overengineered.com/rpc/market"
"math/rand"
pb "overengineered.com/rpc/words"
"time"
)

var market = NewMarket()
var random = Rand{}
func RandString(length int) string {
b := make([]uint8, length, length)

func (s *serverImpl) ListStocks(context.Context, *pb.ListStocksRequest) (*pb.ListStocksResponse, error) {

var names []string

for k, _ := range market.tickers {
names = append(names, k)
for i := range b {
b[i] = uint8(rand.Intn(123-32) + 32)
}

return &pb.ListStocksResponse{
Names: names,
}, nil
return string(b)
}

func randomStockPrice() (res pb.StockPriceResponse, err error) {

var availableKeys []string

for k, v := range market.tickers {
if time.Now().Sub(v).Minutes() > 1 {
availableKeys = append(availableKeys, k)
}
}

if len(availableKeys) > 0 {
randomIdx := random.RandNum(0, len(availableKeys))
randomKey := availableKeys[randomIdx]
lastUpdatedAt := market.tickers[randomKey]

newUpdatedTime := lastUpdatedAt.Add(time.Duration(1 * time.Minute))
resp := pb.StockPriceResponse{
Timestamp: uint64(newUpdatedTime.Unix()),
Ticker: randomKey,
Price: uint32(random.RandNum(32, 67)),
Volume: uint32(random.RandNum(45, 56)),
}
market.tickers[randomKey] = newUpdatedTime
log.Printf("sending res :%T, %v \n", resp, resp)
return resp, nil
func RandomWord() (res pb.GetWordsResponse, err error) {
resp := pb.GetWordsResponse{
Timestamp: uint64(time.Now().Unix()),
Word: RandString(rand.Intn(5) + 1),
}
return pb.StockPriceResponse{}, errors.New("no more ")
}

func (s *serverImpl) GetStockPrice(req *pb.StockPriceRequest, stream pb.StockMarket_GetStockPriceServer) error {
log.Printf("received request : %v \n", req.Name)

for {
res, err := randomStockPrice()
if err != nil {
log.Printf("Error : %v \n", err)
break
} else if err := stream.Send(&res); err != nil {
return err
}
}

return nil
return resp, nil
}

func (s *serverImpl) GetStockPrice_(req *pb.StockPriceRequest, stream pb.StockMarket_GetStockPriceServer) error {
log.Printf("received request : %v \n", req.Name)
func (s *serverImpl) GetWords(req *pb.GetWordsRequest, stream pb.Words_GetWordsServer) error {
log.Printf("received request : %v \n", req)

c := make(chan pb.StockPriceResponse, 100)
c := make(chan pb.GetWordsResponse, 100)

go func(c chan pb.StockPriceResponse) {
go func(c chan pb.GetWordsResponse) {

for {
p, err := randomStockPrice()
p, err := RandomWord()
if err != nil {
log.Printf("Error : %v \n", err)
break
Expand Down

0 comments on commit 73a5f9b

Please sign in to comment.