Example #1
0
func FindPeer(n *core.IpfsNode, cmdparts []string) (string, error) {
	out := new(bytes.Buffer)
	if len(cmdparts) < 3 {
		return fmt.Sprintln("findpeer: '# findpeer peerid'"), ErrArgCount
	}

	var search peer.ID
	if cmdparts[2][0] == '$' {
		n, err := strconv.Atoi(cmdparts[2][1:])
		if err != nil {
			return "", err
		}
		if n >= len(controllers) {
			return "", errors.New("specified peernum out of range")
		}
		search = controllers[n].PeerID()
	} else {
		search = peer.ID(b58.Decode(cmdparts[2]))
	}
	fmt.Fprintf(out, "Searching for peer: %s\n", search)

	ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(time.Second*5))
	p, err := n.Routing.FindPeer(ctx, search)
	if err != nil {
		return "", err
	}

	fmt.Fprintf(out, "%Got peer: %s\n", p)
	return out.String(), nil
}
Example #2
0
func Put(n *core.IpfsNode, cmdparts []string) (string, error) {
	if len(cmdparts) < 4 {
		return fmt.Sprintln("put: '# put key val'"), ErrArgCount
	}
	msg := fmt.Sprintf("putting value: '%s' for key '%s'\n", cmdparts[3], cmdparts[2])
	ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(time.Second*5))
	return msg, n.Routing.PutValue(ctx, u.Key(cmdparts[2]), []byte(cmdparts[3]))
}
Example #3
0
func Provide(n *core.IpfsNode, cmdparts []string) (string, error) {
	if len(cmdparts) < 3 {
		return fmt.Sprintln("provide: '# provide key'"), ErrArgCount
	}
	ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(time.Second*5))
	err := n.Routing.Provide(ctx, u.Key(cmdparts[2]))
	if err != nil {
		return "", err
	}
	return "", nil
}
Example #4
0
func Get(n *core.IpfsNode, cmdparts []string) (string, error) {
	if len(cmdparts) < 3 {
		return fmt.Sprintln("get: '# get key'"), ErrArgCount
	}
	ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(time.Second*5))
	val, err := n.Routing.GetValue(ctx, u.Key(cmdparts[2]))
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("Got value: '%s'\n", string(val)), nil
}
Example #5
0
func AssertGet(n *core.IpfsNode, key, exp string) bool {
	ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(time.Second*5))
	val, err := n.Routing.GetValue(ctx, u.Key(key))
	if err != nil {
		fmt.Printf("Get error: %s\n", err)
		return false
	}

	if string(val) != exp {
		fmt.Printf("expected '%s' but got '%s' instead.\n", exp, string(val))
		return false
	}

	if !logquiet {
		fmt.Println("Expectation Successful!")
	}
	return true
}
Example #6
0
func FindProv(n *core.IpfsNode, cmdparts []string) (string, error) {
	if len(cmdparts) < 3 {
		return fmt.Sprintln("findprov: '# findprov key [count]'"), ErrArgCount
	}
	count := 1
	var err error
	if len(cmdparts) >= 4 {
		count, err = strconv.Atoi(cmdparts[3])
		if err != nil {
			return "", err
		}
	}
	ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(time.Second*5))
	pchan := n.Routing.FindProvidersAsync(ctx, u.Key(cmdparts[2]), count)

	out := new(bytes.Buffer)
	fmt.Fprintf(out, "Providers of '%s'\n", cmdparts[2])
	for p := range pchan {
		fmt.Fprintf(out, "\t%s\n", p)
	}
	return out.String(), nil
}
Example #7
0
// Call sends the request, waits for it to complete, and returns its error status.
func (client *Client) Call(ctx context.Context, req proto.Message, resp proto.Message) error {
	// Setup a new call
	call := &Call{
		Req:     req,
		Resp:    resp,
		cancelc: make(chan interface{}),
	}

	// Make sure a service name is passed
	if serviceName, ok := ServiceNameFromContext(ctx); !ok {
		return ErrServiceNameMissing
	} else {
		call.Service = serviceName
	}

	// If a manual deadline is not passed, setup with default timeout
	if _, ok := ctx.Deadline(); !ok {
		ctx, _ = context.WithDeadline(ctx, time.Now().Add(RequestTimeout))
	}

	// Run the request in a goroutine and write the reponse to c
	c := make(chan error, 1)
	go func() { c <- client.send(ctx, call) }()
	select {
	// Use context done to manually trigger cancel
	case <-ctx.Done():
		// Cancel the request in flight
		call.cancelc <- true
		<-c // Wait for the request to finish
		if ctx.Err() == context.DeadlineExceeded {
			return ErrTimeout
		}
		return ctx.Err()
	// Request finished
	case err := <-c:
		return err
	}
}
Example #8
0
File: nxpipe.go Project: bmatsuo/nx
// ForkWithTimeout returns a ForkContext which adds a t deadline to the supplied
// context.
func ForkWithDeadline(t time.Time) ForkContext {
	return func(c context.Context) (context.Context, context.CancelFunc) {
		return context.WithDeadline(c, t)
	}
}