Example #1
0
// [msg, fromID, uuid]
func incoming_p2p_msg(self *Client, c *protocol.CmdSimple) error {
	fmt.Println(c.GetArgs()[1] + "  says : " + c.GetArgs()[0])
	if len(c.GetArgs()) >= 3 {
		cmd := protocol.NewCmdSimple(protocol.IND_ACK_P2P_STATUS_CMD)
		cmd.AddArg(c.GetArgs()[2])
		cmd.AddArg(protocol.P2P_ACK_READ)
		cmd.AddArg(c.GetArgs()[1])

		err := self.session.Send(libnet.Json(cmd))
		if err != nil {
			log.Error(err.Error())
		}
	}
	return nil
}
Example #2
0
// 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
}
Example #3
0
func incoming_topic_msg(self *Client, c *protocol.CmdSimple) error {
	msg := c.GetArgs()[0]
	topicName := c.GetArgs()[1]
	fromID := c.GetArgs()[2]
	fromType := c.GetArgs()[3]
	fmt.Println("Topic message received :")
	fmt.Println("    TopicName :" + topicName)
	fmt.Println("    FromID    :" + fromID)
	fmt.Println("    FromType  :" + fromType)
	fmt.Println("    Message   :" + msg)
	return nil
}
Example #4
0
func cmd_sendto_rsp(self *Client, c *protocol.CmdSimple) error {
	fmt.Println(c.GetCmdName() + " returns: " + c.GetArgs()[0])
	if c.GetArgs()[0] == protocol.RSP_SUCCESS {
		fmt.Println("uuid: " + c.GetArgs()[1])
	}
	return nil
}
Example #5
0
func cmd_kick_rsp(self *Client, c *protocol.CmdSimple) error {
	fmt.Println(c.GetCmdName() + " returns: " + c.GetArgs()[0])
	if c.GetArgs()[0] != protocol.RSP_SUCCESS {
		return errors.New(c.GetArgs()[0])
	}
	return nil
}
Example #6
0
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
}
Example #7
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()
}
Example #8
0
func cmd_login(self *Client, args []string) error {
	if self.bLogin {
		fmt.Println("You are already login, pls logout first")
		return nil
	}
	if len(args) != 3 && len(args) != 4 {
		return common.SYNTAX_ERROR
	}
	if args[2] != "D" && args[2] != "d" && args[2] != "C" && args[2] != "c" {
		return common.SYNTAX_ERROR
	}

	self.clientID = args[1]

	if args[2] == "D" || args[2] == "d" {
		self.clientType = protocol.DEV_TYPE_WATCH
	} else if args[2] == "C" || args[2] == "c" {
		self.clientType = protocol.DEV_TYPE_CLIENT
	} else {
	}

	self.clientPwd = ""
	if self.clientType == protocol.DEV_TYPE_CLIENT {
		if len(args) != 4 {
			return common.SYNTAX_ERROR
		}
		self.clientPwd = args[3]
	}
	// Load config
	fmt.Println("config file:" + *InputConfFile)

	fmt.Println("config file loaded")
	err := login_gateway(self)
	if err != nil {
		panic(err)
	}

	err = login_server(self)
	if err != nil {
		panic(err)
	}

	self.bLogin = true

	go heartBeat(self.cfg, self.session)

	var c protocol.CmdSimple
	go self.session.Process(func(msg *libnet.InBuffer) error {
		fmt.Println(string(msg.Data))
		err = json.Unmarshal(msg.Data, &c)
		if err != nil {
			log.Error("error:", err)
		}

		fmt.Println("msg received is : " + c.GetCmdName())
		switch c.GetCmdName() {

		case protocol.RSP_GET_TOPIC_LIST_CMD:
			cmd_topic_rsp(self, &c)

		case protocol.RSP_GET_TOPIC_PROFILE_CMD:
			cmd_topic_profile_rsp(self, &c)

		case protocol.RSP_CREATE_TOPIC_CMD:
			cmd_new_rsp(self, &c)

		case protocol.RSP_JOIN_TOPIC_CMD:
			cmd_join_rsp(self, &c)

		case protocol.RSP_QUIT_TOPIC_CMD:
			cmd_quit_rsp(self, &c)

		case protocol.RSP_ADD_2_TOPIC_CMD:
			cmd_add_rsp(self, &c)

		case protocol.RSP_KICK_TOPIC_CMD:
			cmd_kick_rsp(self, &c)

		case protocol.RSP_SEND_P2P_MSG_CMD:
			cmd_sendto_rsp(self, &c)

		case protocol.IND_ACK_P2P_STATUS_CMD:
			fmt.Println("msg sent [uuid=" + c.GetArgs()[0] + "] status: " + c.GetArgs()[1])

		case protocol.IND_SEND_P2P_MSG_CMD:
			incoming_p2p_msg(self, &c)

		case protocol.IND_SEND_TOPIC_MSG_CMD:
			incoming_topic_msg(self, &c)
		}

		return nil
	})
	return nil
}
Example #9
0
func cmd_send_rsp(self *Client, c *protocol.CmdSimple) error {
	fmt.Println(c.GetCmdName() + " returns: " + c.GetArgs()[0])
	return nil
}
Example #10
0
func cmd_topic_profile_rsp(self *Client, c *protocol.CmdSimple) error {
	var num int

	fmt.Println(c.GetCmdName() + " returns: " + c.GetArgs()[0])
	if c.GetArgs()[0] != protocol.RSP_SUCCESS {
		return errors.New(c.GetArgs()[0])
	}
	fmt.Println("GET_TOPIC_PROFILE returns : ")
	fmt.Println("    topicName: " + c.GetArgs()[1])
	fmt.Println("    creator: " + c.GetArgs()[2])
	fmt.Println("    members[]: ")
	num = (len(c.GetArgs()) - 3) / 3
	index := 0
	for {
		if index == num {
			break
		} else {
			fmt.Println("        " + c.GetArgs()[3+3*index] + "\t, " + c.GetArgs()[3+3*index+1] + "\t, " + c.GetArgs()[3+3*index+2])
			index++
		}
	}

	return nil
}
Example #11
0
func cmd_topic_rsp(self *Client, c *protocol.CmdSimple) error {
	var num int
	var err error

	fmt.Println(c.GetCmdName() + " returns: " + c.GetArgs()[0])
	if c.GetArgs()[0] != protocol.RSP_SUCCESS {
		return errors.New(c.GetArgs()[0])
	}
	if num, err = strconv.Atoi(c.GetArgs()[1]); err != nil {
		fmt.Println(err.Error())
		log.Error(err.Error())
		return err
	}
	fmt.Println("GET_TOPIC_LIST returns (" + c.GetArgs()[1] + "): ")
	index := 0
	for {
		if index == num {
			break
		} else {
			fmt.Println(c.GetArgs()[2+index])
			index++
		}
	}

	return nil
}
Example #12
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()
}