-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (87 loc) · 2.46 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
package main
import (
"fmt"
"log"
"net"
"os"
"bufio"
"strings"
"github.com/Shoetan/server"
"github.com/Shoetan/utils"
)
func main() {
host := utils.GetEnv("HOST")
port := utils.GetEnv("PORT")
address := net.JoinHostPort(host, port)
if len(os.Args) < 2 {
fmt.Println("Expected 'start server', 'connect server', or 'send message' subcommands")
os.Exit(1)
}
command := strings.Join(os.Args[1:], " ") // get command from terminal
switch command {
case "start server":
var err error
listener, err := server.StartTcpServer("tcp", address)
if err != nil {
log.Fatalf("Could not start TCP server:%v", err.Error())
}else{
fmt.Printf("TCP server irunning on address 🌐 %v\n", address)
}
defer listener.Close()
// keep server running in and endless loop
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("Error accepting new connections 🛑 %v", err.Error())
continue
}
go utils.HandleConnection(conn)
}
case "connect server":
clientConnection, err := server.ConnectToTcpServer("tcp", address)
if err != nil {
fmt.Println("Could not connect to server")
}
if clientConnection != nil {
fmt.Println("Connected to TCP server ✅ :",clientConnection.RemoteAddr().String() )
utils.SaveConnectionDetails(clientConnection)
} else {
fmt.Println("Failed to establish connection 🛑")
}
exitChan := make(chan bool)
go func () {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("What would you like to do now that you are connected? 😁\n")
fmt.Printf("1. To send message to server: 1 <add message> 💬\n")
fmt.Printf("2. Exit server 🗑 \n")
fmt.Println("Enter choice ")
choice, _ := reader.ReadString('\n')
choice = strings.TrimSpace(choice)
parts := strings.Fields(choice) // seperate the input from the command line into parts
var message string
if len(parts) >= 2 {
choice = parts[0] // assign various parts into respective variables
message = parts[1]
}
switch choice {
case "1":
fmt.Println("You want to send a message 📁")
utils.SendMessage(clientConnection, []byte(message))
case "2":
fmt.Println("You want to exit the server 🗑")
exitChan <- true
return
}
}
}()
value:= <- exitChan
if value {
fmt.Println("Disconnected from the TCP server 😔")
clientConnection.Close()
}
default:
fmt.Println("Unknow command 😭", command)
os.Exit(1)
}
}