Refactoring the client api

This commit is contained in:
mikespook 2013-01-15 17:55:44 +08:00
parent dbc06bf540
commit c00234ba9a
3 changed files with 107 additions and 73 deletions

View File

@ -34,6 +34,13 @@ type Client struct {
in chan []byte in chan []byte
out chan *Job out chan *Job
created chan string
echo chan []byte
status chan *Status
jobhandlers map[string]JobHandler
conn net.Conn conn net.Conn
addr string addr string
ai *autoinc.AutoInc ai *autoinc.AutoInc
@ -46,7 +53,12 @@ type Client struct {
// client, err := client.New("127.0.0.1:4730") // client, err := client.New("127.0.0.1:4730")
func New(addr string) (client *Client, err error) { func New(addr string) (client *Client, err error) {
client = &Client{ client = &Client{
jobCreated: make(chan *Job), created: make(chan string, common.QUEUE_SIZE),
echo: make(chan []byte, common.QUEUE_SIZE),
status: make(chan *Status, common.QUEUE_SIZE),
jobhandlers: make(map[string]JobHandler, common.QUEUE_SIZE),
in: make(chan []byte, common.QUEUE_SIZE), in: make(chan []byte, common.QUEUE_SIZE),
out: make(chan *Job, common.QUEUE_SIZE), out: make(chan *Job, common.QUEUE_SIZE),
addr: addr, addr: addr,
@ -67,7 +79,7 @@ func New(addr string) (client *Client, err error) {
func (client *Client) connect() (err error) { func (client *Client) connect() (err error) {
client.mutex.Lock() client.mutex.Lock()
defer client.mutex.Unlock() defer client.mutex.Unlock()
client.conn, err = net.Dial(common.NETWORK, addr) client.conn, err = net.Dial(common.NETWORK, client.addr)
return return
} }
@ -220,7 +232,18 @@ func (client *Client) err (e error) {
// job handler // job handler
func (client *Client) handleJob(job *Job) { func (client *Client) handleJob(job *Job) {
if h, ok := client.jobhandlers[job.UniqueId]; ok {
h(job)
delete(client.jobhandlers, job.UniqueId)
}
}
func (client *Client) handleEcho(job *Job) {
client.echo <- job.Data
}
func (client *Client) handleCreated(job *Job) {
client.created <- string(job.Data)
} }
// status handler // status handler
@ -230,19 +253,22 @@ func (client *Client) handleStatus(job *Job) {
client.err(common.Errorf("Invalid data: %V", job.Data)) client.err(common.Errorf("Invalid data: %V", job.Data))
return return
} }
handle := string(data[0]) status := &Status{}
known := (data[1][0] == '1') status.Handle = string(data[0])
running := (data[2][0] == '1') status.Known = (data[1][0] == '1')
numerator, err := strconv.ParseUint(string(data[3][0]), 10, 0) status.Running = (data[2][0] == '1')
var err error
status.Numerator, err = strconv.ParseUint(string(data[3][0]), 10, 0)
if err != nil { if err != nil {
client.err(common.Errorf("Invalid handle: %s", data[3][0])) client.err(common.Errorf("Invalid handle: %s", data[3][0]))
return return
} }
denominator, err := strconv.ParseUint(string(data[4][0]), 10, 0) status.Denominator, err = strconv.ParseUint(string(data[4][0]), 10, 0)
if err != nil { if err != nil {
client.err(common.Errorf("Invalid handle: %s", data[4][0])) client.err(common.Errorf("Invalid handle: %s", data[4][0]))
return return
} }
client.status <- status
} }
// Send the job to job server. // Send the job to job server.
@ -251,7 +277,8 @@ func (client *Client) writeJob(job *Job) {
} }
// Internal do // Internal do
func (client *Client) do(funcname string, data []byte, flag uint32) (id int) { func (client *Client) do(funcname string, data []byte,
flag uint32) (id string, handle string) {
id = strconv.Itoa(int(client.ai.Id())) id = strconv.Itoa(int(client.ai.Id()))
l := len(funcname) + len(id) + len(data) + 2 l := len(funcname) + len(id) + len(data) + 2
rel := make([]byte, 0, l) rel := make([]byte, 0, l)
@ -261,6 +288,8 @@ func (client *Client) do(funcname string, data []byte, flag uint32) (id int) {
rel = append(rel, '\x00') // 1 Byte rel = append(rel, '\x00') // 1 Byte
rel = append(rel, data...) // len(data) rel = append(rel, data...) // len(data)
client.writeJob(newJob(common.REQ, flag, rel)) client.writeJob(newJob(common.REQ, flag, rel))
// Waiting for JOB_CREATED
handle = <-client.created
return return
} }
@ -272,8 +301,8 @@ func (client *Client) do(funcname string, data []byte, flag uint32) (id int) {
// flag set the job type, include running level: JOB_LOW, JOB_NORMAL, JOB_HIGH, // flag set the job type, include running level: JOB_LOW, JOB_NORMAL, JOB_HIGH,
// and if it is background job: JOB_BG. // and if it is background job: JOB_BG.
// JOB_LOW | JOB_BG means the job is running with low level in background. // JOB_LOW | JOB_BG means the job is running with low level in background.
func (client *Client) Do(funcname string, data []byte, flag byte) func (client *Client) Do(funcname string, data []byte,
(handle string, err error) { flag byte, jobhandler JobHandler) (handle string) {
var datatype uint32 var datatype uint32
switch flag { switch flag {
case JOB_LOW : case JOB_LOW :
@ -283,11 +312,16 @@ func (client *Client) Do(funcname string, data []byte, flag byte)
default: default:
datatype = common.SUBMIT_JOB datatype = common.SUBMIT_JOB
} }
client.do(funcname, data, datatype) var id string
id, handle = client.do(funcname, data, datatype)
if jobhandler != nil {
client.jobhandlers[id] = jobhandler
}
return
} }
func (client *Client) DoBg(funcname string, data []byte, flag byte) func (client *Client) DoBg(funcname string, data []byte,
(handle string, err error) { flag byte) (handle string) {
var datatype uint32 var datatype uint32
switch flag { switch flag {
case JOB_LOW : case JOB_LOW :
@ -297,25 +331,34 @@ func (client *Client) DoBg(funcname string, data []byte, flag byte)
default: default:
datatype = common.SUBMIT_JOB_BG datatype = common.SUBMIT_JOB_BG
} }
client.do(funcname, data, datatype) _, handle = client.do(funcname, data, datatype)
return
} }
// Get job status from job server. // Get job status from job server.
// !!!Not fully tested.!!! // !!!Not fully tested.!!!
func (client *Client) Status(handle string, handler StatusHandler) { func (client *Client) Status(handle string) (status *Status) {
client.writeJob(newJob(common.REQ, common.GET_STATUS, []byte(handle))) client.writeJob(newJob(common.REQ, common.GET_STATUS, []byte(handle)))
status = <-client.status
return
} }
// Send a something out, get the samething back. // Send a something out, get the samething back.
func (client *Client) Echo(data []byte, handler JobHandler) (echo []byte) { func (client *Client) Echo(data []byte) (r []byte) {
client.writeJob(newJob(common.REQ, common.ECHO_REQ, data)) client.writeJob(newJob(common.REQ, common.ECHO_REQ, data))
client.echo r = <-client.echo
return
} }
// Close // Close
func (client *Client) Close() (err error) { func (client *Client) Close() (err error) {
close(client.in) close(client.in)
close(client.out) close(client.out)
close(client.echo)
close(client.created)
close(client.status)
return client.conn.Close(); return client.conn.Close();
} }

View File

@ -15,22 +15,17 @@ func TestClientAddServer(t *testing.T) {
} }
func TestClientEcho(t *testing.T) { func TestClientEcho(t *testing.T) {
echoHandler = func(job *Job) { if echo := string(client.Echo([]byte("Hello world"))); echo == "Hello world" {
echo := string(job.Data)
if echo == "Hello world" {
t.Log(echo) t.Log(echo)
} else { } else {
t.Errorf("Invalid echo data: %s", job.Data) t.Errorf("Invalid echo data: %s", echo)
} }
return
}
client.Echo([]byte("Hello world"), echoHandler)
} }
func TestClientDoBg(t *testing.T) { func TestClientDoBg(t *testing.T) {
if handle, err := client.DoBg("ToUpper", []byte("abcdef"), if handle := client.DoBg("ToUpper", []byte("abcdef"),
JOB_LOW); err != nil { JOB_LOW); handle == "" {
t.Error(err) t.Error("Handle is empty.")
} else { } else {
t.Log(handle) t.Log(handle)
} }
@ -46,42 +41,31 @@ func TestClientDo(t *testing.T) {
} }
return return
} }
if handle, err := client.Do("ToUpper", []byte("abcdef"), if handle := client.Do("ToUpper", []byte("abcdef"),
JOB_LOW, jobHandler); err != nil { JOB_LOW, jobHandler); handle == "" {
t.Error(err) t.Error("Handle is empty.")
} else { } else {
t.Log(handle) t.Log(handle)
} }
} }
func TestClientStatus(t *testing.T) { func TestClientStatus(t *testing.T) {
statusHandler = func(handler string, known bool,
running bool, numerator uint64, denominator uint64) {
if known {
t.Errorf("The job (%s) shouldn't be known.", handler)
}
if running {
t.Errorf("The job (%s) shouldn't be running.", handler)
}
}
client.Status("handle not exists", statusHandler)
if handle, err := client.Do("Delay5sec", []byte("abcdef"), s1 := client.Status("handle not exists")
JOB_LOW, nil); err != nil { if s1.Known {
t.Error(err) t.Errorf("The job (%s) shouldn't be known.", s1.Handle)
} else { }
t.Log(handle) if s1.Running {
t.Errorf("The job (%s) shouldn't be running.", s1.Handle)
}
statusHandler = func(handler string, known bool, handle := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil);
running bool, numerator uint64, denominator uint64) { s2 := client.Status(handle)
if !known { if !s2.Known {
t.Errorf("The job (%s) shouldn be known.", handler) t.Errorf("The job (%s) should be known.", s2.Handle)
} }
if !running { if s2.Running {
t.Errorf("The job (%s) shouldn be running.", handler) t.Errorf("The job (%s) shouldn't be running.", s2.Handle)
}
}
client.Status(handle, statusHandler)
} }
} }

7
client/status.go Normal file
View File

@ -0,0 +1,7 @@
package client
type Status struct {
Handle string
Known, Running bool
Numerator, Denominator uint64
}