/
service.go
291 lines (249 loc) · 8.31 KB
/
service.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package chatservice
import (
"context"
"fmt"
"sync"
"time"
chatservice "github.com/nsnull0/ChatService/protos/genbuf"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Message is content message sent by user
type Message struct {
SenderAlias string `bson:"sender_alias"`
Message string `bson:"message"`
Time string `bson:"timestamp"`
}
// Room is room created by User
type Room struct {
RoomID string `bson:"room_id"`
Messages []Message `bson:"messages"`
}
// User is sender or receiver
type User struct {
UserAlias string `bson:"user_alias"`
RoomID string `bson:"room_id"`
}
// CollectionUser is for user db
var CollectionUser *mongo.Collection
// CollectionRoom is for room db
var CollectionRoom *mongo.Collection
// CollectionMessages is for message db
var CollectionMessages *mongo.Collection
// Connection is for Server have several connection ?
type Connection struct {
stream chatservice.ChatService_CreateStreamServer
id string
roomID string
active bool
error chan error
}
// Server is the interface service of this service
type Server struct {
Connnection []*Connection
}
// RegisterOrLoginUser is for login or register
func (s *Server) RegisterOrLoginUser(ctx context.Context, req *chatservice.LoginRequest) (*chatservice.LoginResponse, error) {
fmt.Println("Register/Login User is invoked")
reqUser := &User{
UserAlias: req.GetRequest().GetAliasname(),
}
room := &Room{}
filter := bson.M{"user_alias": reqUser.UserAlias}
res := CollectionUser.FindOne(ctx, filter)
if err := res.Decode(reqUser); err != nil {
fmt.Println("User is not existed")
_, errInsert := CollectionUser.InsertOne(ctx, reqUser)
if errInsert != nil {
return nil, status.Errorf(codes.Internal, "Internal Error Occured while creating User %v\n", errInsert)
}
fmt.Printf("User is created %v \n", reqUser)
}
filter = bson.M{"room_id": reqUser.RoomID}
res = CollectionRoom.FindOne(ctx, filter)
if err := res.Decode(room); err != nil {
fmt.Println("User is not join any room")
reqUser.RoomID = ""
} else {
fmt.Printf("User is already join some room %v \n", room.RoomID)
}
fmt.Println("User is valid")
loginRes := &chatservice.LoginResponse{
Result: &chatservice.User{
Aliasname: reqUser.UserAlias,
HasRoom: len(room.RoomID) > 0,
},
}
return loginRes, nil
}
// RegisterOrJoinRoom is to register or join a room, user will move to other room if the requested user don't have room
func (s *Server) RegisterOrJoinRoom(ctx context.Context, req *chatservice.RoomRequest) (*chatservice.RoomResponse, error) {
fmt.Println("Register or Join Room is invoked")
room := &Room{}
user := &User{}
filter := bson.M{"room_id": req.GetRoomId()}
filterUser := bson.M{"user_alias": req.GetAliasName()}
emptyMessages := make([]Message, 0)
res := CollectionRoom.FindOne(ctx, filter)
if err := res.Decode(room); err != nil {
fmt.Println("Room is new and not existed")
room.RoomID = req.GetRoomId()
room.Messages = emptyMessages
_, errInsert := CollectionRoom.InsertOne(ctx, room)
if errInsert != nil {
return nil, status.Errorf(codes.Internal, "Internal Error occured while inserting room %v\n", errInsert)
}
fmt.Printf("Room has created %v\n", room)
}
res = CollectionUser.FindOne(ctx, filterUser)
if err := res.Decode(user); err != nil {
fmt.Println("Client error, should do register user first!")
return nil, status.Errorf(codes.Unauthenticated, "Can't find User with received alias name, %v", err)
}
user.RoomID = room.RoomID
_, errUpdate := CollectionUser.ReplaceOne(ctx, filterUser, user)
if errUpdate != nil {
fmt.Println("Update User Error")
return nil, status.Errorf(codes.Internal, "Internal Error occured while updating User, %v \n", errUpdate)
}
fmt.Printf("User has updated %v \n", user)
fmt.Println("Updating corresponding user and join room")
var roomMessagesRes []*chatservice.UserMessage
for _, element := range room.Messages {
msgRes := &chatservice.UserMessage{
Aliasname: element.SenderAlias,
Message: element.Message,
}
fmt.Printf("message %v\n", element.Message)
roomMessagesRes = append(roomMessagesRes, msgRes)
fmt.Printf("messages %v\n", roomMessagesRes)
}
user.RoomID = room.RoomID
roomResponse := &chatservice.RoomResponse{
Response: &chatservice.Room{
RoomId: room.RoomID,
Messages: roomMessagesRes,
},
}
fmt.Printf("Room has been created %v \n", room.RoomID)
return roomResponse, nil
}
// // SendMessage is for streaming response and request messaging
// func (s *Server) SendMessage(stream chatservice.ChatService_SendMessageServer) error {
// fmt.Println("Stream messaging invoked")
// for {
// req, err := stream.Recv()
// if err == io.EOF {
// fmt.Printf("End of file")
// return nil
// }
// if err != nil {
// log.Fatalf("error while reading messaging stream: %v", err)
// }
// currentTime := time.Now().UTC().String()
// msgObject := Message{
// SenderAlias: req.GetSenderalias(),
// Message: req.GetMessage(),
// Time: currentTime,
// }
// room := &Room{}
// filterRoom := bson.M{"room_id": req.GetRoomId()}
// res := CollectionUser.FindOne(stream.Context(), filterRoom)
// if err := res.Decode(room); err != nil {
// fmt.Println("room not exist")
// return status.Errorf(codes.NotFound, "Room not found %v\n")
// }
// room.Messages = append(room.Messages, msgObject)
// updates := bson.M{
// "$addToSet": bson.M{"messages": bson.M{"$each": room.Messages}},
// }
// _, errUpdate := CollectionRoom.UpdateOne(stream.Context(), filterRoom, updates)
// if errUpdate != nil {
// fmt.Printf("Error on update, %v\n", errUpdate)
// return status.Errorf(codes.Internal, "Internal error occured while updating the message record: %v\n", errUpdate)
// }
// msgRes := &chatservice.SendMessageResponse{
// RoomId: room.RoomID,
// Senderalias: req.GetSenderalias(),
// Message: req.GetMessage(),
// }
// castingErr := stream.Send(msgRes)
// if castingErr != nil {
// log.Fatalf("error while sending casting to the client: %v", castingErr)
// return err
// }
// }
// }
// CreateStream is for stream the message to client
func (s *Server) CreateStream(connect *chatservice.StreamConnect, stream chatservice.ChatService_CreateStreamServer) error {
conn := &Connection{
stream: stream,
id: connect.GetSenderalias(),
roomID: connect.GetRoomId(),
active: true,
error: make(chan error),
}
n := 0
for _, x := range s.Connnection {
if x.id != connect.GetSenderalias() {
s.Connnection[n] = x
n++
}
}
s.Connnection = s.Connnection[:n]
s.Connnection = append(s.Connnection, conn)
return <-conn.error
}
// SendMessage is for broadcast all message to client
func (s *Server) SendMessage(ctx context.Context, req *chatservice.ContentMessage) (*chatservice.Empty, error) {
currentTime := time.Now().UTC().String()
msgObject := Message{
SenderAlias: req.GetSenderId(),
Message: req.GetContent(),
Time: currentTime,
}
room := &Room{}
filterRoom := bson.M{"room_id": req.GetRoomId()}
res := CollectionUser.FindOne(ctx, filterRoom)
if err := res.Decode(room); err != nil {
fmt.Println("room not exist")
status.Errorf(codes.NotFound, "Room not found %v\n")
}
room.Messages = append(room.Messages, msgObject)
updates := bson.M{
"$addToSet": bson.M{"messages": bson.M{"$each": room.Messages}},
}
updateRes, errUpdate := CollectionRoom.UpdateOne(ctx, filterRoom, updates)
if errUpdate != nil {
fmt.Printf("Error on update, %v\n", errUpdate)
status.Errorf(codes.Internal, "Internal error occured while updating the message record: %v\n", errUpdate)
}
fmt.Printf("Messages has been added: %v - %v \n", room.Messages, updateRes.ModifiedCount)
syncWait := sync.WaitGroup{}
finish := make(chan int)
for _, conn := range s.Connnection {
syncWait.Add(1)
go func(messageContent *chatservice.ContentMessage, conn *Connection) {
defer syncWait.Done()
if conn.active {
if req.RoomId == conn.roomID {
err := conn.stream.Send(messageContent)
fmt.Printf("Send Message to: %v\n", conn.stream)
if err != nil {
fmt.Printf("Error while streaming: %v\n", err)
conn.active = false
conn.error <- err
}
}
}
}(req, conn)
}
go func() {
syncWait.Wait()
close(finish)
}()
<-finish
return &chatservice.Empty{}, nil
}