-
Notifications
You must be signed in to change notification settings - Fork 191
/
main.go
155 lines (131 loc) · 3.93 KB
/
main.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
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
package main
// /* These comments before the import "C" are included in the C output. */
// #include <stdlib.h>
// typedef const char* Body;
// typedef int (*sendFunc)(int, int, Body);
// inline int invokeSendFunc(sendFunc send, int port, int reply, Body str) {
// return send(port, reply, str);
// }
import "C"
import (
"context"
"encoding/json"
"net/rpc"
"os"
"path/filepath"
log "github.com/tendermint/tendermint/libs/log"
gaia "github.com/Agoric/agoric-sdk/golang/cosmos/app"
"github.com/Agoric/agoric-sdk/golang/cosmos/daemon"
daemoncmd "github.com/Agoric/agoric-sdk/golang/cosmos/daemon/cmd"
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
)
type goReturn = struct {
str string
err error
}
const SwingSetPort = 123
var vmClientCodec *vm.ClientCodec
var agdServer *vm.AgdServer
// ConnectVMClientCodec creates an RPC client codec and a sender to the
// in-process implementation of the VM.
func ConnectVMClientCodec(ctx context.Context, nodePort int, sendFunc func(int, int, string)) (*vm.ClientCodec, daemoncmd.Sender) {
vmClientCodec = vm.NewClientCodec(ctx, sendFunc)
vmClient := rpc.NewClientWithCodec(vmClientCodec)
sendToNode := func(ctx context.Context, needReply bool, str string) (string, error) {
if str == "shutdown" {
return "", vmClientCodec.Close()
}
msg := vm.Message{
Port: nodePort,
NeedsReply: needReply,
Data: str,
}
var reply string
err := vmClient.Call(vm.ReceiveMessageMethod, msg, &reply)
return reply, err
}
return vmClientCodec, sendToNode
}
//export RunAgCosmosDaemon
func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char) C.int {
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
gaia.DefaultNodeHome = filepath.Join(userHomeDir, ".ag-chain-cosmos")
daemoncmd.AppName = "ag-chain-cosmos"
if err := os.Setenv(daemoncmd.EmbeddedVmEnvVar, "libdaemon"); err != nil {
panic(err)
}
var sendToNode daemoncmd.Sender
sendFunc := func(port int, reply int, str string) {
C.invokeSendFunc(toNode, C.int(port), C.int(reply), C.CString(str))
}
vmClientCodec, sendToNode = ConnectVMClientCodec(
context.Background(),
int(nodePort),
sendFunc,
)
args := make([]string, len(cosmosArgs))
for i, s := range cosmosArgs {
args[i] = C.GoString(s)
}
// fmt.Fprintln(os.Stderr, "Starting Cosmos", args)
os.Args = args
go func() {
// We run in the background, but exit when the job is over.
// swingset.SendToNode("hello from Initial Go!")
exitCode := 0
daemoncmd.OnStartHook = func(srv *vm.AgdServer, logger log.Logger, appOpts servertypes.AppOptions) error {
agdServer = srv
// We tried running start, which should never exit, so exit with non-zero
// code if we ever stop.
exitCode = 99
return nil
}
daemon.RunWithController(sendToNode)
// fmt.Fprintln(os.Stderr, "Shutting down Cosmos")
os.Exit(exitCode)
}()
// fmt.Fprintln(os.Stderr, "Done starting Cosmos")
return SwingSetPort
}
//export ReplyToGo
func ReplyToGo(replyPort C.int, isError C.int, resp C.Body) C.int {
respStr := C.GoString(resp)
// fmt.Printf("Reply to Go %d %s\n", replyPort, respStr)
if err := vmClientCodec.Receive(int(replyPort), int(isError) != 0, respStr); err != nil {
return C.int(1)
}
return C.int(0)
}
type errorWrapper struct {
Error string `json:"error"`
}
//export SendToGo
func SendToGo(port C.int, msg C.Body) C.Body {
msgStr := C.GoString(msg)
// fmt.Fprintln(os.Stderr, "Send to Go", msgStr)
var respStr string
message := &vm.Message{
Port: int(port),
NeedsReply: true,
Data: msgStr,
}
err := agdServer.ReceiveMessage(message, &respStr)
if err == nil {
return C.CString(respStr)
}
// fmt.Fprintln(os.Stderr, "Cannot receive from controller", err)
errResp := errorWrapper{
Error: err.Error(),
}
respBytes, err := json.Marshal(&errResp)
if err != nil {
panic(err)
}
return C.CString(string(respBytes))
}
// Do nothing in main.
func main() {}