/
export.go
127 lines (111 loc) · 3.25 KB
/
export.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
// Copyright (c) 2015 Joern Weissenborn
//
// This file is part of libaursir.
//
// Foobar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libaursir is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libaursir. If not, see <http://www.gnu.org/licenses/>.
package main
import "C"
import (
"fmt"
"github.com/joernweissenborn/aurarath/config"
"github.com/joernweissenborn/aurarath/messages"
"github.com/joernweissenborn/aurarath/servicedescriptor"
"github.com/joernweissenborn/aursir4go"
"github.com/joernweissenborn/eventual2go"
)
var nextExport = 0
func getNextExport() (n int) {
n = nextExport
nextExport++
return
}
var exports = map[int]*aursir4go.Export{}
func newExport(desc *servicedescriptor.ServiceDescriptor, cfg *config.Config) (n int) {
n = getNextExport()
e := aursir4go.NewExport(desc, cfg)
exports[n] = e
request[n] = map[string]*messages.Request{}
waiting_request[n] = []string{}
e.Requests().Listen(getRequest(n))
e.Run()
return
}
var request = map[int]map[string]*messages.Request{}
var waiting_request = map[int][]string{}
func getRequest(n int) eventual2go.Subscriber {
return func(d eventual2go.Data) {
r := d.(*messages.Request)
waiting_request[n] = append(waiting_request[n], r.UUID)
request[n][r.UUID] = r
}
}
//export NewExportYAML
func NewExportYAML(descYAML *C.char, address *C.char) (e C.int) {
fmt.Println(C.GoString(address))
desc := servicedescriptor.FromYaml(C.GoString(descYAML))
cfg := config.Default(C.GoString(address))
// cfg.Logger() = os.Stdout
return C.int(newExport(desc, cfg))
}
//export GetNextRequestId
func GetNextRequestId(e C.int) (uuid *C.char) {
if len(waiting_request[int(e)]) == 0 {
return
} else {
uuid = C.CString(waiting_request[int(e)][0])
waiting_request[int(e)] = waiting_request[int(e)][1:]
return
}
}
//export RetrieveRequestFunction
func RetrieveRequestFunction(e C.int, uuid *C.char) (function *C.char) {
r := request[int(e)][C.GoString(uuid)]
if r != nil {
function = C.CString(r.Function)
}
return
}
//export RetrieveRequestParams
func RetrieveRequestParams(e C.int, uuid *C.char) (parameter *C.char) {
r := request[int(e)][C.GoString(uuid)]
if r != nil {
parameter = C.CString(string(r.Parameter()))
}
return
}
//export Reply
func Reply(e C.int, uuid *C.char, parameter *C.char) C.int {
r := request[int(e)][C.GoString(uuid)]
exp := exports[int(e)]
if r != nil && exp != nil {
exp.ReplyEncoded(r, []byte(C.GoString(parameter)))
delete(request[int(e)], C.GoString(uuid))
}
return C.int(0)
}
//export Emit
func Emit(e C.int, function *C.char, inparameter *C.char, outparameter *C.char) C.int {
exp := exports[int(e)]
if exp != nil {
exp.EmitEncoded(
C.GoString(function),
[]byte(C.GoString(inparameter)),
[]byte(C.GoString(outparameter)),
)
}
return C.int(0)
}
func main() {
//NewExportYAML(new(C.char),new(C.char))
}