-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
94 lines (80 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"errors"
"github.com/hello/webrtc-signaling/signaling"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"net"
)
var (
ErrNotImplemented = errors.New("not implemented")
)
const (
port = ":6556"
)
type HelloSignaling struct {
offer string
answers chan string
}
func (s *HelloSignaling) Join(ctx context.Context, answer *signaling.SdpAnswer) (*signaling.Empty, error) {
log.Println("received join call")
s.answers <- answer.GetSdp()
log.Println("responding with empty")
empty := &signaling.Empty{
Block: true,
}
return empty, nil
}
func (s *HelloSignaling) Wait(empty *signaling.Empty, stream signaling.Signaling_WaitServer) error {
log.Println("Waiting ....")
offer := &signaling.SdpOffer{
Sdp: s.offer,
}
log.Println("about to send offer on stream")
err := stream.Send(offer)
if err != nil {
log.Fatal(err)
}
log.Println("Sent offer")
return nil
}
func (s *HelloSignaling) Start(offer *signaling.SdpOffer, stream signaling.Signaling_StartServer) error {
log.Println("Get SDP", offer.GetSdp())
s.offer = offer.GetSdp()
// Waiting for Answers
log.Println("Waiting for answers")
wait:
for {
select {
case a := <-s.answers:
answer := &signaling.SdpAnswer{
Sdp: a,
}
err := stream.Send(answer)
log.Println("Sent answer")
if err != nil {
log.Fatal(err)
}
break wait
}
}
log.Println("Start Done")
return nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
hs := &HelloSignaling{
offer: "",
answers: make(chan string, 0),
}
signaling.RegisterSignalingServer(s, hs)
log.Println("Serving...")
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}