示例#1
0
func main() {
	flag.Parse()
	if flag.NArg() != 2 {
		usage()
		return
	}

	DeviceId := flag.Args()[0]
	dst := flag.Args()[1]

	UnixDomain = "/tmp/simsdk_" + DeviceId // for RPC
	fmt.Printf("Device Id: [%s], Unix Domain: [%s], Push server: %s\n", DeviceId, UnixDomain, dst)

	raddr, err := net.ResolveTCPAddr("tcp", dst)
	if err != nil {
		log.Errorf("Unknown address: %v", err)
		return
	}
	agent := agent.NewAgent(DeviceId, raddr)
	agent.OnReceiveMsg = OnReceiveMsg
	agent.OnRegResponse = OnRegResponse
	go agent.Run()

	RunRPC(agent)

	//ch := make(chan os.Signal)
	//signal.Notify(ch, syscall.SIGINT, syscall.SIGKILL)
	//s := <-ch

}
示例#2
0
func main() {
	log.SetPrefix("testclient ")

	dst := ""

	for i := 1; i < len(os.Args); i++ {
		switch os.Args[i] {
		case "-i": // device id
			i++
			if i >= len(os.Args) {
				fmt.Printf("missing argument for \"-i\"\n")
				return
			}
			DeviceId = os.Args[i]
		case "-a": // appid
			i++
			if i >= len(os.Args) {
				fmt.Printf("missing argument for \"-a\"\n")
				return
			}
			RegIds[os.Args[i]] = ""
		default:
			if dst == "" {
				dst = os.Args[i]
			} else {
				fmt.Printf("unknown argument %s\n", os.Args[i])
				return
			}
		}
	}

	if dst == "" {
		fmt.Printf("no destination address\n")
		return
	}
	if len(RegIds) == 0 {
		fmt.Printf("no AppId on this device\n")
		return
	}
	if DeviceId == "" {
		rand.Seed(time.Now().Unix())
		DeviceId = strconv.Itoa(rand.Int() % 10000) // a random Id
	}
	LoadRegIds()

	raddr, err := net.ResolveTCPAddr("tcp", dst)
	if err != nil {
		log.Printf("Unknown address: %s", err.Error())
		return
	}

	agent := agent.NewAgent(DeviceId, raddr)
	go agent.Run()

	for appid, _ := range RegIds {
		agent.Regist(appid, appid+"key", RegIds[appid], "")
	}

	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGKILL)
	for {
		s := <-ch
		log.Println("Received signal:", s)
		if len(RegIds) > 0 {
			for appid, regid := range RegIds {
				log.Printf("Unregist AppId: [%s], RegId: [%s]", appid, regid)
				agent.Unregist(appid, "tempkey", RegIds[appid], "")
				delete(RegIds, appid)
				break
			}
		} else {
			log.Printf("Stopped")
			break
		}
	}
}