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 (
"os"
"net"
"log"
// "log"
)
type Client struct {
@ -61,7 +61,34 @@ func (client *Client) work() {
}
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) {
@ -98,7 +125,6 @@ func (client *Client) WriteJob(job *ClientJob) (err os.Error) {
}
func (client *Client) Write(buf []byte) (err os.Error) {
log.Println(buf)
var n int
for i := 0; i < len(buf); i += n {
n, err = client.conn.Write(buf[i:])

View File

@ -2,7 +2,7 @@ package gearman
import (
"testing"
"os"
// "os"
)
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) {
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.")
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
}