From 1750a098b980e0c4a7000ce76022e4acf35f7eca Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 10 Jun 2018 20:14:28 +0300 Subject: [PATCH] Humanized priorities --- doc/call.texi | 4 +- doc/cmds.texi | 4 +- doc/news.ru.texi | 8 ++ doc/news.texi | 8 ++ doc/niceness.texi | 32 +++++- src/cypherpunks.ru/nncp/cfg.go | 8 +- .../nncp/cmd/nncp-bundle/main.go | 8 +- src/cypherpunks.ru/nncp/cmd/nncp-call/main.go | 8 +- .../nncp/cmd/nncp-daemon/main.go | 8 +- src/cypherpunks.ru/nncp/cmd/nncp-exec/main.go | 16 +-- src/cypherpunks.ru/nncp/cmd/nncp-file/main.go | 8 +- src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go | 17 +-- src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go | 8 +- src/cypherpunks.ru/nncp/cmd/nncp-stat/main.go | 15 +-- src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go | 8 +- src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go | 8 +- src/cypherpunks.ru/nncp/nice.go | 108 ++++++++++++++++++ src/cypherpunks.ru/nncp/nice_test.go | 21 ++++ src/cypherpunks.ru/nncp/pkt.go | 4 - 19 files changed, 236 insertions(+), 65 deletions(-) create mode 100644 src/cypherpunks.ru/nncp/nice.go create mode 100644 src/cypherpunks.ru/nncp/nice_test.go diff --git a/doc/call.texi b/doc/call.texi index 63e4c79..edf2d70 100644 --- a/doc/call.texi +++ b/doc/call.texi @@ -10,12 +10,12 @@ calls: - cron: "*/1 * * * MON-FRI" onlinedeadline: 3600 - nice: 64 + nice: PRIORITY+10 - cron: "30 * * * SAT,SUN" onlinedeadline: 1800 maxonlinetime: 1750 - nice: 64 + nice: NORMAL rxrate: 10 txrate: 20 - diff --git a/doc/cmds.texi b/doc/cmds.texi index 49a4f1d..c5f4607 100644 --- a/doc/cmds.texi +++ b/doc/cmds.texi @@ -17,11 +17,9 @@ Nearly all commands have the following common options: will be 4 KiB (containing file itself and some junk). @item -nice Set desired outgoing packet @ref{Niceness, niceness level}. - 1-255 values are allowed. @item -replynice Set desired reply packet @ref{Niceness, niceness level}. Only freq - and exec packets look at that niceness level. 1-255 values are - allowed. + and exec packets look at that niceness level. @item -node Process only single specified node. @item -via diff --git a/doc/news.ru.texi b/doc/news.ru.texi index 4d0451b..b26a633 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -17,6 +17,14 @@ К средствам связанным с online-соединениями (@command{nncp-daemon}, @command{nncp-call}, @command{nncp-caller}) добавлен простой ограничитель скорости. +@item +Возможность задания приоритета символьными обозначениями: +@verb{|NORMAL|}, @verb{|BULK+10|}, @verb{|PRIORITY-5|}, итд. +@item +Изменены значения приоритетов по-умолчанию: +для @command{nncp-exec} с 64 на 96, +для @command{nncp-freq} с 64 на 160, +для @command{nncp-file} с 196 на 224. @end itemize @node Релиз 3.2 diff --git a/doc/news.texi b/doc/news.texi index 974dae9..af44898 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -17,6 +17,14 @@ queue its sending first, interrupting lower priority transmissions. @item Simple packet rate limiter added to online-related tools (@command{nncp-daemon}, @command{nncp-call}, @command{nncp-caller}). +@item +Ability to specify niceness with symbolic notation: +@verb{|NORMAL|}, @verb{|BULK+10|}, @verb{|PRIORITY-5|}, итд. +@item +Changed default niceness levels: +for @command{nncp-exec} from 64 to 96, +for @command{nncp-freq} from 64 to 160, +for @command{nncp-file} from 196 to 224. @end itemize @node Release 3.2 diff --git a/doc/niceness.texi b/doc/niceness.texi index faba50c..4686c09 100644 --- a/doc/niceness.texi +++ b/doc/niceness.texi @@ -6,10 +6,40 @@ command for controlling processes priority. Higher nicer level means that packet is "nicer" and allows other to bypass him -- that means lower transmission precedence. -Send big files with higher nicer level! That will guarantee you that +Send big files with higher nice level! That will guarantee you that higher priority packets, like mail messages, will pass first, even when lower priority packet was already been partially downloaded. There are default niceness levels built-in for @ref{nncp-exec}, @ref{nncp-file} and @ref{nncp-freq} commands. But pay attention that it can give information about underlying payload to the adversary! + +There are 1-255 niceness levels. They could be specified either as +integer, or using aliases with delta modifiers: + +@table @emph +@item FLASH (F) +Urgent priority. +@item PRIORITY (P) +High priority. Command execution/mail use that priority by default. +@item NORMAL (N) +Normal priority. File requests use that priority by default. +@item BULK (B) +Bundles shipped on a "least effort" basis. File transmission use that +priority by default. +@end table + +@verbatim + 1: F-31 65: P-31 129: N-31 193: B-31 + 2: F-30 66: P-30 130: N-30 194: B-30 + ... ... ... ... +32: F 96: P 160: N 224: B +33: F+1 97: P+1 161: N+1 225: B+1 +34: F+2 98: P+2 162: N+2 226: B+2 + ... ... ... ... +64: F+32 128: P+32 192: N+32 255: B+31 | MAX +@end verbatim + +Precedence could be specified both with single-letter aliases and with +whole strings. They are case insensitive. @emph{MAX} is an alias for 255 +niceness level. diff --git a/src/cypherpunks.ru/nncp/cfg.go b/src/cypherpunks.ru/nncp/cfg.go index 381f7de..75d12a5 100644 --- a/src/cypherpunks.ru/nncp/cfg.go +++ b/src/cypherpunks.ru/nncp/cfg.go @@ -67,7 +67,7 @@ type NodeYAML struct { type CallYAML struct { Cron string - Nice *int `yaml:"nice,omitempty"` + Nice *string `yaml:"nice,omitempty"` Xx string `yaml:"xx,omitempty"` RxRate *int `yaml:"rxrate,omitempty"` TxRate *int `yaml:"txrate,omitempty"` @@ -197,10 +197,10 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { nice := uint8(255) if callYml.Nice != nil { - if *callYml.Nice < 1 || *callYml.Nice > 255 { - return nil, errors.New("Nice must be between 1 and 255") + nice, err = NicenessParse(*callYml.Nice) + if err != nil { + return nil, err } - nice = uint8(*callYml.Nice) } var xx TRxTx diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-bundle/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-bundle/main.go index 1e5f8f2..86dc099 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-bundle/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-bundle/main.go @@ -55,7 +55,7 @@ func usage() { func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") - niceRaw = flag.Int("nice", 255, "Minimal required niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness") doRx = flag.Bool("rx", false, "Receive packets") doTx = flag.Bool("tx", false, "Transfer packets") doDelete = flag.Bool("delete", false, "Delete transferred packets") @@ -78,10 +78,10 @@ func main() { fmt.Println(nncp.VersionGet()) return } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) if *doRx && *doTx { log.Fatalln("-rx and -tx can not be set simultaneously") } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go index d304aea..d8adc60 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go @@ -40,7 +40,7 @@ func usage() { func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") - niceRaw = flag.Int("nice", 255, "Minimal required niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness") rxOnly = flag.Bool("rx", false, "Only receive packets") txOnly = flag.Bool("tx", false, "Only transmit packets") rxRate = flag.Int("rxrate", 0, "Maximal receive rate, pkts/sec") @@ -69,10 +69,10 @@ func main() { usage() os.Exit(1) } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) if *rxOnly && *txOnly { log.Fatalln("-rx and -tx can not be set simultaneously") } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go index 1ea30de..ba2d602 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go @@ -41,7 +41,7 @@ func usage() { func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") - niceRaw = flag.Int("nice", 255, "Minimal required niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness") bind = flag.String("bind", "[::]:5400", "Address to bind to") maxConn = flag.Int("maxconn", 128, "Maximal number of simultaneous connections") spoolPath = flag.String("spool", "", "Override path to spool") @@ -61,10 +61,10 @@ func main() { fmt.Println(nncp.VersionGet()) return } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug) if err != nil { diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-exec/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-exec/main.go index 3a5328d..0cf7810 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-exec/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-exec/main.go @@ -40,8 +40,8 @@ func usage() { func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") - niceRaw = flag.Int("nice", nncp.DefaultNiceExec, "Outbound packet niceness") - replyNiceRaw = flag.Int("replynice", nncp.DefaultNiceFile, "Possible reply packet niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceExec), "Outbound packet niceness") + replyNiceRaw = flag.String("replynice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Possible reply packet niceness") minSize = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB") viaOverride = flag.String("via", "", "Override Via path to destination node") spoolPath = flag.String("spool", "", "Override path to spool") @@ -65,14 +65,14 @@ func main() { usage() os.Exit(1) } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) - if *replyNiceRaw < 1 || *replyNiceRaw > 255 { - log.Fatalln("-replynice must be between 1 and 255") + replyNice, err := nncp.NicenessParse(*replyNiceRaw) + if err != nil { + log.Fatalln(err) } - replyNice := uint8(*replyNiceRaw) ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug) if err != nil { diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go index e19eb93..6e7eaf5 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-file/main.go @@ -45,7 +45,7 @@ options by default. You can forcefully turn them off by specifying 0 value. func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") - niceRaw = flag.Int("nice", nncp.DefaultNiceFile, "Outbound packet niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Outbound packet niceness") argMinSize = flag.Int64("minsize", -1, "Minimal required resulting packet size, in KiB") argChunkSize = flag.Int64("chunked", -1, "Split file on specified size chunks, in KiB") viaOverride = flag.String("via", "", "Override Via path to destination node") @@ -70,10 +70,10 @@ func main() { usage() os.Exit(1) } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug) if err != nil { diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go index 1c8e248..14f2fea 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-freq/main.go @@ -25,6 +25,7 @@ import ( "log" "os" "path/filepath" + "strconv" "strings" "cypherpunks.ru/nncp" @@ -40,8 +41,8 @@ func usage() { func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") - niceRaw = flag.Int("nice", nncp.DefaultNiceFreq, "Outbound packet niceness") - replyNiceRaw = flag.Int("replynice", nncp.DefaultNiceFile, "Reply file packet niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFreq), "Outbound packet niceness") + replyNiceRaw = flag.String("replynice", strconv.Itoa(nncp.DefaultNiceFile), "Reply file packet niceness") minSize = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB") viaOverride = flag.String("via", "", "Override Via path to destination node") spoolPath = flag.String("spool", "", "Override path to spool") @@ -65,14 +66,14 @@ func main() { usage() os.Exit(1) } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) - if *replyNiceRaw < 1 || *replyNiceRaw > 255 { - log.Fatalln("-replynice must be between 1 and 255") + replyNice, err := nncp.NicenessParse(*replyNiceRaw) + if err != nil { + log.Fatalln(err) } - replyNice := uint8(*replyNiceRaw) ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug) if err != nil { diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go index f4de406..ff77eb4 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-pkt/main.go @@ -114,8 +114,8 @@ func main() { path = string(pkt.Path[:pkt.PathLen]) } fmt.Printf( - "Packet type: plain\nPayload type: %s\nNiceness: %d\nPath: %s\n", - payloadType, pkt.Nice, path, + "Packet type: plain\nPayload type: %s\nNiceness: %s (%d)\nPath: %s\n", + payloadType, nncp.NicenessFmt(pkt.Nice), pkt.Nice, path, ) return } @@ -148,8 +148,8 @@ func main() { return } fmt.Printf( - "Packet type: encrypted\nNiceness: %d\nSender: %s\nRecipient: %s\n", - pktEnc.Nice, pktEnc.Sender, pktEnc.Recipient, + "Packet type: encrypted\nNiceness: %s (%d)\nSender: %s\nRecipient: %s\n", + nncp.NicenessFmt(pktEnc.Nice), pktEnc.Nice, pktEnc.Sender, pktEnc.Recipient, ) return } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-stat/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-stat/main.go index 94a0bb1..fcc872f 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-stat/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-stat/main.go @@ -99,18 +99,19 @@ func main() { txBytes[job.PktEnc.Nice] = txBytes[job.PktEnc.Nice] + job.Size } fmt.Println(node.Name) - for nice := 0; nice < 256; nice++ { - rxNum, rxExists := rxNums[uint8(nice)] - txNum, txExists := txNums[uint8(nice)] + var nice uint8 + for nice = 1; nice > 0; nice++ { + rxNum, rxExists := rxNums[nice] + txNum, txExists := txNums[nice] if !(rxExists || txExists) { continue } fmt.Printf( - "\tnice:% 3d | Rx: % 10s, % 3d pkts | Tx: % 10s, % 3d pkts\n", - nice, - humanize.IBytes(uint64(rxBytes[uint8(nice)])), + "\tnice:% 4s | Rx: % 10s, % 3d pkts | Tx: % 10s, % 3d pkts\n", + nncp.NicenessFmt(nice), + humanize.IBytes(uint64(rxBytes[nice])), rxNum, - humanize.IBytes(uint64(txBytes[uint8(nice)])), + humanize.IBytes(uint64(txBytes[nice])), txNum, ) } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go index cc3ca67..bb4a586 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go @@ -40,7 +40,7 @@ func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") nodeRaw = flag.String("node", "", "Process only that node") - niceRaw = flag.Int("nice", 255, "Minimal required niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness") dryRun = flag.Bool("dryrun", false, "Do not actually write any tossed data") doSeen = flag.Bool("seen", false, "Create .seen files") cycle = flag.Uint("cycle", 0, "Repeat tossing after N seconds in infinite loop") @@ -65,10 +65,10 @@ func main() { fmt.Println(nncp.VersionGet()) return } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug) if err != nil { diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go index 15e4843..01fe150 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go @@ -45,7 +45,7 @@ func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") nodeRaw = flag.String("node", "", "Process only that node") - niceRaw = flag.Int("nice", 255, "Minimal required niceness") + niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness") rxOnly = flag.Bool("rx", false, "Only receive packets") txOnly = flag.Bool("tx", false, "Only transfer packets") mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories") @@ -71,10 +71,10 @@ func main() { usage() os.Exit(1) } - if *niceRaw < 1 || *niceRaw > 255 { - log.Fatalln("-nice must be between 1 and 255") + nice, err := nncp.NicenessParse(*niceRaw) + if err != nil { + log.Fatalln(err) } - nice := uint8(*niceRaw) if *rxOnly && *txOnly { log.Fatalln("-rx and -tx can not be set simultaneously") } diff --git a/src/cypherpunks.ru/nncp/nice.go b/src/cypherpunks.ru/nncp/nice.go new file mode 100644 index 0000000..eceb6fc --- /dev/null +++ b/src/cypherpunks.ru/nncp/nice.go @@ -0,0 +1,108 @@ +package nncp + +import ( + "errors" + "fmt" + "regexp" + "strconv" + "strings" +) + +const ( + NiceFlash = 32 + NicePriority = 96 + NiceNormal = 160 + NiceBulk = 224 + + DefaultNiceExec = NicePriority + DefaultNiceFreq = NiceNormal + DefaultNiceFile = NiceBulk +) + +var ( + niceRe *regexp.Regexp = regexp.MustCompile(`^(\w+)([-+])(\d+)$`) + niceAliases map[string]uint8 = map[string]uint8{ + "flash": NiceFlash, + "f": NiceFlash, + "priority": NicePriority, + "p": NicePriority, + "normal": NiceNormal, + "n": NiceNormal, + "bulk": NiceBulk, + "b": NiceBulk, + "max": 255, + } +) + +func NicenessParse(s string) (uint8, error) { + if nice, err := strconv.Atoi(s); err == nil { + if nice <= 0 || nice > 255 { + return 0, errors.New("nice out of bounds") + } + return uint8(nice), nil + } + s = strings.ToLower(s) + var baseNice uint8 + var found bool + if baseNice, found = niceAliases[s]; found { + return baseNice, nil + } + matches := niceRe.FindStringSubmatch(s) + if len(matches) != 1+3 { + return 0, errors.New("invalid niceness") + } + baseNice, found = niceAliases[matches[1]] + if !found { + return 0, errors.New("invalid niceness") + } + delta, err := strconv.Atoi(matches[3]) + if err != nil { + return 0, err + } + if matches[2] == "-" { + if delta > 31 { + return 0, errors.New("too big niceness delta") + } + return baseNice - uint8(delta), nil + } else { + if delta > 32 || (baseNice == NiceBulk && delta > 31) { + return 0, errors.New("too big niceness delta") + } + return baseNice + uint8(delta), nil + } +} + +func NicenessFmt(nice uint8) string { + switch { + case nice == 255: + return "MAX" + case NiceFlash-31 < nice && nice < NiceFlash: + return fmt.Sprintf("F-%d", NiceFlash-nice) + case nice == NiceFlash: + return "F" + case NiceFlash < nice && nice <= (NiceFlash+32): + return fmt.Sprintf("F+%d", nice-NiceFlash) + + case NicePriority-31 < nice && nice < NicePriority: + return fmt.Sprintf("P-%d", NicePriority-nice) + case nice == NicePriority: + return "P" + case NicePriority < nice && nice <= (NicePriority+32): + return fmt.Sprintf("P+%d", nice-NicePriority) + + case NiceNormal-31 < nice && nice < NiceNormal: + return fmt.Sprintf("N-%d", NiceNormal-nice) + case nice == NiceNormal: + return "N" + case NiceNormal < nice && nice <= (NiceNormal+32): + return fmt.Sprintf("N+%d", nice-NiceNormal) + + case NiceBulk-31 < nice && nice < NiceBulk: + return fmt.Sprintf("B-%d", NiceBulk-nice) + case nice == NiceBulk: + return "B" + case NiceBulk < nice && nice <= (NiceBulk+30): + return fmt.Sprintf("B+%d", nice-NiceBulk) + } + return strconv.Itoa(int(nice)) +} diff --git a/src/cypherpunks.ru/nncp/nice_test.go b/src/cypherpunks.ru/nncp/nice_test.go new file mode 100644 index 0000000..905cec3 --- /dev/null +++ b/src/cypherpunks.ru/nncp/nice_test.go @@ -0,0 +1,21 @@ +package nncp + +import ( + "strings" + "testing" +) + +func TestNiceSymmetric(t *testing.T) { + var nice uint8 + for nice = 1; nice > 0; nice++ { + s := NicenessFmt(nice) + parsed, err := NicenessParse(s) + if err != nil || parsed != nice { + t.Error(err) + } + parsed, err = NicenessParse(strings.ToLower(s)) + if err != nil || parsed != nice { + t.Error(err) + } + } +} diff --git a/src/cypherpunks.ru/nncp/pkt.go b/src/cypherpunks.ru/nncp/pkt.go index 855037d..1d1ff34 100644 --- a/src/cypherpunks.ru/nncp/pkt.go +++ b/src/cypherpunks.ru/nncp/pkt.go @@ -47,10 +47,6 @@ const ( MaxPathSize = 1<<8 - 1 - DefaultNiceExec = 64 - DefaultNiceFreq = 64 - DefaultNiceFile = 196 - NNCPBundlePrefix = "NNCP" ) -- 2.44.0