Example #1
0
func processIncoming(conn *printserver.ServerConn, msg *stomp.Message) error {
	var job tickets.Ticket
	if err := proto.Unmarshal(msg.Body, &job); err != nil {
		log.Printf("Received malformed job: %s", err)
		return err
	}

	jobId := "t-" + strconv.FormatUint(uint64(job.GetSubmitId()), 10)
	job.Team.Name = proto.String(strings.Replace(job.Team.GetName(), "#", "\\#", -1))

	var buf bytes.Buffer
	if err := documentTemplate.Execute(&buf, &templateData{
		Ticket: &job,
	}); err != nil {
		log.Println(err)
		return err
	}

	cBlob, err := tickets.NewBlob(buf.Bytes())
	if err != nil {
		return err
	}

	result := tickets.BinaryJob{
		JobId:   &jobId,
		Printer: job.Printer,
		Data:    cBlob,
	}

	return conn.Send(&result)
}
Example #2
0
func processSubmit(db *sqlx.DB, sender func(msg proto.Message) error, rows grabber.RowOrRows) error {
	sub, err := scanSubmit(rows)
	if err != nil {
		return err
	}
	related, err := findRelatedSubmits(db, sub)
	if err != nil {
		return err
	}

	result := tickets.Ticket{
		SubmitId: proto.Uint32(uint32(sub.ID)),
		Printer:  &sub.Printer,
		Computer: &tickets.Computer{Id: &sub.ComputerID, Name: &sub.ComputerName},
		Area:     &tickets.IdName{Id: proto.Uint32(uint32(sub.AreaID)), Name: &sub.AreaName},
		Contest:  &tickets.IdName{Id: proto.Uint32(uint32(sub.Contest)), Name: &sub.ContestName},
		Problem:  &tickets.Ticket_Problem{Id: &sub.Task, Name: &sub.ProblemName},
	}

	teamName := sub.SchoolName
	if sub.TeamNum.Valid && sub.TeamNum.Int64 > 0 {
		teamName = teamName + " #" + strconv.FormatInt(sub.TeamNum.Int64, 10)
	}
	result.Team = &tickets.IdName{Id: proto.Uint32(uint32(sub.Team)), Name: &teamName}
	result.JudgeTime = proto.Uint64(uint64(sub.Touched.UnixNano() / 1000))

	result.Submit = make([]*tickets.Ticket_Submit, 0)
	result.Submit = append(result.Submit, createSubmit(db, sub, len(related)+1))
	for count := len(related); count > 0; {
		count -= 1
		result.Submit = append(result.Submit, createSubmit(db, related[count], count))
	}

	if err = sender(&result); err != nil {
		return err
	}
	if _, err = db.Exec("Update Submits set Printed = Touched where ID = ?", sub.ID); err != nil {
		return err
	}
	fmt.Printf("Printed submit %d\n", sub.ID)
	return nil
}