go fmt & better examples

This commit is contained in:
Xing Xing 2014-01-09 17:58:02 +08:00
parent e701be9288
commit 87631cc2e5
9 changed files with 67 additions and 26 deletions

View File

@ -3,10 +3,10 @@
package client package client
import ( import (
"bufio"
"io" "io"
"net" "net"
"sync" "sync"
"bufio"
) )
// One client connect to one server. // One client connect to one server.

View File

@ -61,7 +61,7 @@ func decodeResponse(data []byte) (resp *Response, l int, err error) {
return return
} }
dl := int(binary.BigEndian.Uint32(data[8:12])) dl := int(binary.BigEndian.Uint32(data[8:12]))
if a < minPacketLength + dl { if a < minPacketLength+dl {
err = fmt.Errorf("Invalid data: %V", data) err = fmt.Errorf("Invalid data: %V", data)
return return
} }

View File

@ -7,7 +7,6 @@ import (
) )
func main() { func main() {
var wg sync.WaitGroup
// Set the autoinc id generator // Set the autoinc id generator
// You can write your own id generator // You can write your own id generator
// by implementing IdGenerator interface. // by implementing IdGenerator interface.
@ -22,27 +21,58 @@ func main() {
log.Println(e) log.Println(e)
} }
echo := []byte("Hello\x00 world") echo := []byte("Hello\x00 world")
wg.Add(1)
echomsg, err := c.Echo(echo) echomsg, err := c.Echo(echo)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
log.Println(string(echomsg)) log.Println(string(echomsg))
wg.Done()
jobHandler := func(resp *client.Response) { jobHandler := func(resp *client.Response) {
log.Printf("%s", resp.Data) switch resp.DataType {
wg.Done() case client.WorkException:
fallthrough
case client.WorkFail:
fallthrough
case client.WorkComplate:
if data, err := resp.Result(); err == nil {
log.Printf("RESULT: %V\n", data)
} else {
log.Printf("RESULT: %s\n", err)
}
case client.WorkWarning:
fallthrough
case client.WorkDate:
if data, err := resp.Update(); err == nil {
log.Printf("UPDATE: %V\n", data)
} else {
log.Printf("UPDATE: %V, %s\n", data, err)
}
case client.WorkStatus:
if data, err := resp.Status(); err == nil {
log.Printf("STATUS: %V\n", data)
} else {
log.Printf("STATUS: %s\n", err)
}
default:
log.Printf("UNKNOWN: %V", resp.Data)
}
} }
handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler) handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
wg.Add(1)
status, err := c.Status(handle) status, err := c.Status(handle)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
log.Printf("%t", status) log.Printf("%t", status)
wg.Wait() _, err = c.Do("Foobar", echo, client.JobNormal, jobHandler)
if err != nil {
log.Fatalln(err)
}
log.Println("Press Ctrl-C to exit ...")
var mutex sync.Mutex
mutex.Lock()
mutex.Lock()
} }

View File

@ -22,6 +22,16 @@ func ToUpperDelay10(job worker.Job) ([]byte, error) {
return data, nil return data, nil
} }
func Foobar(job worker.Job) ([]byte, error) {
log.Printf("Foobar: Data=[%s]\n", job.Data())
for i := 0; i < 10; i++ {
job.SendWarning([]byte{byte(i)})
job.SendData([]byte{byte(i)})
job.UpdateStatus(i+1, 100)
}
return job.Data(), nil
}
func main() { func main() {
log.Println("Starting ...") log.Println("Starting ...")
defer log.Println("Shutdown complete!") defer log.Println("Shutdown complete!")
@ -44,11 +54,12 @@ func main() {
return nil return nil
} }
w.AddServer("tcp4", "127.0.0.1:4730") w.AddServer("tcp4", "127.0.0.1:4730")
w.AddFunc("ToUpper", ToUpper, worker.Immediately) w.AddFunc("Foobar", Foobar, worker.Unlimited)
w.AddFunc("ToUpper", ToUpper, worker.Unlimited)
w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5) w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20) w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately) w.AddFunc("SysInfo", worker.SysInfo, worker.Unlimited)
w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately) w.AddFunc("MemInfo", worker.MemInfo, worker.Unlimited)
if err := w.Ready(); err != nil { if err := w.Ready(); err != nil {
log.Fatal(err) log.Fatal(err)
return return

View File

@ -1,11 +1,11 @@
package worker package worker
import ( import (
"bufio"
"io" "io"
"net" "net"
"strings" "strings"
"sync" "sync"
"bufio"
) )
// The agent of job server. // The agent of job server.