func SliceStringToTextList(seg *capn.Segment, m []string) capn.TextList { lst := seg.NewTextList(len(m)) for i := range m { lst.Set(i, string(m[i])) } return lst }
func SliceByteToUInt8List(seg *capn.Segment, m []byte) capn.UInt8List { lst := seg.NewUInt8List(len(m)) for i := range m { lst.Set(i, uint8(m[i])) } return lst }
func (t *Topology) AddToSegAutoRoot(seg *capn.Segment) msgs.Topology { topology := msgs.AutoNewTopology(seg) topology.SetClusterId(t.ClusterId) topology.SetVersion(t.Version) hosts := seg.NewTextList(len(t.Hosts)) topology.SetHosts(hosts) for idx, host := range t.Hosts { hosts.Set(idx, host) } topology.SetF(t.F) topology.SetMaxRMCount(t.MaxRMCount) topology.SetAsyncFlush(t.AsyncFlush) rms := seg.NewUInt32List(len(t.AllRMs)) topology.SetRms(rms) for idx, rmId := range t.AllRMs { rms.Set(idx, uint32(rmId)) } accounts := msgs.NewAccountList(seg, len(t.Accounts)) topology.SetAccounts(accounts) idx := 0 for un, pw := range t.Accounts { account := accounts.At(idx) idx++ account.SetUsername(un) account.SetPassword(pw) } return topology }
// reduce boilerplate, dump this segment to disk. func SegToFile(seg *capn.Segment, filePath string) { file, err := os.Create(filePath) if err != nil { panic(err) } seg.WriteTo(file) file.Close() }
func ShowSeg(msg string, seg *capn.Segment) []byte { pre := bytes.Buffer{} seg.WriteTo(&pre) fmt.Printf("%s\n", msg) by := pre.Bytes() ShowBytes(by, 10) return by }
func SegToBytes(seg *capn.Segment) []byte { if seg == nil { log.Fatal("SegToBytes called with nil segment!") } buf := new(bytes.Buffer) if _, err := seg.WriteTo(buf); err != nil { log.Fatal("Error when writing segment to bytes:", err) } return buf.Bytes() }
func Nester1GoToCapn(seg *capn.Segment, src *Nester1) air.Nester1Capn { //fmt.Printf("\n\n Nester1GoToCapn sees seg = '%#v'\n", seg) dest := air.AutoNewNester1Capn(seg) mylist1 := seg.NewTextList(len(src.Strs)) for i := range src.Strs { mylist1.Set(i, string(src.Strs[i])) } dest.SetStrs(mylist1) //fmt.Printf("after Nester1GoToCapn setting\n") return dest }
func (vc *VectorClock) AddToSeg(seg *capn.Segment) msgs.VectorClock { if vc == nil { vcCap := msgs.NewVectorClock(seg) vcCap.SetVarUuids(seg.NewDataList(0)) vcCap.SetValues(seg.NewUInt64List(0)) return vcCap } else if vc.cap == nil { vcCap := msgs.NewVectorClock(seg) vc.cap = &vcCap vUUIds := seg.NewDataList(len(vc.Clock)) values := seg.NewUInt64List(len(vc.Clock)) vcCap.SetVarUuids(vUUIds) vcCap.SetValues(values) idx := 0 for vUUId, ver := range vc.Clock { vUUIds.Set(idx, vUUId[:]) values.Set(idx, ver) idx++ } return vcCap } else { return *vc.cap } }
func HashTableGoToCapn(seg *capn.Segment, src *HashTable) HashTableCapn { dest := AutoNewHashTableCapn(seg) dest.SetCellSz(uint64(src.CellSz)) dest.SetArraySize(uint64(src.ArraySize)) dest.SetPopulation(uint64(src.Population)) mylist1 := seg.NewUInt8List(len(src.Offheap)) for i := range src.Offheap { mylist1.Set(i, uint8(src.Offheap[i])) } dest.SetOffheap(mylist1.ToArray()) return dest }
func MmapMallocGoToCapn(seg *capn.Segment, src *MmapMalloc) MmapMallocCapn { dest := AutoNewMmapMallocCapn(seg) dest.SetPath(src.Path) dest.SetFd(int64(src.Fd)) dest.SetFileBytesLen(src.FileBytesLen) dest.SetBytesAlloc(src.BytesAlloc) mylist1 := seg.NewUInt8List(len(src.Mem)) for i := range src.Mem { mylist1.Set(i, uint8(src.Mem[i])) } dest.SetMem(mylist1.ToArray()) return dest }
// notifyContact routes a message to a single contact. func (r *BroadcastRouter) notifyContact(deliveries chan<- bool, url string, segment *capn.Segment, logID string) { buf := bytes.Buffer{} segment.WriteTo(&buf) req, err := http.NewRequest("PUT", url, &buf) if err != nil { if r.logger.ShouldLog(ERROR) { r.logger.Error("router", "Router request failed", LogFields{"rid": logID, "error": err.Error()}) } deliveries <- false return } req.Header.Set(HeaderID, logID) if r.logger.ShouldLog(DEBUG) { r.logger.Debug("router", "Sending request", LogFields{"rid": logID, "url": url}) } req.Header.Add("Content-Type", "application/json") resp, err := r.rclient.Do(req) if err != nil { if r.logger.ShouldLog(ERROR) { r.logger.Error("router", "Router send failed", LogFields{"rid": logID, "error": err.Error()}) } deliveries <- false return } defer resp.Body.Close() // Discard the response body. If the body is not fully consumed, the HTTP // client will not reuse the underlying TCP connection. io.Copy(ioutil.Discard, resp.Body) if resp.StatusCode != 200 { if r.logger.ShouldLog(DEBUG) { r.logger.Debug("router", "Denied", LogFields{"rid": logID, "url": url}) } deliveries <- false return } if r.logger.ShouldLog(INFO) { r.logger.Info("router", "Server accepted", LogFields{"rid": logID, "url": url}) } deliveries <- true }
func (c Conds) AddToSeg(seg *capn.Segment) msgs.ConditionPair_List { cap := msgs.NewConditionPairList(seg, len(c)) idx := 0 for rmId, condSup := range c { condSupCap := msgs.NewConditionPair(seg) condSupCap.SetRmId(uint32(rmId)) condSupCap.SetCondition(condSup.Cond.AddToSeg(seg)) suppliersCap := seg.NewUInt32List(len(condSup.Suppliers)) for idx, rmId := range condSup.Suppliers { suppliersCap.Set(idx, uint32(rmId)) } condSupCap.SetSuppliers(suppliersCap) cap.Set(idx, condSupCap) idx++ } return cap }
func (sts *SimpleTxnSubmitter) setAllocations(allocIdx int, rmIdToActionIndices map[common.RMId]*[]int, allocations *msgs.Allocation_List, seg *capn.Segment, active bool, rmIds []common.RMId) { for _, rmId := range rmIds { actionIndices := *(rmIdToActionIndices[rmId]) sort.Ints(actionIndices) allocation := allocations.At(allocIdx) allocation.SetRmId(uint32(rmId)) actionIndicesCap := seg.NewUInt16List(len(actionIndices)) allocation.SetActionIndices(actionIndicesCap) for k, v := range actionIndices { actionIndicesCap.Set(k, uint16(v)) } if active { allocation.SetActive(sts.connections[rmId].BootCount()) } else { allocation.SetActive(0) } allocIdx++ } }
func RWTestGoToCapn(seg *capn.Segment, src *RWTest) air.RWTestCapn { dest := air.AutoNewRWTestCapn(seg) // NestMatrix -> Nester1Capn (go slice to capn list) if len(src.NestMatrix) > 0 { //typedList := air.NewNester1CapnList(seg, len(src.NestMatrix)) plist := seg.NewPointerList(len(src.NestMatrix)) i := 0 for _, ele := range src.NestMatrix { //plist.Set(i, capn.Object(Nester1GoToCapn(seg, &ele))) r := capn.Object(SliceNester1ToNester1CapnList(seg, ele)) plist.Set(i, r) i++ } //dest.SetNestMatrix(typedList) dest.SetNestMatrix(plist) } return dest }
// shell out to display capnp bytes as human-readable text. Data flow: // in-memory capn segment -> stdin to capnp decode -> stdout human-readble string form func CapnpDecodeSegment(seg *capn.Segment, capnpExePath string, capnpSchemaFilePath string, typeName string) string { // set defaults if capnpExePath == "" { capnpExePath = CheckAndGetCapnpPath() } if capnpSchemaFilePath == "" { capnpSchemaFilePath = "aircraftlib/aircraft.capnp" } if typeName == "" { typeName = "Z" } cs := []string{"decode", "--short", capnpSchemaFilePath, typeName} cmd := exec.Command(capnpExePath, cs...) cmdline := capnpExePath + " " + strings.Join(cs, " ") buf := new(bytes.Buffer) seg.WriteTo(buf) cmd.Stdin = buf var errout bytes.Buffer cmd.Stderr = &errout bs, err := cmd.Output() if err != nil { if err.Error() == "exit status 1" { cwd, _ := os.Getwd() fmt.Fprintf(os.Stderr, "\nCall to capnp in CapnpDecodeSegment(): '%s' in dir '%s' failed with status 1\n", cmdline, cwd) fmt.Printf("stderr: '%s'\n", string(errout.Bytes())) fmt.Printf("stdout: '%s'\n", string(bs)) } panic(err) } return strings.TrimSpace(string(bs)) }
func (cts *ClientTxnSubmitter) translateUpdates(seg *capn.Segment, updates map[*msgs.Update][]*msgs.Action) cmsgs.ClientUpdate_List { clientUpdates := cmsgs.NewClientUpdateList(seg, len(updates)) idx := 0 for update, actions := range updates { clientUpdate := clientUpdates.At(idx) idx++ clientUpdate.SetVersion(update.TxnId()) clientActions := cmsgs.NewClientActionList(seg, len(actions)) clientUpdate.SetActions(clientActions) for idy, action := range actions { clientAction := clientActions.At(idy) clientAction.SetVarId(action.VarId()) switch action.Which() { case msgs.ACTION_MISSING: clientAction.SetDelete() case msgs.ACTION_WRITE: clientAction.SetWrite() write := action.Write() clientWrite := clientAction.Write() clientWrite.SetValue(write.Value()) references := write.References() clientReferences := seg.NewDataList(references.Len()) clientWrite.SetReferences(clientReferences) for idz, n := 0, references.Len(); idz < n; idz++ { ref := references.At(idz) clientReferences.Set(idz, ref.Id()) positions := common.Positions(ref.Positions()) cts.hashCache.AddPosition(common.MakeVarUUId(ref.Id()), &positions) } default: panic(fmt.Sprintf("Unexpected action type: %v", action.Which())) } } } return clientUpdates }
func AutoNewZ(s *C.Segment) Z { return Z(s.NewStructAR(8, 1)) }
func NewRootZ(s *C.Segment) Z { return Z(s.NewRootStruct(8, 1)) }
func NewZ(s *C.Segment) Z { return Z(s.NewStruct(8, 1)) }
func NewRoutableList(s *C.Segment, sz int) Routable_List { return Routable_List(s.NewCompositeList(16, 2, sz)) }
func ReadRootRoutable(s *C.Segment) Routable { return Routable(s.Root(0).ToStruct()) }
func AutoNewRoutable(s *C.Segment) Routable { return Routable(s.NewStructAR(16, 2)) }
func NewRootRoutable(s *C.Segment) Routable { return Routable(s.NewRootStruct(16, 2)) }
func NewRootRecord(s *C.Segment) Record { return Record(s.NewRootStruct(16, 0)) }
func NewRequestList(s *C.Segment, sz int) Request_List { return Request_List(s.NewCompositeList(16, 1, sz)) }
func NewRangesList(s *C.Segment, sz int) Ranges_List { return Ranges_List(s.NewCompositeList(8, 1, sz)) }
func ReadRootRanges(s *C.Segment) Ranges { return Ranges(s.Root(0).ToStruct()) }
func ReadRootZ(s *C.Segment) Z { return Z(s.Root(0).ToStruct()) }
func NewZList(s *C.Segment, sz int) Z_List { return Z_List(s.NewCompositeList(8, 1, sz)) }
func NewRootRanges(s *C.Segment) Ranges { return Ranges(s.NewRootStruct(8, 1)) }