-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
62 lines (49 loc) · 1.25 KB
/
client.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
package main
import (
"bufio"
"fmt"
"gophertalk/cmd/client/terminal"
discovery2 "gophertalk/cmd/client/terminal/input/discovery"
"gophertalk/internal/conv"
"log"
"net"
"os"
)
const (
connTypeClient = "tcp"
)
var user *terminal.ConnectedUser
func main() {
msgDiscoveryChain := buildDiscovery()
conn := connectToServer()
user = terminal.DoLogin(conn)
go user.StartReadMessages()
terminal.PrintHelp()
user.StartInputListener(msgDiscoveryChain)
}
func connectToServer() net.Conn {
fmt.Println("Type the address server (Ex: 'localhost:8080'): ")
reader := bufio.NewReader(os.Stdin)
bytes, err := reader.ReadBytes('\n')
if err != nil {
log.Fatal(err)
}
server := conv.BytesToStringWithoutDelim(bytes)
fmt.Println("Connecting to " + connTypeClient + " server " + server)
conn, err := net.Dial(connTypeClient, server)
if err != nil {
fmt.Println("Error connecting:", err.Error())
os.Exit(1)
}
return conn
}
func buildDiscovery() discovery2.MessageDiscoverer {
help := &discovery2.HelpDiscoverer{}
listUsers := &discovery2.ListUsersDiscoverer{}
setUser := &discovery2.SetUserDiscoverer{}
messageServer := &discovery2.MessageServerDiscoverer{}
help.SetNext(listUsers)
listUsers.SetNext(setUser)
setUser.SetNext(messageServer)
return help
}