Beispiel #1
0
func TestLuaEval(t *T) {
	c1, err := redis.Dial("tcp", "127.0.0.1:6379")
	require.Nil(t, err)
	c2, err := cluster.New("127.0.0.1:7000")
	require.Nil(t, err)

	cs := []Cmder{c1, c2}

	for _, c := range cs {
		script, key, val := randTestScript()
		s, err := LuaEval(c, script, 1, key, val).Str()
		require.Nil(t, err)
		assert.Equal(t, "OK", s)

		// The second time the command will be hashed
		script, key, val = randTestScript()
		s, err = LuaEval(c, script, 1, key, val).Str()
		require.Nil(t, err)
		assert.Equal(t, "OK", s)

		s, err = c.Cmd("GET", key).Str()
		require.Nil(t, err)
		assert.Equal(t, val, s)
	}
}
Beispiel #2
0
func main() {
	c, err := cluster.New("localhost:7000")
	if err != nil {
		log.Fatal(err)
	}

	oldKeys := make(chan string, 1000)

	doRand := time.Tick(100 * time.Millisecond)
	doOldRand := time.Tick(1 * time.Second)

	for {
		select {
		case <-doRand:
			key := randString()
			doGetSet(c, key)
			select {
			case oldKeys <- key:
			default:
			}

		case <-doOldRand:
			select {
			case key := <-oldKeys:
				doGetSet(c, key)
			default:
			}
		}
	}
}
Beispiel #3
0
func main() {
	fromInfo := entity.NewFromInfo()
	fromInfo.Ctx = 1
	fromInfo.FromUid = 1012616
	fromInfo.FromIid = "Qereodsjfeefjdfsfsd"
	fromInfo.FromNjs = "node-01"

	client, _ := cluster.New(hostRedis)
	for {
		str, _ := client.Cmd("lrange", "op_1012623_xxxxxxxxxxxxxxxxsdsdsdxxx", 0, -1).Str()

		fmt.Println("--: " + str)
	}
}
Beispiel #4
0
// Similar to TestScan, but scans over a whole cluster
func TestClusterScan(t *T) {
	cluster, err := cluster.New("127.0.0.1:7000")
	require.Nil(t, err)

	prefix := "scanTestPrefix"

	fullMap := map[string]bool{}
	for i := 0; i < 100; i++ {
		key := prefix + ":" + strconv.Itoa(i)
		fullMap[key] = true
		require.Nil(t, cluster.Cmd("SET", key, "1").Err)
	}

	// make sure we get all results when scanning with an existing prefix
	ch := make(chan string)
	go func() {
		err = Scan(cluster, ch, "SCAN", "", prefix+":*")
	}()
	testMap := map[string]bool{}
	for key := range ch {
		testMap[key] = true
	}
	require.Nil(t, err)
	assert.Equal(t, fullMap, testMap)

	// make sure we don't get any results when scanning with a non-existing
	// prefix
	ch = make(chan string)
	go func() {
		err = Scan(cluster, ch, "SCAN", "", prefix+"DNE:*")
	}()
	testMap = map[string]bool{}
	for key := range ch {
		testMap[key] = true
	}
	require.Nil(t, err)
	assert.Equal(t, map[string]bool{}, testMap)
}
Beispiel #5
0
// Similar to TestScanner, but scans over a whole cluster
func TestScannerCluster(t *T) {
	cluster, err := cluster.New("127.0.0.1:7000")
	require.Nil(t, err)
	prefix, fullMap := randPrefix(t, cluster, 100)

	// make sure we get all results when scanning with an existing prefix
	testMap := map[string]bool{}
	sc := NewScanner(cluster, ScanOpts{Command: "SCAN", Pattern: prefix + ":*"})
	for sc.HasNext() {
		testMap[sc.Next()] = true
	}
	require.Nil(t, sc.Err())
	assert.Equal(t, fullMap, testMap)

	// make sure we don't get any results when scanning with a non-existing
	// prefix
	testMap = map[string]bool{}
	sc = NewScanner(cluster, ScanOpts{Command: "SCAN", Pattern: prefix + "DNE:*"})
	for sc.HasNext() {
		testMap[sc.Next()] = true
	}
	require.Nil(t, sc.Err())
	assert.Empty(t, testMap)
}
Beispiel #6
0
func main() {
	// redis
	redisClient, _ := cluster.New(hostRedis)
	go func() {
		for {
			//			str, err := redisClient.Cmd("rpop", fmt.Sprintf("%d_%s", UID, IID)).Str()
			//			if err != nil {
			//				log.Println("noti redis error:", err)
			//			}
			//			if str != "" {
			//				fmt.Println("--: " + str)
			//			}

			str, _ := redisClient.Cmd("rpop", fmt.Sprintf("op_%d_%s", UID, IID)).Str()
			//			if err != nil {
			//				log.Println("op redis error:", err)
			//			}
			if str != "" {
				fmt.Println("--: " + str)
			}

			//				dec := json.NewDecoder(strings.NewReader(str))
			//				dec.UseNumber()
			//				udsResult := &UdsResult{}
			//				dec.Decode(udsResult)

			//				ctx := udsResult.Ctx
			//				context, ok := udsCallCtxs[ctx]
			//				if !ok {
			//					fmt.Println(ctx, "is nil. mn: ", udsResult.Mn)
			//					continue
			//				}

			//				clientResult := &ClientResult{}
			//				clientResult.Ctx = context.ClientContextId
			//				code, _ := strconv.ParseInt(udsResult.Code, 10, 32)
			//				clientResult.Code = int(code)
			//				clientResult.Mn = context.ClientMn + "_result"
			//				clientResult.Body = udsResult.Body

			//				b, _ := json.Marshal(clientResult)

			//				so := context.So
			//				(*so).Emit("videbonjs10", string(b))
			//				delete(udsCallCtxs, ctx)

		}
	}()
	// uds
	socket, _ := thrift.NewTSocketTimeout(hostUds, 3*time.Second)
	transport := thrift.NewTBufferedTransport(socket, 10240)
	protocol := thrift.NewTBinaryProtocolTransport(transport)
	//mprotocol := thrift.NewTMultiplexedProtocol(protocol, "userManager")
	//userManagerClient = service.NewUserManagerClientProtocol(transport, protocol, mprotocol)
	mprotocol := thrift.NewTMultiplexedProtocol(protocol, "chatManager")
	chatManagerClient := service.NewChatManagerClientProtocol(transport, protocol, mprotocol)
	err := transport.Open()
	if err != nil {
		log.Fatal("transport open error:", err)
	}

	fromInfo := entity.NewFromInfo()
	fromInfo.Ctx = getUdsCtx()
	fromInfo.FromUid = UID
	fromInfo.FromIid = IID
	fromInfo.FromNjs = NJS

	//	context := Context{}
	//	context.So = so
	//	context.ClientContextId = msg.Ctx
	//	context.ClientMn = msg.Mn
	//udsCallCtxs[fromInfo.Ctx] = context

	var index int64 = 0
	for i := 0; i < 100; i++ {
		func() {
			index++
			log.Println(index)
			message := &entity.ChatMessage{
				Uid:     1000501,
				Gid:     803,
				Cmt:     1,
				Content: fmt.Sprintf("message testing %d", index),
			}
			mid, err := chatManagerClient.SendChatMessage(message, fromInfo)
			if err != nil {
				log.Fatal("SendChatMessage error:", err)
				//delete(udsCallCtxs, fromInfo.Ctx)
			}
			log.Println("mid:", mid)
		}()

		//		err = transport.Close()
		//		if err != nil {
		//			log.Fatal("transport close error:", err)
		//		}
	}

	time.Sleep(10 * time.Second)

	//	// socketio
	//	server, err := socketio.NewServer(nil)
	//	if err != nil {
	//		log.Fatal(err)
	//	}
	//	server.On("connection", func(so socketio.Socket) {
	//		log.Println("on connection")
	//		so.Join("chat")
	//		so.On("videboc10", func(msg ClientMessage) {
	//			log.Println("on videboc10:", msg)

	//			handleClientMessage(msg, &so)
	//		})
	//		so.On("disconnection", func(reason string) {
	//			log.Println("on disconnection. reason: " + reason)
	//		})

	//		so.On("error", func(error string) {
	//			log.Println("on error. error:", error)
	//		})
	//	})
	//	server.On("error", func(so socketio.Socket, err error) {
	//		log.Println("error:", err)
	//	})

	//	http.Handle("/socket.io/", server)
	//	http.Handle("/", http.FileServer(http.Dir("./asset")))
	//	log.Println("Serving at localhost:5000...")
	//	log.Fatal(http.ListenAndServe(":5000", nil))
}
Beispiel #7
0
func main() {
	socket, _ := thrift.NewTSocketTimeout(hostUds, 30000000)
	transport := thrift.NewTBufferedTransport(socket, 10240)
	protocol := thrift.NewTBinaryProtocolTransport(transport)
	mprotocol := thrift.NewTMultiplexedProtocol(protocol, "userManager")
	userManagerClient := service.NewUserManagerClientProtocol(transport, protocol, mprotocol)
	_ = transport.Open()

	fromInfo := entity.NewFromInfo()
	fromInfo.Ctx = 1
	fromInfo.FromUid = UID
	fromInfo.FromIid = IID
	fromInfo.FromNjs = NJS

	redisClusterClient, _ := cluster.New(hostRedis)
	count := 0
	go func() {
		for {
			udsResult, _ := redisClusterClient.Cmd("rpop", fromInfo.FromNjs).Str()
			if udsResult != "" {
				fmt.Println(udsResult)
				count++
				fmt.Println(count)
				result := &UdsResult{}
				dec := json.NewDecoder(strings.NewReader(udsResult))
				dec.UseNumber()

				if err := dec.Decode(result); err != nil {
					log.Fatal(err)
				}

				//				fmt.Println(result)
				//				b, _ := json.Marshal(result)
				//				fmt.Println(string(b))
			}
		}
	}()

	// socketio
	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	server.On("connection", func(so socketio.Socket) {
		log.Println("on connection")
		so.Join("chat")
		so.On("videboc10", func(msg map[string]interface{}) {
			log.Println("on videboc10:", msg)

			dec := json.NewDecoder(strings.NewReader(msg))
			dec.UseNumber()
			clientMessage := &ClientMessage{}
			if err := dec.Decode(clientMessage); err != nil {
				log.Fatal(err)
			}

			log.Println(clientMessage.Mn)

			if "login" == clientMessage.Mn {
				_loginName := clientMessage.Body["login_name"]
				_phoneInfo := clientMessage.Body["phone_info"]
				_encryptMode := clientMessage.Body["encryptMode"]
				_isSecreted := clientMessage.Body["isSecreted"]
				_iid := clientMessage.Body["iid"]

				loginName, _ := _loginName.(string)
				phoneInfo, _ := _phoneInfo.(string)
				encryptMode, _ := _encryptMode.(string) // AES
				isSecreted, _ := _isSecreted.(bool)     // TRUE
				iid, _ := _iid.(string)

				fmt.Println(loginName)
				fmt.Println(phoneInfo)
				fmt.Println(encryptMode)
				fmt.Println(isSecreted)
				fmt.Println(iid)

				reUid := regexp.MustCompile(`^\d{0,10}$`)
				rePhone := regexp.MustCompile(`^\d{11,12}$`)
				if reUid.MatchString(loginName) {
					uid, _ := strconv.ParseInt(loginName, 10, 32)
					err := userManagerClient.GetUserById(uid, fromInfo)
					if err != nil {
						fmt.Println(err)
					}

				} else if rePhone.MatchString(loginName) {

				} else {

				}
			}

		})
		so.On("disconnection", func(reason string) {
			log.Println("on disconnection. reason:", reason)
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		log.Println("error:", err)
	})

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	log.Println("Serving at localhost:5000...")
	log.Fatal(http.ListenAndServe(":5000", nil))
}