Пример #1
0
func (helper *Helper) GenerateToken(id uint64) ([]byte, error) {
	token, err := generator.GenRandomToken()
	if err != nil {
		return nil, err
	}

	conn, err := redispool.GetClient(helper.redishost)
	if err != nil {
		return nil, err
	}

	key := DeviceTokenKeyPrefix + strconv.FormatUint(id, 10)

	_, err = conn.Do("SET", key, token)
	if err != nil {
		return nil, err
	}
	_, err = conn.Do("EXPIRE", key, DeviceTokenExpires)
	if err != nil {
		return nil, err
	}

	return token, nil

}
Пример #2
0
func (mgr *Manager) SetHeartbeat(id uint64) error {
	status, err := mgr.GetStatus(id)
	if err != nil {
		return err
	}

	if err != nil {
		return nil
	}
	if status == nil {
		return errors.New("device offline.")
	}

	key := OnlineStatusKeyPrefix + strconv.FormatUint(id, 10)
	conn, err := redispool.GetClient(mgr.redishost)
	if err != nil {
		return err
	}

	_, err = conn.Do("EXPIRE", key, status.HeartbeatInterval+status.HeartbeatInterval/2)
	if err != nil {
		return err
	}

	return nil
}
Пример #3
0
func (q *Queue) Receive(topic string, target interface{}) error {
	conn, err := redispool.GetClient(q.redishost)
	if err != nil {
		return err
	}
	_, err = conn.Do("SUBSCRIBE", topic)
	if err != nil {
		return err
	}

	result, err := conn.Receive()
	if err != nil {
		return err
	}

	strMsg := string(result.([]interface{})[2].([]byte))

	err = serializer.String2Struct(strMsg, target)
	if err != nil {
		return err
	}

	return nil

}
Пример #4
0
func (mgr *Manager) GetOffline(id uint64) error {
	key := OnlineStatusKeyPrefix + strconv.FormatUint(id, 10)
	conn, err := redispool.GetClient(mgr.redishost)
	if err != nil {
		return err
	}

	_, err = conn.Do("DEL", key)
	if err != nil {
		return err
	}

	return nil
}
Пример #5
0
func (helper *Helper) ClearToken(id uint64) error {
	key := DeviceTokenKeyPrefix + strconv.FormatUint(id, 10)

	conn, err := redispool.GetClient(helper.redishost)
	if err != nil {
		return err
	}

	_, err = conn.Do("DEL", key)
	if err != nil {
		return err
	}

	return nil
}
Пример #6
0
func (q *Queue) Send(topic string, msg interface{}) error {
	conn, err := redispool.GetClient(q.redishost)
	if err != nil {
		return err
	}

	msgStr, err := serializer.Struct2String(msg)
	if err != nil {
		return err
	}

	_, err = conn.Do("PUBLISH", topic, msgStr)
	if err != nil {
		return err
	}

	return nil
}
Пример #7
0
func (mgr *Manager) GetStatus(id uint64) (*Status, error) {
	key := OnlineStatusKeyPrefix + strconv.FormatUint(id, 10)
	conn, err := redispool.GetClient(mgr.redishost)
	if err != nil {
		return nil, err
	}

	status := &Status{}
	// get status from redis
	bufferStr, err := redis.String(conn.Do("GET", key))
	if err != nil {
		return nil, err
	}
	err = serializer.String2Struct(bufferStr, status)
	if err != nil {
		return nil, err
	}

	return status, nil
}
Пример #8
0
func (mgr *Manager) GetOnline(id uint64, status Status) error {
	key := OnlineStatusKeyPrefix + strconv.FormatUint(id, 10)
	conn, err := redispool.GetClient(mgr.redishost)
	if err != nil {
		return err
	}
	// serialize and store the device's online status info in redis
	bufferStr, err := serializer.Struct2String(status)
	if err != nil {
		return err
	}
	_, err = conn.Do("SET", key, bufferStr)
	if err != nil {
		return err
	}
	_, err = conn.Do("EXPIRE", key, status.HeartbeatInterval+status.HeartbeatInterval/2)
	if err != nil {
		return err
	}

	return nil
}
Пример #9
0
func (helper *Helper) ValidateToken(id uint64, token []byte) error {
	key := DeviceTokenKeyPrefix + strconv.FormatUint(id, 10)

	conn, err := redispool.GetClient(helper.redishost)
	if err != nil {
		return err
	}

	readToken, err := conn.Do("GET", key)
	if err != nil {
		return err
	}

	if !reflect.DeepEqual(readToken, token) {
		return errors.New("token not match.")
	}

	_, err = conn.Do("EXPIRE", key, DeviceTokenExpires)
	if err != nil {
		return err
	}

	return nil
}