Esempio n. 1
0
func TestGetDeviceList(t *testing.T) {
	err := conf.LoadConfig(config)
	if err != nil {
		t.Errorf("load config failed, %s", err)
	}
	devList, err := GetDeviceList(sso_tk, DEV_ROUTER)
	if err != nil {
		t.Errorf("%s", err)
	}
	if len(devList) == 0 {
		t.FailNow()
	}
}
Esempio n. 2
0
func BenchmarkGetDeviceList(b *testing.B) {
	b.StopTimer()
	err := conf.LoadConfig(config)
	if err != nil {
		b.Errorf("load config failed, %s", err)
	}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		_, err := GetDeviceList(sso_tk, DEV_ROUTER)
		if err != nil {
			b.Errorf("%s", err)
		}
	}
}
Esempio n. 3
0
func TestFoo(t *testing.T) {
	if err := conf.LoadConfig("../pushd/conf/conf.json"); err != nil {
		t.Errorf("load config failed, %s", err)
	}
	ins := newLetvAuth(&conf.Config)
	ok, uid := ins.Auth("nosuchtoken")
	if ok {
		t.FailNow()
	}
	ok, uid = ins.Auth("102304f687BrX9DhNzo2LnEm1qjEpRrhhIqm1DqGyWbXQaEPUNMInqXcO7s2bChpFIeYz1Xq")
	if !ok {
		t.FailNow()
	}
	t.Logf("uid: %s", uid)
}
Esempio n. 4
0
File: main.go Progetto: houcy/push
func main() {
	var (
		configFile    = flag.String("c", "./conf/conf.json", "Config file")
		logConfigFile = flag.String("l", "./conf/log.xml", "Log config file")
	)

	flag.Parse()

	err := conf.LoadConfig(*configFile)
	if err != nil {
		fmt.Printf("LoadConfig (%s) failed: (%s)\n", *configFile, err)
		os.Exit(1)
	}

	err = log.RegisterCustomFormatter("Ms", utils.CreateMsFormatter)
	if err != nil {
		fmt.Printf("Failed to create custom formatter: (%s)\n", err)
		os.Exit(1)
	}

	logger, err := log.LoggerFromConfigAsFile(*logConfigFile)
	if err != nil {
		fmt.Printf("Load log config failed: (%s)\n", err)
		os.Exit(1)
	}

	log.ReplaceLogger(logger)

	storage.NewInstance(&conf.Config)
	auth.NewInstance(&conf.Config)

	setMsgID()
	setPappID()
	mqProducer, err := mq.NewProducer(
		conf.Config.Rabbit.Uri,
		conf.Config.Rabbit.Exchange,
		conf.Config.Rabbit.ExchangeType,
		conf.Config.Rabbit.Key,
		false)
	if err != nil {
		log.Warnf("new mq produccer failed: %s", err)
		os.Exit(1)
	}
	err = zk.InitWatcher(
		conf.Config.ZooKeeper.Addr,
		conf.Config.ZooKeeper.Timeout*time.Second,
		conf.Config.ZooKeeper.Path)
	if err != nil {
		log.Warnf("init zk watcher failed: %s", err)
		os.Exit(1)
	}
	rpcClient, err = mq.NewRpcClient(conf.Config.Rabbit.Uri, "gibbon_rpc_exchange")
	if err != nil {
		log.Criticalf("Create RPC client failed: %s", err)
		os.Exit(1)
	}

	wg := &sync.WaitGroup{}
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		sig := <-c
		log.Infof("Received signal '%v', exiting\n", sig)
		close(msgBox)
		mqProducer.Shutdown()
		rpcClient.Close()
		wg.Done()
	}()

	wg.Add(1)
	go startHttp(conf.Config.PushAPI, conf.Config.Control.CommandTimeout)

	for {
		select {
		case m, ok := <-msgBox:
			if !ok {
				os.Exit(0)
			}

			v, err := json.Marshal(m)
			if err != nil {
				log.Infof("failed to encode with Msg:", err)
				continue
			}

			if _, err := storage.Instance.HashSet(
				"db_msg_"+m.AppId,
				strconv.FormatInt(m.MsgId, 10), v); err != nil {
				log.Infof("failed to put Msg into redis:", err)
				continue
			}

			if m.Options.TTL > 0 {
				_, err = storage.Instance.HashSet(
					"db_offline_msg_"+m.AppId,
					fmt.Sprintf("%v_%v_%v", m.MsgId, m.CTime, m.Options.TTL+m.CTime), v)
				if err != nil {
					log.Infof("failed to put offline Msg into redis:", err)
					continue
				}
			}

			d := map[string]interface{}{
				"appid": m.AppId,
				"msgid": m.MsgId,
			}
			data, err := json.Marshal(d)
			if err != nil {
				log.Infof("failed to jsonencode with data:", err)
				continue
			}

			if err := mqProducer.Publish(data); err != nil {
				log.Infof("failed to publish data:", err)
				continue
			}
		}
	}
	wg.Wait()
}
Esempio n. 5
0
func main() {
	var (
		configFile    = flag.String("c", "./conf/conf.json", "Config file")
		logConfigFile = flag.String("l", "./conf/log.xml", "Log config file")
	)
	flag.Parse()

	err := conf.LoadConfig(*configFile)
	if err != nil {
		fmt.Printf("LoadConfig (%s) failed: (%s)\n", *configFile, err)
		os.Exit(1)
	}

	err = log.RegisterCustomFormatter("Ms", utils.CreateMsFormatter)
	if err != nil {
		fmt.Printf("Failed to create custom formatter: (%s)\n", err)
		os.Exit(1)
	}

	logger, err := log.LoggerFromConfigAsFile(*logConfigFile)
	if err != nil {
		fmt.Printf("Load log config failed: (%s)\n", err)
		os.Exit(1)
	}

	log.ReplaceLogger(logger)

	wg := &sync.WaitGroup{}
	c := make(chan os.Signal, 1)
	ctrl := make(chan bool, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

	wg.Add(1)
	go func() {
		sig := <-c
		log.Infof("Received signal '%v', exiting\n", sig)
		close(ctrl)
		wg.Done()
	}()

	go startHttp(conf.Config.Sync.Url)
	waitingMsgs := make(map[string]*SyncMsg)
	go func() {
		log.Infof("message handler routine start")
		wg.Add(1)
		for {
			select {
			case _ = <-ctrl:
				log.Infof("message handler routine stop")
				wg.Done()
				return
			//case msg, ok := <-msgBox:
			case msg := <-msgBox:
				key := fmt.Sprintf("%s_%s_%s_%s", msg.AppId, msg.UserId, msg.SendId, msg.RegId)
				if _, ok := waitingMsgs[key]; !ok {
					waitingMsgs[key] = msg
				}
			case <-time.After(conf.Config.Sync.Interval * time.Second):
				for _, msg := range waitingMsgs {
					push(msg)
				}
				waitingMsgs = map[string]*SyncMsg{}
			}
		}
	}()
	wg.Wait()
}
Esempio n. 6
-1
File: notify.go Progetto: houcy/push
func main() {
	logConfigFile := flag.String("l", "./conf/log.xml", "Log config file")
	configFile := flag.String("c", "./conf/conf.json", "Config file")

	flag.Parse()

	logger, err := log.LoggerFromConfigAsFile(*logConfigFile)
	if err != nil {
		fmt.Printf("Load log config failed: (%s)\n", err)
		os.Exit(1)
	}

	log.ReplaceLogger(logger)

	err = conf.LoadConfig(*configFile)
	if err != nil {
		log.Warnf("LoadConfig (%s) failed: (%s)\n", *configFile, err)
		os.Exit(1)
	}

	http.HandleFunc("/api/v1/notify", postNotify)
	err = http.ListenAndServe(conf.Config.Notify.Addr, nil)
	if err != nil {
		log.Warnf("failed to ListenAndServe: ", err)
		os.Exit(1)
	}
}