コード例 #1
0
ファイル: push.go プロジェクト: ming-hai/appmsgsrv
// 按 key 推送.
func push(key string, msgBytes []byte, expire int) (ret int, mid int64) {
	node := myrpc.GetComet(key)

	if node == nil || node.CometRPC == nil {
		logger.Errorf("Get comet node failed [key=%s]", key)

		return NotFoundServer, 0
	}

	client := node.CometRPC.Get()
	if client == nil {
		logger.Errorf("Get comet node RPC client failed [key=%s]", key)

		return NotFoundServer, 0
	}

	pushArgs := &myrpc.CometPushPrivateArgs{Msg: json.RawMessage(msgBytes), Expire: uint(expire), Key: key}

	if err := client.Call(myrpc.CometServicePushPrivate, pushArgs, &mid); err != nil {
		logger.Errorf("client.Call(\"%s\", \"%v\", &ret) error(%v)", myrpc.CometServicePushPrivate, string(msgBytes), err)

		return InternalErr, 0
	}

	logger.Infof("Pushed a message to [key=%s]", key)
	return OK, mid
}
コード例 #2
0
ファイル: admin.go プロジェクト: ming-hai/appmsgsrv
// PushPrivate handle for push private message.
func PushPrivate(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method Not Allowed", 405)
		return
	}
	body := ""

	res := map[string]interface{}{"ret": app.OK}

	defer app.RetPWrite(w, r, res, &body, time.Now())
	// param
	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		res["ret"] = app.ParamErr
		logger.Errorf("ioutil.ReadAll() failed (%s)", err.Error())
		return
	}
	body = string(bodyBytes)
	params := r.URL.Query()
	key := params.Get("key")
	expireStr := params.Get("expire")
	if key == "" {
		res["ret"] = app.ParamErr
		return
	}
	expire, err := strconv.ParseUint(expireStr, 10, 32)
	if err != nil {
		res["ret"] = app.ParamErr
		logger.Errorf("strconv.ParseUint(\"%s\", 10, 32) error(%v)", expireStr, err)
		return
	}
	node := myrpc.GetComet(key)
	if node == nil || node.CometRPC == nil {
		res["ret"] = app.NotFoundServer
		return
	}
	client := node.CometRPC.Get()
	if client == nil {
		res["ret"] = app.NotFoundServer
		return
	}
	rm := json.RawMessage(bodyBytes)
	msg, err := rm.MarshalJSON()
	if err != nil {
		res["ret"] = app.ParamErr
		logger.Errorf("json.RawMessage(\"%s\").MarshalJSON() error(%v)", body, err)
		return
	}
	args := &myrpc.CometPushPrivateArgs{Msg: json.RawMessage(msg), Expire: uint(expire), Key: key}
	ret := 0
	if err := client.Call(myrpc.CometServicePushPrivate, args, &ret); err != nil {
		logger.Errorf("client.Call(\"%s\", \"%v\", &ret) error(%v)", myrpc.CometServicePushPrivate, args, err)
		res["ret"] = app.InternalErr
		return
	}
	return
}
コード例 #3
0
ファイル: handle.go プロジェクト: ming-hai/appmsgsrv
// GetServer handle for server get
func GetServer0(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "Method Not Allowed", 405)
		return
	}
	params := r.URL.Query()
	key := params.Get("key")
	callback := params.Get("callback")
	protoStr := params.Get("proto")
	res := map[string]interface{}{"ret": app.OK, "msg": "ok"}
	defer app.RetWrite(w, r, res, callback, time.Now())
	if key == "" {
		res["ret"] = app.ParamErr
		return
	}
	proto, err := strconv.Atoi(protoStr)
	if err != nil {
		logger.Errorf("strconv.Atoi(\"%s\") error(%v)", protoStr, err)
		res["ret"] = app.ParamErr
		return
	}
	// Match a push-server with the value computed through ketama algorithm
	node := myrpc.GetComet(key)
	if node == nil {
		res["ret"] = app.NotFoundServer
		return
	}
	addr := node.Addr[proto]
	if addr == nil || len(addr) == 0 {
		res["ret"] = app.NotFoundServer
		return
	}
	server := ""
	// Select the best ip
	if app.Conf.Router != "" {
		server = routerCN.SelectBest(r.RemoteAddr, addr)
		logger.Tracef("select the best ip:\"%s\" match with remoteAddr:\"%s\" , from ip list:\"%v\"", server, r.RemoteAddr, addr)
	}
	if server == "" {
		logger.Tracef("remote addr: \"%s\" chose the ip: \"%s\"", r.RemoteAddr, addr[0])
		server = addr[0]
	}
	res["data"] = map[string]interface{}{"server": server}
	return
}