-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.go
More file actions
169 lines (130 loc) · 4.34 KB
/
Client.go
File metadata and controls
169 lines (130 loc) · 4.34 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"time"
)
const (
TYPE = "tcp"
SERVER_ROUTER = "localhost:5556"
)
func printTransmissionMetrics() {
var total_time time.Duration = 0 * time.Second
fmt.Println("Transmission Rate Metrics: ")
for index, element := range transmissionTimes {
total_time = total_time + element
//fmt.Print("Transmission #[%v]: [%v]", index, element)
fmt.Print("Transmission #")
fmt.Print(index)
fmt.Print(": ")
fmt.Println(element)
}
fmt.Print("Average transmission time (in seconds): ")
fmt.Println(int(total_time) / 1000 / len(transmissionTimes))
return
}
var transmissionTimes = make([]time.Duration, 0) //List(slice) of transmission times
func main() {
//Create a buffer to interface with the os.Stdin InputStream
reader := bufio.NewScanner(os.Stdin)
//Print out a prompt to the client
fmt.Print("Server Router Address (leave empty for default): ")
//Block until the enter key is pressed, then read any new content into <text>
reader.Scan()
serverRouterAddress := reader.Text()
if len(serverRouterAddress) == 0 {
serverRouterAddress = SERVER_ROUTER
}
connection, err := net.Dial(TYPE, serverRouterAddress)
if err != nil {
fmt.Println("Failed to create connection to the server. Is the server listening?")
os.Exit(1)
}
//Defer closing the connection to the remote listener until this function's scope closes
defer connection.Close()
serverConnectionDetails := connection.RemoteAddr()
connectionIdStr := serverConnectionDetails.Network() + "://" + serverConnectionDetails.String()
fmt.Println("Connected to: " + connectionIdStr)
//Create a buffer to interface with the remote connection
connReader := bufio.NewScanner(connection)
connection.Write([]byte("CLIENT\n"))
connReader.Scan()
connReader.Text()
fmt.Println("Connected to ServerRouter")
var text string = ""
var useTerminal bool = true
fmt.Print("Enter path to file you would like to transmit (leave empty to enter custom text): ")
reader.Scan()
path := reader.Text()
message_split := []string{}
if len(path) != 0 {
text_array, _ := ioutil.ReadFile(path) // getting file
text = string(text_array) // setting to string
message_split = strings.Split(text, "\n") // splitting string at new line chars
useTerminal = false
}
if useTerminal {
for {
fmt.Print("Text to Send: ")
//Block until the enter key is pressed, then read any new content into <text>
reader.Scan()
text = reader.Text()
if len(text) > 0 {
fmt.Println("Sent to [" + connectionIdStr + "]: " + text)
//Use the Fprintf to send the inputted text to the remote connection
connection.Write([]byte(text + "\n"))
// getting time message was sent to compare with time reply was received
timeSent := time.Now()
if text != "EXIT" {
//Block until a newline character is received from the connection
connReader.Scan()
message := connReader.Text()
fmt.Println("Received from [" + connectionIdStr + "]: " + message)
if len(message) == 0 {
break
}
fmt.Println(message)
//Sdding transmission times to list (slice)
transmissionTimes = append(transmissionTimes, time.Since(timeSent))
} else {
printTransmissionMetrics()
break
}
}
}
} else {
for _, element := range message_split {
element = strings.Trim(element, "\n")
if len(element) > 0 {
fmt.Println("Sent to [" + connectionIdStr + "]: " + text)
//Use the Fprintf to send the inputted text to the remote connection
connection.Write([]byte(element + "\n"))
// getting time message was sent to compare with time reply was received
timeSent := time.Now()
if element != "EXIT" {
//Block until a newline character is received from the connection
connReader.Scan()
message := connReader.Text()
fmt.Println("Received from [" + connectionIdStr + "]: " + message)
if len(message) == 0 {
break
}
//Sdding transmission times to list (slice)
transmissionTimes = append(transmissionTimes, time.Since(timeSent))
//Print out the response to the console
//fmt.Println("Received from [" + connectionIdStr + "]: " + message)
} else {
break
}
}
}
printTransmissionMetrics()
//Notify the server that we've started
connection.Write([]byte("EXIT\n"))
time.Sleep(10000 * time.Millisecond)
}
}