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

	pp := NewProtoProc(self)

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

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

	return err
}
Exemple #4
0
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()
}