示例#1
0
func sendRequest(c *node.Client, req *msg.OcReq) *msg.OcResp {
	// Parse/attach payments
	if *fDefer != "" {
		pv, err := msg.NewPaymentValueParseString(*fDefer)
		if err != nil {
			log.Fatal(err.Error())
		}
		req.AttachDeferredPayment(pv)
	}

	// TODO(ortutay): handle non-deferred payments
	err := c.SignRequest(req)
	if err != nil {
		log.Fatal(err.Error())
	}
	if *fVerbosity > 0 {
		fmt.Printf("sending request to %v\n%v\n\n", *fAddr, req.String())
	}
	resp, err := c.SendRequest(*fAddr, req)
	if err != nil {
		log.Fatal(err.Error())
	}
	fmt.Printf("%v\n", resp.String())
	if resp.Status == msg.PLEASE_PAY {
		var pr msg.PaymentRequest
		err := json.Unmarshal(resp.Body, &pr)
		if err != nil {
			panic(err)
		}
		fmt.Printf("Server is requesting payment: %v%v to %v\n",
			util.S2B(pr.Amount), pr.Currency, pr.Addr)
	}
	return resp
}
示例#2
0
func (ss *StoreService) PeriodicWake() {
	now := time.Now().Unix()
	if ss.lastWake == 0 {
		ss.lastWake = now
	}
	period := int64(10)
	if now-ss.lastWake < period {
		return
	}
	ss.lastWake = now
	d := util.GetOrCreateDB(containersDB())
	keys := d.Keys()
	for {
		key := <-keys
		if len(key) == 0 {
			break
		}
		bytesUsed := 0
		id := msg.OcID(key)
		container := NewContainerFromDisk(id)
		seenBlocks := make(map[string]bool)
		for _, blobID := range container.BlobIDs {
			// TODO(ortutay): don't read blocks from disk just to find sizes
			blob, err := NewBlobFromDisk(blobID)
			if err != nil {
				continue
			}
			for _, block := range blob.Blocks {
				if _, ok := seenBlocks[block.ID.String()]; ok {
					continue
				}
				seenBlocks[block.ID.String()] = true
				bytesUsed += len(block.Data)
			}
		}
		costPv := costForBytesSeconds(bytesUsed, int(period))
		fmt.Printf("bytes %v used by %v..., cost += %f %v\n",
			bytesUsed, id.String()[:8], util.S2B(costPv.Amount), costPv.Currency)
		rec := rep.Record{
			Role:         rep.SERVER,
			Service:      SERVICE_NAME,
			Method:       PUT_METHOD,
			Timestamp:    int(now),
			ID:           id,
			Status:       rep.SUCCESS_UNPAID,
			PaymentValue: costPv,
			Perf:         nil,
		}
		rep.Put(&rec)
	}
}
示例#3
0
func main() {
	goopt.Parse(nil)
	util.SetAppDir(*fAppDir)

	ocCred, err := cred.NewOcCredLoadOrCreate("")
	if err != nil {
		log.Fatal(err.Error())
	}

	bConf, err := util.LoadBitcoindConf("")
	if err != nil {
		log.Fatal(err.Error())
	}

	pvLower, err := msg.NewPaymentValueParseString(*fCoinsLower)
	if err != nil {
		log.Fatal(err)
	}
	pvUpper, err := msg.NewPaymentValueParseString(*fCoinsUpper)
	if err != nil {
		log.Fatal(err)
	}
	coins, err := cred.GetBtcCredInRange(pvLower.Amount, pvUpper.Amount, bConf)
	if err != nil {
		log.Fatal(err.Error())
	}

	c := node.Client{
		BtcConf: bConf,
		Cred: cred.Cred{
			OcCred:  *ocCred,
			BtcConf: bConf,
			Coins:   *coins,
		},
	}

	var body []byte
	if !termutil.Isatty(os.Stdin.Fd()) {
		var err error
		body, err = ioutil.ReadAll(os.Stdin)
		util.Ferr(err)
	}

	cmdArgs := make([]string, 0)
	for _, arg := range os.Args[1:] {
		if arg[0] != '-' {
			cmdArgs = append(cmdArgs, arg)
		}
	}
	if len(cmdArgs) == 0 {
		// TODO(ortutay): print usage info
		return
	}

	var req *msg.OcReq
	switch cmdArgs[0] {
	case "quote":
		qReq, err := makeQuoteReq(cmdArgs[1:])
		if err != nil {
			log.Fatal(err.Error())
		}
		resp := sendRequest(&c, qReq)
		if resp.Status == msg.OK {
			var pv msg.PaymentValue
			err := json.Unmarshal(resp.Body, &pv)
			if err != nil {
				panic(err)
			}
			fmt.Printf("\n%v%v\n", util.S2B(pv.Amount), pv.Currency)
		}
	case "call":
		req, err = makeReq(cmdArgs[1:], body)
		if err != nil {
			log.Fatal(err.Error())
		}
		resp := sendRequest(&c, req)
		switch fmt.Sprintf("%v.%v", req.Service, req.Method) {
		case "payment.balance":
			{
				var br payment.BalanceResponse
				err := json.Unmarshal(resp.Body, &br)
				if err != nil {
					log.Fatalf("malformed response")
				}
				fmt.Printf("\nServer reports balance of %v%v (max allowed is %v%v)\n",
					util.S2B(br.Balance.Amount), br.Balance.Currency,
					util.S2B(br.MaxBalance.Amount), br.MaxBalance.Currency)
			}
		}
	case "pay":
		payBtc(&c, cmdArgs)
	case "listrep":
		sel := rep.Record{}
		if len(cmdArgs) > 1 {
			selJson := cmdArgs[1]
			err := json.Unmarshal([]byte(selJson), &sel)
			if err != nil {
				log.Fatal(err.Error())
			}
			fmt.Printf("sel json: %v %v\n", selJson, sel)
		}
		err := rep.PrettyPrint(&sel)
		if err != nil {
			log.Fatal(err.Error())
		}
	default:
		fmt.Printf("unrecognized command: %v", cmdArgs)
	}
}