Пример #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)
	}
}
Пример #2
0
func subPut(conn *client.Conn, args []string) {
	conn.SubPut([]byte(args[1]), []byte(args[2]), encode(args[3]))
}