-
Notifications
You must be signed in to change notification settings - Fork 6
/
imageflow_job.go
140 lines (116 loc) · 2.82 KB
/
imageflow_job.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
// +build !windows
package imageflow
/*
#cgo LDFLAGS: -L./ -limageflow
#include "imageflow.h"
#include <stdlib.h>
*/
import "C"
import (
"errors"
"unsafe"
)
// job to perform a task in imageflow
type job struct {
inner *C.struct_imageflow_context
allocs []unsafe.Pointer
err bool
}
// CheckError is used to check if the context has error or not
func (job job) CheckError() bool {
if job.err {
return true
}
if job.inner == nil {
return true
}
val := C.imageflow_context_has_error(job.inner)
if val == C.bool(true) {
job.err = true
return true
}
return bool(val)
}
// AddInput add input to context
func (job *job) AddInput(id uint, b []byte) error {
if job.CheckError() {
return job.ReadError()
}
cb := C.CBytes(b)
job.allocs = append(job.allocs, cb)
result := C.imageflow_context_add_input_buffer(job.inner, C.int(id),
(*C.uchar)(cb), C.ulong(len(b)), C.imageflow_lifetime_lifetime_outlives_function_call)
if !bool(result) {
return job.ReadError()
}
return nil
}
// AddOutput add output to context
func (job *job) AddOutput(id uint) error {
if job.CheckError() {
return job.ReadError()
}
result := C.imageflow_context_add_output_buffer(job.inner, C.int(id))
if !bool(result) {
return job.ReadError()
}
return nil
}
// Message execute a command
func (job *job) Message(message []byte) error {
if job.CheckError() {
return job.ReadError()
}
jsize := C.ulong(len(message))
cs := C.CString("v1/execute")
cs_ptr := unsafe.Pointer(cs)
cb := C.CBytes(message)
cb_ptr := (*C.uchar)(cb)
job.allocs = append(job.allocs, cb, cs_ptr)
C.imageflow_context_send_json(job.inner, cs, cb_ptr, jsize)
if job.CheckError() {
return job.ReadError()
}
return nil
}
// New Create a context
func newJob() job {
v := C.imageflow_context_create(3, 0)
return job{inner: v}
}
// Frees the context and C allocations.
func (j *job) CleanUp() {
C.imageflow_context_destroy(j.inner)
for _, alloc := range j.allocs {
C.free(alloc)
}
}
// GetOutput from the context
func (job *job) GetOutput(id uint) ([]byte, error) {
if job.CheckError() {
return nil, job.ReadError()
}
size := C.size_t(unsafe.Sizeof(uintptr(0)))
cb := C.malloc(size) // cbytes
cb_ptr := (*C.uchar)(cb)
defer C.free(cb)
length := 0
length_ptr := (*C.ulong)(unsafe.Pointer(&length))
result := C.imageflow_context_get_output_buffer_by_id(
job.inner, C.int(id),
(&cb_ptr), length_ptr)
if !bool(result) {
return nil, job.ReadError()
}
return C.GoBytes((unsafe.Pointer)(cb_ptr), C.int(length)), nil
}
// ReadError from the context
func (job *job) ReadError() error {
l := 0
le := (*C.ulong)(unsafe.Pointer(&l))
byt := make([]byte, 512)
for !bool(C.imageflow_context_error_write_to_buffer(job.inner, (*C.char)(unsafe.Pointer(&byt[0])), C.ulong(len(byt)), le)) {
byt = make([]byte, len(byt)*2)
}
return errors.New(string(byt[0 : l-1]))
}