Exemple #1
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
}
Exemple #2
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
}
Exemple #3
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
}
Exemple #4
0
func (self *MsgServer) parseProtocol(cmd []byte, session *libnet.Session) error {
	var c protocol.CmdSimple
	err := json.Unmarshal(cmd, &c)
	if err != nil {
		glog.Error("error:", err)
		return err
	}

	pp := NewProtoProc(self)

	switch c.GetCmdName() {
	case protocol.SEND_PING_CMD:
		err = pp.procPing(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	case protocol.SUBSCRIBE_CHANNEL_CMD:
		pp.procSubscribeChannel(&c, session)
	case protocol.SEND_CLIENT_ID_CMD:
		err = pp.procClientID(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	case protocol.SEND_MESSAGE_P2P_CMD:
		err = pp.procSendMessageP2P(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	case protocol.ROUTE_MESSAGE_P2P_CMD:
		err = pp.procRouteMessageP2P(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	case protocol.CREATE_TOPIC_CMD:
		err = pp.procCreateTopic(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	case protocol.JOIN_TOPIC_CMD:
		err = pp.procJoinTopic(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	case protocol.SEND_MESSAGE_TOPIC_CMD:
		err = pp.procSendMessageTopic(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	}

	return err
}
Exemple #5
0
func (self *Gateway) parseProtocol(cmd []byte, session *libnet.Session) error {
	var c protocol.CmdSimple
	err := json.Unmarshal(cmd, &c)
	if err != nil {
		return err
	}

	pp := NewProtoProc(self)

	switch c.GetCmdName() {
	case protocol.REQ_LOGIN_CMD:
		err = pp.procLogin(&c, session)
		if err != nil {
			return err
		}
	}

	return err
}
Exemple #6
0
func (self *Gateway) parseProtocol(cmd []byte, session *libnet.Session) error {
	var c protocol.CmdSimple
	err := json.Unmarshal(cmd, &c)
	if err != nil {
		glog.Error("error:", err)
		return err
	}

	pp := NewProtoProc(self)

	switch c.GetCmdName() {
	case protocol.REQ_MSG_SERVER_CMD:
		err = pp.procReqMsgServer(&c, session)
		if err != nil {
			glog.Error("error:", err)
			return err
		}
	}

	return err
}
Exemple #7
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
}
Exemple #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
}
Exemple #9
0
func cmd_send_rsp(self *Client, c *protocol.CmdSimple) error {
	fmt.Println(c.GetCmdName() + " returns: " + c.GetArgs()[0])
	return nil
}
Exemple #10
0
func (self *MsgServer) parseProtocol(cmd []byte, session *libnet.Session) error {
	var c protocol.CmdSimple

	// receive msg, that means client alive
	if session.State != nil {
		self.scanSessionMutex.Lock()
		session.State.(*base.SessionState).Alive = true
		self.scanSessionMutex.Unlock()
	}
	err := json.Unmarshal(cmd, &c)
	if err != nil {
		log.Error("error:", err)
		return err
	}

	pp := NewProtoProc(self)

	self.readMutex.Lock()
	defer self.readMutex.Unlock()

	log.Infof("[%s]->[%s]", session.Conn().RemoteAddr().String(), self.cfg.LocalIP)
	log.Info(c)
	switch c.GetCmdName() {
	case protocol.SEND_PING_CMD:
		err = pp.procPing(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}
	case protocol.SUBSCRIBE_CHANNEL_CMD:
		pp.procSubscribeChannel(&c, session)

	case protocol.REQ_LOGIN_CMD:
		err = pp.procLogin(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_LOGOUT_CMD:
		err = pp.procLogout(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_SEND_P2P_MSG_CMD:
		err = pp.procSendMessageP2P(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}
	case protocol.ROUTE_SEND_P2P_MSG_CMD:
		err = pp.procRouteMessageP2P(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	// p2p ack
	case protocol.IND_ACK_P2P_STATUS_CMD:
		err = pp.procP2pAck(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}
	// p2p ack
	case protocol.ROUTE_ACK_P2P_STATUS_CMD:
		err = pp.procP2pAck(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_SEND_TOPIC_MSG_CMD:
		err = pp.procSendTopicMsg(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}
	case protocol.ROUTE_SEND_TOPIC_MSG_CMD:
		err = pp.procRouteTopicMsg(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_CREATE_TOPIC_CMD:
		err = pp.procCreateTopic(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_ADD_2_TOPIC_CMD:
		err = pp.procAdd2Topic(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_KICK_TOPIC_CMD:
		err = pp.procKickTopic(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_JOIN_TOPIC_CMD:
		err = pp.procJoinTopic(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_QUIT_TOPIC_CMD:
		err = pp.procQuitTopic(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_GET_TOPIC_LIST_CMD:
		err = pp.procGetTopicList(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}

	case protocol.REQ_GET_TOPIC_PROFILE_CMD:
		err = pp.procGetTopicProfile(&c, session)
		if err != nil {
			log.Error("error:", err)
			return err
		}
	}

	return err
}