Example #1
0
func handleUDP(ch chan []byte, c *client.Conn, queueSize *int32) {
	var err error
	var mess common.Message
	for bytes := range ch {
		err = json.Unmarshal(bytes, &mess)
		if err == nil {
			if mess.Type == common.View {
				// Create a byte encoded timestamp for now
				t := time.Now().UnixNano()
				encT := godCommon.EncodeInt64(t)
				// Make the object id active
				c.SubPut(activeObjectsKey, []byte(mess.Object), encT)
				// Create a key for the views of this user
				vKey := uViewsKey(mess.User)
				// Make sure the sub tree is mirrored
				c.SubAddConfiguration(vKey, "mirrored", "yes")
				// Log this view
				c.SubPut(vKey, []byte(mess.Object), encT)
				// Create an encoded timestamp for something older than timeout
				tooOld := time.Now().Add(-time.Hour * 24 * time.Duration((*timeout))).UnixNano()
				// Delete all viewed entries with timestamp older than that
				for _, item := range c.MirrorSlice(vKey, nil, godCommon.EncodeInt64(tooOld), true, true) {
					c.SubDel(vKey, item.Value)
				}
				// Delete all active entries with timestamp older than that
				for _, item := range c.MirrorSlice(activeObjectsKey, nil, godCommon.EncodeInt64(tooOld), true, true) {
					c.SubDel(activeObjectsKey, item.Value)
				}
			} else if mess.Type == common.Like {
				// Record the liked object under user
				c.SubPut(uLikesKey(mess.User), []byte(mess.Object), godCommon.EncodeFloat64(mess.Weight))
				// Record the liker under the liked object
				c.SubPut(oLikesKey(mess.Object), []byte(mess.User), nil)
				if !mess.DontActivate {
					// Make the object id active
					c.SubPut(activeObjectsKey, []byte(mess.Object), nil)
				}
				// Create an encoded timestamp for something older than timeout
				tooOld := time.Now().Add(-time.Hour * 24 * time.Duration((*timeout))).UnixNano()
				// Delete all active entries with timestamp older than that
				for _, item := range c.MirrorSlice(activeObjectsKey, nil, godCommon.EncodeInt64(tooOld), true, true) {
					c.SubDel(activeObjectsKey, item.Value)
				}
			} else if mess.Type == common.Deactivate {
				// Remote the object id from the active objects
				c.SubDel(activeObjectsKey, []byte(mess.Object))
			}
		} else {
			fmt.Printf("When parsing %v: %v\n", string(bytes), err)
		}
		atomic.AddInt32(queueSize, -1)
	}
}
Example #2
0
func encode(s string) []byte {
	switch *enc {
	case stringFormat:
		return []byte(s)
	case floatFormat:
		result, err := strconv.ParseFloat(s, 64)
		if err != nil {
			panic(err)
		}
		return common.EncodeFloat64(result)
	case intFormat:
		result, err := strconv.ParseInt(s, 10, 64)
		if err != nil {
			panic(err)
		}
		return common.EncodeInt64(result)
	case bigFormat:
		result, ok := new(big.Int).SetString(s, 10)
		if !ok {
			panic(fmt.Errorf("Bad BigInt format: %v", s))
		}
		return common.EncodeBigInt(result)
	}
	panic(fmt.Errorf("Unknown encoding: %v", *enc))
}
Example #3
0
func TestIntegerSum(t *testing.T) {
	found := integerSum([][]byte{common.EncodeInt64(1)}, [][]byte{common.EncodeInt64(2), common.EncodeInt64(3), common.EncodeInt64(4)}, 1)
	expected := [][]byte{common.EncodeInt64(10)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", found, expected)
	}
	found = integerSum(nil, [][]byte{common.EncodeInt64(1), common.EncodeInt64(2), common.EncodeInt64(3), common.EncodeInt64(4)}, 1)
	expected = [][]byte{common.EncodeInt64(10)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", found, expected)
	}
	found = integerSum([][]byte{common.EncodeInt64(1)}, [][]byte{common.EncodeInt64(2)}, 2)
	expected = [][]byte{common.EncodeInt64(5)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", found, expected)
	}
}