Added 'Do' function to the client.

This commit is contained in:
mikespook 2011-05-19 23:29:34 +08:00
parent 2f4eb65c85
commit c5b9f19962
3 changed files with 40 additions and 6 deletions

View File

@ -3,7 +3,7 @@ package gearman
import ( import (
"os" "os"
"net" "net"
"log" // "log"
) )
type Client struct { type Client struct {
@ -61,7 +61,34 @@ func (client *Client) work() {
} }
func (client *Client) Do(funcname string, data []byte, flag byte) (err os.Error) { func (client *Client) Do(funcname string, data []byte, flag byte) (err os.Error) {
return var datatype uint32
if flag & JOB_NORMAL == JOB_NORMAL {
if flag & JOB_BG == JOB_BG {
datatype = SUBMIT_JOB_BG
} else {
datatype = SUBMIT_JOB
}
} else if flag & JOB_LOW == JOB_LOW {
if flag & JOB_BG == JOB_BG {
datatype = SUBMIT_JOB_LOW_BG
} else {
datatype = SUBMIT_JOB_LOW
}
} else if flag & JOB_HIGH == JOB_HIGH {
if flag & JOB_BG == JOB_BG {
datatype = SUBMIT_JOB_HIGH_BG
} else {
datatype = SUBMIT_JOB_HIGH
}
}
rel := make([]byte, 0, 1024 * 64)
rel = append(rel, []byte(funcname) ...)
rel = append(rel, '\x00')
rel = append(rel, '\xFF')
rel = append(rel, '\x00')
rel = append(rel, data ...)
job := NewClientJob(REQ, datatype, data)
return client.WriteJob(job)
} }
func (client *Client) Echo(data []byte) (err os.Error) { func (client *Client) Echo(data []byte) (err os.Error) {
@ -78,7 +105,7 @@ func (client *Client) LastResult() (job *ClientJob) {
<-client.JobQueue <-client.JobQueue
} }
} }
return <-client.JobQueue return <-client.JobQueue
} }
func (client *Client) LastError() (err os.Error) { func (client *Client) LastError() (err os.Error) {
@ -98,7 +125,6 @@ func (client *Client) WriteJob(job *ClientJob) (err os.Error) {
} }
func (client *Client) Write(buf []byte) (err os.Error) { func (client *Client) Write(buf []byte) (err os.Error) {
log.Println(buf)
var n int var n int
for i := 0; i < len(buf); i += n { for i := 0; i < len(buf); i += n {
n, err = client.conn.Write(buf[i:]) n, err = client.conn.Write(buf[i:])

View File

@ -2,7 +2,7 @@ package gearman
import ( import (
"testing" "testing"
"os" // "os"
) )
var client *Client var client *Client
@ -24,6 +24,12 @@ func TestClientEcho(t * testing.T) {
} }
} }
func TestClientDo(t * testing.T) {
if err := client.Do("ToUpper", []byte("abcdef"), JOB_NORMAL); err != nil {
t.Error(err)
}
}
/* /*
func TestClientLastResult(t * testing.T) { func TestClientLastResult(t * testing.T) {
job := client.LastResult() job := client.LastResult()

View File

@ -78,7 +78,9 @@ func getError(data []byte) (eno os.Errno, err os.Error) {
err = os.NewError("The input is not a error data.") err = os.NewError("The input is not a error data.")
return return
} }
eno, err = rel[0], rel[1] l := len(rel[0])
eno = os.Errno(byteToUint32([4]byte{rel[0][l-4], rel[0][l-3], rel[0][l-2], rel[0][l - 1]}))
err = os.NewError(string(rel[1]))
return return
} }