Exemplo n.º 1
0
// Encode a job to byte slice
func (job *Job) Encode() (data []byte) {
	var l int
	if job.DataType == common.WORK_FAIL {
		l = len(job.Handle)
	} else {
		l = len(job.Data)
		if job.Handle != "" {
			l += len(job.Handle) + 1
		}
	}
	data = make([]byte, 0, l+12)

	magiccode := common.Uint32ToBytes(job.magicCode)
	datatype := common.Uint32ToBytes(job.DataType)
	datalength := common.Uint32ToBytes(uint32(l))

	data = append(data, magiccode[:]...)
	data = append(data, datatype[:]...)
	data = append(data, datalength[:]...)
	if job.Handle != "" {
		data = append(data, []byte(job.Handle)...)
		if job.DataType != common.WORK_FAIL {
			data = append(data, 0)
		}
	}
	data = append(data, job.Data...)
	return
}
Exemplo n.º 2
0
// Encode a job to byte slice
func (job *Job) Encode() (data []byte) {
	l := len(job.Data)
	tl := l + 12
	data = make([]byte, tl)

	magiccode := common.Uint32ToBytes(job.magicCode)
	datatype := common.Uint32ToBytes(job.DataType)
	datalength := common.Uint32ToBytes(uint32(l))

	for i := 0; i < tl; i++ {
		switch {
		case i < 4:
			data[i] = magiccode[i]
		case i < 8:
			data[i] = datatype[i-4]
		case i < 12:
			data[i] = datalength[i-8]
		default:
			data[i] = job.Data[i-12]
		}
	}
	// Alternative
	/*
	   data = append(data, magiccode[:] ...)
	   data = append(data, datatype[:] ...)
	   data = append(data, datalength[:] ...)
	   data = append(data, job.Data ...)
	*/
	return
}
Exemplo n.º 3
0
// inner add function
func (worker *Worker) addFunc(funcname string, timeout uint32) {
	var datatype uint32
	var data []byte
	if timeout == 0 {
		datatype = common.CAN_DO
		data = []byte(funcname)
	} else {
		datatype = common.CAN_DO_TIMEOUT
		data = []byte(funcname + "\x00")
		t := common.Uint32ToBytes(timeout)
		data = append(data, t[:]...)
	}
	job := newJob(common.REQ, datatype, data)
	worker.broadcast(job)

}