コード例 #1
0
ファイル: client_topic.go プロジェクト: miffa/FishChatServer
func main() {
	flag.Parse()
	cfg, err := LoadConfig(*InputConfFile)
	if err != nil {
		log.Error(err.Error())
		return
	}

	gatewayClient, err := libnet.Dial("tcp", cfg.GatewayServer)
	if err != nil {
		panic(err)
	}

	log.Info("req msg_server...")
	cmd := protocol.NewCmdSimple(protocol.REQ_MSG_SERVER_CMD)

	err = gatewayClient.Send(libnet.Json(cmd))
	if err != nil {
		log.Error(err.Error())
	}

	fmt.Println("input my id :")
	var input string
	if _, err := fmt.Scanf("%s\n", &input); err != nil {
		log.Error(err.Error())
	}
	var c protocol.CmdSimple
	err = gatewayClient.ProcessOnce(func(msg *libnet.InBuffer) error {
		log.Info(string(msg.Data))
		err = json.Unmarshal(msg.Data, &c)
		if err != nil {
			log.Error("error:", err)
		}
		return nil
	})
	if err != nil {
		log.Error(err.Error())
	}

	gatewayClient.Close()

	msgServerClient, err := libnet.Dial("tcp", string(c.GetArgs()[0]))
	if err != nil {
		panic(err)
	}

	msgServerMap[string(c.GetArgs()[0])] = msgServerClient

	log.Info("test.. send id...")
	cmd = protocol.NewCmdSimple(protocol.SEND_CLIENT_ID_CMD)
	cmd.AddArg(input)

	err = msgServerClient.Send(libnet.Json(cmd))
	if err != nil {
		log.Error(err.Error())
	}

	go heartBeat(cfg, msgServerClient)

	log.Info("test.. send topic msg...")
	cmd = protocol.NewCmdSimple(protocol.CREATE_TOPIC_CMD)
	fmt.Println("want to create a topic (y/n) :")
	if _, err := fmt.Scanf("%s\n", &input); err != nil {
		log.Error(err.Error())
	}
	if input == "y" {
		fmt.Println("CREATE_TOPIC_CMD | input topic name :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)

		err = msgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}
	}

	cmd = protocol.NewCmdSimple(protocol.JOIN_TOPIC_CMD)

	fmt.Println("JOIN_TOPIC_CMD | input topic name :")
	if _, err := fmt.Scanf("%s\n", &input); err != nil {
		log.Error(err.Error())
	}

	cmd.AddArg(input)
	fmt.Println("JOIN_TOPIC_CMD | input your ID :")
	if _, err := fmt.Scanf("%s\n", &input); err != nil {
		log.Error(err.Error())
	}

	cmd.AddArg(input)

	err = msgServerClient.Send(libnet.Json(cmd))
	if err != nil {
		log.Error(err.Error())
	}

	err = msgServerClient.ProcessOnce(func(msg *libnet.InBuffer) error {
		log.Info(string(msg.Data))
		err = json.Unmarshal(msg.Data, &c)
		if err != nil {
			log.Error("error:", err)
		}
		return nil
	})
	if err != nil {
		log.Error(err.Error())
	}

	topicMsgServerAddrstring := string(c.GetArgs()[0])
	topicName := string(c.GetArgs()[1])

	fmt.Println("topicMsgServerAddrstring :" + topicMsgServerAddrstring)
	fmt.Println("topicName :" + topicName)

	if _, f := msgServerMap[topicMsgServerAddrstring]; f == false {
		fmt.Println("to another server")
		newMsgServerClient, err := libnet.Dial("tcp", topicMsgServerAddrstring)
		if err != nil {
			panic(err)
		}

		log.Info("test.. send id...")
		cmd = protocol.NewCmdSimple(protocol.SEND_CLIENT_ID_CMD)
		cmd.AddArg(input)

		err = newMsgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}

		go heartBeat(cfg, newMsgServerClient)

		cmd = protocol.NewCmdSimple(protocol.JOIN_TOPIC_CMD)

		fmt.Println("JOIN_TOPIC_CMD | input topic name :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)
		fmt.Println("JOIN_TOPIC_CMD | input your ID :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)

		err = newMsgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}

		msgServerMap[topicMsgServerAddrstring] = newMsgServerClient

		cmd = protocol.NewCmdSimple(protocol.SEND_MESSAGE_TOPIC_CMD)

		fmt.Println("SEND_MESSAGE_TOPIC_CMD | input topic name :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)
		fmt.Println("SEND_MESSAGE_TOPIC_CMD | input message :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)

		err = newMsgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}

		newMsgServerClient.Process(func(msg *libnet.InBuffer) error {
			log.Info(string(msg.Data))
			return nil
		})
	} else {
		cmd = protocol.NewCmdSimple(protocol.SEND_MESSAGE_TOPIC_CMD)

		fmt.Println("SEND_MESSAGE_TOPIC_CMD | input topic name :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)
		fmt.Println("SEND_MESSAGE_TOPIC_CMD | input message :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)

		err = msgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}

		defer msgServerClient.Close()

		msgServerClient.Process(func(msg *libnet.InBuffer) error {
			log.Info(string(msg.Data))
			return nil
		})
	}
	log.Flush()
}
コード例 #2
0
ファイル: client_p2p.go プロジェクト: miffa/FishChatServer
func main() {
	flag.Parse()
	cfg, err := LoadConfig(*InputConfFile)
	if err != nil {
		log.Error(err.Error())
		return
	}

	fmt.Println("req GatewayServer...")

	gatewayClient, err := libnet.Dial("tcp", cfg.GatewayServer)
	if err != nil {
		panic(err)
	}

	cmd := protocol.NewCmdSimple(protocol.REQ_MSG_SERVER_CMD)

	err = gatewayClient.Send(libnet.Json(cmd))
	if err != nil {
		log.Error(err.Error())
	}

	fmt.Println("input my id :")
	var myID string
	var input string
	if _, err := fmt.Scanf("%s\n", &myID); err != nil {
		log.Error(err.Error())
	}
	var c protocol.CmdSimple
	err = gatewayClient.ProcessOnce(func(msg *libnet.InBuffer) error {
		log.Info(string(msg.Data))
		err = json.Unmarshal(msg.Data, &c)
		if err != nil {
			log.Error("error:", err)
		}
		return nil
	})
	if err != nil {
		log.Error(err.Error())
	}

	gatewayClient.Close()

	fmt.Println("req GatewayServer end...")

	msgServerClient, err := libnet.Dial("tcp", string(c.GetArgs()[0]))
	if err != nil {
		panic(err)
	}

	fmt.Println("send your id...")
	cmd = protocol.NewCmdSimple(protocol.SEND_CLIENT_ID_CMD)
	cmd.AddArg(myID)

	err = msgServerClient.Send(libnet.Json(cmd))
	if err != nil {
		log.Error(err.Error())
	}

	go heartBeat(cfg, msgServerClient)

	//log.Info("the msg you want to send...")

	go msgServerClient.Process(func(msg *libnet.InBuffer) error {
		log.Info(string(msg.Data))
		err = json.Unmarshal(msg.Data, &c)
		if err != nil {
			log.Error("error:", err)
		}

		fmt.Println("my uuid is : ", c.GetArgs()[2])

		cmd = protocol.NewCmdSimple(protocol.P2P_ACK_CMD)
		cmd.AddArg(myID)
		cmd.AddArg(c.GetArgs()[2])

		err = msgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}

		fmt.Println(c.GetArgs()[1] + "  says : " + c.GetArgs()[0])

		return nil
	})

	for {
		cmd = protocol.NewCmdSimple(protocol.SEND_MESSAGE_P2P_CMD)

		fmt.Println("send the id you want to talk :")
		if _, err = fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)

		fmt.Println("input msg :")
		if _, err = fmt.Scanf("%s\n", &input); err != nil {
			log.Error(err.Error())
		}

		cmd.AddArg(input)

		cmd.AddArg(myID)

		err = msgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}
	}

	defer msgServerClient.Close()

	// msgServerClient.Process(func(msg *libnet.InBuffer) error {
	// 	log.Info(string(msg.Data))
	// 	return nil
	// })

	log.Flush()
}