-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
78 lines (61 loc) · 1.54 KB
/
demo.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
package pvm_rpc
import (
"fmt"
"os"
"time"
"github.com/kxait/pvm-rpc/pvm"
)
func main() {
if len(os.Args) < 2 {
panic(":(")
}
_, err := pvm.Initsend(pvm.DataDefault)
if err != nil {
println("pvm.Initsend (pvm_initsend()) failed. this might indicate that you do not have pvm on your system or it is not running.")
panic(err)
}
mode := os.Args[1]
if mode == "client" {
hostname, _ := os.Hostname()
fmt.Printf("hello from child!\n")
parentId, err := pvm.Parent()
pvm.Initsend(pvm.DataDefault)
if err != nil {
panic(err)
}
target := Target{TaskId: parentId}
for {
time.Sleep(1 * time.Millisecond)
result := <-target.Call("ping", hostname)
if result.Err != nil {
panic(result.Err)
}
fmt.Printf("server responded: %s\n", result.Response.Content)
}
} else if mode == "server" {
pvm.CatchoutStdout()
result, err := pvm.Spawn("pvm_rpc", []string{"client"}, pvm.TaskDefault, "", 3)
if err != nil {
panic(err)
}
defer pvm.Kill(result.TIds[0])
fmt.Printf("child spawned with id %d\n", result.TIds[0])
server := RpcServer{Handlers: make(map[MessageType]RpcHandler)}
server.Handlers["ping"] = func(m *Message) (*Message, error) {
fmt.Printf("%s\n", m.Content)
return m.CreateResponse("pong"), nil
}
fmt.Printf("registered handler\n")
var erro error
var hadMessage bool = false
for erro == nil {
if !hadMessage {
time.Sleep(1 * time.Millisecond)
}
hadMessage, erro = server.StepEventLoop()
}
fmt.Printf("server stopped working: %s\n", erro)
} else {
panic(":(")
}
}