Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calling Fluency from C using GraalVM:SubstrateVM
$ javac -cp fluency-1.9.0-SNAPSHOT-shadow.jar org/komamitsu/fluencycaller/FluencyCaller.java
$ native-image --class-path fluency-1.9.0-SNAPSHOT-shadow.jar:. --shared -H:Name=libfluency --verbose
$ cc -Wall -I. -L. -lfluency main.c
$ ./a.out
package org.komamitsu.fluencycaller;
import org.graalvm.nativeimage.IsolateThread;
import org.graalvm.nativeimage.c.function.CEntryPoint;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.komamitsu.fluency.Fluency;
import java.io.IOException;
public class FluencyCaller
{
private static Fluency fluency;
@CEntryPoint(name = "init")
public static void init(IsolateThread isolateThread, CCharPointer host, int port)
throws IOException
{
if (fluency != null) {
throw new IllegalStateException("The Fluency instance is already created");
}
fluency = Fluency.defaultFluency(CTypeConversion.toJavaString(host), port);
}
@CEntryPoint(name = "emit")
public static void emit(IsolateThread isolateThread, CCharPointer tag, CCharPointer msgpackedData, int dataLen)
throws IOException
{
byte[] bytes = new byte[dataLen];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = msgpackedData.read(i);
}
fluency.emit(CTypeConversion.toJavaString(tag), bytes, 0, dataLen);
}
@CEntryPoint(name = "flush")
public static void flush(IsolateThread isolateThread)
throws IOException
{
if (fluency == null) {
throw new IllegalStateException("The Fluency instance isn't created");
}
fluency.flush();
}
@CEntryPoint(name = "close")
public static void close(IsolateThread isolateThread)
throws IOException
{
if (fluency == null) {
throw new IllegalStateException("The Fluency instance isn't created");
}
fluency.close();
}
}
#include <stdio.h>
#include <stdlib.h>
#include <libfluency.h>
int main(void)
{
int ret;
graal_create_isolate_params_t isolate_params;
graal_isolate_t* isolate;
ret = graal_create_isolate(&isolate_params, &isolate);
if (ret != 0) {
fprintf(stderr, "graal_create_isolate: %d\n", ret);
exit(1);
}
init(graal_current_thread(isolate), "127.0.0.1", 24224);
printf("init finished\n");
char data[] = {130, 164, 110, 97, 109, 101, 169, 107, 111, 109, 97, 109, 105, 116, 115, 117, 163, 97, 103, 101, 43};
emit(graal_current_thread(isolate), "foodb.bartbl", data, sizeof(data)/sizeof(data[0]));
printf("emit finished (size: %d)\n", sizeof(data)/sizeof(data[0]));
close(graal_current_thread(isolate));
printf("close finished\n");
ret = graal_tear_down_isolate(isolate);
if (ret != 0) {
fprintf(stderr, "graal_tear_down_isolate: %d\n", ret);
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment