本文整理汇总了Golang中github.com/tanuki-ipfs/core/log.If函数的典型用法代码示例。如果您正苦于以下问题:Golang If函数的具体用法?Golang If怎么用?Golang If使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了If函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: WritePeersToDAG
//WritePeersToDAG writes peer list and returns merkledag key.
func (m *Self) WritePeersToDAG(peers Peers, kprev key.Key) (key.Key, error) {
log.Println("writing peers prev:", kprev)
var nodes NodesProto
nodes.Id = make([][]byte, len(peers))
i := 0
for _, p := range peers {
nodes.Id[i] = []byte(p.ID)
i++
}
dat, err := proto.Marshal(&nodes)
if log.If(err) {
return "", err
}
n := &merkledag.Node{Data: dat}
if kprev != "" {
prev, errr := m.GetDAGNode(kprev)
if log.If(errr) {
return "", errr
}
if err = n.AddNodeLink("prev", prev); log.If(err) {
return "", err
}
}
k, err := m.AddDAGNode(n, true)
if log.If(err) {
return "", err
}
return k, nil
}
示例2: FromDAGNode
//FromDAGNode creates Records from links of merkledag n and retuns it.
//links must contain "Stamp" and "Thread"
func FromDAGNode(self *peer.Self, n *merkledag.Node, sign []byte) (*Record, error) {
r := &Record{
self: self,
Stamp: time.Unix(0, 0),
Sign: sign,
Contents: make(map[string][]byte),
}
var err error
r.Key, err = n.Key()
if log.If(err) {
return nil, err
}
for _, l := range n.Links {
nn, err := r.self.GetLinkedNode(n, l.Name)
if log.If(err) {
return nil, err
}
r.Contents[l.Name] = nn.Data
switch l.Name {
case "thread":
r.Thread = string(nn.Data)
case "stamp":
stamp := binary.BigEndian.Uint64(nn.Data)
r.Stamp = time.Unix(0, int64(stamp))
}
}
if err := r.check(false); log.If(err) {
return nil, err
}
return r, nil
}
示例3: StartListen
//StartListen starts listening and calls accept fucn if accepted.
func (m *Self) StartListen(accept func(net.Stream) error) error {
list, err := corenet.Listen(m.ipfsNode, "/app/"+m.RootPath)
if log.If(err) {
return err
}
log.Printf("starting to listen at peer: %s\n", m.ipfsNode.Identity.Pretty())
go func() {
for {
con, err := list.Accept()
defer func() {
log.If(con.Close())
}()
if log.If(err) {
continue
}
peer := con.Conn().RemotePeer()
log.Printf("Connection from: %s\n", peer)
p, err := FromBytes([]byte(peer), m)
if log.If(err) {
continue
}
if !p.IsAllowed() {
log.Println(p, "denifed")
continue
}
log.If(accept(con))
}
}()
return nil
}
示例4: Slice
//Slice returns sorted records slice.
//if insufficient number it returns atmost records slice.
func (t *Thread) Slice(from, to int, spam slice.Key) (record.Records, error) {
sl := make(record.Records, 0, to-from)
troot, err := t.self.GetDAGNode(t.root)
log.Println(t.root)
if log.If(err) {
return nil, err
}
root, err := t.next(troot)
if log.If(err) {
return nil, err
}
for i := 0; root != nil && i < to; i++ {
kkk, _ := root.Key()
if spam != nil {
if _, exist := spam[kkk]; exist {
log.Println(kkk, "is regarded as spam")
i--
continue
}
}
if i >= from {
r, errr := t.toRecord(root)
if log.If(errr) {
return nil, errr
}
sl = append(sl, r)
}
root, err = t.next(root)
if log.If(err) {
return nil, err
}
}
return sl, nil
}
示例5: New
//New reeturns Manager obj.
func New(cfg *config.Config, rootPath string) (*Manager, error) {
self := peer.NewSelf(cfg, rootPath)
m := &Manager{
self: self,
}
p := self.ToPeer()
recent, err := p.ReadRecent()
if log.If(err) {
log.Println(err)
}
m.recent, err = thread.NewList(self, recent)
m.peers, err = p.ReadPeers()
log.If(err)
if len(m.peers) == 0 {
m.peers = self.LoadInitPeers()
}
m.tags, err = p.ReadTags()
log.If(err)
m.spams, err = p.ReadSpams()
log.If(err)
m.readThreads = self.ReadThreads()
if err := self.StartListen(m.accept); log.If(err) {
return nil, err
}
go func() {
for {
m.cron()
time.Sleep(time.Hour)
}
}()
return m, nil
}
示例6: search
//search serches nearest and bigger than stamp and update all DAG after calling func f if needed.
func (t *Thread) search(root *merkledag.Node, stamp time.Time,
f func(bool, *merkledag.Node) (bool, error)) (*merkledag.Node, error) {
link, err := getLinkName(root)
if log.If(err) {
return nil, err
}
var eq bool
if link != nil {
eq = link.stamp.Equal(stamp)
}
if link == nil || (eq || link.stamp.Before(stamp)) {
updated, errr := f(eq, root)
log.If(errr)
if updated {
return root, errr
}
return nil, errr
}
n, err := t.self.GetLinkedNode(root, link.string())
if log.If(err) {
return nil, err
}
updated, err := t.search(n, stamp, f)
if err != nil {
return nil, err
}
if updated != nil {
root, err = root.UpdateNodeLink(link.string(), updated)
log.If(err)
return root, err
}
return nil, err
}
示例7: Slice
//Slice returns from~to record slice from thread..
func (l *List) Slice(thread string, from, to int) (record.Records, error) {
t, err := l.GetThread(thread)
if log.If(err) {
return nil, err
}
rs, err := t.Slice(from, to, nil)
log.If(err)
return rs, err
}
示例8: GetDAGNode
//GetDAGNode gets dag node from path ph.
func (p *Peer) GetDAGNode(ph string) (*merkledag.Node, error) {
log.Println("getting dag from", ph)
k, err := p.nameResolve()
if log.If(err) {
return nil, err
}
node, err := p.myself.GetPathNode(k, ph)
log.If(err)
return node, err
}
示例9: Signer
//Signer returns one who Signs if exists.
func (r *Record) Signer() (ipfspeer.ID, error) {
pubKey, err := crypto.UnmarshalPublicKey(r.Contents["pubkey"])
if log.If(err) {
return "", err
}
var id ipfspeer.ID
id, err = ipfspeer.IDFromPublicKey(pubKey)
log.If(err)
return id, err
}
示例10: ExchangeRecent
//ExchangeRecent sends hash and peers of recent hash and returns received recent key.
func (m *Self) ExchangeRecent(target *Peer, recent key.Key, peers key.Key) (key.Key, error) {
con, err := corenet.Dial(m.ipfsNode, target.ID, "/app/"+m.RootPath)
if log.If(err) {
return "", err
}
if err := WriteRequest(con, recent, peers); log.If(err) {
return "", err
}
rrecent, err := ReadResponse(con)
log.If(err)
return rrecent, err
}
示例11: toDAGNode
//toDAGNode returns merkledag with Contents.
//Contents must contain "Stamp" and "Thread"
func (r *Record) toDAGNode() error {
var err error
n := &merkledag.Node{}
for k, v := range r.Contents {
if err := n.AddNodeLink(k, &merkledag.Node{Data: v}); log.If(err) {
return err
}
}
r.Key, err = r.self.AddDAGNode(n, false)
log.If(err)
return err
}
示例12: GetThread
//GetThread returns thread obj .
func (l *List) GetThread(name string) (*Thread, error) {
n, err := l.self.GetDAGNode(l.key)
if log.If(err) {
return nil, err
}
var tkey key.Key
if l, errr := n.GetNodeLink(name); errr == nil {
tkey = key.Key(l.Hash)
}
t, err := New(l.self, name, tkey)
log.If(err)
return t, err
}
示例13: NewSelf
//NewSelf make repo if needed ,starts daemon and returns Self obj.
func NewSelf(cfg *config.Config, rootPath string) *Self {
InitRepo(cfg.IpfsRepo)
n := &merkledag.Node{}
k, err := n.Key()
log.IfFatal(err)
//workaround "ERROR bitswap: failed to find any peer in table"
i := 0
var node *core.IpfsNode
var ctx context.Context
var cancel context.CancelFunc
for i = 0; i < 10; i++ {
log.Println("setting up node...")
r, err := fsrepo.Open(cfg.IpfsRepo)
log.IfFatal(err)
ctx, cancel = context.WithCancel(context.Background())
node, err = core.NewNode(ctx, &core.BuildCfg{
Repo: r,
Online: true,
})
log.IfFatal(err)
if err := node.Routing.Provide(ctx, k); log.If(err) {
cancel()
log.If(node.Close())
log.Println("retrying...")
continue
}
break
}
if i == 10 {
log.Fatal("cannot provide a key to network")
}
self := &Self{
RootPath: rootPath,
ipfsNode: node,
ctx: ctx,
cancel: cancel,
cfg: cfg,
}
self.follow = FromStringSlice(cfg.FollowPeers, self)
parent, err := self.ToPeer().GetDAGNode("")
if log.If(err) {
parent = &merkledag.Node{}
}
self.myIpns, err = parent.Key()
log.IfFatal(err)
if _, err = parent.GetNodeLink(rootPath); err != nil {
log.Println("initializing DAGs for saving status")
self.makeInitNodes(parent)
}
return self
}
示例14: FromPubkey
//FromPubkey returns new Peer obj from RSA pubkey.
func FromPubkey(pk []byte, my *Self) (*Peer, error) {
rpk, err := crypto.UnmarshalPublicKey(pk)
if log.If(err) {
return nil, err
}
id, err := peer.IDFromPublicKey(rpk)
if log.If(err) {
return nil, err
}
return &Peer{
ID: id,
myself: my,
}, nil
}
示例15: AddDAGNode
//AddDAGNode adds dagNode and returns key
func (m *Self) AddDAGNode(dagNode *merkledag.Node, pin bool) (key.Key, error) {
if err := m.ipfsNode.DAG.AddRecursive(dagNode); log.If(err) {
return "", err
}
k, err := dagNode.Key()
if log.If(err) {
return "", err
}
log.Println("added DAG", k.B58String())
if pin {
err = m.ipfsNode.Pinning.Pin(m.ctx, dagNode, true)
}
log.If(err)
return k, err
}