-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatserver.go
124 lines (90 loc) · 2.84 KB
/
chatserver.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
package proto
import (
"log"
"math/rand"
"sync"
"time"
)
type message struct {
ClientUid string
MessageText string
MessageUniqueCode int
ClientUniqueCode int
}
type messageHandle struct {
MessageQueue []message
mutex sync.Mutex
}
var messageHandleObject = messageHandle{}
type ChatServer struct {
SenderServer
//proto.UnimplementedSenderServer
}
func (s *ChatServer) SendMessage(in Sender_SendMessageServer) error {
clientUniqueCode := rand.Intn(1e6)
errch := make(chan error)
// receive message - init a goroutine
go receiveFromStream(in, clientUniqueCode, errch)
// send message - init a goroutine
go sendToStream(in, clientUniqueCode, errch)
return <-errch
}
// receive messages
func receiveFromStream(in_ Sender_SendMessageServer, clientUniqueCode_ int, errch_ chan error) {
// implement a loop
for {
msg, err := in_.Recv()
if err != nil {
log.Printf("Error in receiving message from client :: %v", err)
errch_ <- err
} else {
messageHandleObject.mutex.Lock()
messageHandleObject.MessageQueue = append(messageHandleObject.MessageQueue, message{
ClientUid: msg.Uid,
MessageText: msg.Text,
MessageUniqueCode: rand.Intn(1e8),
ClientUniqueCode: clientUniqueCode_,
})
log.Printf("%v", messageHandleObject.MessageQueue[len(messageHandleObject.MessageQueue)-1])
messageHandleObject.mutex.Unlock()
}
}
}
// send message
func sendToStream(in_ Sender_SendMessageServer, clientUniqueCode_ int, errch_ chan error) {
// implement a loop
for {
// loop through messages in MessageQueue
for {
time.Sleep(500 * time.Millisecond)
messageHandleObject.mutex.Lock()
if len(messageHandleObject.MessageQueue) == 0 {
messageHandleObject.mutex.Unlock()
break
}
senderUniqueCode := messageHandleObject.MessageQueue[0].ClientUniqueCode
senderName4Client := messageHandleObject.MessageQueue[0].ClientUid
message4Client := messageHandleObject.MessageQueue[0].MessageText
messageHandleObject.mutex.Unlock()
//send message to designated client (do not send to the same client)
if senderUniqueCode != clientUniqueCode_ {
err := in_.Send(&FromServer{Uid: senderName4Client, Text: message4Client})
if err != nil {
errch_ <- err
}
messageHandleObject.mutex.Lock()
if len(messageHandleObject.MessageQueue) > 1 {
messageHandleObject.MessageQueue = messageHandleObject.MessageQueue[1:] // delete the message at index 0 after sending to receiver
} else {
messageHandleObject.MessageQueue = []message{}
}
messageHandleObject.mutex.Unlock()
}
}
time.Sleep(100 * time.Millisecond)
}
}
//func (s *server) SendMessage(ctx, in *pb.FromClient) (*pb.FromServer, error) {
// log.Printf("Message from %v > %v", in.GetUid(), in.GetText())
// return &pb.FromServer{Uid: in.GetUid() + " > " + in.GetText()}, nil
//}