-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
144 lines (123 loc) · 3.03 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
package main
import (
"bufio"
"context"
"fmt"
"log"
"net"
"strings"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go/v4"
"google.golang.org/api/option"
)
type server struct {
listener *net.Listener
commands chan command
users map[net.Addr]*client
app *firebase.App
ctx *context.Context
dbClient *firestore.Client
}
func newServer(fbKeyPath string) *server {
ctx := context.Background()
sa := option.WithCredentialsFile(fbKeyPath)
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
dbClient, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
return &server{
commands: make(chan command),
users: make(map[net.Addr]*client),
app: app,
ctx: &ctx,
dbClient: dbClient,
}
}
func (s *server) router() {
for cmd := range s.commands {
switch cmd.id {
case CMD_USERS:
s.listUsers(cmd.client)
case CMD_MSG:
text := strings.Join(cmd.args[1:], " ")
s.broadcast(cmd.client, cmd.client.name+": "+text)
go s.updateDatabase(text, cmd.client.name)
case CMD_QUIT:
s.quit(cmd.client)
}
}
}
func (s *server) newClient(conn net.Conn) {
log.Printf("New client has connected: %s", conn.RemoteAddr().String())
conn.Write([]byte("Please enter username: "))
username, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
conn.Write([]byte("Invalid input"))
return
}
username = strings.ToLower(strings.Trim(username, "\r\n"))
c := &client{
conn: conn,
name: username,
commands: s.commands,
}
s.users[c.conn.RemoteAddr()] = c
s.broadcast(c, fmt.Sprintf("%s has join the chat", c.name))
c.showHist(s)
c.msg(fmt.Sprintf("%s, Welcome to the chat", c.name))
c.inputReader()
}
func (s *server) start(addr string) {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("Failed to start server: %s", err.Error())
}
s.listener = &listener
go s.router()
log.Printf("Server started on: %s", addr)
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Panicf("Unable to accept connection: %s", err.Error())
continue
}
go s.newClient(conn)
}
}
func (s *server) broadcast(sender *client, msg string) {
for addr, u := range s.users {
fmt.Println(addr)
if addr != sender.conn.RemoteAddr() {
u.msg(msg)
}
}
}
func (s *server) updateDatabase(text string, name string) {
_, _, err := s.dbClient.Collection("chats").Add(*s.ctx, map[string]interface{}{
"text": text,
"name": name,
"createdAt": firestore.ServerTimestamp,
})
if err != nil {
fmt.Printf("Failed adding aturing: %v", err.Error())
}
}
func (s *server) listUsers(c *client) {
users := make([]string, 0, len(s.users))
for _, u := range s.users {
users = append(users, u.name)
}
c.msg(strings.Join(users, ", "))
}
func (s *server) quit(c *client) {
s.broadcast(c, fmt.Sprintf("%s has left the chat", c.name))
c.msg(fmt.Sprintf("Bye, %s!", c.name))
delete(s.users, c.conn.RemoteAddr())
c.conn.Close()
log.Printf("Client has disconnected: %s", c.conn.RemoteAddr().String())
}