2 NNCP -- Node to Node copy
3 Copyright (C) 2016-2019 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "github.com/dustin/go-humanize"
30 func (ctx *Ctx) Humanize(s string) string {
31 s = strings.TrimRight(s, "\n")
32 splitted := strings.SplitN(s, " ", 4)
33 if len(splitted) != 4 {
37 if splitted[0] == "E" {
40 when, err := time.Parse(time.RFC3339Nano, splitted[1])
44 who := splitted[2][1:]
45 closingBracket := strings.LastIndex(splitted[3], "]")
46 if closingBracket == -1 {
49 rem := strings.Trim(splitted[3][closingBracket+1:], " ")
50 sds := make(map[string]string)
52 re := regexp.MustCompile(`\w+="[^"]+"`)
53 for _, pair := range re.FindAllString(splitted[3][:closingBracket+1], -1) {
54 sep := strings.Index(pair, "=")
55 sds[pair[:sep]] = pair[sep+2 : len(pair)-1]
59 node, err := ctx.FindNode(nodeS)
64 if sizeRaw, exists := sds["size"]; exists {
65 sp, err := strconv.ParseUint(sizeRaw, 10, 64)
69 size = humanize.IBytes(uint64(sp))
78 "File %s (%s) transfer to %s:%s: %s",
79 sds["src"], size, nodeS, sds["dst"], rem,
83 "File request from %s:%s to %s: %s",
84 nodeS, sds["src"], sds["dst"], rem,
88 "Exec to %s@%s (%s): %s",
89 nodeS, sds["dst"], size, rem,
93 "Transitional packet to %s (%s) (nice %s): %s",
94 nodeS, size, sds["nice"], rem,
99 if err, exists := sds["err"]; exists {
106 "Got exec from %s to %s (%s)",
107 nodeS, sds["dst"], size,
110 msg = fmt.Sprintf("Got file %s (%s) from %s", sds["dst"], size, nodeS)
112 msg = fmt.Sprintf("Got file request %s to %s", sds["src"], nodeS)
115 node, err := ctx.FindNode(nodeT)
120 "Got transitional packet from %s to %s (%s)",
126 if err, exists := sds["err"]; exists {
130 msg = fmt.Sprintf("Checking: %s/%s/%s", sds["node"], sds["xx"], sds["pkt"])
131 if err, exists := sds["err"]; exists {
132 msg += fmt.Sprintf(" %s", err)
137 msg = "Packet transfer, received from"
139 msg = "Packet transfer, sent to"
144 msg += " node " + nodeS
147 msg += fmt.Sprintf(" (%s)", size)
149 if err, exists := sds["err"]; exists {
157 msg = "Bundle transfer, received from"
159 msg = "Bundle transfer, sent to"
164 msg += " node " + nodeS
166 msg += " " + sds["pkt"]
168 msg += fmt.Sprintf(" (%s)", size)
170 if err, exists := sds["err"]; exists {
174 msg += "removing " + sds["file"]
176 msg = fmt.Sprintf("Connection to %s", nodeS)
177 if err, exists := sds["err"]; exists {
181 rx, err := strconv.ParseUint(sds["rxbytes"], 10, 64)
185 rxs, err := strconv.ParseUint(sds["rxspeed"], 10, 64)
189 tx, err := strconv.ParseUint(sds["txbytes"], 10, 64)
193 txs, err := strconv.ParseUint(sds["txspeed"], 10, 64)
198 "Finished call with %s: %s received (%s/sec), %s transferred (%s/sec)",
200 humanize.IBytes(uint64(rx)), humanize.IBytes(uint64(rxs)),
201 humanize.IBytes(uint64(tx)), humanize.IBytes(uint64(txs)),
206 if peer, exists := sds["peer"]; exists {
207 msg += fmt.Sprintf(": %s", peer)
210 nice, err := NicenessParse(sds["nice"])
214 msg += fmt.Sprintf("SP with %s (nice %s)", nodeS, NicenessFmt(nice))
219 if err, exists := sds["err"]; exists {
224 nice, err := NicenessParse(sds["nice"])
229 "Packet %s (%s) (nice %s)",
234 offsetParsed, err := strconv.ParseUint(sds["offset"], 10, 64)
238 sizeParsed, err := strconv.ParseUint(sds["size"], 10, 64)
242 msg += fmt.Sprintf(": %d%%", 100*offsetParsed/sizeParsed)
249 msg = fmt.Sprintf("%s has got for us: ", nodeS)
251 msg = fmt.Sprintf("We have got for %s: ", nodeS)
255 msg += fmt.Sprintf("%s packets, %s", sds["pkts"], size)
257 msg = fmt.Sprintf("%s has %s (%s): %s", nodeS, sds["pkt"], size, rem)
267 fullsize, err := strconv.ParseUint(sds["fullsize"], 10, 64)
271 sizeParsed, err := strconv.ParseUint(sds["size"], 10, 64)
278 100*sizeParsed/fullsize,
279 humanize.IBytes(uint64(sizeParsed)),
280 humanize.IBytes(uint64(fullsize)),
285 msg = fmt.Sprintf("Packet %s is retreived (%s)", sds["pkt"], size)
287 msg = fmt.Sprintf("Packet %s is sent", sds["pkt"])
292 chunkNum, exists := sds["chunk"]
295 "Reassembling chunked file \"%s\" (chunk %s): %s",
302 "Reassembling chunked file \"%s\": %s",
307 if err, exists := sds["err"]; exists {
311 msg = fmt.Sprintf("Acquire lock for %s: %s", sds["path"], sds["err"])
315 return fmt.Sprintf("%s %s%s", when.Format(time.RFC3339), level, msg)