-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_system.go
88 lines (69 loc) · 1.6 KB
/
remote_system.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
package quacktors
import (
"errors"
"fmt"
"github.com/Azer0s/qpmd"
"net"
)
//TODO: log
//RemoteSystem is the struct representation of a system
//(i.e. a collection of PIDs that have been assigned a name)
//on a remote machine.
type RemoteSystem struct {
MachineId string
Address string
Port uint16
Machine *Machine
}
func (r *RemoteSystem) sayHello() error {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", r.Address, r.Port))
if err != nil {
return err
}
err = sendRequest(conn, qpmd.Request{
RequestType: qpmd.REQUEST_HELLO,
Data: make(map[string]interface{}),
})
if err != nil {
return err
}
res, err := readResponse(conn)
if err != nil {
return err
}
if res.ResponseType != qpmd.RESPONSE_OK {
return errors.New("remote system returned non okay result")
}
return nil
}
//Remote gets a remote PID by its handler name.
func (r *RemoteSystem) Remote(handlerName string) (*Pid, error) {
if !r.Machine.connected {
return nil, errors.New("remote machine is not connected")
}
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", r.Address, r.Port))
if err != nil {
return nil, err
}
err = sendRequest(conn, qpmd.Request{
RequestType: qpmd.REQUEST_LOOKUP,
Data: map[string]interface{}{
handler: handlerName,
},
})
if err != nil {
return nil, err
}
res, err := readResponse(conn)
if err != nil {
return nil, err
}
if res.ResponseType != qpmd.RESPONSE_OK {
return nil, errors.New("remote system returned non okay result")
}
pid, err := parsePid(res.Data[pidVal].(map[string]interface{}))
if err != nil {
return nil, err
}
return pid, nil
}