Example #1
0
File: ab.go Project: Preetam/libab
// Destroy stops the Node and frees up all of its resources.
func (n *Node) Destroy() error {
	registeredNodesLock.Lock()
	defer registeredNodesLock.Unlock()
	if int(C.ab_destroy(n.ptr)) < 0 {
		return errors.New("ab: failed to destroy Node")
	}
	delete(registeredNodes, n.callbacksNum)
	return nil
}
Example #2
0
File: ab.go Project: Preetam/libab
// NewNode creates a new libab instance with the given ID, listen address, callback handler,
// and cluster size.
// The ID should be unique across the cluster.
// The listen address can be either an IPv4 or IPv6 address in the following forms:
// 	"127.0.0.1:2020"
// 	"[::1]:2020"
// The cluster size is the size of the entire cluster including this node.
func NewNode(id uint64,
	listen string,
	callbackHandler CallbackHandler,
	clusterSize int) (*Node, error) {

	// Create a new Go handle
	n := &Node{
		callbackHandler: callbackHandler,
	}

	// Set C callbacks
	cCallbacks := C.ab_callbacks_t{}
	C.set_callbacks(&cCallbacks)

	// Register the node
	registeredNodesLock.Lock()
	defer registeredNodesLock.Unlock()
	registrationCounter++
	registeredNodes[registrationCounter] = n
	n.callbacksNum = registrationCounter

	// Create the ab_node_t handle
	ptr := C.ab_node_create(C.uint64_t(id), C.int(clusterSize), cCallbacks,
		unsafe.Pointer(&n.callbacksNum))

	// Start listening
	listenStr := C.CString(listen)
	defer C.free(unsafe.Pointer(listenStr))
	ret := C.ab_listen(ptr, listenStr)
	if ret < 0 {
		C.ab_destroy(ptr)
		return nil, errors.New("ab: error listening on address")
	}
	n.ptr = ptr

	return n, nil
}