-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
152 lines (134 loc) · 4.21 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"crypto/sha256"
"flag"
"fmt"
"google.golang.org/grpc"
glog "google.golang.org/grpc/grpclog"
lpb "grpchat/proto"
"log"
"net"
"os"
"sync"
"time"
)
type Connection struct {
stream lpb.Broadcast_CreateStreamServer
id string
active bool
error chan error
}
type Server struct {
lpb.UnimplementedBroadcastServer
Connections map[string]*Connection
}
type ChatRoom struct {
id [32]byte
name string
}
type ChatUser struct {
username string
id int64
}
var (
grpcLog glog.LoggerV2
serverPort = flag.Int("p", 50051, "The server port")
chatRooms map[string]ChatRoom
users map[string]ChatUser
users2ChatRooms map[string][]string
users2connections map[string]*Connection
chat2Connections map[string]map[string]*Connection
)
func init() {
chatRooms = make(map[string]ChatRoom)
users = make(map[string]ChatUser)
users2ChatRooms = make(map[string][]string)
grpcLog = glog.NewLoggerV2(os.Stdout, os.Stdout, os.Stdout)
chatRooms = make(map[string]ChatRoom)
users2connections = make(map[string]*Connection)
chat2Connections = make(map[string]map[string]*Connection)
}
func (s *Server) GetChatRooms(_ context.Context, req *lpb.User) (*lpb.GetChatResp, error) {
var chatRoomsForUser []string
if _, userExists := users[req.Name]; userExists != true {
//timestamp := time.Now()
for _, chatName := range users2ChatRooms[req.Name] {
chatRoomsForUser = append(chatRoomsForUser, chatName)
}
}
return &lpb.GetChatResp{ChatNames: chatRoomsForUser}, nil
}
func (s *Server) CreateChatRoom(_ context.Context, req *lpb.CreateChatReq) (*lpb.GetChatResp, error) {
chatIdStr := make([]string, 0)
if _, roomExists := chatRooms[req.ChatName]; roomExists != true {
timestamp := time.Now()
id := sha256.Sum256([]byte(timestamp.String() + req.ChatName))
chatRooms[req.ChatName] = ChatRoom{id, req.ChatName}
fmt.Printf("User %s created chat room %s\n", req.User.Name, req.ChatName)
}
users2ChatRooms[req.User.Name] = append(users2ChatRooms[req.User.Name], req.ChatName)
if _, connInCat := chat2Connections[req.ChatName]; connInCat != true {
chat2Connections[req.ChatName] = make(map[string]*Connection)
}
if _, userConnForChat := chat2Connections[req.ChatName][req.User.Name]; userConnForChat != true {
chat2Connections[req.ChatName][req.User.Name] = users2connections[req.User.Name]
}
chatIdStr = append(chatIdStr, req.ChatName)
return &lpb.GetChatResp{ChatNames: chatIdStr}, nil
}
func (s *Server) CreateStream(protoConn *lpb.Connect, stream lpb.Broadcast_CreateStreamServer) error {
conn := &Connection{
stream, protoConn.User.Id, true, make(chan error),
}
if _, userExists := users[protoConn.User.Name]; userExists != true {
for _, chatName := range users2ChatRooms[protoConn.User.Name] {
chat2Connections[chatName][protoConn.User.Name] = conn
}
}
users2connections[protoConn.User.Name] = conn
fmt.Printf("CreateStream from user: %s\n", protoConn.User.Name)
s.Connections[protoConn.User.Name] = conn
return <-conn.error
}
func (s *Server) BroadcastMessage(_ context.Context, msg *lpb.Message) (*lpb.Close, error) {
wait := sync.WaitGroup{}
doneChan := make(chan int)
// send the message to each connection that exists in the current chat room
for _, conn := range chat2Connections[msg.ChatName] {
wait.Add(1)
go func(msg *lpb.Message, conn *Connection) {
defer wait.Done()
if conn.active {
err := conn.stream.Send(msg)
grpcLog.Info("Sending msg with user id ", msg.Id, " in chat room ", msg.ChatName, " to: ", conn.stream)
if err != nil {
grpcLog.Error("Error with Stream: %s - Error: %v", conn.stream, err)
conn.active = false
conn.error <- err
}
}
}(msg, conn)
}
go func() {
wait.Wait()
close(doneChan)
}()
<-doneChan
return &lpb.Close{}, nil
}
func main() {
flag.Parse()
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", *serverPort))
if err != nil {
log.Fatalf("error creating the server %v", err)
}
grpcServer := grpc.NewServer()
server := &Server{}
server.Connections = users2connections
lpb.RegisterBroadcastServer(grpcServer, server)
grpcLog.Info("Starting server at :", *serverPort)
if err := grpcServer.Serve(listener); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}