From 153066eb035fe96df7922c2c8922e8e0e6d32d34 Mon Sep 17 00:00:00 2001 From: mikespook Date: Tue, 15 Mar 2011 20:21:42 +0800 Subject: [PATCH] finished some worker's apis. --- src/pkg/gearman/Makefile | 19 ++++++++++ src/pkg/gearman/client.go | 6 ++++ src/pkg/gearman/gearman.go | 6 ++++ src/pkg/gearman/gearman_test.go | 13 +++++++ src/pkg/gearman/worker.go | 62 +++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 src/pkg/gearman/Makefile create mode 100644 src/pkg/gearman/client.go create mode 100644 src/pkg/gearman/gearman.go create mode 100644 src/pkg/gearman/gearman_test.go create mode 100644 src/pkg/gearman/worker.go diff --git a/src/pkg/gearman/Makefile b/src/pkg/gearman/Makefile new file mode 100644 index 0000000..147ef13 --- /dev/null +++ b/src/pkg/gearman/Makefile @@ -0,0 +1,19 @@ +# Copyright 2009 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +include $(GOROOT)/src/Make.inc + +TARG=gearman +CGOFILES=\ + gearman.go\ + worker.go\ + client.go\ + +CLEANFILES+=gearman_test + +include $(GOROOT)/src/Make.pkg + +%: install %.go + $(GC) $*.go + $(LD) -o $@ $*.$O diff --git a/src/pkg/gearman/client.go b/src/pkg/gearman/client.go new file mode 100644 index 0000000..6731e83 --- /dev/null +++ b/src/pkg/gearman/client.go @@ -0,0 +1,6 @@ +package gearman + +// #cgo: LDFLAGS: -lgearman +// #include + +import "C" diff --git a/src/pkg/gearman/gearman.go b/src/pkg/gearman/gearman.go new file mode 100644 index 0000000..6731e83 --- /dev/null +++ b/src/pkg/gearman/gearman.go @@ -0,0 +1,6 @@ +package gearman + +// #cgo: LDFLAGS: -lgearman +// #include + +import "C" diff --git a/src/pkg/gearman/gearman_test.go b/src/pkg/gearman/gearman_test.go new file mode 100644 index 0000000..e8c3007 --- /dev/null +++ b/src/pkg/gearman/gearman_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "gearman" + "fmt" +) + +func main() { + worker := gearman.GearmanWorkerCreate() + worker.AddServer("127.0.0.1", 4730) + worker.Work() + worker.Free() +} diff --git a/src/pkg/gearman/worker.go b/src/pkg/gearman/worker.go new file mode 100644 index 0000000..4d15871 --- /dev/null +++ b/src/pkg/gearman/worker.go @@ -0,0 +1,62 @@ +package gearman + +// #cgo LDFLAGS: -lgearman +// #include +import "C" + +import( + "log" + "unsafe" +) + +type GearmanWorker struct { + worker C.gearman_worker_st +} +// Create gearman worker +func GearmanWorkerCreate() * GearmanWorker { + + worker := new(GearmanWorker) + if C.gearman_worker_create(&worker.worker) == nil { + log.Panic("Memory allocation failure on worker creation") + } + return worker +} + +// get error +func (worker * GearmanWorker) Error() string { + return C.GoString(C.gearman_worker_error(&worker.worker)) +} + +// add server +func (worker * GearmanWorker) AddServer(host string, port uint16) { + h := C.CString(host) + defer C.free(unsafe.Pointer(h)) + if C.gearman_worker_add_server(&worker.worker, h, + C.in_port_t(port)) != C.GEARMAN_SUCCESS { + log.Panic(worker.Error()) + } +} + +// add function +func (worker * GearmanWorker) AddFunction(funcname string, timeout uint32, f interface{}, context interface{}) { + fn := C.CString(funcname) + defer C.free(unsafe.Pointer(fn)) + if C.gearman_worker_add_function(&worker.worker, fn, C.uint32_t(timeout), C.gearman_worker_fn(&unsafe.Pointer(&f)), unsafe.Pointer(&context)) != C.GEARMAN_SUCCESS { + log.Panic(worker.Error()) + } +} + +// work +func (worker * GearmanWorker) Work() { + for { + if C.gearman_worker_work(&worker.worker) != C.GEARMAN_SUCCESS { + log.Panic(worker.Error()) + } + } +} + +// free +// should used as defer +func (worker * GearmanWorker) Free() { + C.gearman_worker_free(&worker.worker) +}