-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
159 lines (134 loc) · 3.19 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/TransactPRO/workshop-golang-19032016/client"
"github.com/TransactPRO/workshop-golang-19032016/server"
"github.com/TransactPRO/workshop-golang-19032016/ui"
"github.com/TransactPRO/workshop-golang-19032016/util"
)
var (
userName = flag.String("u", "", "master host")
masterMode = flag.Bool("m", false, "run in master mode")
masterHost = flag.String("master-host", "127.0.0.1", "master host")
httpPort = flag.Int("http-port", 8081, "master's HTTP port")
tcpPort = flag.Int("tcp-port", 8082, "master's TCP port")
)
var (
gui *ui.UI
srv *server.Server
l *server.Listener
c *client.Client
clientsMessages = make(chan util.Message)
textBoxMessages = make(chan string)
newUsersCh = make(chan string)
)
// shutdown stops all the running services and terminates the process.
func shutdown() {
log.Println("Shutting down..")
if srv != nil {
srv.Stop()
}
if l != nil {
l.Stop()
}
if c != nil {
c.Close()
}
os.Exit(0)
}
func writeToChatView(msg util.Message) {
gui.WriteToView(ui.ChatView, fmt.Sprintf("[%s] %s: %s", util.ParseTime(msg.Timestamp), msg.User, msg.Contents))
}
func processClientsMessages() {
for msg := range clientsMessages {
writeToChatView(msg)
if *masterMode {
l.SendToClients(util.Command{
ID: "MSG",
OriginUser: msg.User,
Message: msg,
})
}
}
}
func processMyMessages() {
for msgStr := range textBoxMessages {
msg := util.Message{
User: *userName,
Contents: msgStr,
Timestamp: time.Now(),
}
writeToChatView(msg)
if *masterMode {
l.SendToClients(util.Command{
ID: "MSG",
OriginUser: *userName,
Message: msg,
})
} else {
client.SendMessageToMaster(msg, *masterHost, *httpPort)
}
}
}
func processNewUsers() {
for user := range newUsersCh {
processNewUser(user, false)
}
}
func processNewUser(newUser string, notifyClients bool) {
gui.WriteToView(ui.UsersView, newUser)
if notifyClients {
l.SendToClients(util.Command{
ID: "USER",
OriginUser: newUser,
})
}
}
func main() {
flag.Parse()
if *userName == "" {
log.Fatal("provide the username!")
}
var err error
gui, err = ui.DeployGUI(shutdown, textBoxMessages)
if err != nil {
log.Fatal(err)
}
if *masterMode {
// Create a new server with the desired parameters.
srv = server.New("/", *httpPort, clientsMessages)
// Start the server (initialize the TCP listener).
err = srv.Start()
if err != nil {
log.Fatal(err)
}
// Create a new TCP listener.
l, err = server.NewListener(*tcpPort, processNewUser, *userName)
if err != nil {
log.Fatal(err)
}
// Start the listener.
l.Start()
} else {
c, err = client.NewClient(*userName, *masterHost, *tcpPort, clientsMessages, newUsersCh)
if err != nil {
log.Fatal(err)
}
initUsers := c.ListenToMasterCommands()
for _, initUser := range initUsers {
if initUser != *userName {
processNewUser(initUser, false)
}
}
go processNewUsers()
}
// Writing the incoming data to the "clientsMessages" channel.
go processClientsMessages()
processNewUser(*userName, false)
go processMyMessages()
select {}
}