func TestClient() { time.Sleep(1000 * time.Millisecond) conn, err := net.Dial("tcp", "localhost:7777") if err != nil { fmt.Println(err) } conn.Write([]byte(command.EncodeRedisProtocol(command.DecodeText("set clive handsome")))) response, _ := bufio.NewReader(conn).ReadString('\n') fmt.Println("receive response: " + response) conn.Write([]byte(command.EncodeRedisProtocol(command.DecodeText(("get clive"))))) response, _ = bufio.NewReader(conn).ReadString('\n') fmt.Println("receive response: " + response) }
func HandleConnection(conn net.Conn) { for { var cmd *command.Command = command.DecodeRedisProtocol(conn) if cmd == nil { break } //fmt.Println("recv: "+ command.EncodeText(*cmd)) //通过key的第一部分来确定分区 if cmd.Argc > 1 { index := strings.Index(cmd.Args[1], ".") if index == -1 { cmd.PartitionKey = cmd.Args[1] } else { cmd.PartitionKey = cmd.Args[1][:index] } server, _ := c.Get(cmd.PartitionKey) //fmt.Println("lookup node for key:"+cmd.PartitionKey+"->"+server) if server != hostname { //fmt.Println("forward to another node!") Send(server, command.EncodeRedisProtocol(*cmd)) } else { DoCommand(conn, *cmd) } } else { DoCommand(conn, *cmd) } } }
func main() { Init() hostname, _ = os.Hostname() servers = make(map[string]string) servers["clive"] = ip // servers["clive0"]="7.7.7.7" // servers["clive1"]="7.7.7.8" // servers["clive2"]="7.7.7.9" for k, _ := range servers { c.Add(k) } fmt.Println(flag.Args()) if flag.Args()[1] == "server" { if flag.Args()[0] == "start" { StartServer() } else if flag.Args()[0] == "stop" { //quit <- 0 } } if flag.Args()[1] == "client" { fmt.Print("life is short, i use go -.-\n>") conn, _ := net.Dial("tcp", server) for { line, _, _ := bufio.NewReader(os.Stdin).ReadLine() conn.Write([]byte(command.EncodeRedisProtocol(command.DecodeText(string(line))))) response, _ := bufio.NewReader(conn).ReadBytes('\n') fmt.Println(response) fmt.Print(">") } } //client.TestClient() //load Graph, why so slow if flag.Args()[1] == "loadGraph" { time.Sleep(100 * time.Millisecond) fmt.Println("loadGraph start: " + time.Now().Format("2006-01-02 15:04:05")) client.LoadWebGraph() fmt.Println("loadGraph end: " + time.Now().Format("2006-01-02 15:04:05")) } if flag.Args()[1] == "pageRank" { fmt.Println("pagerank start: " + time.Now().Format("2006-01-02 15:04:05")) client.PageRank() fmt.Println("pagerank end: " + time.Now().Format("2006-01-02 15:04:05")) time.Sleep(1000 * time.Second) } }
func SendCommand(conn net.Conn, cmd command.Command) string { conn.Write([]byte(command.EncodeRedisProtocol(cmd))) response, _, _ := bufio.NewReader(conn).ReadLine() return string(response) //fmt.Println("receive response: "+response) }