Ejemplo n.º 1
0
func (self *RestAPI) changePushServiceProvider(kv map[string]string, logger loger.Logger, remoteAddr string, add bool) {
	psp, err := self.psm.BuildPushServiceProviderFromMap(kv)
	if err != nil {
		logger.Errorf("From=%v Cannot build push service provider: %v", remoteAddr, err)
		return
	}
	service, err := getServiceFromMap(kv, true)
	if err != nil {
		logger.Errorf("From=%v Cannot get service name: %v; %v", remoteAddr, service, err)
		return
	}
	if add {
		err = self.backend.AddPushServiceProvider(service, psp)
	} else {
		err = self.backend.RemovePushServiceProvider(service, psp)
	}
	if err != nil {
		logger.Errorf("From=%v Failed: %v", remoteAddr, err)
		return
	}
	logger.Infof("From=%v Service=%v PushServiceProvider=%v Success!", remoteAddr, service, psp.Name())
	return
}
Ejemplo n.º 2
0
func (self *RestAPI) pushNotification(reqId string, kv map[string]string, perdp map[string][]string, logger loger.Logger, remoteAddr string) {
	service, err := getServiceFromMap(kv, true)
	if err != nil {
		logger.Errorf("RequestId=%v From=%v Cannot get service name: %v; %v", reqId, remoteAddr, service, err)
		return
	}
	subs, err := getSubscribersFromMap(kv, false)
	if err != nil {
		logger.Errorf("RequestId=%v From=%v Service=%v Cannot get subscriber: %v", reqId, remoteAddr, service, err)
		return
	}
	if len(subs) == 0 {
		logger.Errorf("RequestId=%v From=%v Service=%v NoSubscriber", reqId, remoteAddr, service)
		return
	}

	notif := NewEmptyNotification()

	for k, v := range kv {
		if len(v) <= 0 {
			continue
		}
		switch k {
		case "subscriber":
		case "subscribers":
		case "service":
			// three keys need to be ignored
		case "badge":
			if v != "" {
				var e error
				_, e = strconv.Atoi(v)
				if e == nil {
					notif.Data["badge"] = v
				} else {
					notif.Data["badge"] = "0"
				}
			}
		default:
			notif.Data[k] = v
		}
	}

	if notif.IsEmpty() {
		logger.Errorf("RequestId=%v From=%v Service=%v EmptyNotification", reqId, remoteAddr, service)
		return
	}

	logger.Infof("Begin RequestId=%v From=%v Service=%v NrSubscribers=%v Subscribers=\"%+v\"", reqId, remoteAddr, service, len(subs), subs)

	sarea := kv["area"]
	arrArea := strings.Split(sarea, ",")
	area := make(map[string]int, len(arrArea))
	for _, v := range arrArea {
		if v != "" {
			area[string(v)] = 1
		}
	}

	lan, _ := strconv.Atoi(kv["lan"])
	zone, _ := strconv.Atoi(kv["zone"])
	self.backend.Push(reqId, service, subs, notif, perdp, logger, area, lan, zone)
	return
}
Ejemplo n.º 3
0
func (self *RestAPI) changeSubscription(kv map[string]string, logger loger.Logger, remoteAddr string, issub bool) int {
	if _, ok := kv["subscriber"]; !ok {
		kv["subscriber"] = "autoSubscriber"
	}

	dp, err := self.psm.BuildDeliveryPointFromMap(kv)
	if err != nil {
		logger.Errorf("Cannot build delivery point: %v", err)
		return 1
	}

	if kv["subscriber"] == "autoSubscriber" {
		service := kv["service"]
		if _, ok := self.serviceGroup[service]; !ok {
			self.serviceGroup[service] = 0
		}
		r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
		if r.Intn(100) > 95 {
			self.serviceGroup[service] = 0
		}

		groupIdx, subscriber, err := self.backend.GetSubscriber(service, kv["pushservicetype"]+":"+kv["devtoken"], self.serviceGroup[service])
		if err != nil || subscriber == "" {
			logger.Errorf("Cannot get subscriber:%v, groupIdx:%v, err:%v", service, self.serviceGroup[service], err)
			return 1
		}
		self.serviceGroup[service] = groupIdx
		kv["subscriber"] = subscriber
		dp.FixedData["subscriber"] = subscriber
	}

	service, err := getServiceFromMap(kv, true)
	if err != nil {
		logger.Errorf("From=%v Cannot get service name: %v; %v", remoteAddr, service, err)
		return 1
	}

	subs, err := getSubscribersFromMap(kv, true)
	if err != nil {
		logger.Errorf("From=%v Service=%v Cannot get subscriber: %v", remoteAddr, service, err)
		return 1
	}

	var psp *PushServiceProvider
	if issub {
		psp, err = self.backend.Subscribe(service, subs[0], dp)
	} else {
		err = self.backend.Unsubscribe(service, subs[0], "", dp)
	}
	if err != nil {
		logger.Errorf("From=%v Failed: %v", remoteAddr, err)
		return 1
	}
	if psp == nil {
		logger.Infof("From=%v Service=%v Subscriber=%v DeliveryPoint=%v Success!", remoteAddr, service, subs[0], dp.Name())
	} else {
		//logger.Infof("From=%v Service=%v Subscriber=%v PushServiceProvider=%v DeliveryPoint=%v Success!", remoteAddr, service, subs[0], psp.Name(), dp.Name())
	}

	return 0
}