Esempio n. 1
0
func (client *Client) runForever() {
	for {
		select {
		case <-client.closeChan:
			return
		default:
			C.replicant_client_block(client.ptr, 250)
			var loop_status C.enum_replicant_returncode
			client.mutex.Lock()
			reqid := int64(C.replicant_client_loop(client.ptr, 0, &loop_status))
			if reqid < 0 && loop_status == TIMEOUT {
				// pass
			} else if reqid < 0 && loop_status == NONE_PENDING {
				// pass
			} else if reqid < 0 && loop_status == INTERRUPTED {
				// pass
			} else if reqid < 0 {
				e := Error{Status(loop_status),
					C.GoString(C.replicant_client_error_message(client.ptr)),
					C.GoString(C.replicant_client_error_location(client.ptr))}
				client.errChan <- e
			} else if c, ok := (client.ops)[reqid]; ok {
				e := Error{Status(loop_status),
					C.GoString(C.replicant_client_error_message(client.ptr)),
					C.GoString(C.replicant_client_error_location(client.ptr))}
				c <- e
				delete(client.ops, reqid)
			}
			client.mutex.Unlock()
		}
	}
}
Esempio n. 2
0
func (client *Client) GenerateUniqueNumber() (num uint64, err *Error) {
	err = nil
	var c_status C.enum_replicant_returncode
	var c_num C.uint64_t
	done := make(chan Error)
	client.mutex.Lock()
	reqid := C.replicant_client_generate_unique_number(client.ptr, &c_status, &c_num)
	if reqid >= 0 {
		client.ops[int64(reqid)] = done
	} else {
		err = &Error{Status(c_status),
			C.GoString(C.replicant_client_error_message(client.ptr)),
			C.GoString(C.replicant_client_error_location(client.ptr))}
	}
	client.mutex.Unlock()
	if reqid >= 0 {
		rc := <-done
		num = uint64(c_num)
		if c_status != SUCCESS {
			err = &rc
			err.Status = Status(c_status)
		}
	}
	return
}
Esempio n. 3
0
func (client *Client) Call(obj string, fun string, input []byte, flags uint) (output []byte, err *Error) {
	output = nil
	err = &Error{SUCCESS, "success", ""}
	c_obj := C.CString(obj)
	defer C.free(unsafe.Pointer(c_obj))
	c_func := C.CString(fun)
	defer C.free(unsafe.Pointer(c_func))
	c_flags := C.unsigned(flags)
	var c_status C.enum_replicant_returncode
	var c_output *C.char = nil
	var c_output_sz C.size_t = 0
	done := make(chan Error)
	client.mutex.Lock()
	reqid := C.replicant_client_call(client.ptr, c_obj, c_func,
		(*C.char)(unsafe.Pointer(&input[0])), C.size_t(len(input)),
		c_flags, &c_status, &c_output, &c_output_sz)
	if reqid >= 0 {
		client.ops[int64(reqid)] = done
	} else {
		err = &Error{Status(c_status),
			C.GoString(C.replicant_client_error_message(client.ptr)),
			C.GoString(C.replicant_client_error_location(client.ptr))}
	}
	client.mutex.Unlock()
	if reqid >= 0 {
		rc := <-done
		if c_status == SUCCESS {
			if c_output_sz > 0 {
				output = C.GoBytes(unsafe.Pointer(c_output), C.int(c_output_sz))
			} else {
				output = make([]byte, 0)
			}
		} else {
			err = &rc
			err.Status = Status(c_status)
		}
	}
	return
}
Esempio n. 4
0
func (client *Client) Poke() (err *Error) {
	err = nil
	var c_status C.enum_replicant_returncode
	done := make(chan Error)
	client.mutex.Lock()
	reqid := C.replicant_client_poke(client.ptr, &c_status)
	if reqid >= 0 {
		client.ops[int64(reqid)] = done
	} else {
		err = &Error{Status(c_status),
			C.GoString(C.replicant_client_error_message(client.ptr)),
			C.GoString(C.replicant_client_error_location(client.ptr))}
	}
	client.mutex.Unlock()
	if reqid >= 0 {
		rc := <-done
		if c_status != SUCCESS {
			err = &rc
			err.Status = Status(c_status)
		}
	}
	return
}