func handleRequest(c net.Conn, message interface{}) { o.Debug("Request Recieved. Decoding!") ptr, ok := message.(*o.ProtoTaskRequest) if !ok { o.Assert("CC stuffed up - handleRequest got something that wasn't a ProtoTaskRequest.") } job := o.JobFromProto(ptr) /* search the registry for the job */ o.Debug("Request for Job.ID %d", job.Id) existing := o.JobGet(job.Id) if nil != existing { if existing.MyResponse.IsFinished() { o.Debug("job%d: Resending Response", job.Id) sendResponse(c, existing.MyResponse) } } else { // check to see if we have the score // add the Job to our Registry job.MyResponse = o.NewTaskResponse() job.MyResponse.Id = job.Id job.MyResponse.State = o.RESP_PENDING o.JobAdd(job) o.Info("Added New Job %d to our local registry", job.Id) // and then push it onto the pending job list so we know it needs actioning. appendPendingJob(job) } }
func handleAudienceRequest(c net.Conn) { defer c.Close() c.SetTimeout(0) r, _ := c.(io.Reader) w, _ := c.(io.Writer) dec := json.NewDecoder(r) enc := json.NewEncoder(w) outobj := new(GenericJsonRequest) err := dec.Decode(outobj) if err != nil { o.Warn("Error decoding JSON talking to audience: %s", err) return } if nil == outobj.Op { o.Warn("Malformed JSON message talking to audience. Missing Op") return } switch *(outobj.Op) { case "status": if nil == outobj.Id { o.Warn("Malformed Status message talking to audience. Missing Job ID") return } job := o.JobGet(*outobj.Id) jresp := new([2]interface{}) if nil != job { jresp[0] = "OK" iresp := NewJsonStatusResponse() switch job.State { case o.JOB_PENDING: iresp.Status = "PENDING" case o.JOB_SUCCESSFUL: iresp.Status = "OK" case o.JOB_FAILED_PARTIAL: iresp.Status = "PARTIAL_FAIL" case o.JOB_FAILED: iresp.Status = "FAIL" default: o.Fail("Blargh. %d is an unknown job state!", job.State) } resnames := o.JobGetResultNames(*outobj.Id) for i := range resnames { tr := o.JobGetResult(*outobj.Id, resnames[i]) if nil != tr { presp := NewJsonPlayerStatus() switch tr.State { case o.RESP_RUNNING: presp.Status = "PENDING" case o.RESP_FINISHED: presp.Status = "OK" case o.RESP_FAILED: presp.Status = "FAIL" case o.RESP_FAILED_UNKNOWN_SCORE: presp.Status = "UNK_SCORE" case o.RESP_FAILED_HOST_ERROR: presp.Status = "HOST_ERROR" case o.RESP_FAILED_UNKNOWN: presp.Status = "UNKNOWN_FAILURE" } for k, v := range tr.Response { presp.Response[k] = v } iresp.Players[resnames[i]] = presp } } jresp[1] = iresp } else { jresp[0] = "Error" jresp[1] = nil } enc.Encode(jresp) o.Debug("Status...") case "queue": if nil == outobj.Score { o.Warn("Malformed Queue message talking to audience. Missing Score") sendQueueFailureResponse("Missing Score", enc) return } if nil == outobj.Scope { o.Warn("Malformed Queue message talking to audience. Missing Scope") sendQueueFailureResponse("Missing Scope", enc) return } if nil == outobj.Players || len(outobj.Players) < 1 { o.Warn("Malformed Queue message talking to audience. Missing Players") sendQueueFailureResponse("Missing Players", enc) return } for _, player := range outobj.Players { if !HostAuthorised(player) { o.Warn("Malformed Queue message - unknown player %s specified.", player) sendQueueFailureResponse("Invalid Player", enc) return } } job := NewRequest() job.Score = *outobj.Score switch *outobj.Scope { case "one": job.Scope = o.SCOPE_ONEOF case "all": job.Scope = o.SCOPE_ALLOF default: sendQueueFailureResponse("Invalid Scope", enc) return } job.Players = outobj.Players job.Params = outobj.Params QueueJob(job) sendQueueSuccessResponse(job, enc) default: o.Warn("Unknown operation talking to audience: \"%s\"", *(outobj.Op)) return } _ = enc }