コード例 #1
0
ファイル: server.go プロジェクト: alvin921/FishChatServer
func (self *Monitor) connectMsgServer(ms string) (*libnet.Session, error) {
	client, err := libnet.Dial("tcp", ms)
	if err != nil {
		log.Error(err.Error())
		panic(err)
	}

	return client, err
}
コード例 #2
0
ファイル: client.go プロジェクト: alvin921/FishChatServer
// return (ClientID, MsgServerAddr)
func login_gateway(self *Client) error {
	fmt.Println("req GatewayServer...")

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

	cmd := protocol.NewCmdSimple(protocol.REQ_LOGIN_CMD)
	cmd.AddArg(self.clientID)
	cmd.AddArg(self.clientType)
	cmd.AddArg(self.clientPwd)

	err = gatewayClient.Send(libnet.Json(cmd))
	if 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...")

	if c.GetArgs()[0] != protocol.RSP_SUCCESS {
		log.Errorf("login gateway error: %s", c.GetArgs()[0])
		return errors.New(c.GetArgs()[0])
	}
	self.uuid = c.GetArgs()[1]
	self.msAddr = c.GetArgs()[2]

	return nil
}
コード例 #3
0
ファイル: client.go プロジェクト: alvin921/FishChatServer
func login_server(self *Client) error {
	var err error

	self.session, err = libnet.Dial("tcp", self.msAddr)
	if err != nil {
		panic(err)
	}

	fmt.Println("req to login msg server...")
	cmd := protocol.NewCmdSimple(protocol.REQ_LOGIN_CMD)
	cmd.AddArg(self.clientID)
	cmd.AddArg(self.uuid)

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

	var c protocol.CmdSimple
	err = self.session.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())
	}
	if c.GetArgs()[0] != protocol.RSP_SUCCESS {
		log.Errorf("login msgserver error: %s", c.GetArgs()[0])
		return errors.New(c.GetArgs()[0])
	}

	fmt.Println("login msg server SUCCESS")
	return nil
}
コード例 #4
0
func main() {
	flag.Parse()
	cfg, err := LoadConfig(*InputConfFile)
	if err != nil {
		glog.Error(err.Error())
		return
	}

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

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

	//glog.Info("...")
	cmd := protocol.NewCmdSimple(protocol.REQ_MSG_SERVER_CMD)

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

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

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

	go heartBeat(cfg, msgServerClient)

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

	go msgServerClient.Process(func(msg *libnet.InBuffer) error {
		//glog.Info(string(msg.Data))
		err = json.Unmarshal(msg.Data, &c)
		if err != nil {
			glog.Error("error:", err)
		}
		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 {
			glog.Error(err.Error())
		}

		cmd.AddArg(input)

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

		cmd.AddArg(input)

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

		cmd.AddArg(input)

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

	defer msgServerClient.Close()

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

	glog.Flush()
}
コード例 #5
0
func main() {
	flag.Parse()
	cfg, err := LoadConfig(*InputConfFile)
	if err != nil {
		glog.Error(err.Error())
		return
	}

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

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

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

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

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

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

	go heartBeat(cfg, msgServerClient)

	glog.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 {
		glog.Error(err.Error())
	}
	if input == "y" {
		fmt.Println("CREATE_TOPIC_CMD | input topic name :")
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			glog.Error(err.Error())
		}

		cmd.AddArg(input)

		err = msgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			glog.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 {
		glog.Error(err.Error())
	}

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

	cmd.AddArg(input)

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

	err = msgServerClient.ProcessOnce(func(msg *libnet.InBuffer) error {
		glog.Info(string(msg.Data))
		err = json.Unmarshal(msg.Data, &c)
		if err != nil {
			glog.Error("error:", err)
		}
		return nil
	})
	if err != nil {
		glog.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)
		}

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

		err = newMsgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			glog.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 {
			glog.Error(err.Error())
		}

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

		cmd.AddArg(input)

		err = newMsgServerClient.Send(libnet.Json(cmd))
		if err != nil {
			glog.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 {
			glog.Error(err.Error())
		}

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

		cmd.AddArg(input)

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

		newMsgServerClient.Process(func(msg *libnet.InBuffer) error {
			glog.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 {
			glog.Error(err.Error())
		}

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

		cmd.AddArg(input)

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

		defer msgServerClient.Close()

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