Exemple #1
0
func (self *KiteFileStore) Query(messageId string) *MessageEntity {

	lock, _, el := self.hash(messageId)
	lock.RLock()
	defer lock.RUnlock()
	e, ok := el[messageId]
	if !ok {
		return nil
	}

	v := e.Value.(*opBody)

	//wait save done
	self.waitSaveDone(v)

	data, err := self.snapshot.Query(v.Id)
	if nil != err {
		// log.Error("KiteFileStore|Query|Entity|FAIL|%s|%d", err, v.Id)
		return nil
	}

	var msg interface{}
	msgType := data[0]
	switch msgType {
	case protocol.CMD_BYTES_MESSAGE:
		var bms protocol.BytesMessage
		err = protocol.UnmarshalPbMessage(data[1:], &bms)
		msg = &bms
	case protocol.CMD_STRING_MESSAGE:
		var sms protocol.StringMessage
		err = protocol.UnmarshalPbMessage(data[1:], &sms)
		msg = &sms
	default:
		log.ErrorLog("kite_store", "KiteFileStore|Query|INVALID|MSGTYPE|%d", msgType)
		return nil
	}

	if nil != err {
		log.ErrorLog("kite_store", "KiteFileStore|Query|UnmarshalPbMessage|Entity|FAIL|%s", err)
		return nil
	} else {
		entity := NewMessageEntity(protocol.NewQMessage(msg))
		//merge data
		entity.Commit = v.Commit
		entity.FailGroups = v.FailGroups
		entity.SuccGroups = v.SuccGroups
		entity.NextDeliverTime = v.NextDeliverTime
		entity.DeliverCount = v.DeliverCount
		return entity
	}

}
//对于请求事件
func (self *PacketHandler) handlePacket(pevent *PacketEvent) (IEvent, error) {
	var err error
	var event IEvent
	packet := pevent.Packet
	//根据类型反解packet
	switch packet.CmdType {
	//连接授权确认
	case protocol.CMD_CONN_AUTH:
		var auth protocol.ConnAuthAck
		err = protocol.UnmarshalPbMessage(packet.Data, &auth)
		if nil == err {
			pevent.RemoteClient.Attach(packet.Opaque, &auth)
			event = &SunkEvent{}
		}

	//心跳
	case protocol.CMD_HEARTBEAT:
		var hearbeat protocol.HeartBeat
		err = protocol.UnmarshalPbMessage(packet.Data, &hearbeat)
		if nil == err {
			hb := &hearbeat
			// log.Printf("PacketHandler|handlePacket|HeartBeat|%t\n", hb)
			event = NewHeartbeatEvent(pevent.RemoteClient, packet.Opaque, hb.GetVersion())
		}
		//消息持久化
	case protocol.CMD_MESSAGE_STORE_ACK:
		var pesisteAck protocol.MessageStoreAck
		err = protocol.UnmarshalPbMessage(packet.Data, &pesisteAck)
		if nil == err {
			//直接通知当前的client对应chan
			pevent.RemoteClient.Attach(packet.Opaque, &pesisteAck)
			event = eventSunk
		}

	case protocol.CMD_TX_ACK:
		var txAck protocol.TxACKPacket
		err = protocol.UnmarshalPbMessage(packet.Data, &txAck)
		if nil == err {
			event = newAcceptEvent(protocol.CMD_TX_ACK, &txAck, pevent.RemoteClient, packet.Opaque)
		}
	//发送的是bytesmessage
	case protocol.CMD_BYTES_MESSAGE:
		var msg protocol.BytesMessage
		err = protocol.UnmarshalPbMessage(packet.Data, &msg)
		if nil == err {
			event = newAcceptEvent(protocol.CMD_BYTES_MESSAGE, &msg, pevent.RemoteClient, packet.Opaque)
		}
	//发送的是StringMessage
	case protocol.CMD_STRING_MESSAGE:
		var msg protocol.StringMessage
		err = protocol.UnmarshalPbMessage(packet.Data, &msg)
		if nil == err {
			event = newAcceptEvent(protocol.CMD_STRING_MESSAGE, &msg, pevent.RemoteClient, packet.Opaque)
		}
	}

	return event, err

}
Exemple #3
0
//对于请求事件
func (self *PacketHandler) handlePacket(pevent *PacketEvent) (IEvent, error) {
	var err error
	var event IEvent

	packet := pevent.Packet
	//根据类型反解packet
	switch packet.CmdType {
	//连接的元数据
	case protocol.CMD_CONN_META:
		var connMeta protocol.ConnMeta
		err = protocol.UnmarshalPbMessage(packet.Data, &connMeta)
		if nil == err {
			meta := &connMeta
			event = newAccessEvent(meta.GetGroupId(), meta.GetSecretKey(), pevent.RemoteClient, packet.Opaque)
		}

	//心跳
	case protocol.CMD_HEARTBEAT:
		var hearbeat protocol.HeartBeat
		err = protocol.UnmarshalPbMessage(packet.Data, &hearbeat)
		if nil == err {
			event = newAcceptEvent(protocol.CMD_HEARTBEAT, &hearbeat, pevent.RemoteClient, packet.Opaque)
		}
		//投递结果确认
	case protocol.CMD_DELIVER_ACK:
		var delAck protocol.DeliverAck
		err = protocol.UnmarshalPbMessage(packet.Data, &delAck)

		if nil == err {
			event = newAcceptEvent(protocol.CMD_DELIVER_ACK, &delAck, pevent.RemoteClient, packet.Opaque)
		}

	case protocol.CMD_TX_ACK:
		var txAck protocol.TxACKPacket
		err = protocol.UnmarshalPbMessage(packet.Data, &txAck)
		if nil == err {
			event = newTxAckEvent(&txAck, packet.Opaque, pevent.RemoteClient)
		}

	//发送的是bytesmessage
	case protocol.CMD_BYTES_MESSAGE:
		var msg protocol.BytesMessage
		err = protocol.UnmarshalPbMessage(packet.Data, &msg)
		if nil == err {
			event = newAcceptEvent(protocol.CMD_BYTES_MESSAGE, &msg, pevent.RemoteClient, packet.Opaque)
		}
	//发送的是StringMessage
	case protocol.CMD_STRING_MESSAGE:
		var msg protocol.StringMessage
		err = protocol.UnmarshalPbMessage(packet.Data, &msg)
		if nil == err {
			event = newAcceptEvent(protocol.CMD_STRING_MESSAGE, &msg, pevent.RemoteClient, packet.Opaque)
		}
	}

	return event, err

}
func (self convertor) Convert2Entity(fv []interface{}, entity *store.MessageEntity, filter func(colname string) bool) {
	val := reflect.ValueOf(entity)
	elem := val.Elem()
	//可能存在过滤的值需要记录应该读取那个位置的参数
	j := 0
	for i, c := range self.columns {
		if filter(c.columnName) {
			j++
			continue
		}
		i = i - j
		v := fv[i]
		rval := reflect.ValueOf(v)
		rv := rval.Elem().Interface()
		fn := elem.FieldByName(c.fieldName)

		k := fn.Kind()

		// log.Debug("convertor|Convert2Entity|%s|%s\n", c.fieldName, rv)

		switch k {
		case reflect.Int8:
			fn.Set(reflect.ValueOf(rv.(int8)))
		case reflect.Int:
			fn.Set(reflect.ValueOf(rv.(int)))
		case reflect.Int16:
			fn.Set(reflect.ValueOf(rv.(int16)))
		case reflect.Int32:
			fn.Set(reflect.ValueOf(rv.(int32)))
		case reflect.Int64:
			fn.SetInt((rv.(int64)))
		case reflect.Uint8:
			fn.Set(reflect.ValueOf(rv.(uint8)))
		case reflect.Uint:
			fn.Set(reflect.ValueOf(rv.(uint)))
		case reflect.Uint16:
			fn.Set(reflect.ValueOf(rv.(uint16)))
		case reflect.Uint32:
			fn.Set(reflect.ValueOf(rv.(uint32)))
		case reflect.Uint64:
			fn.SetUint((rv.(uint64)))
		case reflect.Bool:
			fn.SetBool(rv.(bool))
		case reflect.String:
			fn.SetString((rv.(string)))
		case reflect.Ptr:
			{
				hd, hok := rv.([]byte)
				_, ok := fn.Interface().(*protocol.Header)
				if ok && hok {
					var header protocol.Header
					if hok {
						//头部用PB反序列化
						err := protocol.UnmarshalPbMessage(hd, &header)
						if nil != err {
							log.ErrorLog("kite_store", "convertor|Convert2Entity|Unmarshal Header|FAIL|%s|%s\n", err, c.fieldName)
						}
					}
					fn.Set(reflect.ValueOf(&header))
				} else if hok {
					fn.SetBytes(hd)
				} else {
					log.ErrorLog("kite_store", "convertor|Convert2Entity|FAIL|UnSupport Ptr DataType|%s|%t|%s|%s\n", c.fieldName, rv, hok, ok)
					return
				}
			}
		case reflect.Array, reflect.Slice:
			k := fn.Type().Elem().Kind()
			if k == reflect.String {
				var data []string
				err := json.Unmarshal([]byte(rv.(string)), &data)
				if nil != err {
					log.ErrorLog("kite_store", "convertor|Convert2Entity|FAIL|UnSupport SLICE|%s|%s\n", c.fieldName, rv)
				}
				fn.Set(reflect.ValueOf(data))
			} else if k == reflect.Uint8 {
				fn.SetBytes(rv.([]byte))
			} else {
				log.ErrorLog("kite_store", "convertor|Convert2Entity|FAIL|UnSupport SLICE DataType|%s|%s\n", c.columnName, fn.Elem().Kind())
				return
			}
		default:
			if c.columnName == "body" {
				_, ok := rv.([]byte)
				if ok {
					fn.Set(rval.Elem())
				} else {
					log.ErrorLog("kite_store", "convertor|Convert2Entity|FAIL|UnSupport BodyType |REQ:[]byte|%s|%T\n", c.fieldName, rv)
					return
				}
			} else {
				log.ErrorLog("kite_store", "convertor|Convert2Entity|FAIL|UnSupport DataType|%s|%s\n", c.fieldName, rv)
			}
		}
	}
}