go fmt & better examples
This commit is contained in:
parent
e701be9288
commit
87631cc2e5
@ -3,10 +3,10 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
// One client connect to one server.
|
||||
|
@ -61,7 +61,7 @@ func decodeResponse(data []byte) (resp *Response, l int, err error) {
|
||||
return
|
||||
}
|
||||
dl := int(binary.BigEndian.Uint32(data[8:12]))
|
||||
if a < minPacketLength + dl {
|
||||
if a < minPacketLength+dl {
|
||||
err = fmt.Errorf("Invalid data: %V", data)
|
||||
return
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var wg sync.WaitGroup
|
||||
// Set the autoinc id generator
|
||||
// You can write your own id generator
|
||||
// by implementing IdGenerator interface.
|
||||
@ -22,27 +21,58 @@ func main() {
|
||||
log.Println(e)
|
||||
}
|
||||
echo := []byte("Hello\x00 world")
|
||||
wg.Add(1)
|
||||
echomsg, err := c.Echo(echo)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
log.Println(string(echomsg))
|
||||
wg.Done()
|
||||
jobHandler := func(resp *client.Response) {
|
||||
log.Printf("%s", resp.Data)
|
||||
wg.Done()
|
||||
switch resp.DataType {
|
||||
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)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
wg.Add(1)
|
||||
status, err := c.Status(handle)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
@ -22,6 +22,16 @@ func ToUpperDelay10(job worker.Job) ([]byte, error) {
|
||||
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() {
|
||||
log.Println("Starting ...")
|
||||
defer log.Println("Shutdown complete!")
|
||||
@ -44,11 +54,12 @@ func main() {
|
||||
return nil
|
||||
}
|
||||
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("ToUpperTimeOut20", ToUpperDelay10, 20)
|
||||
w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately)
|
||||
w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately)
|
||||
w.AddFunc("SysInfo", worker.SysInfo, worker.Unlimited)
|
||||
w.AddFunc("MemInfo", worker.MemInfo, worker.Unlimited)
|
||||
if err := w.Ready(); err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
|
@ -1,11 +1,11 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
// The agent of job server.
|
||||
|
Loading…
Reference in New Issue
Block a user