Esempio n. 1
0
// Pickup : Get messages from a remote node
func (node *Node) Pickup(rpub bc.PubKey, lastTime int64, channelNames ...string) (api.Bundle, error) {
	node.debugMsg("Pickup called")
	var retval api.Bundle
	var msgs []string

	retval.Time = lastTime

	for _, mail := range node.outbox {
		if lastTime < mail.timeStamp {
			pickupMsg := false
			if len(channelNames) > 0 {
				for _, channelName := range channelNames {
					if channelName == mail.channel {
						pickupMsg = true
					}
				}
			} else {
				pickupMsg = true
			}
			if pickupMsg == true {
				msgs = append(msgs, mail.msg)
				retval.Time = mail.timeStamp
			}
		}
	}

	// transmit
	if len(msgs) > 0 {
		j, err := json.Marshal(msgs)
		if err != nil {
			return retval, err
		}
		cipher, err := node.routingKey.EncryptMessage(j, rpub)
		if err != nil {
			return retval, err
		}
		retval.Data = cipher
		return retval, err
	}
	node.debugMsg("Pickup returned")
	return retval, nil
}
Esempio n. 2
0
// Pickup : Get messages from a remote node
func (node *Node) Pickup(rpub bc.PubKey, lastTime int64, channelNames ...string) (api.Bundle, error) {
	node.debugMsg("Pickup called")
	c := node.db()
	var retval api.Bundle
	wildcard := false
	if len(channelNames) < 1 {
		wildcard = true // if no channels are given, get everything
	} else {
		for _, cname := range channelNames {
			for _, char := range cname {
				if strings.Index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321", string(char)) == -1 {
					return retval, errors.New("Invalid character in channel name!")
				}
			}
		}
	}
	sqlq := "SELECT msg, timestamp FROM outbox"
	if lastTime != 0 {
		sqlq += " WHERE (int64(" + strconv.FormatInt(lastTime, 10) +
			") < timestamp)"
	}
	if !wildcard && len(channelNames) > 0 { // QL is broken?  couldn't make it work with prepared stmts
		if lastTime != 0 {
			sqlq += " AND"
		} else {
			sqlq += " WHERE"
		}
		sqlq = sqlq + " channel IN( \"" + channelNames[0] + "\""
		for i := 1; i < len(channelNames); i++ {
			sqlq = sqlq + ",\"" + channelNames[i] + "\""
		}
		sqlq = sqlq + " )"
	}
	// todo:  ORDER BY breaks on android/arm and returns nothing without error, report to cznic
	//			sqlq = sqlq + " ORDER BY timestamp ASC;"
	sqlq = sqlq + ";"
	r := transactQuery(c, sqlq)

	var msgs []string
	for r.Next() {
		var msg string
		var ts int64
		r.Scan(&msg, &ts)
		if ts > lastTime { // do this instead of ORDER BY, for android
			lastTime = ts
		}
		msgs = append(msgs, msg)
	}
	retval.Time = lastTime
	if len(msgs) > 0 {
		j, err := json.Marshal(msgs)
		if err != nil {
			return retval, err
		}
		cipher, err := node.routingKey.EncryptMessage(j, rpub)
		if err != nil {
			return retval, err
		}
		retval.Data = cipher
		return retval, err
	}
	node.debugMsg("Pickup returned")
	return retval, nil
}