-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
169 lines (141 loc) · 3.44 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"./validation"
"bufio"
"flag"
"fmt"
"net"
)
// Client struct
type Client struct {
incoming chan message
outgoing chan string
reader *bufio.Reader
writer *bufio.Writer
address string
connection net.Conn
}
type message struct {
text string
address string
}
// Read line by line into the client.incoming
func (client *Client) Read() {
for {
line, err := client.reader.ReadString('\n')
if err != nil {
client.connection.Close()
fmt.Println(fmt.Sprintf("Read error %s", err))
break
}
client.incoming <- message{
text: line,
address: client.address,
}
fmt.Println(fmt.Sprintf("Client.Read %s", line))
}
}
// Write client outgoing data to the client writer
func (client *Client) Write() {
for data := range client.outgoing {
client.writer.WriteString(data)
client.writer.Flush()
}
}
// Listen for reads and writes on the client
func (client *Client) Listen() {
go client.Read()
go client.Write()
}
// NewClient returns new instance of client.
func NewClient(connection net.Conn) *Client {
writer := bufio.NewWriter(connection)
reader := bufio.NewReader(connection)
address := connection.RemoteAddr().String()
client := &Client{
incoming: make(chan message),
outgoing: make(chan string),
reader: reader,
writer: writer,
address: address,
connection: connection,
}
client.Listen()
fmt.Println("NewClient")
return client
}
// ChatRoom struct
type ChatRoom struct {
clients []*Client
joins chan net.Conn
incoming chan message
outgoing chan string
story string
lastMsgUserAddress string
}
// Broadcast data to all connected chatRoom.clients
func (chatRoom *ChatRoom) Broadcast(data string) {
for _, client := range chatRoom.clients {
client.outgoing <- fmt.Sprintf("%s\n", data)
}
}
// Join attaches a new client to the chatRoom clients
func (chatRoom *ChatRoom) Join(connection net.Conn) {
client := NewClient(connection)
chatRoom.clients = append(chatRoom.clients, client)
client.outgoing <- fmt.Sprintf("%s\n", chatRoom.story)
go func() {
for {
chatRoom.incoming <- <-client.incoming
}
}()
}
// Listen to all incoming messages for the chatRoom
func (chatRoom *ChatRoom) Listen() {
go func() {
for {
select {
case data := <-chatRoom.incoming:
msg, err := validation.ValidateMsg(data.text)
if err == nil && chatRoom.lastMsgUserAddress != data.address {
fmt.Println(fmt.Sprintf("chatRoom.Broadcast %s", msg))
chatRoom.Broadcast(msg)
if len(chatRoom.story) == 0 {
chatRoom.story = msg
} else {
chatRoom.story = fmt.Sprintf("%s %s", chatRoom.story, msg)
}
chatRoom.lastMsgUserAddress = data.address
}
case conn := <-chatRoom.joins:
fmt.Println("chatRoom.join")
chatRoom.Join(conn)
}
}
}()
}
// NewChatRoom factories a ChatRoom instance
func NewChatRoom() *ChatRoom {
chatRoom := &ChatRoom{
clients: make([]*Client, 0),
joins: make(chan net.Conn),
incoming: make(chan message),
outgoing: make(chan string),
}
chatRoom.Listen()
fmt.Println("NewChatRoom")
return chatRoom
}
func main() {
var server string
var port int
flag.StringVar(&server, "server", "", "Server host")
flag.IntVar(&port, "port", 8080, "Server port")
flag.Parse()
listener, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", server, port))
chatRoom := NewChatRoom()
for {
conn, _ := listener.Accept()
chatRoom.joins <- conn
}
}