2 NNCP -- Node to Node copy
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
28 "github.com/dustin/go-humanize"
31 func (ctx *Ctx) Humanize(s string) string {
32 s = strings.TrimRight(s, "\n")
33 splitted := strings.SplitN(s, " ", 4)
34 if len(splitted) != 4 {
38 if splitted[0] == "E" {
41 when, err := time.Parse(time.RFC3339Nano, splitted[1])
45 who := splitted[2][1:]
46 closingBracket := strings.LastIndex(splitted[3], "]")
47 if closingBracket == -1 {
50 rem := strings.Trim(splitted[3][closingBracket+1:], " ")
51 sds := make(map[string]string)
53 re := regexp.MustCompile(`\w+="[^"]+"`)
54 for _, pair := range re.FindAllString(splitted[3][:closingBracket+1], -1) {
55 sep := strings.Index(pair, "=")
56 sds[pair[:sep]] = pair[sep+2 : len(pair)-1]
60 node, err := ctx.FindNode(nodeS)
65 if sizeRaw, exists := sds["size"]; exists {
66 sp, err := strconv.ParseUint(sizeRaw, 10, 64)
70 size = humanize.IBytes(uint64(sp))
79 "File %s (%s) transfer to %s:%s: %s",
80 sds["src"], size, nodeS, sds["dst"], rem,
84 "File request from %s:%s to %s: %s",
85 nodeS, sds["src"], sds["dst"], rem,
89 "Mail to %s@%s (%s): %s",
90 nodeS, strings.Replace(sds["dst"], " ", ",", -1), size, rem,
94 "Transitional packet to %s (%s) (nice %s): %s",
95 nodeS, size, sds["nice"], rem,
100 if err, exists := sds["err"]; exists {
107 "Got mail from %s to %s (%s)",
108 nodeS, strings.Replace(sds["dst"], " ", ",", -1), size,
111 msg = fmt.Sprintf("Got file %s (%s) from %s", sds["dst"], size, nodeS)
113 msg = fmt.Sprintf("Got file request %s to %s", sds["src"], nodeS)
116 node, err := ctx.FindNode(nodeT)
121 "Got transitional packet from %s to %s (%s)",
127 if err, exists := sds["err"]; exists {
132 "Integrity check: %s/%s/%s %s",
133 sds["node"], sds["xx"], sds["pkt"], sds["err"],
138 msg = "Packet transfer, received from"
140 msg = "Packet transfer, sent to"
145 msg += " node " + nodeS
148 msg += fmt.Sprintf(" (%s)", size)
150 if err, exists := sds["err"]; exists {
154 msg = fmt.Sprintf("Daemon listening on %s", sds["bind"])
156 msg = fmt.Sprintf("Connected to %s", nodeS)
158 rx, err := strconv.ParseUint(sds["rxbytes"], 10, 64)
162 rxs, err := strconv.ParseUint(sds["rxspeed"], 10, 64)
166 tx, err := strconv.ParseUint(sds["txbytes"], 10, 64)
170 txs, err := strconv.ParseUint(sds["txspeed"], 10, 64)
175 "Finished call with %s: %s received (%s/sec), %s transferred (%s/sec)",
177 humanize.IBytes(uint64(rx)), humanize.IBytes(uint64(rxs)),
178 humanize.IBytes(uint64(tx)), humanize.IBytes(uint64(txs)),
183 msg = fmt.Sprintf("%s has got for us: ", nodeS)
185 msg = fmt.Sprintf("We have got for %s: ", nodeS)
189 msg += fmt.Sprintf("%s packets, %s", sds["pkts"], size)
199 fullsize, err := strconv.ParseUint(sds["fullsize"], 10, 64)
203 sizeParsed, err := strconv.ParseUint(sds["size"], 10, 64)
210 100*sizeParsed/fullsize,
211 humanize.IBytes(uint64(sizeParsed)),
212 humanize.IBytes(uint64(fullsize)),
217 msg = fmt.Sprintf("Packet %s is retreived (%s)", sds["hash"], size)
219 msg = fmt.Sprintf("Packet %s is sent", sds["hash"])
226 return fmt.Sprintf("%s %s%s", when.Format(time.RFC3339), level, msg)