Beispiel #1
0
func sn2db(v uint64) string {

	b := make([]byte, 8)
	b[0] = uint8(v >> 56)
	b[1] = uint8(v >> 48)
	b[2] = uint8(v >> 40)
	b[3] = uint8(v >> 32)
	b[4] = uint8(v >> 24)
	b[5] = uint8(v >> 16)
	b[6] = uint8(v >> 8)
	b[7] = uint8(v)
	return enc.UnsafeBytesToString(b)
}
Beispiel #2
0
func MakeMessageAttr(name string, sqsAttr *sqsmsg.UserAttribute) *MessageAttribute {
	if strings.HasPrefix(sqsAttr.Type, "Binary") {
		encodedBin := make([]byte, base64.StdEncoding.EncodedLen(len(sqsAttr.Value)))
		base64.StdEncoding.Encode(encodedBin, enc.UnsafeStringToBytes(sqsAttr.Value))
		return &MessageAttribute{
			Name:        name,
			Type:        sqsAttr.Type,
			BinaryValue: enc.UnsafeBytesToString(encodedBin),
		}
	} else {
		return &MessageAttribute{
			Name:        name,
			Type:        sqsAttr.Type,
			StringValue: sqsAttr.Value,
		}
	}
}
Beispiel #3
0
func MarshalTest() {
	//m := NewPQMsgMetaData("ereteteyeueueueueueueueu", 10, 123456789023, 1234)
	st := time.Now().UnixNano()
	var i uint64
	for i = 0; i < 200000000; i++ {
		b := make([]byte, 8)
		b[0] = uint8(i >> 56)
		b[1] = uint8(i >> 48)
		b[2] = uint8(i >> 40)
		b[3] = uint8(i >> 32)
		b[4] = uint8(i >> 24)
		b[5] = uint8(i >> 16)
		b[6] = uint8(i >> 8)
		b[7] = uint8(i)
		enc.UnsafeBytesToString(b)
	}
	ft := time.Now().UnixNano() - st
	fmt.Println("Data prepared in", float64(ft)/1000000, "ms")
}
Beispiel #4
0
func (tok *Tokenizer) ReadTokens(reader io.Reader) ([]string, error) {
	var err error
	var token []byte = make([]byte, 0, INIT_TOKEN_BUFFER_LEN)
	var binTokenLen int
	var state int = STATE_PARSE_TEXT_TOKEN

	result := make([]string, 0, MAX_TOKENS_PER_MSG)

	for {
		if tok.bufPos >= tok.bufLen {
			// Read more data from the network reader
			tok.bufPos = 0
			tok.bufLen, err = reader.Read(tok.buffer)
			if nil != err {
				return nil, err
			}
			if tok.bufLen == 0 {
				continue
			}
		}

		// Tokenize content of the buffer
		if state == STATE_PARSE_BINARY_PAYLOAD {
			availableBytes := tok.bufLen - tok.bufPos
			if availableBytes > binTokenLen {
				availableBytes = binTokenLen
			}
			token = append(token, tok.buffer[tok.bufPos:tok.bufPos+availableBytes]...)
			binTokenLen -= availableBytes

			tok.bufPos += availableBytes

			if binTokenLen <= 0 {
				// Binary token complete
				state = STATE_PARSE_TEXT_TOKEN
				result = append(result, enc.UnsafeBytesToString(token))
				if len(result) > MAX_TOKENS_PER_MSG {
					return nil, mpqerr.ERR_TOK_TOO_MANY_TOKENS
				}
				token = make([]byte, 0, INIT_TOKEN_BUFFER_LEN)
			}
			continue
		}

		val := tok.buffer[tok.bufPos]
		tok.bufPos += 1

		if val >= START_ASCII_RANGE && val <= END_ASCII_RANGE {
			token = append(token, val)
		} else if len(token) > 0 {
			if token[0] == '$' {
				binTokenLen, err = strconv.Atoi(enc.UnsafeBytesToString(token[1:]))
				if err == nil && (binTokenLen < 1 || binTokenLen > MAX_BINARY_TOKEN_LEN) {
					return nil, mpqerr.ERR_TOK_PARSING_ERROR
				}
				state = STATE_PARSE_BINARY_PAYLOAD
				token = make([]byte, 0, binTokenLen)
			} else {
				result = append(result, enc.UnsafeBytesToString(token))
				if len(result) > MAX_TOKENS_PER_MSG {
					return nil, mpqerr.ERR_TOK_TOO_MANY_TOKENS
				}
				if val == SYMBOL_CR {
					return result, nil
				}
				token = make([]byte, 0, INIT_TOKEN_BUFFER_LEN)
			}
		} else {
			if val == SYMBOL_CR {
				return result, nil
			}
		}
		if len(token) > MAX_TEXT_TOKEN_LEN {
			return nil, mpqerr.ERR_TOK_TOKEN_TOO_LONG
		}
	}
}
Beispiel #5
0
func PushAMessage(pq *pqueue.PQueue, senderId string, paramList []string) sqs_response.SQSResponse {
	out := &MessageParams{
		DelaySeconds: -1,
		MessageBody:  "",
	}
	attrs, err := urlutils.ParseNNotationAttr("MessageAttribute.", paramList, out.Parse, NewReqQueueAttr)
	if err != nil {
		return err
	}
	attrsLen := len(attrs)
	outAttrs := make(map[string]*sqsmsg.UserAttribute)

	for i := 1; i <= attrsLen; i++ {
		a, ok := attrs[i]
		if !ok {
			return sqserr.InvalidParameterValueError("The request must contain non-empty message attribute name.")
		}
		reqMsgAttr, _ := a.(*ReqMsgAttr)

		sqs_err := validation.ValidateMessageAttrName(reqMsgAttr.Name)
		if sqs_err != nil {
			return sqs_err
		}

		sqs_err = validation.ValidateMessageAttrName(reqMsgAttr.DataType)
		if sqs_err != nil {
			return sqs_err
		}

		if reqMsgAttr.BinaryValue != "" && reqMsgAttr.StringValue != "" {
			return sqserr.InvalidParameterValueError(
				"Message attribute name '%s' has multiple values.", reqMsgAttr.Name)
		}

		if _, ok := outAttrs[reqMsgAttr.Name]; ok {
			return sqserr.InvalidParameterValueError(
				"Message attribute name '%s' already exists.", reqMsgAttr.Name)
		}

		if strings.HasPrefix(reqMsgAttr.DataType, "Number") {
			if _, err := strconv.Atoi(reqMsgAttr.StringValue); err != nil {
				return sqserr.InvalidParameterValueError(
					"Could not cast message attribute '%s' value to number.", reqMsgAttr.Name)
			}
		}

		if reqMsgAttr.BinaryValue != "" {
			if reqMsgAttr.DataType != "Binary" {
				return sqserr.InvalidParameterValueError(
					"The message attribute '%s' with type 'Binary' must use field 'Binary'", reqMsgAttr.Name)
			}
			outAttrs[reqMsgAttr.Name] = &sqsmsg.UserAttribute{
				Type:  reqMsgAttr.DataType,
				Value: reqMsgAttr.BinaryValue,
			}
			continue
		}

		if reqMsgAttr.StringValue != "" {
			if reqMsgAttr.DataType != "String" && reqMsgAttr.DataType != "Number" {
				return sqserr.InvalidParameterValueError(
					"The message attribute '%s' with type 'String' must use field 'String'", reqMsgAttr.Name)
			}
			outAttrs[reqMsgAttr.Name] = &sqsmsg.UserAttribute{
				Type:  reqMsgAttr.DataType,
				Value: reqMsgAttr.StringValue,
			}
		}
	}

	msgId := IdGen.RandId()
	if out.DelaySeconds < 0 {
		out.DelaySeconds = pq.Config().DeliveryDelay
	} else if out.DelaySeconds > conf.CFG_PQ.MaxDeliveryDelay {
		return sqserr.InvalidParameterValueError(
			"Delay secods must be between 0 and %d", conf.CFG_PQ.MaxDeliveryDelay/1000)
	}
	bodyMd5str := fmt.Sprintf("%x", md5.Sum(enc.UnsafeStringToBytes(out.MessageBody)))
	attrMd5 := CalcAttrMd5(outAttrs)

	msgPayload := sqsmsg.SQSMessagePayload{
		UserAttributes:         outAttrs,
		MD5OfMessageBody:       bodyMd5str,
		MD5OfMessageAttributes: attrMd5,
		SenderId:               senderId,
		SentTimestamp:          strconv.FormatInt(utils.Uts(), 10),
		Payload:                out.MessageBody,
	}

	d, marshalErr := msgPayload.Marshal()
	if marshalErr != nil {
		log.Error("Failed to serialize message payload: %v", err)
	}
	payload := enc.UnsafeBytesToString(d)

	resp := pq.Push(msgId, payload, pq.Config().MsgTtl, out.DelaySeconds, 1)
	if resp.IsError() {
		e, _ := resp.(error)
		return sqserr.InvalidParameterValueError(e.Error())
	}

	return &SendMessageResponse{
		MessageId:              msgId,
		MD5OfMessageBody:       bodyMd5str,
		MD5OfMessageAttributes: attrMd5,
		RequestId:              "req",
	}
}