-
Notifications
You must be signed in to change notification settings - Fork 0
/
harness.go
127 lines (109 loc) · 2.5 KB
/
harness.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package negentropy
import (
"bufio"
"encoding/hex"
"fmt"
"os"
"strconv"
"strings"
)
func split(s string, delim rune) []string {
return strings.FieldsFunc(s, func(r rune) bool {
return r == delim
})
}
func test() {
frameSizeLimit := uint64(0)
if env, exists := os.LookupEnv("FRAMESIZELIMIT"); exists {
var err error
frameSizeLimit, err = strconv.ParseUint(env, 10, 64)
if err != nil {
panic(fmt.Errorf("invalid FRAMESIZELIMIT: %w", err))
}
}
storage := NewVector()
var ne *Negentropy
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}
items := split(line, ',')
switch items[0] {
case "item":
if len(items) != 3 {
panic("wrong num of fields")
}
created, err := strconv.ParseUint(items[1], 10, 64)
if err != nil {
panic(err)
}
id, err := hex.DecodeString(items[2]) // Assume fromHex translates hex string to []byte
if err != nil {
panic(err)
}
storage.Insert(created, id)
case "seal":
storage.Seal()
neg, err := NewNegentropy(storage, frameSizeLimit)
if err != nil {
panic(err)
}
ne = neg
case "initiate":
q, err := ne.Initiate()
if err != nil {
panic(err)
}
if frameSizeLimit != 0 && uint64(len(q)) > frameSizeLimit {
panic("initiate frameSizeLimit exceeded")
}
fmt.Println("msg,", hex.EncodeToString(q)) // Assume toHex converts []byte to hex string
case "msg":
var q []byte
if len(items) >= 2 {
s, err := hex.DecodeString(items[1])
if err != nil {
panic(err)
}
q = s
}
if (*ne).IsInitiator {
var have, need []string
resp, err := ne.ReconcileWithIDs(q, &have, &need)
if err != nil {
panic(fmt.Sprintf("Reconciliation failed: %v", err))
}
for _, id := range have {
fmt.Printf("have,%s\n", hex.EncodeToString([]byte(id)))
}
for _, id := range need {
fmt.Printf("need,%s\n", hex.EncodeToString([]byte(id)))
}
if resp == nil {
fmt.Println("done")
continue
}
q = resp
} else {
s, err := ne.Reconcile(q)
if err != nil {
panic(fmt.Sprintf("Reconciliation failed: %v", err))
}
q = s
}
if frameSizeLimit > 0 && uint64(len(q)) > frameSizeLimit {
panic("frameSizeLimit exceeded")
}
fmt.Printf("msg,%s\n", hex.EncodeToString(q))
// Handle message processing
// Similar to the C++ logic but adapted to Go
default:
panic("unknown cmd: " + items[0])
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
}