You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
2.8 KiB

  1. // Copyright 2011 Xing Xing <mikespook@gmail.com>.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT
  4. // license that can be found in the LICENSE file.
  5. package client
  6. import (
  7. "bytes"
  8. "bitbucket.org/mikespook/gearman-go/common"
  9. )
  10. // An error handler
  11. type ErrorHandler func(error)
  12. // Client side job
  13. type Job struct {
  14. Data []byte
  15. Handle, UniqueId string
  16. magicCode, DataType uint32
  17. }
  18. // Create a new job
  19. func newJob(magiccode, datatype uint32, data []byte) (job *Job) {
  20. return &Job{magicCode: magiccode,
  21. DataType: datatype,
  22. Data: data}
  23. }
  24. // Decode a job from byte slice
  25. func decodeJob(data []byte) (job *Job, err error) {
  26. if len(data) < 12 {
  27. return nil, common.Errorf("Invalid data: %V", data)
  28. }
  29. datatype := common.BytesToUint32([4]byte{data[4], data[5], data[6], data[7]})
  30. l := common.BytesToUint32([4]byte{data[8], data[9], data[10], data[11]})
  31. if len(data[12:]) != int(l) {
  32. return nil, common.Errorf("Invalid data: %V", data)
  33. }
  34. data = data[12:]
  35. return newJob(common.RES, datatype, data), nil
  36. }
  37. // Encode a job to byte slice
  38. func (job *Job) Encode() (data []byte) {
  39. l := len(job.Data) + 12
  40. data = make([]byte, l)
  41. magiccode := common.Uint32ToBytes(job.magicCode)
  42. datatype := common.Uint32ToBytes(job.DataType)
  43. datalength := common.Uint32ToBytes(uint32(l))
  44. for i := 0; i < l; i ++ {
  45. switch {
  46. case i < 4:
  47. data[i] = magiccode[i]
  48. case i < 8:
  49. data[i] = datatype[i - 4]
  50. case i < 12:
  51. data[i] = datalength[i - 8]
  52. default:
  53. data[i] = job.Data[i - 12]
  54. }
  55. }
  56. return
  57. }
  58. // Extract the job's result.
  59. func (job *Job) Result() (data []byte, err error) {
  60. switch job.DataType {
  61. case common.WORK_FAIL:
  62. job.Handle = string(job.Data)
  63. return nil, common.ErrWorkFail
  64. case common.WORK_EXCEPTION:
  65. err = common.ErrWorkException
  66. fallthrough
  67. case common.WORK_COMPLETE:
  68. s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
  69. if len(s) != 2 {
  70. return nil, common.Errorf("Invalid data: %V", job.Data)
  71. }
  72. job.Handle = string(s[0])
  73. data = s[1]
  74. default:
  75. err = common.ErrDataType
  76. }
  77. return
  78. }
  79. // Extract the job's update
  80. func (job *Job) Update() (data []byte, err error) {
  81. if job.DataType != common.WORK_DATA && job.DataType != common.WORK_WARNING {
  82. err = common.ErrDataType
  83. return
  84. }
  85. s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
  86. if len(s) != 2 {
  87. err = common.ErrInvalidData
  88. return
  89. }
  90. if job.DataType == common.WORK_WARNING {
  91. err = common.ErrWorkWarning
  92. }
  93. job.Handle = string(s[0])
  94. data = s[1]
  95. return
  96. }