-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
77 lines (70 loc) · 1.74 KB
/
socket.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
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func ConnectToPinit() net.Conn {
conn, err := net.Dial("tcp", "127.0.0.1:49001")
if err != nil {
fmt.Println(os.Args[0] + ": error: failed to connect to pinit")
return nil
}
response, err := bufio.NewReader(conn).ReadString('\n')
if strings.Split(response, ": ")[0] == "INFO" {
return conn
} else {
return nil
}
}
func StartService(conn net.Conn, service string) bool {
conn.Write([]byte("START " + service + "\n"))
response, err := bufio.NewReader(conn).ReadString('\n')
response = strings.Trim(response, "\n")
if err != nil {
fmt.Println(os.Args[0] + ": error: " + err.Error())
}
params := strings.Split(response, " ")
switch params[1] {
case "FAIL":
fmt.Println(os.Args[0] + ": start service failed")
return false
case "SUCCESS":
return true
default:
fmt.Println(os.Args[0] + ": unknown error")
return false
}
}
func StopService(conn net.Conn, service string) bool {
conn.Write([]byte("STOP " + service + "\n"))
response, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
fmt.Println(os.Args[0] + ": error: " + err.Error())
}
response = strings.Trim(response, "\n")
params := strings.Split(response, " ")
switch params[1] {
case "FAIL":
fmt.Println(os.Args[0] + ": stop service failed. Maybe, service already stopped")
return false
case "SUCCESS":
return true
default:
fmt.Println(os.Args[0] + ": unknown error")
return false
}
}
func SendPing(conn net.Conn) bool {
conn.Write([]byte("PING\n"))
response, err := bufio.NewReader(conn).ReadString('\n')
response = strings.Trim(response, "\n")
if err != nil {
fmt.Println(os.Args[0] + ": error: " + err.Error())
return false
}
fmt.Println("Response:", response)
return true
}