-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
173 lines (138 loc) · 3.55 KB
/
Copy pathclient.go
File metadata and controls
173 lines (138 loc) · 3.55 KB
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
170
171
172
173
package main
import (
"net"
"log"
"bufio"
"fmt"
"os"
"strings"
)
var userName string
func main() {
conn, err := net.Dial("tcp","localhost:9999")
if err!=nil {
log.Fatalf("Conection refused %v",err)
}
defer conn.Close()
setUserName(conn)
conn.Write([]byte("U/;"+userName))
showMenu()
go ServerMessageHanlder(conn)
for true {
InputHandler(conn)
}
}
func ServerMessageHanlder(conn net.Conn){
reader := bufio.NewReader(conn)
for true {
message, err := reader.ReadString('\n')
if err!=nil {
log.Fatal("Conection lost %v",err)
}
message = Decode(message)
fmt.Printf(" %v", message)
}
}
func InputHandler(conn net.Conn){
reader := bufio.NewReader(os.Stdin)
for true {
m, _ := reader.ReadString('\n')
option, args := parseInput(m)
if(option==""){
fmt.Printf("Please select an option. Remember: 0 shows the menu")
}else{
option = strings.Replace(option,"\n","",-1)
switch option {
//Show the menu
case "0":
showMenu()
//Create chatroom
case "1":
if(args==""){
fmt.Printf("Not Args found. Example: '1 NewChatRoom'")
}else{
//message := Encode(args)
conn.Write([]byte("C/;"+args))
}
//List chatroom
case "2":
//message := Encode(args)
conn.Write([]byte("L/;\n"))
//Join Existing chatroom
case "3":
if(args==""){
fmt.Println("Not Args found. Example: '3 ExistingChatRoom'")
}else{
//message := Encode(args)
conn.Write([]byte("J/;"+args))
}
//Send message
case "4":
if(args==""){
fmt.Println("Not Args found. Example: '4 hello everyone!'")
}else{
//message := Encode(args)
conn.Write([]byte("M/;"+args))
}
//Leave chatroom
case "5":
if(args==""){
fmt.Println("Not Args found. Example: '5 ExistingChatRoom'")
}else{
//message := Encode(args)
conn.Write([]byte("Q/;"+args))
}
default:
//conn.Write([]byte("M/;"+args))
}
}
}
}
func parseInput(m string)(string, string){
splitted := strings.SplitN(m," ",2)
if(len(splitted)>1){
return splitted[0],splitted[1]
}
if(len(splitted)==1){
return splitted[0],""
}
return "",""
}
func showMenu(){
fmt.Println("")
fmt.Println("--PLEASE SELECT THE DESIRED OPTION:\n")
fmt.Println(" 1. Create a chatroom. Args: Name")
fmt.Println(" 2. List chatrooms.")
fmt.Println(" 3. Join existing chatroom. Args: Name")
fmt.Println(" 4. Send Message to all joined chatrooms Args: Message")
fmt.Println(" 5. Quit chatroom. Args: Name")
fmt.Println(" 0. Show Menu")
fmt.Println("")
fmt.Println(" Example: '3 chatroom2'")
fmt.Println("")
fmt.Println("")
fmt.Println("No option sends a meesage to all joined chatrooms")
fmt.Println("")
}
func setUserName(conn net.Conn){
fmt.Println("Please set your username:")
reader := bufio.NewReader(os.Stdin)
m, _ := reader.ReadString('\n')
userName = m
}
func Decode(value string) (string) {
var ENCODING_UNENCODED_TOKENS = []string{"%", ":", "[", "]", ",", "\""}
var ENCODING_ENCODED_TOKENS = []string{"%25", "%3A", "%5B", "%5D", "%2C", "%22"}
return replace(ENCODING_ENCODED_TOKENS,ENCODING_UNENCODED_TOKENS, value)
}
func Encode(value string) (string) {
var ENCODING_UNENCODED_TOKENS = []string{"%", ":", "[", "]", ",", "\""}
var ENCODING_ENCODED_TOKENS = []string{"%25", "%3A", "%5B", "%5D", "%2C", "%22"}
return replace(ENCODING_UNENCODED_TOKENS, ENCODING_ENCODED_TOKENS, value)
}
func replace(fromTokens []string, toTokens []string, value string) (string) {
for i:=0; i<len(fromTokens); i++ {
value = strings.Replace(value, fromTokens[i], toTokens[i], -1)
}
return value;
}