示例#1
0
func (self *PushBackEnd) httpGetWork(host string) {
	workUrl := "http://" + host + "/client?action=getwork&addr=" + self.httpHost
	wait := true
	for {
		if wait {
			time.Sleep(time.Duration(5) * time.Second)
		}

		data, err := httpRequest(workUrl)
		if err != nil || data == nil {
			wait = true
			self.logChan <- NewInfof("get work fail: %v", workUrl)
			continue
		}

		if string(data) == "[]" {
			wait = true
			continue
		}

		js, err := NewJson(data)
		if err != nil {
			wait = true
			self.logChan <- NewInfof("get work unmarshal err:%v  body: %v", err, string(data))
			continue
		}

		id, _ := js.Get("Id").Int()
		msg, _ := js.Get("Msg").String()
		sarea, _ := js.Get("Area").String()
		strings.Split(sarea, ",")
		area := make(map[string]int, len(sarea))
		for _, v := range sarea {
			area[string(v)] = 1
		}

		lan, _ := js.Get("Lan").Int()
		service, _ := js.Get("Service").String()
		subscriber, _ := js.Get("Subscriber").String()
		zone, _ := js.Get("ZoneCondition").Int()
		pushTime, _ := js.Get("PushTime").Int()

		if msg == "" {
			self.logChan <- NewInfof("work arg error msg")
			continue
		}
		if service == "" {
			self.logChan <- NewInfof("work arg error service")
			continue
		}
		if subscriber == "" {
			self.logChan <- NewInfof("work arg error subscriber")
			continue
		}

		self.logChan <- NewInfof("New Work id:%v msg:%v area:%v lan:%v service:%v subscriber:%v zone:%v pushTime:%v", id, msg, area, lan, service, subscriber, pushTime, zone)

		rid := randomUniqId()
		subs := make([]string, 0)
		perdp := make(map[string][]string, 0)
		subs = append(subs, subscriber)
		notif := NewEmptyNotification()
		notif.Data["msg"] = msg
		logLevel := loger.LOGLEVEL_INFO
		weblogger := loger.NewLogger(nil, "[Push]", logLevel)
		logger := loger.MultiLogger(weblogger, self.loggers[LOGGER_PUSH])
		successPush := self.Push(rid, service, subs, notif, perdp, logger, area, lan, zone)

		reqUrl := "http://" + host + "/client?action=workreq&id=" + strconv.Itoa(id) + "&zone=" + strconv.Itoa(zone) + "&pushtime=" + strconv.Itoa(pushTime) + "&service=" + service + "&subscriber=" + subscriber + "&pushamount=" + strconv.Itoa(successPush)
		_, reqErr := httpRequest(reqUrl)
		if reqErr != nil {
			self.logChan <- NewInfof("req work fail: %v", reqUrl)
		}

		wait = false
	}
}
示例#2
0
func (self *RestAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	remoteAddr := r.RemoteAddr

	w.Header().Set("Access-Control-Allow-Origin", "*")
	switch r.URL.Path {
	case QUERY_NUMBER_OF_DELIVERY_POINTS_URL:
		r.ParseForm()
		n := self.numberOfDeliveryPoints(r.Form, self.loggers[LOGGER_WEB], remoteAddr)
		fmt.Fprintf(w, "%v\r\n", n)
		return
	case VERSION_INFO_URL:
		fmt.Fprintf(w, "%v\r\n", self.version)
		self.loggers[LOGGER_WEB].Infof("Checked version from %v", remoteAddr)
		return
	case STOP_PROGRAM_URL:
		self.stop(w, remoteAddr)
	case CLIENT_PROCESS_TASK:
		r.ParseForm()
		kv := make(map[string]string, len(r.Form))
		for k, v := range r.Form {
			kv[k] = v[0]
		}
		ret, err := self.pushMan.ProcessClientRequest(kv)
		if err != nil {
			fmt.Fprintf(w, "%v\r\n", err)
		} else {
			fmt.Fprintf(w, "%v", ret)
		}
		return
	}
	r.ParseForm()
	kv := make(map[string]string, len(r.Form))
	perdp := make(map[string][]string, 3)
	perdpPrefix := "uniqush.perdp."
	for k, v := range r.Form {
		if len(k) > len(perdpPrefix) {
			if k[:len(perdpPrefix)] == perdpPrefix {
				key := k[len(perdpPrefix):]
				perdp[key] = v
				continue
			}
		}
		if len(v) > 0 {
			kv[k] = v[0]
		}
	}
	writer := w
	logLevel := loger.LOGLEVEL_INFO

	self.waitGroup.Add(1)
	defer self.waitGroup.Done()
	switch r.URL.Path {
	case ADD_PUSH_SERVICE_PROVIDER_TO_SERVICE_URL:
		weblogger := loger.NewLogger(writer, "[AddPushServiceProvider]", logLevel)
		logger := loger.MultiLogger(weblogger, self.loggers[LOGGER_ADDPSP])
		self.changePushServiceProvider(kv, logger, remoteAddr, true)
	case REMOVE_PUSH_SERVICE_PROVIDER_TO_SERVICE_URL:
		weblogger := loger.NewLogger(writer, "[RemovePushServiceProvider]", logLevel)
		logger := loger.MultiLogger(weblogger, self.loggers[LOGGER_RMPSP])
		self.changePushServiceProvider(kv, logger, remoteAddr, false)
	case ADD_DELIVERY_POINT_TO_SERVICE_URL:
		weblogger := loger.NewLogger(writer, "[Subscribe]", logLevel)
		logger := loger.MultiLogger(weblogger, self.loggers[LOGGER_SUB])
		if self.changeSubscription(kv, logger, remoteAddr, true) == 0 {
			fmt.Fprintf(w, "%v", 0)
		}
	case REMOVE_DELIVERY_POINT_FROM_SERVICE_URL:
		weblogger := loger.NewLogger(writer, "[Unsubscribe]", logLevel)
		logger := loger.MultiLogger(weblogger, self.loggers[LOGGER_UNSUB])
		self.changeSubscription(kv, logger, remoteAddr, false)
	case PUSH_NOTIFICATION_URL:
		weblogger := loger.NewLogger(writer, "[Push]", logLevel)
		logger := loger.MultiLogger(weblogger, self.loggers[LOGGER_PUSH])
		rid := randomUniqId()
		self.pushNotification(rid, kv, perdp, logger, remoteAddr)
	}
}