]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/jobs.go
Old magic numbers knowledge
[nncp.git] / src / jobs.go
index 37a31b1bcb2d6ec3eee58b17430e361cdd8c1b0d..e1344ebf65b8b21c33e136d45eeed4b1292f245b 100644 (file)
@@ -19,11 +19,13 @@ package nncp
 
 import (
        "bytes"
+       "fmt"
        "os"
        "path/filepath"
        "strings"
 
        xdr "github.com/davecgh/go-xdr/xdr2"
+       "github.com/dustin/go-humanize"
 )
 
 type TRxTx string
@@ -39,7 +41,7 @@ type Job struct {
        PktEnc   *PktEnc
        Path     string
        Size     int64
-       HshValue *[32]byte
+       HshValue *[MTHSize]byte
 }
 
 func (ctx *Ctx) HdrRead(fd *os.File) (*PktEnc, []byte, error) {
@@ -58,21 +60,29 @@ func (ctx *Ctx) HdrRead(fd *os.File) (*PktEnc, []byte, error) {
 func (ctx *Ctx) HdrWrite(pktEncRaw []byte, tgt string) error {
        tmpHdr, err := ctx.NewTmpFile()
        if err != nil {
-               ctx.LogE("hdr-write", []LE{}, err, "new")
+               ctx.LogE("hdr-write-tmp-new", nil, err, func(les LEs) string {
+                       return "Header writing: new temporary file"
+               })
                return err
        }
        if _, err = tmpHdr.Write(pktEncRaw); err != nil {
-               ctx.LogE("hdr-write", []LE{}, err, "write")
+               ctx.LogE("hdr-write-write", nil, err, func(les LEs) string {
+                       return "Header writing: writing"
+               })
                os.Remove(tmpHdr.Name())
                return err
        }
        if err = tmpHdr.Close(); err != nil {
-               ctx.LogE("hdr-write", []LE{}, err, "close")
+               ctx.LogE("hdr-write-close", nil, err, func(les LEs) string {
+                       return "Header writing: closing"
+               })
                os.Remove(tmpHdr.Name())
                return err
        }
        if err = os.Rename(tmpHdr.Name(), tgt+HdrSuffix); err != nil {
-               ctx.LogE("hdr-write", []LE{}, err, "rename")
+               ctx.LogE("hdr-write-rename", nil, err, func(les LEs) string {
+                       return "Header writing: renaming"
+               })
                return err
        }
        return err
@@ -129,16 +139,23 @@ func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock bool) chan Job {
                        }
                        pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
                        fd.Close()
-                       if err != nil || pktEnc.Magic != MagicNNCPEv4 {
+                       if err != nil || pktEnc.Magic != MagicNNCPEv5.B {
                                continue
                        }
-                       ctx.LogD("jobs", LEs{
+                       ctx.LogD("job", LEs{
                                {"XX", string(xx)},
                                {"Node", pktEnc.Sender},
                                {"Name", name},
                                {"Nice", int(pktEnc.Nice)},
                                {"Size", fi.Size()},
-                       }, "taken")
+                       }, func(les LEs) string {
+                               return fmt.Sprintf(
+                                       "Job %s/%s/%s nice: %s size: %s",
+                                       pktEnc.Sender, string(xx), name,
+                                       NicenessFmt(pktEnc.Nice),
+                                       humanize.IBytes(uint64(fi.Size())),
+                               )
+                       })
                        if !hdrExists && ctx.HdrUsage {
                                ctx.HdrWrite(pktEncRaw, pth)
                        }
@@ -146,7 +163,7 @@ func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock bool) chan Job {
                                PktEnc:   pktEnc,
                                Path:     pth,
                                Size:     fi.Size(),
-                               HshValue: new([32]byte),
+                               HshValue: new([MTHSize]byte),
                        }
                        copy(job.HshValue[:], hshValue)
                        jobs <- job