]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/jobs.go
ee7052d6d8d490228f12e512b031ca2af36ca495
[nncp.git] / src / cypherpunks.ru / nncp / jobs.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package nncp
20
21 import (
22         "io"
23         "os"
24         "path/filepath"
25         "strconv"
26
27         "github.com/davecgh/go-xdr/xdr2"
28 )
29
30 type TRxTx string
31
32 const (
33         TRx TRxTx = "rx"
34         TTx TRxTx = "tx"
35 )
36
37 type Job struct {
38         PktEnc   *PktEnc
39         Fd       *os.File
40         Size     int64
41         HshValue *[32]byte
42 }
43
44 func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
45         rxPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx))
46         jobs := make(chan Job, 16)
47         go func() {
48                 defer close(jobs)
49                 dir, err := os.Open(rxPath)
50                 if err != nil {
51                         return
52                 }
53                 fis, err := dir.Readdir(0)
54                 dir.Close()
55                 if err != nil {
56                         return
57                 }
58                 for _, fi := range fis {
59                         hshValue, err := FromBase32(fi.Name())
60                         if err != nil {
61                                 continue
62                         }
63                         fd, err := os.Open(filepath.Join(rxPath, fi.Name()))
64                         if err != nil {
65                                 continue
66                         }
67                         var pktEnc PktEnc
68                         if _, err = xdr.Unmarshal(fd, &pktEnc); err != nil || pktEnc.Magic != MagicNNCPEv4 {
69                                 fd.Close()
70                                 continue
71                         }
72                         fd.Seek(0, io.SeekStart)
73                         ctx.LogD("jobs", SDS{
74                                 "xx":   string(xx),
75                                 "node": pktEnc.Sender,
76                                 "name": fi.Name(),
77                                 "nice": strconv.Itoa(int(pktEnc.Nice)),
78                                 "size": strconv.FormatInt(fi.Size(), 10),
79                         }, "taken")
80                         job := Job{
81                                 PktEnc:   &pktEnc,
82                                 Fd:       fd,
83                                 Size:     fi.Size(),
84                                 HshValue: new([32]byte),
85                         }
86                         copy(job.HshValue[:], hshValue)
87                         jobs <- job
88                 }
89         }()
90         return jobs
91 }