Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
grpc refactoring (#162)
Browse files Browse the repository at this point in the history
* grpc refactoring

* typo fix
  • Loading branch information
agnek authored Dec 16, 2020
1 parent 0b70b03 commit 14a3d00
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
23 changes: 19 additions & 4 deletions cmd/signal/grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package main
import (
"flag"
"fmt"
"net"
"os"

grpc "github.com/pion/ion-avp/cmd/signal/grpc/server"
pb "github.com/pion/ion-avp/cmd/signal/grpc/proto"
"github.com/pion/ion-avp/cmd/signal/grpc/server"
avp "github.com/pion/ion-avp/pkg"
log "github.com/pion/ion-log"
"github.com/spf13/viper"
"google.golang.org/grpc"
)

var (
Expand Down Expand Up @@ -74,7 +77,19 @@ func main() {
fixByFunc := []string{}
log.Init(conf.Log.Level, fixByFile, fixByFunc)

grpc.NewServer(addr, conf, map[string]avp.ElementFun{
// fill in elements
})
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Panicf("failed to listen: %v", err)
}
log.Infof("--- AVP Node Listening at %s ---", addr)

s := grpc.NewServer()
srv := server.NewAVPServer(conf, map[string]avp.ElementFun{})
pb.RegisterAVPServer(s, srv)

if err := s.Serve(lis); err != nil {
log.Panicf("failed to serve: %v", err)
}

log.Infof("server finished")
}
20 changes: 2 additions & 18 deletions cmd/signal/grpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package server

import (
"io"
"net"

pb "github.com/pion/ion-avp/cmd/signal/grpc/proto"
avp "github.com/pion/ion-avp/pkg"
log "github.com/pion/ion-log"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -17,24 +15,10 @@ type server struct {
avp *AVP
}

// NewServer creates a new grpc avp server
func NewServer(addr string, conf avp.Config, elems map[string]avp.ElementFun) *grpc.Server {
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Panicf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterAVPServer(s, &server{
func NewAVPServer(conf avp.Config, elems map[string]avp.ElementFun) pb.AVPServer {
return &server{
avp: NewAVP(conf, elems),
})

log.Infof("--- AVP Node Listening at %s ---", addr)

if err := s.Serve(lis); err != nil {
log.Panicf("failed to serve: %v", err)
}

return s
}

// Signal handler for avp server
Expand Down
21 changes: 18 additions & 3 deletions examples/save-to-webm/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package main
import (
"flag"
"fmt"
"net"
"os"
"path"

grpc "github.com/pion/ion-avp/cmd/signal/grpc/server"
pb "github.com/pion/ion-avp/cmd/signal/grpc/proto"
"github.com/pion/ion-avp/cmd/signal/grpc/server"
avp "github.com/pion/ion-avp/pkg"
"github.com/pion/ion-avp/pkg/elements"
log "github.com/pion/ion-log"
"github.com/spf13/viper"
"google.golang.org/grpc"
)

type webmsaver struct {
Expand Down Expand Up @@ -93,9 +96,21 @@ func main() {
fixByFunc := []string{}
log.Init(conf.Avp.Log.Level, fixByFile, fixByFunc)

grpc.NewServer(addr, conf.Avp, map[string]avp.ElementFun{
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Panicf("failed to listen: %v", err)
}
log.Infof("--- AVP Node Listening at %s ---", addr)

s := grpc.NewServer()
srv := server.NewAVPServer(conf.Avp, map[string]avp.ElementFun{
"webmsaver": createWebmSaver,
})
pb.RegisterAVPServer(s, srv)

if err := s.Serve(lis); err != nil {
log.Panicf("failed to serve: %v", err)
}

select {}
log.Infof("server finished")
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919 h1:tmXTu+dfa+d9Evp8NpJdgOy6+rt8/x4yG7qPBrtNfLY=
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down

0 comments on commit 14a3d00

Please sign in to comment.