本文整理汇总了Golang中github.com/jbenet/go-ipfs/util.DOut函数的典型用法代码示例。如果您正苦于以下问题:Golang DOut函数的具体用法?Golang DOut怎么用?Golang DOut使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DOut函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleFindPeer
func (dht *IpfsDHT) handleFindPeer(p *peer.Peer, pmes *PBDHTMessage) {
resp := Message{
Type: pmes.GetType(),
ID: pmes.GetId(),
Response: true,
}
defer func() {
mes := swarm.NewMessage(p, resp.ToProtobuf())
dht.netChan.Outgoing <- mes
}()
level := pmes.GetValue()[0]
u.DOut("handleFindPeer: searching for '%s'\n", peer.ID(pmes.GetKey()).Pretty())
closest := dht.routingTables[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
if closest == nil {
u.PErr("handleFindPeer: could not find anything.\n")
return
}
if len(closest.Addresses) == 0 {
u.PErr("handleFindPeer: no addresses for connected peer...\n")
return
}
// If the found peer further away than this peer...
if kb.Closer(dht.self.ID, closest.ID, u.Key(pmes.GetKey())) {
return
}
u.DOut("handleFindPeer: sending back '%s'\n", closest.ID.Pretty())
resp.Peers = []*peer.Peer{closest}
resp.Success = true
}
示例2: peerWantsBlock
// peerWantsBlock will check if we have the block in question,
// and then if we do, check the ledger for whether or not we should send it.
func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) {
u.DOut("peer [%s] wants block [%s]\n", p.ID.Pretty(), u.Key(want).Pretty())
ledger := bs.getLedger(p)
dsk := ds.NewKey(want)
blk_i, err := bs.datastore.Get(dsk)
if err != nil {
if err == ds.ErrNotFound {
ledger.Wants(u.Key(want))
}
u.PErr("datastore get error: %v\n", err)
return
}
blk, ok := blk_i.([]byte)
if !ok {
u.PErr("data conversion error.\n")
return
}
if ledger.ShouldSend() {
u.DOut("Sending block to peer.\n")
bblk, err := blocks.NewBlock(blk)
if err != nil {
u.PErr("newBlock error: %v\n", err)
return
}
bs.SendBlock(p, bblk)
ledger.SentBytes(len(blk))
} else {
u.DOut("Decided not to send block.")
}
}
示例3: GetBlock
// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
u.DOut("BlockService GetBlock: '%s'\n", k.Pretty())
dsk := ds.NewKey(string(k))
datai, err := s.Datastore.Get(dsk)
if err == nil {
u.DOut("Blockservice: Got data in datastore.\n")
bdata, ok := datai.([]byte)
if !ok {
return nil, fmt.Errorf("data associated with %s is not a []byte", k)
}
return &blocks.Block{
Multihash: mh.Multihash(k),
Data: bdata,
}, nil
} else if err == ds.ErrNotFound && s.Remote != nil {
u.DOut("Blockservice: Searching bitswap.\n")
blk, err := s.Remote.GetBlock(k, time.Second*5)
if err != nil {
return nil, err
}
return blk, nil
} else {
u.DOut("Blockservice GetBlock: Not found.\n")
return nil, u.ErrNotFound
}
}
示例4: ResolvePath
// ResolvePath fetches the node for given path. It uses the first
// path component as a hash (key) of the first node, then resolves
// all other components walking the links, with ResolveLinks.
func (s *Resolver) ResolvePath(fpath string) (*merkledag.Node, error) {
u.DOut("Resolve: '%s'\n", fpath)
fpath = path.Clean(fpath)
parts := strings.Split(fpath, "/")
// skip over empty first elem
if len(parts[0]) == 0 {
parts = parts[1:]
}
// if nothing, bail.
if len(parts) == 0 {
return nil, fmt.Errorf("ipfs path must contain at least one component")
}
// first element in the path is a b58 hash (for now)
h, err := mh.FromB58String(parts[0])
if err != nil {
return nil, err
}
u.DOut("Resolve dag get.\n")
nd, err := s.DAG.Get(u.Key(h))
if err != nil {
return nil, err
}
return s.ResolveLinks(nd, parts[1:])
}
示例5: Ping
// Ping a peer, log the time it took
func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) error {
// Thoughts: maybe this should accept an ID and do a peer lookup?
u.DOut("Enter Ping.\n")
pmes := Message{ID: swarm.GenerateMessageID(), Type: PBDHTMessage_PING}
mes := swarm.NewMessage(p, pmes.ToProtobuf())
before := time.Now()
responseChan := dht.listener.Listen(pmes.ID, 1, time.Minute)
dht.netChan.Outgoing <- mes
tout := time.After(timeout)
select {
case <-responseChan:
roundtrip := time.Since(before)
p.SetLatency(roundtrip)
u.DOut("Ping took %s.\n", roundtrip.String())
return nil
case <-tout:
// Timed out, think about removing peer from network
u.DOut("[%s] Ping peer [%s] timed out.", dht.self.ID.Pretty(), p.ID.Pretty())
dht.listener.Unlisten(pmes.ID)
return u.ErrTimeout
}
}
示例6: Print
func (l *logDhtRPC) Print() {
b, err := json.Marshal(l)
if err != nil {
u.DOut(err.Error())
} else {
u.DOut(string(b))
}
}
示例7: handleMessages
// Read in all messages from swarm and handle them appropriately
// NOTE: this function is just a quick sketch
func (dht *IpfsDHT) handleMessages() {
u.DOut("Begin message handling routine\n")
errs := dht.network.GetErrChan()
for {
select {
case mes, ok := <-dht.netChan.Incoming:
if !ok {
u.DOut("handleMessages closing, bad recv on incoming\n")
return
}
pmes := new(PBDHTMessage)
err := proto.Unmarshal(mes.Data, pmes)
if err != nil {
u.PErr("Failed to decode protobuf message: %s\n", err)
continue
}
dht.Update(mes.Peer)
// Note: not sure if this is the correct place for this
if pmes.GetResponse() {
dht.listener.Respond(pmes.GetId(), mes)
continue
}
//
u.DOut("[peer: %s]\nGot message type: '%s' [id = %x, from = %s]\n",
dht.self.ID.Pretty(),
PBDHTMessage_MessageType_name[int32(pmes.GetType())],
pmes.GetId(), mes.Peer.ID.Pretty())
switch pmes.GetType() {
case PBDHTMessage_GET_VALUE:
go dht.handleGetValue(mes.Peer, pmes)
case PBDHTMessage_PUT_VALUE:
go dht.handlePutValue(mes.Peer, pmes)
case PBDHTMessage_FIND_NODE:
go dht.handleFindPeer(mes.Peer, pmes)
case PBDHTMessage_ADD_PROVIDER:
go dht.handleAddProvider(mes.Peer, pmes)
case PBDHTMessage_GET_PROVIDERS:
go dht.handleGetProviders(mes.Peer, pmes)
case PBDHTMessage_PING:
go dht.handlePing(mes.Peer, pmes)
case PBDHTMessage_DIAGNOSTIC:
go dht.handleDiagnostic(mes.Peer, pmes)
default:
u.PErr("Recieved invalid message type")
}
case err := <-errs:
u.PErr("dht err: %s\n", err)
case <-dht.shutdown:
return
}
}
}
示例8: FindProviders
// FindProviders searches for peers who can provide the value for given key.
func (dht *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) ([]*peer.Peer, error) {
ll := startNewRPC("FindProviders")
defer func() {
ll.EndLog()
ll.Print()
}()
u.DOut("Find providers for: '%s'\n", key)
p := dht.routingTables[0].NearestPeer(kb.ConvertKey(key))
if p == nil {
return nil, kb.ErrLookupFailure
}
for level := 0; level < len(dht.routingTables); {
pmes, err := dht.findProvidersSingle(p, key, level, timeout)
if err != nil {
return nil, err
}
if pmes.GetSuccess() {
u.DOut("Got providers back from findProviders call!\n")
provs := dht.addPeerList(key, pmes.GetPeers())
ll.Success = true
return provs, nil
}
u.DOut("Didnt get providers, just closer peers.\n")
closer := pmes.GetPeers()
if len(closer) == 0 {
level++
continue
}
if peer.ID(closer[0].GetId()).Equal(dht.self.ID) {
u.DOut("Got myself back as a closer peer.")
return nil, u.ErrNotFound
}
maddr, err := ma.NewMultiaddr(closer[0].GetAddr())
if err != nil {
// ??? Move up route level???
panic("not yet implemented")
}
np, err := dht.network.GetConnection(peer.ID(closer[0].GetId()), maddr)
if err != nil {
u.PErr("[%s] Failed to connect to: %s\n", dht.self.ID.Pretty(), closer[0].GetAddr())
level++
continue
}
p = np
}
return nil, u.ErrNotFound
}
示例9: getDiagnostic
func (dht *IpfsDHT) getDiagnostic(timeout time.Duration) ([]*diagInfo, error) {
u.DOut("Begin Diagnostic")
//Send to N closest peers
targets := dht.routingTables[0].NearestPeers(kb.ConvertPeerID(dht.self.ID), 10)
// TODO: Add timeout to this struct so nodes know when to return
pmes := Message{
Type: PBDHTMessage_DIAGNOSTIC,
ID: swarm.GenerateMessageID(),
}
listenChan := dht.listener.Listen(pmes.ID, len(targets), time.Minute*2)
pbmes := pmes.ToProtobuf()
for _, p := range targets {
mes := swarm.NewMessage(p, pbmes)
dht.netChan.Outgoing <- mes
}
var out []*diagInfo
after := time.After(timeout)
for count := len(targets); count > 0; {
select {
case <-after:
u.DOut("Diagnostic request timed out.")
return out, u.ErrTimeout
case resp := <-listenChan:
pmesOut := new(PBDHTMessage)
err := proto.Unmarshal(resp.Data, pmesOut)
if err != nil {
// NOTE: here and elsewhere, need to audit error handling,
// some errors should be continued on from
return out, err
}
dec := json.NewDecoder(bytes.NewBuffer(pmesOut.GetValue()))
for {
di := new(diagInfo)
err := dec.Decode(di)
if err != nil {
break
}
out = append(out, di)
}
}
}
return nil, nil
}
示例10: handleConnection
func (dl *DaemonListener) handleConnection(conn net.Conn) {
defer conn.Close()
dec := json.NewDecoder(conn)
var command Command
err := dec.Decode(&command)
if err != nil {
fmt.Fprintln(conn, err)
return
}
u.DOut("Got command: %v\n", command)
switch command.Command {
case "add":
err = commands.Add(dl.node, command.Args, command.Opts, conn)
case "cat":
err = commands.Cat(dl.node, command.Args, command.Opts, conn)
case "ls":
err = commands.Ls(dl.node, command.Args, command.Opts, conn)
case "pin":
err = commands.Pin(dl.node, command.Args, command.Opts, conn)
case "peers":
err = commands.Peers(dl.node, command.Args, command.Opts, conn)
default:
err = fmt.Errorf("Invalid Command: '%s'", command.Command)
}
if err != nil {
fmt.Fprintln(conn, err)
}
}
示例11: GetBlock
// GetBlock attempts to retrieve a particular block from peers, within timeout.
func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
*blocks.Block, error) {
u.DOut("Bitswap GetBlock: '%s'\n", k.Pretty())
begin := time.Now()
tleft := timeout - time.Now().Sub(begin)
provs_ch := bs.routing.FindProvidersAsync(k, 20, timeout)
valchan := make(chan []byte)
after := time.After(tleft)
// TODO: when the data is received, shut down this for loop ASAP
go func() {
for p := range provs_ch {
go func(pr *peer.Peer) {
blk, err := bs.getBlock(k, pr, tleft)
if err != nil {
u.PErr("getBlock returned: %v\n", err)
return
}
select {
case valchan <- blk:
default:
}
}(p)
}
}()
select {
case blkdata := <-valchan:
close(valchan)
return blocks.NewBlock(blkdata)
case <-after:
return nil, u.ErrTimeout
}
}
示例12: findProvidersSingle
func (dht *IpfsDHT) findProvidersSingle(p *peer.Peer, key u.Key, level int, timeout time.Duration) (*PBDHTMessage, error) {
pmes := Message{
Type: PBDHTMessage_GET_PROVIDERS,
Key: string(key),
ID: swarm.GenerateMessageID(),
Value: []byte{byte(level)},
}
mes := swarm.NewMessage(p, pmes.ToProtobuf())
listenChan := dht.listener.Listen(pmes.ID, 1, time.Minute)
dht.netChan.Outgoing <- mes
after := time.After(timeout)
select {
case <-after:
dht.listener.Unlisten(pmes.ID)
return nil, u.ErrTimeout
case resp := <-listenChan:
u.DOut("FindProviders: got response.\n")
pmesOut := new(PBDHTMessage)
err := proto.Unmarshal(resp.Data, pmesOut)
if err != nil {
return nil, err
}
return pmesOut, nil
}
}
示例13: Attr
// Attr returns the attributes of a given node.
func (s *Node) Attr() fuse.Attr {
u.DOut("Node attr.\n")
if len(s.Nd.Links) > 0 {
return fuse.Attr{Mode: os.ModeDir | 0555}
}
size, _ := s.Nd.Size()
return fuse.Attr{Mode: 0444, Size: uint64(size)}
}
示例14: handleGetValue
func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *PBDHTMessage) {
u.DOut("handleGetValue for key: %s\n", pmes.GetKey())
dskey := ds.NewKey(pmes.GetKey())
resp := &Message{
Response: true,
ID: pmes.GetId(),
Key: pmes.GetKey(),
}
iVal, err := dht.datastore.Get(dskey)
if err == nil {
u.DOut("handleGetValue success!\n")
resp.Success = true
resp.Value = iVal.([]byte)
} else if err == ds.ErrNotFound {
// Check if we know any providers for the requested value
provs := dht.providers.GetProviders(u.Key(pmes.GetKey()))
if len(provs) > 0 {
u.DOut("handleGetValue returning %d provider[s]\n", len(provs))
resp.Peers = provs
resp.Success = true
} else {
// No providers?
// Find closest peer on given cluster to desired key and reply with that info
level := 0
if len(pmes.GetValue()) < 1 {
// TODO: maybe return an error? Defaulting isnt a good idea IMO
u.PErr("handleGetValue: no routing level specified, assuming 0\n")
} else {
level = int(pmes.GetValue()[0]) // Using value field to specify cluster level
}
u.DOut("handleGetValue searching level %d clusters\n", level)
closer := dht.routingTables[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
if closer.ID.Equal(dht.self.ID) {
u.DOut("Attempted to return self! this shouldnt happen...\n")
resp.Peers = nil
goto out
}
// If this peer is closer than the one from the table, return nil
if kb.Closer(dht.self.ID, closer.ID, u.Key(pmes.GetKey())) {
resp.Peers = nil
u.DOut("handleGetValue could not find a closer node than myself.\n")
} else {
u.DOut("handleGetValue returning a closer peer: '%s'\n", closer.ID.Pretty())
resp.Peers = []*peer.Peer{closer}
}
}
} else {
//temp: what other errors can a datastore return?
panic(err)
}
out:
mes := swarm.NewMessage(p, resp.ToProtobuf())
dht.netChan.Outgoing <- mes
}
示例15: Lookup
// Lookup performs a lookup under this node.
func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
u.DOut("Lookup '%s'\n", name)
nd, err := s.Ipfs.Resolver.ResolveLinks(s.Nd, []string{name})
if err != nil {
// todo: make this error more versatile.
return nil, fuse.ENOENT
}
return &Node{Ipfs: s.Ipfs, Nd: nd}, nil
}