-
Notifications
You must be signed in to change notification settings - Fork 113
/
Client.java
70 lines (61 loc) · 2.44 KB
/
Client.java
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
import com.sun.jna.*;
import java.util.*;
import java.lang.Long;
public class Client {
public interface Awesome extends Library {
// GoSlice class maps to:
// C type struct { void *data; GoInt len; GoInt cap; }
public class GoSlice extends Structure {
public static class ByValue extends GoSlice implements Structure.ByValue {}
public Pointer data;
public long len;
public long cap;
protected List getFieldOrder(){
return Arrays.asList(new String[]{"data","len","cap"});
}
}
// GoString class maps to:
// C type struct { const char *p; GoInt n; }
public class GoString extends Structure {
public static class ByValue extends GoString implements Structure.ByValue {}
public String p;
public long n;
protected List getFieldOrder(){
return Arrays.asList(new String[]{"p","n"});
}
}
// Foreign functions
public long Add(long a, long b);
public double Cosine(double val);
public void Sort(GoSlice.ByValue vals);
public long Log(GoString.ByValue str);
}
static public void main(String argv[]) {
Awesome awesome = (Awesome) Native.loadLibrary(
"./awesome.so", Awesome.class);
System.out.printf("awesome.Add(12, 99) = %s\n", awesome.Add(12, 99));
System.out.printf("awesome.Cosine(1.0) = %s\n", awesome.Cosine(1.0));
// Call Sort
// First, prepare data array
long[] nums = new long[]{53,11,5,2,88};
Memory arr = new Memory(nums.length * Native.getNativeSize(Long.TYPE));
arr.write(0, nums, 0, nums.length);
// fill in the GoSlice class for type mapping
Awesome.GoSlice.ByValue slice = new Awesome.GoSlice.ByValue();
slice.data = arr;
slice.len = nums.length;
slice.cap = nums.length;
awesome.Sort(slice);
System.out.print("awesome.Sort(53,11,5,2,88) = [");
long[] sorted = slice.data.getLongArray(0,nums.length);
for(int i = 0; i < sorted.length; i++){
System.out.print(sorted[i] + " ");
}
System.out.println("]");
// Call Log
Awesome.GoString.ByValue str = new Awesome.GoString.ByValue();
str.p = "Hello Java!";
str.n = str.p.length();
System.out.printf("msgid %d\n", awesome.Log(str));
}
}