Пример #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)
}
Пример #2
0
func processJob(db *sqlx.DB, sender func(msg proto.Message) error, rows grabber.RowOrRows) error {
	job, err := scanJob(rows)
	if err != nil {
		return err
	}

	result := tickets.PrintJob{
		JobId:     proto.Uint32(uint32(job.JobID)),
		Filename:  &job.Filename,
		Printer:   &job.Printer,
		Computer:  &tickets.Computer{Id: &job.ComputerID, Name: &job.ComputerName},
		Contest:   &tickets.IdName{Id: proto.Uint32(uint32(job.ContestID)), Name: &job.ContestName},
		Area:      &tickets.IdName{Id: proto.Uint32(uint32(job.AreaID)), Name: &job.AreaName},
		Charset:   proto.String("windows-1251"),
		Team:      &tickets.IdName{Id: proto.Uint32(uint32(job.TeamID)), Name: &job.SchoolName},
		Timestamp: proto.Uint64(uint64(job.Arrived.UnixNano()) / 1000),
	}

	if job.TeamNum.Valid && job.TeamNum.Int64 > 0 {
		result.Team.Name = proto.String(result.Team.GetName() + " #" + strconv.FormatInt(job.TeamNum.Int64, 10))
	}

	result.Data, err = tickets.NewBlob(job.Data)
	if err != nil {
		return err
	}

	if err = sender(&result); err != nil {
		return err
	}

	if _, err = db.Exec("Update PrintJobs set Printed = 255 where ID = ?", job.JobID); err != nil {
		return err
	}
	fmt.Printf("Printed job %d\n", job.JobID)
	return nil
}
Пример #3
0
func (s *server) processIncoming(conn *printserver.ServerConn, msg *stomp.Message) error {
	var job tickets.BinaryJob
	if err := proto.Unmarshal(msg.Body, &job); err != nil {
		log4go.Error("Received malformed job: %s", err)
		return err
	}

	jobDir := filepath.Join(s.Workdir, job.GetJobId())
	os.MkdirAll(jobDir, os.ModePerm) // err?

	sourceName := fmt.Sprintf("%s.tex", job.GetJobId())

	buf, err := job.GetData().Bytes()
	if err != nil {
		return err
	}
	if err = ioutil.WriteFile(filepath.Join(jobDir, sourceName), buf, os.ModePerm); err != nil {
		return err
	}

	cmd := exec.Command("latex", "-interaction=batchmode", sourceName)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		log.Printf("Latex error: %s\n", err)
		return nil
	}

	cmd = exec.Command("latex", "-interaction=batchmode", sourceName)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		log.Printf("Latex error: %s\n", err)
		return nil
	}

	dviName := fmt.Sprintf("%s.dvi", job.GetJobId())
	cmd = exec.Command("dvips", "-t", "a4", dviName)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		log.Printf("Dvips error: %s\n", err)
		return nil
	}

	psName := fmt.Sprintf("%s.ps", job.GetJobId())
	content, err := ioutil.ReadFile(filepath.Join(jobDir, psName))
	if err != nil {
		log.Printf("Where's my file? %s\n", err)
		return nil
	}

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

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

	content, err = proto.Marshal(&result)
	if err != nil {
		return err
	}

	return conn.Send(&result)
}
Пример #4
0
func (s *server) processIncoming(conn *printserver.ServerConn, msg *stomp.Message) error {
	var job tickets.PrintJob
	if err := proto.Unmarshal(msg.Body, &job); err != nil {
		log4go.Error("Received malformed job: %s", err)
		return err
	}

	jobId := "s-" + strconv.FormatUint(uint64(job.GetJobId()), 10)
	job.Team.Name = proto.String(texEscape(job.Team.GetName()))

	jobDir := filepath.Join(s.Workdir, jobId)
	os.MkdirAll(jobDir, os.ModePerm) // err?

	sourceLang := filepath.Ext(job.GetFilename())
	if sourceLang != "" {
		sourceLang = s.languages[sourceLang[1:]]
	}
	if sourceLang == "" {
		sourceLang = "txt"
	}

	sourceName := fmt.Sprintf("%s-source.%s", jobId, sourceLang)
	outputName := fmt.Sprintf("%s-hl.tex", jobId)
	styleName := fmt.Sprintf("%s-style.sty", jobId)

	buf, err := job.GetData().Bytes()
	if err != nil {
		return err
	}

	if err = ioutil.WriteFile(filepath.Join(jobDir, sourceName), buf, os.ModePerm); err != nil {
		return err
	}

	sourceCharset := job.GetCharset()
	if sourceCharset == "" {
		sourceCharset = "cp1251"
	}

	/*args := []string{"--out-format=latex",
		"--syntax=" + sourceLang,
		"--style=print",
		"--input=" + sourceName,
		"--output=" + outputName,
		"--fragment",
		"--replace-quotes",
		"--replace-tabs=4",
		"--wrap",
		"--wrap-no-numbers",
		"--encoding=" + sourceCharset,
		"--style-outfile=" + styleName}
	if sourceLang == "txt" {
		args = append(args, "--line-numbers")
	}
	*/
	args := []string{"-l", "text", "-f", "latex", "-O", "linenos=1,tabsize=4", "-o", outputName, sourceName}
	cmd := exec.Command("pygmentize", args...)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		return err
	}

	cmd = exec.Command("pygmentize", "-f", "latex", "-S", "bw")
	out, err := cmd.Output()
	if err != nil {
		return err
	}

	if err = ioutil.WriteFile(filepath.Join(jobDir, styleName), out, os.ModePerm); err != nil {
		return err
	}

	job.Filename = proto.String(texEscape(job.GetFilename()))
	data := templateData{
		PrintJob: &job,
	}

	var contentSource io.Reader
	if contentFile, err := os.Open(filepath.Join(jobDir, outputName)); err == nil {
		contentSource = contentFile
		defer contentFile.Close()
	} else {
		return err
	}

	contents, err := ioutil.ReadAll(contentSource)
	if err != nil {
		return err
	}

	data.IncludeText = string(contents)
	contents, err = ioutil.ReadFile(filepath.Join(jobDir, styleName))
	if err == nil && len(contents) != 0 {
		data.StyleText = string(contents)
	}

	var output bytes.Buffer

	if err = documentTemplate.Execute(&output, &data); err != nil {
		return err
	}

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

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

	return conn.Send(&result)
}