Exemple #1
0
// Discover funtion discovers the server and returns the data sent by the server.
func (c *Client) Discover() (*Response, error) {
	if c.Port == "" {
		c.Port = "3456"
	}
	if c.BufSize <= 0 {
		c.BufSize = 1024
	}
	if c.Timeout <= 0 {
		c.Timeout = 2 * time.Minute
	}
	if c.Deadline <= 0 {
		c.Deadline = 10 * time.Second
	}
	if c.Keepalive <= 0 {
		c.Keepalive = 10 * time.Second
	}
	var err error
	if c.Id == "" {
		c.Id, err = rand.Uuid()
		if err != nil {
			return nil, e.Forward(err)
		}
	}
	c.stopKa = make(chan chan struct{})
	c.InitMCast()
	err = c.getInt()
	if err != nil {
		return nil, e.Forward(err)
	}
	resp, err := c.getAddr()
	if err != nil {
		return nil, e.Forward(err)
	}
	return resp, nil
}
Exemple #2
0
func Put(tx *bolt.Tx, bucket []byte, keys [][]byte, data []byte) error {
	var err error
	var buf []byte
	var b *bolt.Bucket
	b, err = tx.CreateBucketIfNotExists(bucket)
	if err != nil {
		return e.Forward(err)
	}
	if len(keys) == 0 {
		return e.New("no keys")
	}
	if len(keys) >= 2 {
		for i := 0; i < len(keys)-1; i++ {
			buf = b.Get(keys[i])
			if buf == nil {
				id, err := rand.Uuid()
				if err != nil {
					return e.Forward(err)
				}
				buf = []byte(id)
				err = b.Put(keys[i], buf)
				if err != nil {
					return e.Forward(err)
				}
			}
			b, err = tx.CreateBucket(buf)
			if e.Contains(err, "bucket already exists") {
				b = tx.Bucket(buf)
			} else if err != nil {
				return e.Forward(err)
			}
		}
	}
	err = b.Put(keys[len(keys)-1], data)
	if err != nil {
		return e.Forward(err)
	}
	return nil
}