136 lines
3.1 KiB
Go
136 lines
3.1 KiB
Go
package main
|
|
|
|
/*
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <errno.h>
|
|
|
|
#include "client.h"
|
|
#include "netio.h"
|
|
#include "controller.capnp.h"
|
|
|
|
static int init_config(xce_client_config_t *config) {
|
|
config->nmasters = 1;
|
|
config->masters[0] = "localhost";
|
|
config->master_ports[0] = 9990;
|
|
config->from = "xce_apiserver";
|
|
config->key = "keepsecret";
|
|
return 0;
|
|
}
|
|
|
|
static xce_client_config_t config;
|
|
static struct {
|
|
xcecli_ctx_t ctx;
|
|
} xce_api_server_ctx;
|
|
|
|
static uintptr_t go_handler = 0;
|
|
|
|
extern int go_callback(uintptr_t, xce_request_t*, void *);
|
|
|
|
static int xce_apiserver_handler(xce_request_t *req, void *arg) {
|
|
int rc = -EINVAL;
|
|
|
|
if (go_handler != 0) {
|
|
rc = go_callback(go_handler, req, arg);
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
static int xce_apiserver_init(uintptr_t handler) {
|
|
int rc;
|
|
|
|
init_config(&config);
|
|
|
|
go_handler = handler;
|
|
|
|
rc = xcecli_ctx_init(&(xce_api_server_ctx.ctx), &config);
|
|
if (rc < 0) {
|
|
return rc;
|
|
}
|
|
|
|
xcecli_set_handler(xce_api_server_ctx.ctx, (xce_request_handler_t)xce_apiserver_handler, NULL);
|
|
|
|
rc = xcecli_start(xce_api_server_ctx.ctx);
|
|
if (rc < 0) {
|
|
return rc;
|
|
}
|
|
|
|
rc = xcecli_join(xce_api_server_ctx.ctx, HostRole_apiserver, NULL);
|
|
|
|
return rc;
|
|
}
|
|
|
|
static void* xce_request_body(xce_request_t *req) {
|
|
switch (req->body_which) {
|
|
case Request_body_configpushmsg:
|
|
return req->body.configpushmsg;
|
|
case Request_body_datamsg:
|
|
return req->body.datamsg;
|
|
default:
|
|
return NULL;
|
|
}
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
"runtime/cgo"
|
|
)
|
|
|
|
//export go_callback
|
|
func go_callback(h C.uintptr_t, req *C.xce_request_t, args unsafe.Pointer) C.int {
|
|
fn := cgo.Handle(h).Value().(func(*C.xce_request_t,unsafe.Pointer) C.int)
|
|
return fn(req, args);
|
|
}
|
|
|
|
func handle_config(config *C.xce_configdata_t) C.int {
|
|
switch (config.data_which) {
|
|
case C.ConfigData_data_clusterconfig:
|
|
fmt.Printf("cluster configuration received\n")
|
|
default:
|
|
//
|
|
fmt.Printf("unhandled config request %d\n", config.data_which)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func handle_dataset(dataset *C.dataset_t) C.int {
|
|
fmt.Printf("handle dataset table %d\n", dataset.table)
|
|
return 0
|
|
}
|
|
func go_req_handler(request *C.xce_request_t, arg unsafe.Pointer) C.int {
|
|
var from = C.GoString(request.from.name)
|
|
var body_which = request.body_which
|
|
|
|
fmt.Println("api server handler called")
|
|
fmt.Printf("opcode %d body_which %d from %s\n", request.opcode, body_which, from)
|
|
|
|
switch (request.opcode) {
|
|
case C.OPCode_push:
|
|
switch (body_which) {
|
|
case C.Request_body_configpushmsg:
|
|
// handle push config
|
|
return handle_config((*C.xce_configdata_t)(unsafe.Pointer(C.xce_request_body(request))))
|
|
case C.Request_body_datamsg:
|
|
// handle pushed data msg
|
|
return handle_dataset((*C.dataset_t)(unsafe.Pointer(C.xce_request_body(request))))
|
|
default:
|
|
// something wrong
|
|
}
|
|
default:
|
|
}
|
|
|
|
C.free_Request(request)
|
|
|
|
return 0
|
|
}
|
|
|
|
func main() {
|
|
h := cgo.NewHandle(go_req_handler)
|
|
rc := C.xce_apiserver_init(C.uintptr_t(h))
|
|
fmt.Printf("rc %d\n", rc);
|
|
h.Delete()
|
|
}
|