reset changelog for debian package
[debian/orchestra.git] / src / player / task_response.go
1 // task_response.go
2 //
3 package main
4
5 import (
6         o "orchestra"
7 )
8
9 type TaskResponse struct {
10         id              uint64                          
11         State           ResponseState                   `json:"state"`
12         Response        map[string]string               `json:"response"`
13         // player only fields
14         RetryTime       int64                           `json:"retrytime"`
15 }
16
17 // Response related magic
18
19 func NewTaskResponse() (resp *TaskResponse) {
20         resp = new(TaskResponse)
21         resp.Response = make(map[string]string)
22
23         return resp
24 }
25
26 func (resp *TaskResponse) IsFinished() bool {
27         return resp.State.Finished()
28 }
29
30 func (resp *TaskResponse) DidFail() bool {
31         return resp.State.Failed()
32 }
33
34 func (resp *TaskResponse) CanRetry() bool {
35         return resp.State.CanRetry()
36 }
37
38
39 func ResponseFromProto(ptr *o.ProtoTaskResponse) (r *TaskResponse) {
40         r = new(TaskResponse)
41
42         switch (*(ptr.Status)) {
43         case o.ProtoTaskResponse_JOB_INPROGRESS:
44                 r.State = RESP_RUNNING
45         case o.ProtoTaskResponse_JOB_SUCCESS:
46                 r.State = RESP_FINISHED
47         case o.ProtoTaskResponse_JOB_FAILED:
48                 r.State = RESP_FAILED
49         case o.ProtoTaskResponse_JOB_HOST_FAILURE:
50                 r.State = RESP_FAILED_HOST_ERROR
51         case o.ProtoTaskResponse_JOB_UNKNOWN:
52                 r.State = RESP_FAILED_UNKNOWN_SCORE
53         case o.ProtoTaskResponse_JOB_UNKNOWN_FAILURE:
54                 fallthrough
55         default:
56                 r.State = RESP_FAILED_UNKNOWN
57         }
58
59         r.id = *(ptr.Id)
60         r.Response = o.MapFromProtoJobParameters(ptr.Response)
61
62         return r
63 }
64
65 func (resp *TaskResponse) Encode() (ptr *o.ProtoTaskResponse) {
66         ptr = new(o.ProtoTaskResponse)
67         
68         switch resp.State {
69         case RESP_RUNNING:
70                 ptr.Status = o.NewProtoTaskResponse_TaskStatus(o.ProtoTaskResponse_JOB_INPROGRESS)
71         case RESP_FINISHED:
72                 ptr.Status = o.NewProtoTaskResponse_TaskStatus(o.ProtoTaskResponse_JOB_SUCCESS)
73         case RESP_FAILED:
74                 ptr.Status = o.NewProtoTaskResponse_TaskStatus(o.ProtoTaskResponse_JOB_FAILED)
75         case RESP_FAILED_UNKNOWN_SCORE:
76                 ptr.Status = o.NewProtoTaskResponse_TaskStatus(o.ProtoTaskResponse_JOB_UNKNOWN)
77         case RESP_FAILED_HOST_ERROR:
78                 ptr.Status = o.NewProtoTaskResponse_TaskStatus(o.ProtoTaskResponse_JOB_HOST_FAILURE)
79         case RESP_FAILED_UNKNOWN:
80                 ptr.Status = o.NewProtoTaskResponse_TaskStatus(o.ProtoTaskResponse_JOB_UNKNOWN_FAILURE)
81         }
82         ptr.Id = new(uint64)
83         *ptr.Id = resp.id
84         ptr.Response = o.ProtoJobParametersFromMap(resp.Response)
85
86         return ptr
87 }
88