Example #1
0
File: rpc.go Project: awgh/ratnet
// PublicRPC : Entrypoint for RPC functions that are exposed to the public/Internet
func (node *Node) PublicRPC(method string, args ...string) (string, error) {
	switch method {
	case "ID":
		var i bc.PubKey
		i, err := node.ID()
		if err != nil {
			return "", err
		}
		return i.ToB64(), nil

	case "Pickup":
		if len(args) < 2 {
			return "", errors.New("Invalid argument count.")
		}

		var i int64
		i, err := strconv.ParseInt(args[1], 10, 64)
		if err != nil {
			return "", errors.New("Invalid argument.")
		}

		rpk := node.routingKey.GetPubKey().Clone()
		if err := rpk.FromB64(args[0]); err != nil {
			return "", errors.New("Invalid argument.")
		}

		b, err := node.Pickup(rpk, i, args[2:]...)
		if err != nil {
			return "", errors.New("Invalid argument.")
		}

		var j []byte
		if j, err = json.Marshal(b); err != nil {
			return "", err
		}
		return string(j), err

	case "Dropoff":
		if len(args) < 1 {
			return "", errors.New("Invalid argument count.")
		}
		var bundle api.Bundle
		if err := json.Unmarshal([]byte(args[0]), &bundle); err != nil {
			return "", errors.New("JSON Decode Error in Dropoff RPC Bundle Unpack")
		}
		return "", node.Dropoff(bundle)

	default:
		return "", errors.New("No such method: " + method)
	}
}
Example #2
0
File: poll.go Project: awgh/ratnet
// pollServer will keep trying until either we get a result or the timeout expires
func (p *Poll) pollServer(transport api.Transport, node api.Node, host string, pubsrv bc.PubKey) (bool, error) {

	// Pickup Local
	rpubkey, err := transport.RPC(host, "ID")
	if err != nil {
		return false, err
	}
	rpk := pubsrv.Clone()
	if err := rpk.FromB64(string(rpubkey)); err != nil {
		return false, err
	}

	toRemoteRaw, err := node.Pickup(rpk, p.lastPollLocal)
	if err != nil {
		return false, err
	}

	// Pickup Remote
	toLocalRaw, err := transport.RPC(host, "Pickup", pubsrv.ToB64(), strconv.FormatInt(p.lastPollRemote, 10))
	if err != nil {
		return false, err
	}
	var toLocal api.Bundle
	if err := json.Unmarshal(toLocalRaw, &toLocal); err != nil {
		return false, err
	}

	p.lastPollLocal = toRemoteRaw.Time
	p.lastPollRemote = toLocal.Time

	toRemote, err := json.Marshal(toRemoteRaw)
	if err != nil {
		return false, err
	}

	// Dropoff Remote
	if len(toRemoteRaw.Data) > 0 {
		if _, err := transport.RPC(host, "Dropoff", string(toRemote)); err != nil {
			return false, err
		}
	}
	// Dropoff Local
	if len(toLocal.Data) > 0 {
		if err := node.Dropoff(toLocal); err != nil {
			return false, err
		}
	}
	return true, nil
}