-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondlab.go
70 lines (59 loc) · 1.53 KB
/
secondlab.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
package main
import (
"fmt"
"github.com/paterson/secondlab/httpserver"
"github.com/paterson/secondlab/workermanager"
"net"
"os"
"strings"
)
const (
NUMBER_OF_WORKERS = 3
HELO_TEXT = "HELO "
KILL_SERVICE = "KILL_SERVICE\n"
)
func main() {
// Listen for incoming connection
listener, err := httpserver.Listen()
checkError(err)
jobs := make(chan workermanager.Job, 100)
workermanager.Start(NUMBER_OF_WORKERS, jobs)
defer listener.Close() // Close the listener when the application closes.
for {
connection, err := listener.Accept() // Accept incoming connection
checkError(err)
job := workermanager.Job{Action: handleRequest, Conn: connection}
jobs <- job
}
}
func handleRequest(connection net.Conn) {
message, err := httpserver.Read(connection)
checkError(err)
fmt.Println("Request Received:", message)
if strings.HasPrefix(message, HELO_TEXT) {
var suffix = message[len(HELO_TEXT):len(message)]
respondToHello(connection, suffix)
} else if message == KILL_SERVICE {
killService(connection)
} else {
doNothing(connection)
}
}
func killService(connection net.Conn) {
connection.Close()
os.Exit(0)
}
func respondToHello(connection net.Conn, message string) {
response := fmt.Sprintf("HELO %sIP:10.62.0.92\nPort:%s\nStudentID:12305503\n", message, httpserver.Port())
connection.Write([]byte(response))
connection.Close()
}
func doNothing(connection net.Conn) {
connection.Close()
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s\n", err.Error())
os.Exit(1)
}
}