Exemplo n.º 1
0
func (this *CacheComponent) Save(msg protocol.Message) error {
	this.CacheLock.Lock()
	defer this.CacheLock.Unlock()
	if this.Cache == nil {
		this.Cache = make(map[string]CacheDev, 0)
	}

	if _, ok := this.Cache[msg.DestUuid]; !ok {
		val := CacheDev{
			MDev: make(map[uint32]*CacheList, 0),
		}
		this.Cache[msg.DestUuid] = val
	}

	if _, ok := this.Cache[msg.DestUuid].MDev[msg.DeviceId]; !ok {
		val := &CacheList{
			List:  list.New(),
			Total: 0,
		}
		this.Cache[msg.DestUuid].MDev[msg.DeviceId] = val
	}

	this.Cache[msg.DestUuid].MDev[msg.DeviceId].Total++
	id := this.Cache[msg.DestUuid].MDev[msg.DeviceId].Total
	msg.DataLen = 4 + msg.DataLen
	buf := make([]byte, msg.DataLen)
	binary.BigEndian.PutUint32(buf[0:4], id)
	if len(msg.Data) > 0 {
		copy(buf[4:msg.DataLen], msg.Data)
	}
	msg.Data = buf

	msg.MsgType = protocol.MT_PUSH_WITH_RESPONSE
	msg.PushType = protocol.PT_PUSHSERVER
	msg.Number &= HEAD_RESET

	e := this.Cache[msg.DestUuid].MDev[msg.DeviceId].List.PushBack(CacheEntry{msg, id})
	if e == nil {
		err := errors.New("CacheComponent.Save() Error: can not save msg to list")
		fmt.Println(err.Error())
		return err
	}

	return nil
}
func (this *DispatcherComponent) handleKeepAlive(msg *protocol.Message) error {
	if msg == nil {
		err := errors.New("DispatcherComponent.handleKeepAlive() Error: invalid argument")
		fmt.Println(err.Error())
		return err
	}
	buffPool := buffpool.GetBuffPool()
	err := buffPool.UpdateClientStatus(msg.Number, msg.UserUuid, msg.DeviceId, msg.Token, true)
	if err != nil {
		err = errors.New("DispatcherComponent.handleKeepAlive() Error: can not update client's connections status" + err.Error())
		fmt.Println(err.Error())
		return err
	}
	msg.PushType = protocol.PT_PUSHSERVER
	msg.UserUuid, msg.DestUuid = msg.DestUuid, msg.UserUuid
	GetDispatcherOutComponent().EPushBack(msg, false)
	return nil
}